Giter Club home page Giter Club logo

sp2's Issues

Type error happend by using union type string literal value for $path from $bind.

example code:

import { $bind } from "@sp2/updater";

type Hoge = {
  fuga: {
    key1: string;
    key2: string;
  };
};

const { $push, $path } = $bind<Hoge>();

$push($path("fuga", "key1"), "hoge"); // valid

const key = Math.random() > 0.5 ? "key1" : "key2";

$push($path("fuga", key), "hoge"); // $path is type error

I see here.
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L21-L28
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L151-L154

The $path method is generating some depth path type and this using like following codes.
K1 extends keyof T, K2 extends keyof T[K1] ...
However if type K[i] had union type value, looks like TS compiler is checking 'left side value can extends all of expanded union type value?'.
And i think this issue can be solved by using like following codes.
K1 extends Extract<K1, keyof T>, K2 extends Extract<K2, T[K1]> ...

But if resolved this issue, #37 Error is still showing...

Add an adaptor for mongodb

sp2 provides slightly different formats from MongoDB's formats.
To fill the gap, sp2 should provide converters of operations.

function toMongoUpdateOperation(operation: GeneralUpdateOperation): GeneralUpdateOperation
function toMongoFindOperation(operation: FindOperation): FindOperation

$path: Support optional property

import { $bind } from 'sp2'

type SomeType = {
    exact: {
        deepNestedExact: {
            count: number
        }
    },
    optional?: {
        deepNestedOptional?: {
            count: number
        }
    }
}

const { $path } = $bind<SomeType>()

$path('exact', 'deepNestedExact', 'count') // OK
$path('optional', 'deepNestedOptional') // Error: Argument of type '"deepNestedOptional"' is not assignable to parameter of type 'never'.

Hi.
I found an error according to the optional property.

How can I traverse optional property?

Add $append operator

$append operator is syntax sugar for $set operator.

{ $set: { 'obj.foo': 123, 'obj.bar': 'abc', 'obj.baz': true } }

can be written as

{ $append: { obj: { foo: 123, bar: 'abc', baz: true } }

[$path] I got an error when I put a nullable property in the argument of $path method

import { $bind } from "@sp2/updater";

type MapKeys = "key1";
interface Map { map: { [key in MapKeys]?: any } };

const { $set, $path } = $bind<Map>();
const operation = $set($path("map", "key1"));
error TS2345: Argument of type 'BoundDocumentPath2<Map, "map", "key1">' is not assignable to parameter of type '[string, any]'.
  Type 'String & { keys: ["map", "key1"]; object: Map; }' is missing the following properties from type '[string, any]': 0, 1, pop, push, and 13 more.

const operation = $set($path("map", "key1"));
                              ~~~~~~~~~~~~~~~~~~~~

Review request of static analysis results

I forked sp2 into my git and executed static analysis by using two types of tools: SonarCloud and DeepScan. The results between them are different because the analysis policies and the rules used by each other are different.

I'm sure your review will be helpful to others.
Click the badge below to see results.

  1. results analyzed by DeepScan: DeepScan grade

  2. results analyzed by SonarCloud: Quality Gate Status

FYI
SonarCloud ( https://sonarcloud.io/ )

  • analysis on 20+ different languages (including Javascript)
  • possible to analyze source from various perspectives:
    Reliability,
    Security (Hotspot, OWASPs Top 10, SNAS Top 25, CWE),
    Maintainability,
    Coverage,
    Duplications,
    Complexity
  • A total of 226 rules are applied to Javascript and 172 to TypeScript

DeepScan ( https://deepscan.io/ )

  • specialized in Javascript ( JavaScript, TypeScript, React and Vue.js )
  • semantic analysis (enables finding issues that syntax-based linters can't)
  • analyzed by using ESLint and 201 rules (As of June 5, 2020)

Add benchmark tests

Add benchmark tests on benchmark module.

  • create module

update()

  • non-nested $set operation
  • nested $set operation
  • $push operation
  • mixed operation
  • comparison with immer
  • comparison with Immutable.js
  • comparison with other state-updating libraries

retrieve()

  • simple find operation
  • mixed operation

Not work when setting a value whose type is type variable

type TargetObject<T> = { foo: T };

function bar<T>(obj: TargetObject<T>, value: T) {
  const { $set, $docPath } = $bind<TargetObject<T>>();
  const path = $docPath("foo");
  const operation = $set(path, value); // this occurs an error
}

Maybe type inference at 2nd argument of $set is not working.

  const operation = $set(path, value as NestedValue<TargetObject<T>, "foo", "","","","","","","","",""); 

This can suppress the error, but I want easier way.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.