@rimbu/deep 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,7 +84,7 @@ console.log(
84
84
 
85
85
  ## Contributing
86
86
 
87
- Feel very welcome to contribute to further improve Rimbu. Please read our [Contributing guide](../../CONTRIBUTING.md).
87
+ Feel very welcome to contribute to further improve Rimbu. Please read our [Contributing guide](https://github.com/rimbu-org/rimbu/blob/main/CONTRIBUTING.md).
88
88
 
89
89
  ## Contributors
90
90
 
package/dist/main/path.js CHANGED
@@ -59,8 +59,9 @@ var Path;
59
59
  try {
60
60
  for (var items_2 = tslib_1.__values(items), items_2_1 = items_2.next(); !items_2_1.done; items_2_1 = items_2.next()) {
61
61
  var item = items_2_1.value;
62
- current[item] = {};
63
- current = current[item];
62
+ var next = {};
63
+ current[item] = (0, internal_1.patchNested)(next);
64
+ current = next;
64
65
  }
65
66
  }
66
67
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
@@ -1 +1 @@
1
- {"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":";;;;AAGA,uCAAmC;AAcnC,IAAiB,IAAI,CAqFpB;AArFD,WAAiB,IAAI;IAwBnB;;;;;;;;;;;;;OAaG;IACH,SAAgB,GAAG,CACjB,MAAuB,EACvB,IAAO;;QAEP,IAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAE9B,IAAI,MAAM,GAAQ,MAAM,CAAC;;YAEzB,KAAmB,IAAA,UAAA,iBAAA,KAAK,CAAA,4BAAA,+CAAE;gBAArB,IAAM,IAAI,kBAAA;gBACb,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;aACvB;;;;;;;;;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAbe,QAAG,MAalB,CAAA;IAED;;;;;;;;;;OAUG;IACH,SAAgB,MAAM,CACpB,MAAuB,EACvB,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AM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAG,CAAC;QAE1B,IAAM,IAAI,GAAwB,EAAE,CAAC;QAErC,IAAI,OAAO,GAAG,IAAI,CAAC;;YAEnB,KAAmB,IAAA,UAAA,iBAAA,KAAK,CAAA,4BAAA,+CAAE;gBAArB,IAAM,IAAI,kBAAA;gBACb,IAAM,IAAI,GAAG,EAAE,CAAC;gBAChB,OAAO,CAAC,IAAI,CAAC,GAAG,IAAA,sBAAW,EAAC,IAAI,CAAC,CAAC;gBAClC,OAAO,GAAG,IAAI,CAAC;aAChB;;;;;;;;;QAED,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAEtB,OAAO,IAAA,gBAAK,EAAI,MAAM,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC;IArBe,WAAM,SAqBrB,CAAA;AACH,CAAC,EAtFgB,IAAI,GAAJ,YAAI,KAAJ,YAAI,QAsFpB"}
@@ -2,8 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Protected = void 0;
4
4
  /**
5
- * Returns the same value wrapped in the Protected type
5
+ * Returns the same value wrapped in the `Protected` type.
6
6
  * @param value - the value to wrap
7
+ * @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
8
+ * type to a value
9
+ * @example
10
+ * ```ts
11
+ * const obj = Protected({ a: 1, b: { c: true, d: [1] } })
12
+ * obj.a = 2 // compiler error: a is readonly
13
+ * obj.b.c = false // compiler error: c is readonly
14
+ * obj.b.d.push(2) // compiler error: d is a readonly array
15
+ * (obj as any).b.d.push(2) // will actually mutate the object
16
+ * ```
17
+
18
+ * '
7
19
  */
8
20
  function Protected(value) {
9
21
  return value;
@@ -1 +1 @@
1
- {"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":";;;AAkBA;;;GAGG;AACH,SAAgB,SAAS,CAAI,KAAQ;IACnC,OAAO,KAAY,CAAC;AACtB,CAAC;AAFD,8BAEC"}
1
+ {"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":";;;AA2BA;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,SAAS,CAAI,KAAQ;IACnC,OAAO,KAAY,CAAC;AACtB,CAAC;AAFD,8BAEC"}
@@ -1,4 +1,4 @@
1
- import { patch } from './internal';
1
+ import { patch, patchNested } from './internal';
2
2
  export var Path;
3
3
  (function (Path) {
4
4
  /**
@@ -41,8 +41,9 @@ export var Path;
41
41
  const root = {};
42
42
  let current = root;
43
43
  for (const item of items) {
44
- current[item] = {};
45
- current = current[item];
44
+ const next = {};
45
+ current[item] = patchNested(next);
46
+ current = next;
46
47
  }
47
48
  current[last] = value;
48
49
  return patch(source, root);
@@ -1 +1 @@
1
- {"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAcnC,MAAM,KAAW,IAAI,CAqFpB;AArFD,WAAiB,IAAI;IAwBnB;;;;;;;;;;;;;OAaG;IACH,SAAgB,GAAG,CACjB,MAAuB,EACvB,IAAO;QAEP,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAE9B,IAAI,MAAM,GAAQ,MAAM,CAAC;QAEzB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;YACxB,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;SACvB;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAbe,QAAG,MAalB,CAAA;IAED;;;;;;;;;;OAUG;IACH,SAAgB,MAAM,CACpB,MAAuB,EACvB,IAAO,EACP,KAAgC;QAEhC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC9B,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAG,CAAC;QAE1B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xB,MAAM,IAAI,GAAG,EAAE,CAAC;YAChB,OAAO,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;YAClC,OAAO,GAAG,IAAI,CAAC;SAChB;QAED,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAEtB,OAAO,KAAK,CAAI,MAAM,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC;IArBe,WAAM,SAqBrB,CAAA;AACH,CAAC,EAtFgB,IAAI,KAAJ,IAAI,QAsFpB"}
@@ -1,6 +1,18 @@
1
1
  /**
2
- * Returns the same value wrapped in the Protected type
2
+ * Returns the same value wrapped in the `Protected` type.
3
3
  * @param value - the value to wrap
4
+ * @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
5
+ * type to a value
6
+ * @example
7
+ * ```ts
8
+ * const obj = Protected({ a: 1, b: { c: true, d: [1] } })
9
+ * obj.a = 2 // compiler error: a is readonly
10
+ * obj.b.c = false // compiler error: c is readonly
11
+ * obj.b.d.push(2) // compiler error: d is a readonly array
12
+ * (obj as any).b.d.push(2) // will actually mutate the object
13
+ * ```
14
+
15
+ * '
4
16
  */
5
17
  export function Protected(value) {
6
18
  return value;
@@ -1 +1 @@
1
- {"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":"AAkBA;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAI,KAAQ;IACnC,OAAO,KAAY,CAAC;AACtB,CAAC"}
1
+ {"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":"AA2BA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,SAAS,CAAI,KAAQ;IACnC,OAAO,KAAY,CAAC;AACtB,CAAC"}
@@ -1,13 +1,34 @@
1
- import type { IsPlainObj } from '@rimbu/base';
1
+ import type { IsAny, IsPlainObj } from '@rimbu/base';
2
2
  /**
3
3
  * A deep readonly typed version of given type T. Makes all properties or elements read only.
4
+ * It maps types using the following rules:
5
+ * - arrays and tuples become readonly counterparts, and all element types are wrapped in `Protected` if applicable
6
+ * - Maps of key type K and value type V become Maps of key type `Protected<K>` and value type `Protected<V>`
7
+ * - Sets of element type E become Sets of element type `Protected<E>`
8
+ * - Promises of value type E become Promises of value type `Protected<E>`
9
+ * - Objects that have only simple properties (no functions or iterators) will have all the properties as Protected if applicable
10
+ * - Any other type will not be mapped
4
11
  * @typeparam T - the input type
5
12
  */
6
- export declare type Protected<T> = T extends readonly (infer E)[] ? readonly Protected<E>[] : T extends Map<infer K, infer V> ? Map<Protected<K>, Protected<V>> : T extends Set<infer E> ? Set<Protected<E>> : T extends Promise<infer E> ? Promise<Protected<E>> : IsPlainObj<T> extends true ? {
13
+ export declare type Protected<T> = IsAny<T> extends true ? T : T extends readonly any[] & infer A ? {
14
+ readonly [K in keyof A]: Protected<A[K]>;
15
+ } : T extends Map<infer K, infer V> ? Map<Protected<K>, Protected<V>> : T extends Set<infer E> ? Set<Protected<E>> : T extends Promise<infer E> ? Promise<Protected<E>> : IsPlainObj<T> extends true ? {
7
16
  readonly [K in keyof T]: Protected<T[K]>;
8
17
  } : T;
9
18
  /**
10
- * Returns the same value wrapped in the Protected type
19
+ * Returns the same value wrapped in the `Protected` type.
11
20
  * @param value - the value to wrap
21
+ * @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
22
+ * type to a value
23
+ * @example
24
+ * ```ts
25
+ * const obj = Protected({ a: 1, b: { c: true, d: [1] } })
26
+ * obj.a = 2 // compiler error: a is readonly
27
+ * obj.b.c = false // compiler error: c is readonly
28
+ * obj.b.d.push(2) // compiler error: d is a readonly array
29
+ * (obj as any).b.d.push(2) // will actually mutate the object
30
+ * ```
31
+
32
+ * '
12
33
  */
13
34
  export declare function Protected<T>(value: T): Protected<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rimbu/deep",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Tools to use handle plain JS objects as immutable objects",
5
5
  "keywords": [
6
6
  "immutable",
@@ -57,8 +57,8 @@
57
57
  },
58
58
  "sideEffects": false,
59
59
  "dependencies": {
60
- "@rimbu/base": "^0.9.0",
61
- "@rimbu/common": "^0.9.3",
60
+ "@rimbu/base": "^0.9.1",
61
+ "@rimbu/common": "^0.9.4",
62
62
  "tslib": "^2.4.0"
63
63
  },
64
64
  "publishConfig": {
@@ -68,5 +68,5 @@
68
68
  "index": "src/index.ts",
69
69
  "replacer": "../../config/denoify-rimbu-replacer.js"
70
70
  },
71
- "gitHead": "4efaf8c469d606381517984436383fd6b1b61ec0"
71
+ "gitHead": "60d3b52050fc30f10921cbf92d637362d663d7e0"
72
72
  }
package/src/path.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Update } from '@rimbu/common';
2
2
  import type { IsPlainObj, PlainObj } from '@rimbu/base';
3
3
 
4
- import { patch } from './internal';
4
+ import { patch, patchNested } from './internal';
5
5
 
6
6
  /**
7
7
  * A string representing a path into an (nested) object of type T.
@@ -92,8 +92,9 @@ export namespace Path {
92
92
  let current = root;
93
93
 
94
94
  for (const item of items) {
95
- current[item] = {};
96
- current = current[item];
95
+ const next = {};
96
+ current[item] = patchNested(next);
97
+ current = next;
97
98
  }
98
99
 
99
100
  current[last] = value;
package/src/protected.ts CHANGED
@@ -1,11 +1,20 @@
1
- import type { IsPlainObj } from '@rimbu/base';
1
+ import type { IsAny, IsPlainObj } from '@rimbu/base';
2
2
 
3
3
  /**
4
4
  * A deep readonly typed version of given type T. Makes all properties or elements read only.
5
+ * It maps types using the following rules:
6
+ * - arrays and tuples become readonly counterparts, and all element types are wrapped in `Protected` if applicable
7
+ * - Maps of key type K and value type V become Maps of key type `Protected<K>` and value type `Protected<V>`
8
+ * - Sets of element type E become Sets of element type `Protected<E>`
9
+ * - Promises of value type E become Promises of value type `Protected<E>`
10
+ * - Objects that have only simple properties (no functions or iterators) will have all the properties as Protected if applicable
11
+ * - Any other type will not be mapped
5
12
  * @typeparam T - the input type
6
13
  */
7
- export type Protected<T> = T extends readonly (infer E)[]
8
- ? readonly Protected<E>[]
14
+ export type Protected<T> = IsAny<T> extends true
15
+ ? T
16
+ : T extends readonly any[] & infer A
17
+ ? { readonly [K in keyof A]: Protected<A[K]> }
9
18
  : T extends Map<infer K, infer V>
10
19
  ? Map<Protected<K>, Protected<V>>
11
20
  : T extends Set<infer E>
@@ -17,8 +26,20 @@ export type Protected<T> = T extends readonly (infer E)[]
17
26
  : T;
18
27
 
19
28
  /**
20
- * Returns the same value wrapped in the Protected type
29
+ * Returns the same value wrapped in the `Protected` type.
21
30
  * @param value - the value to wrap
31
+ * @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
32
+ * type to a value
33
+ * @example
34
+ * ```ts
35
+ * const obj = Protected({ a: 1, b: { c: true, d: [1] } })
36
+ * obj.a = 2 // compiler error: a is readonly
37
+ * obj.b.c = false // compiler error: c is readonly
38
+ * obj.b.d.push(2) // compiler error: d is a readonly array
39
+ * (obj as any).b.d.push(2) // will actually mutate the object
40
+ * ```
41
+
42
+ * '
22
43
  */
23
44
  export function Protected<T>(value: T): Protected<T> {
24
45
  return value as any;