Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deep): fix Path.update not updating values correctly #80

Merged
merged 4 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions deno_dist/base/plain-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ export type IsPlainObj<T> = T extends
? false
: IsObjWithoutFunctions<T>;

/**
* Utility type that will only accept objects that are considered 'plain objects' according
* to the `IsPlainObj` predicate type.
* @typeparam T - the value type to test
*/
export type PlainObj<T> = IsPlainObj<T> extends true ? T : never;

/**
* Utility type that will only return true if the input type T is equal to `any`.
* @typeparam T - the value type to test
*/
export type IsAny<T> = 0 extends 1 & T ? true : false;

/**
* Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
* data object.
Expand Down
7 changes: 4 additions & 3 deletions deno_dist/deep/path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Update } from '../common/mod.ts';
import type { IsPlainObj, PlainObj } from '../base/mod.ts';

import { patch } from './internal.ts';
import { patch, patchNested } from './internal.ts';

/**
* A string representing a path into an (nested) object of type T.
Expand Down Expand Up @@ -92,8 +92,9 @@ export namespace Path {
let current = root;

for (const item of items) {
current[item] = {};
current = current[item];
const next = {};
current[item] = patchNested(next);
current = next;
}

current[last] = value;
Expand Down
29 changes: 25 additions & 4 deletions deno_dist/deep/protected.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import type { IsPlainObj } from '../base/mod.ts';
import type { IsAny, IsPlainObj } from '../base/mod.ts';

/**
* A deep readonly typed version of given type T. Makes all properties or elements read only.
* It maps types using the following rules:
* - arrays and tuples become readonly counterparts, and all element types are wrapped in `Protected` if applicable
* - Maps of key type K and value type V become Maps of key type `Protected<K>` and value type `Protected<V>`
* - Sets of element type E become Sets of element type `Protected<E>`
* - Promises of value type E become Promises of value type `Protected<E>`
* - Objects that have only simple properties (no functions or iterators) will have all the properties as Protected if applicable
* - Any other type will not be mapped
* @typeparam T - the input type
*/
export type Protected<T> = T extends readonly (infer E)[]
? readonly Protected<E>[]
export type Protected<T> = IsAny<T> extends true
? T
: T extends readonly any[] & infer A
? { readonly [K in keyof A]: Protected<A[K]> }
: T extends Map<infer K, infer V>
? Map<Protected<K>, Protected<V>>
: T extends Set<infer E>
Expand All @@ -17,8 +26,20 @@ export type Protected<T> = T extends readonly (infer E)[]
: T;

/**
* Returns the same value wrapped in the Protected type
* Returns the same value wrapped in the `Protected` type.
* @param value - the value to wrap
* @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
* type to a value
* @example
* ```ts
* const obj = Protected({ a: 1, b: { c: true, d: [1] } })
* obj.a = 2 // compiler error: a is readonly
* obj.b.c = false // compiler error: c is readonly
* obj.b.d.push(2) // compiler error: d is a readonly array
* (obj as any).b.d.push(2) // will actually mutate the object
* ```

* '
*/
export function Protected<T>(value: T): Protected<T> {
return value as any;
Expand Down
6 changes: 5 additions & 1 deletion packages/actor/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/test/**.test.ts"]
"testMatch": ["**/test/**.test.ts"],
"moduleNameMapper": {
"@rimbu/actor": ["<rootDir>/src/index.ts"]
},
"collectCoverageFrom": ["src/**"]
}
2 changes: 1 addition & 1 deletion packages/actor/test/actor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, Obs } from '../src';
import { Actor, Obs } from '@rimbu/actor';

describe('Actor', () => {
it('sets initial state', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/actor/test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Obs, Command } from '../src';
import { Obs, Command } from '@rimbu/actor';

describe('Command', () => {
it('executes command', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/actor/test/obs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Obs } from '../src';
import { Obs } from '@rimbu/actor';
import { OrderedHashMap } from '@rimbu/core';
import { Patch, patchNested as $ } from '@rimbu/deep';

Expand Down
2 changes: 1 addition & 1 deletion packages/actor/test/process-queue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProcessQueue } from '../src';
import { ProcessQueue } from '@rimbu/actor';

async function wait() {
return new Promise((res) => setTimeout(res, 100));
Expand Down
5 changes: 4 additions & 1 deletion packages/actor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"extends": "../../config/tsconfig.base.json",
"include": ["src"],
"include": ["src", "test"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist",
"noEmit": true,
"paths": {
"@rimbu/actor": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/actor/tsconfig.main.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/main"
"outDir": "./dist/main",
"paths": {
"@rimbu/actor": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/actor/tsconfig.module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/module"
"outDir": "./dist/module",
"paths": {
"@rimbu/actor": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/actor/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/types"
"outDir": "./dist/types",
"paths": {
"@rimbu/actor": ["./src/index.ts"]
}
}
}
6 changes: 5 additions & 1 deletion packages/base/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/test/**.test.ts"]
"testMatch": ["**/test/**.test.ts"],
"moduleNameMapper": {
"@rimbu/base": ["<rootDir>/src/index.ts"]
},
"collectCoverageFrom": ["src/**"]
}
11 changes: 11 additions & 0 deletions packages/base/src/plain-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ export type IsPlainObj<T> = T extends
? false
: IsObjWithoutFunctions<T>;

/**
* Utility type that will only accept objects that are considered 'plain objects' according
* to the `IsPlainObj` predicate type.
* @typeparam T - the value type to test
*/
export type PlainObj<T> = IsPlainObj<T> extends true ? T : never;

/**
* Utility type that will only return true if the input type T is equal to `any`.
* @typeparam T - the value type to test
*/
export type IsAny<T> = 0 extends 1 & T ? true : false;

/**
* Companion function to the `IsRecord<T>` type that checks whether the given object is a pure
* data object.
Expand Down
2 changes: 1 addition & 1 deletion packages/base/test/arr.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TraverseState } from '@rimbu/common';
import { Arr } from '../src';
import { Arr } from '@rimbu/base';

const empty: number[] = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/base/test/entry.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entry } from '../src';
import { Entry } from '@rimbu/base';

describe('Entry', () => {
it('first', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/base/test/rimbu-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RimbuError } from '../src';
import { RimbuError } from '@rimbu/base';

describe('RimbuError', () => {
it('throwEmptyCollectionAssumedNonEmptyError', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"extends": "../../config/tsconfig.base.json",
"include": ["src"],
"include": ["src", "test"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist",
"noEmit": true
"noEmit": true,
"paths": {
"@rimbu/base": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/base/tsconfig.main.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/main"
"outDir": "./dist/main",
"paths": {
"@rimbu/base": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/base/tsconfig.module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/module"
"outDir": "./dist/module",
"paths": {
"@rimbu/base": ["./src/index.ts"]
}
}
}
5 changes: 4 additions & 1 deletion packages/base/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"include": ["src"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist/types"
"outDir": "./dist/types",
"paths": {
"@rimbu/base": ["./src/index.ts"]
}
}
}
7 changes: 6 additions & 1 deletion packages/bimap/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/test/**.test.ts"]
"testMatch": ["**/test/**.test.ts"],
"moduleNameMapper": {
"@rimbu/bimap/custom": ["<rootDir>/src/custom/index.ts"],
"@rimbu/bimap": ["<rootDir>/src/main/index.ts"]
},
"collectCoverageFrom": ["src/**"]
}
2 changes: 1 addition & 1 deletion packages/bimap/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../config/tsconfig.base.json",
"include": ["src"],
"include": ["src", "test"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist",
Expand Down
7 changes: 6 additions & 1 deletion packages/bimultimap/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/test/**.test.ts"]
"testMatch": ["**/test/**.test.ts"],
"moduleNameMapper": {
"@rimbu/bimultimap/custom": ["<rootDir>/src/custom/index.ts"],
"@rimbu/bimultimap": ["<rootDir>/src/main/index.ts"]
},
"collectCoverageFrom": ["src/**"]
}
2 changes: 1 addition & 1 deletion packages/bimultimap/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../../config/tsconfig.base.json",
"include": ["src"],
"include": ["src", "test"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist",
Expand Down
3 changes: 2 additions & 1 deletion packages/collection-types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"@rimbu/collection-types/map-custom": ["./src/map-custom/index.ts"],
"@rimbu/collection-types/set-custom": ["./src/set-custom/index.ts"],
"@rimbu/collection-types/map": ["./src/map/index.ts"],
"@rimbu/collection-types/set": ["./src/set/index.ts"]
"@rimbu/collection-types/set": ["./src/set/index.ts"],
"@rimbu/collection-types": ["./src/main/index.ts"]
}
}
}
3 changes: 2 additions & 1 deletion packages/collection-types/tsconfig.main.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@rimbu/collection-types/map-custom": ["./src/map-custom/index.ts"],
"@rimbu/collection-types/set-custom": ["./src/set-custom/index.ts"],
"@rimbu/collection-types/map": ["./src/map/index.ts"],
"@rimbu/collection-types/set": ["./src/set/index.ts"]
"@rimbu/collection-types/set": ["./src/set/index.ts"],
"@rimbu/collection-types": ["./src/main/index.ts"]
}
}
}
3 changes: 2 additions & 1 deletion packages/collection-types/tsconfig.module.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@rimbu/collection-types/map-custom": ["./src/map-custom/index.ts"],
"@rimbu/collection-types/set-custom": ["./src/set-custom/index.ts"],
"@rimbu/collection-types/map": ["./src/map/index.ts"],
"@rimbu/collection-types/set": ["./src/set/index.ts"]
"@rimbu/collection-types/set": ["./src/set/index.ts"],
"@rimbu/collection-types": ["./src/main/index.ts"]
}
}
}
3 changes: 2 additions & 1 deletion packages/collection-types/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@rimbu/collection-types/map-custom": ["./src/map-custom/index.ts"],
"@rimbu/collection-types/set-custom": ["./src/set-custom/index.ts"],
"@rimbu/collection-types/map": ["./src/map/index.ts"],
"@rimbu/collection-types/set": ["./src/set/index.ts"]
"@rimbu/collection-types/set": ["./src/set/index.ts"],
"@rimbu/collection-types": ["./src/main/index.ts"]
}
}
}
6 changes: 5 additions & 1 deletion packages/common/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": ["**/test/**.test.ts"]
"testMatch": ["**/test/**.test.ts"],
"moduleNameMapper": {
"@rimbu/common": "<rootDir>/src/index.ts"
},
"collectCoverageFrom": ["src/**"]
}
2 changes: 1 addition & 1 deletion packages/common/test/async-optlazy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncOptLazy } from '../src';
import { AsyncOptLazy } from '@rimbu/common';

describe('AsyncOptLazy', () => {
it('toMaybePromise', async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/common/test/async-reducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AsyncStream } from '@rimbu/stream';
import { AsyncReducer } from '..';
// import { AsyncReducer } from '../src';
import { AsyncReducer } from '@rimbu/common';

describe('AsyncReducer', () => {
it('create', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/comp.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Comp } from '../src';
import { Comp } from '@rimbu/common';

describe('Comp', () => {
it('stringComp', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/eq.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Eq } from '../src';
import { Eq } from '@rimbu/common';

describe('Eq', () => {
it('objectIs', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/err.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Err, ErrBase } from '../src';
import { Err, ErrBase } from '@rimbu/common';

describe('Err/Base', () => {
it('throws error', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/test/index-range.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IndexRange } from '../src';
import { IndexRange } from '@rimbu/common';

describe('IndexRange', () => {
it('getIndexRangeIndices', () => {
Expand Down
Loading