@rimbu/deep 2.0.0 → 2.0.2

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.
Files changed (56) hide show
  1. package/README.md +37 -27
  2. package/dist/bun/deep.mts +2 -2
  3. package/dist/bun/match.mts +27 -25
  4. package/dist/bun/patch.mts +10 -9
  5. package/dist/bun/path.mts +94 -95
  6. package/dist/bun/protected.mts +18 -17
  7. package/dist/bun/selector.mts +22 -20
  8. package/dist/bun/tuple.mts +1 -1
  9. package/dist/cjs/deep.cjs +197 -576
  10. package/dist/cjs/deep.cjs.map +1 -0
  11. package/dist/cjs/deep.d.cts +284 -0
  12. package/dist/cjs/index.cjs +18 -662
  13. package/dist/cjs/index.cjs.map +1 -0
  14. package/dist/cjs/index.d.cts +18 -0
  15. package/dist/cjs/internal.cjs +8 -582
  16. package/dist/cjs/internal.cjs.map +1 -0
  17. package/dist/cjs/internal.d.cts +7 -0
  18. package/dist/cjs/match.cjs +250 -343
  19. package/dist/cjs/match.cjs.map +1 -0
  20. package/dist/cjs/match.d.cts +140 -0
  21. package/dist/cjs/patch.cjs +120 -93
  22. package/dist/cjs/patch.cjs.map +1 -0
  23. package/dist/cjs/patch.d.cts +89 -0
  24. package/dist/cjs/path.cjs +114 -573
  25. package/dist/cjs/path.cjs.map +1 -0
  26. package/dist/cjs/path.d.cts +199 -0
  27. package/dist/cjs/protected.cjs +2 -17
  28. package/dist/cjs/protected.cjs.map +1 -0
  29. package/dist/cjs/selector.cjs +32 -574
  30. package/dist/cjs/selector.cjs.map +1 -0
  31. package/dist/cjs/selector.d.cts +47 -0
  32. package/dist/cjs/tuple.cjs +162 -71
  33. package/dist/cjs/tuple.cjs.map +1 -0
  34. package/dist/esm/match.mjs.map +1 -1
  35. package/dist/esm/patch.mjs.map +1 -1
  36. package/dist/{types → esm}/path.d.mts +4 -1
  37. package/dist/esm/path.mjs.map +1 -1
  38. package/dist/esm/protected.d.mts +17 -0
  39. package/dist/esm/selector.mjs.map +1 -1
  40. package/dist/esm/tuple.d.mts +142 -0
  41. package/package.json +20 -14
  42. package/src/deep.mts +2 -2
  43. package/src/match.mts +27 -25
  44. package/src/patch.mts +10 -9
  45. package/src/path.mts +94 -95
  46. package/src/protected.mts +18 -17
  47. package/src/selector.mts +22 -20
  48. package/src/tuple.mts +1 -1
  49. /package/dist/{types/protected.d.mts → cjs/protected.d.cts} +0 -0
  50. /package/dist/{types/tuple.d.mts → cjs/tuple.d.cts} +0 -0
  51. /package/dist/{types → esm}/deep.d.mts +0 -0
  52. /package/dist/{types → esm}/index.d.mts +0 -0
  53. /package/dist/{types → esm}/internal.d.mts +0 -0
  54. /package/dist/{types → esm}/match.d.mts +0 -0
  55. /package/dist/{types → esm}/patch.d.mts +0 -0
  56. /package/dist/{types → esm}/selector.d.mts +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deep.cjs","sourceRoot":"","sources":["../../_cjs_prepare/deep.cts"],"names":[],"mappings":";;;AAwBA,0BAEC;AAeD,8BAIC;AAgBD,8BAIC;AAkBD,kCAKC;AAcD,8BAEC;AAgBD,0BAMC;AAiBD,kCAKC;AAeD,gCAIC;AAiBD,4BAUC;AAgBD,oCASC;AA8ID,4BAEC;AA1WD,+CAAsC;AAEtC,yCAAgD;AAAvC,kGAAA,KAAK,OAAA;AACd,yCAAgD;AAAvC,kGAAA,KAAK,OAAA;AACd,uCAAuD;AAA9C,iGAAA,KAAK,OAAA;AAAE,mGAAA,OAAO,OAAA;AAEvB,+CAAuD;AAA9C,sGAAA,MAAM,OAAA;AAEf;;;;;;;;;;;;;;GAcG;AACH,SAAgB,OAAO,CAAI,MAAS;IAClC,OAAO,MAAsB,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,SAAS,CACvB,IAAO;IAEP,OAAO,UAAC,MAAM,IAAK,OAAA,mBAAI,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,EAAxB,CAAwB,CAAC;AAC9C,CA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,CAAC,MAAM,CAAC,mBAAI,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,CAAC,CAAC;AACzD,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,YAAY,CAK1B,IAAO,EACP,QAA4B;IAE5B,OAAO,UAAC,MAAM,IAAK,OAAA,mBAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,EAArC,CAAqC,CAAC;AAC3D,CAAC;AAkID;;;;;;;;;;;GAWG;AACH,SAAgB,QAAQ;IACtB,OAAO,mBAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,284 @@
1
+ import type { Match, Patch, Path, Protected, Selector } from './internal.cjs';
2
+ export { match, type Match } from './match.cjs';
3
+ export { patch, type Patch } from './patch.cjs';
4
+ export { getAt, patchAt, type Path } from './path.cjs';
5
+ export type { Protected } from './protected.cjs';
6
+ export { select, type Selector } from './selector.cjs';
7
+ /**
8
+ * Returns the same value wrapped in the `Protected` type.
9
+ * @typeparam T - the source value type
10
+ * @param source - the value to wrap
11
+ * @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
12
+ * type to a value
13
+ * @example
14
+ * ```ts
15
+ * const obj = Deep.protect({ a: 1, b: { c: true, d: [1] } })
16
+ * obj.a = 2 // compiler error: a is readonly
17
+ * obj.b.c = false // compiler error: c is readonly
18
+ * obj.b.d.push(2) // compiler error: d is a readonly array
19
+ * (obj as any).b.d.push(2) // will actually mutate the object
20
+ * ```
21
+ */
22
+ export declare function protect<T>(source: T): Protected<T>;
23
+ /**
24
+ * Returns a function that gets the value at the given string `path` inside an object.
25
+ * @typeparam T - the input value type
26
+ * @typeparam P - the string literal path type in the object
27
+ * @param path - the string path in the object
28
+ * @param source - the value from which to extract the path value
29
+ * @example
30
+ * ```ts
31
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
32
+ * items.map(Deep.getAtWith('a.c'));
33
+ * // => ['a', 'b']
34
+ * ```
35
+ */
36
+ export declare function getAtWith<T, P extends Path.Get<T>>(path: P): (source: T) => Path.Result<T, P>;
37
+ /**
38
+ * Returns a function that patches a given `source` with the given `patchItems`.
39
+ * @typeparam T - the patch value type
40
+ * @typeparam TE - utility type
41
+ * @typeparam TT - utility type
42
+ * @param patchItem - the `Patch` definition to update the given value of type `T` with.
43
+ * @param source - the value to use the given `patchItem` on.
44
+ * @example
45
+ * ```ts
46
+ * const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }];
47
+ * items.map(Deep.patchWith([{ a: v => v + 1 }]));
48
+ * // => [{ a: 2, b: 'a' }, { a: 3, b: 'b' }]
49
+ * ```
50
+ */
51
+ export declare function patchWith<T, TE extends T = T, TT = T>(patchItem: Patch<TT, TE>): (source: TE) => T;
52
+ /**
53
+ * Returns a function that patches a given `value` with the given `patchItems` at the given `path`.
54
+ * @typeparam T - the patch value type
55
+ * @typeparam P - the string literal path type in the object
56
+ * @typeparam TE - utility type
57
+ * @typeparam TT - utility type
58
+ * @param path - the string path in the object
59
+ * @param patchItem - the `Patch` definition to update the value at the given `path` in `T` with.
60
+ * @param source - the value to use the given `patchItem` on at the given `path`.
61
+ * @example
62
+ * ```ts
63
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
64
+ * items.map(Deep.patchAtWith('a', [{ b: (v) => v + 1 }]));
65
+ * // => [{ a: { b: 2, c: 'a' } }, { a: { b: 3, c: 'b' } }]
66
+ * ```
67
+ */
68
+ export declare function patchAtWith<T, P extends Path.Set<T>, TE extends T = T, TT = T>(path: P, patchItem: Patch<Path.Result<TE, P>, Path.Result<TT, P>>): (source: T) => T;
69
+ /**
70
+ * Returns a function that matches a given `value` with the given `matcher`.
71
+ * @typeparam T - the patch value type
72
+ * @param matcher - a matcher object that matches input values.
73
+ * @param source - the value to use the given `patchItem` on at the given `path`.
74
+ * @example
75
+ * ```ts
76
+ * const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }];
77
+ * items.filter(Deep.matchWith({ a: 2 }));
78
+ * // => [{ a: 2, b: 'b' }]
79
+ * ```
80
+ */
81
+ export declare function matchWith<T>(matcher: Match<T>): (source: T) => boolean;
82
+ /**
83
+ * Returns true if the given `value` object matches the given `matcher` at the given `path`, false otherwise.
84
+ * @typeparam T - the input value type
85
+ * @typeparam P - the string literal path type in the object
86
+ * @param source - the input value
87
+ * @param path - the string path in the object
88
+ * @param matcher - a matcher object or a function taking the matcher API and returning a match object
89
+ * @example
90
+ * ```ts
91
+ * const input = { a: 1, b: { c: true, d: 'a' } }
92
+ * Deep.matchAt(input, 'b', { c: true })
93
+ * // => true
94
+ * ```
95
+ */
96
+ export declare function matchAt<T, P extends Path.Get<T>>(source: T, path: P, matcher: Match<Path.Result<T, P>>): boolean;
97
+ /**
98
+ * Returns a function that matches a given `value` with the given `matcher` at the given string `path`.
99
+ * @typeparam T - the patch value type
100
+ * @typeparam P - the string literal path type in the object
101
+ * @typeparam TE - utility type
102
+ * @param path - the string path in the object
103
+ * @param matcher - a matcher object that matches input values.
104
+ * @param source - the value to use the given `matcher` on at the given `path`.
105
+ * @example
106
+ * ```ts
107
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
108
+ * items.filter(Deep.matchAtWith('a.b', 2));
109
+ * // => [{ a: 2, b: 'b' }]
110
+ * ```
111
+ */
112
+ export declare function matchAtWith<T, P extends Path.Get<T>, TE extends T = T>(path: P, matcher: Match<Path.Result<T & TE, P>>): (source: T) => boolean;
113
+ /**
114
+ * Returns a function that selects a certain shape from a given `value` with the given `selector`.
115
+ * @typeparam T - the patch value type
116
+ * @typeparam SL - the selector shape type
117
+ * @param selector - a shape indicating the selection from the source values
118
+ * @param source - the value to use the given `selector` on.
119
+ * @example
120
+ * ```ts
121
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
122
+ * items.map(Deep.selectWith({ q: 'a.c', z: ['a.b', v => v.a.b + 1] as const }));
123
+ * // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }]
124
+ * ```
125
+ */
126
+ export declare function selectWith<T, SL extends Selector<T>>(selector: Selector.Shape<SL>): (source: T) => Selector.Result<T, SL>;
127
+ /**
128
+ * Returns the result of applying the given `selector` shape to the given `source` value.
129
+ * @typeparam T - the patch value type
130
+ * @typeparam P - the string literal path type in the object
131
+ * @typeparam SL - the selector shape type
132
+ * @param source - the source value to select from
133
+ * @param path - the string path in the object
134
+ * @param selector - a shape indicating the selection from the source value at the given path
135
+ * @example
136
+ * ```ts
137
+ * const item = { a: { b: 1, c: 'a' } };
138
+ * Deep.selectAt(item, 'a', { q: 'c', z: ['b', v => v.b + 1] as const });
139
+ * // => { q: 'a', z: [1, 2] }
140
+ * ```
141
+ */
142
+ export declare function selectAt<T, P extends Path.Get<T>, SL extends Selector<Path.Result<T, P>>>(source: T, path: P, selector: Selector.Shape<SL>): Selector.Result<Path.Result<T, P>, SL>;
143
+ /**
144
+ * Returns a function that selects a certain shape from a given `value` with the given `selector` at the given string `path`.
145
+ * @typeparam T - the patch value type
146
+ * @typeparam P - the string literal path type in the object
147
+ * @typeparam SL - the selector shape type
148
+ * @param path - the string path in the object
149
+ * @param selector - a shape indicating the selection from the source values
150
+ * @example
151
+ * ```ts
152
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
153
+ * items.map(Deep.selectAtWith('a', { q: 'c', z: ['b', v => v.b + 1] as const }));
154
+ * // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }]
155
+ * ```
156
+ */
157
+ export declare function selectAtWith<T, P extends Path.Get<T>, SL extends Selector<Path.Result<T, P>>>(path: P, selector: Selector.Shape<SL>): (source: T) => Selector.Result<Path.Result<T, P>, SL>;
158
+ /**
159
+ * Typed and curried Deep API, used in situations where the target type
160
+ * is known but the value will be applied later.
161
+ * @typeparam T - the target type
162
+ * @example
163
+ * ```ts
164
+ * const s = { a: 1, b: { c: 'a', d: true }};
165
+ * const upd = Deep.withType<typeof s>().patchWith([{ a: (v) => v + 1 }]);
166
+ * upd(s);
167
+ * // => { a: 2, b: { c: 'a', d: true }}
168
+ * ```
169
+ */
170
+ export interface WithType<T> {
171
+ /**
172
+ * Returns a function that given an object returns the value at the given `path`.
173
+ * @typeparam P - a Path in object type T
174
+ * @param path - the path into the object
175
+ * @example
176
+ * ```ts
177
+ * const value = { a: { b: { c: 5 } } }
178
+ * const getValue = Deep.withType<typeof value>().getAtWith('a.b.c');
179
+ * getValue(value);
180
+ * // => 5
181
+ * ```
182
+ */
183
+ getAtWith<P extends Path.Get<T>>(path: P): (source: T) => Path.Result<T, P>;
184
+ /**
185
+ * Returns a function that patches a given `value` with the given `patchItems`.
186
+ * @typeparam TT - utility type
187
+ * @param patchItem - the `Patch` definition to update the given value with
188
+ * @param source - the value to use the given `patchItem` on.
189
+ * @example
190
+ * ```ts
191
+ * const value = { a: 1, b: 'a' };
192
+ * const upd = Deep.withType<typeof value>().patch([{ a: v => v + 1 }]);
193
+ * upd(value);
194
+ * // => { a: 2, b: 'a' }
195
+ * ```
196
+ */
197
+ patchWith<TE extends T = T, TT = T>(patchItem: Patch<TT, TE>): (source: TE) => T;
198
+ /**
199
+ * Returns a function that patches a given `value` with the given `patchItems` at the given `path`.
200
+ * @typeparam P - the string literal path type in the object
201
+ * @typeparam TT - utility type
202
+ * @param path - the string path in the object
203
+ * @param patchItem - the `Patch` definition to update the given value with
204
+ * @param source - the value to use the given `patchItem` on at the given `path`.
205
+ * @example
206
+ * ```ts
207
+ * const value = { a: { b: 1, c: 'a' } };
208
+ * const upd = Deep.withType<typeof value>().patchAtWith('a', [{ b: (v) => v + 1 }])
209
+ * upd(value);
210
+ * // => { a: { b: 2, c: 'a' } }
211
+ * ```
212
+ */
213
+ patchAtWith<P extends Path.Set<T>, TT = T>(path: P, patchItem: Patch<Path.Result<T, P>, Path.Result<TT, P>>): (source: T) => T;
214
+ /**
215
+ * Returns a function that matches a given `value` with the given `matcher`.
216
+ * @param matcher - a matcher object that matches input values.
217
+ * @param source - the value to use the given `matcher` on.
218
+ * @example
219
+ * ```ts
220
+ * const value = { a: 1, b: 'a' };
221
+ * const m = Deep.withType<typeof value>().matchWith({ a: 1 });
222
+ * m(value);
223
+ * // => true
224
+ * ```
225
+ */
226
+ matchWith(matcher: Match<T>): (source: T) => boolean;
227
+ /**
228
+ * Returns a function that matches a given `value` with the given `matcher` at the given string `path`.
229
+ * @typeparam P - the string literal path type in the object
230
+ * @param path - the string path in the object
231
+ * @param matcher - a matcher object that matches input values.
232
+ * @param source - the value to use the given `matcher` on at the given `path`.
233
+ * @example
234
+ * ```ts
235
+ * const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
236
+ * items.filter(Deep.matchAtWith('a.b', 2));
237
+ * // => [{ a: 2, b: 'b' }]
238
+ * ```
239
+ */
240
+ matchAtWith<P extends Path.Get<T>>(path: P, matcher: Match<Path.Result<T, P>>): (source: T) => boolean;
241
+ /**
242
+ * Returns a function that selects a certain shape from a given `value` with the given `selector`.
243
+ * @typeparam SL - the selector shape type
244
+ * @param selector - a shape indicating the selection from the source values
245
+ * @param source - the value to use the given `selector` on.
246
+ * @example
247
+ * ```ts
248
+ * const value = { a: { b: 1, c: 'a' } };
249
+ * const sel = Deep.withType<typeof value>().selectWith({ q: 'a.b' });
250
+ * sel(value);
251
+ * // => { q: 1 }
252
+ * ```
253
+ */
254
+ selectWith<SL extends Selector<T>>(selector: Selector.Shape<SL>): (source: T) => Selector.Result<T, SL>;
255
+ /**
256
+ * Returns a function that selects a certain shape from a given `value` with the given `selector` at the given string `path`.
257
+ * @typeparam P - the string literal path type in the object
258
+ * @typeparam SL - the selector shape type
259
+ * @param path - the string path in the object
260
+ * @param selector - a shape indicating the selection from the source values
261
+ * @param source - the value to use the given `selector` on at the given `path`.
262
+ * @example
263
+ * ```ts
264
+ * const value = { a: { b: 1, c: 'a' } };
265
+ * const sel = Deep.withType<typeof value>().selectAtWith('a', { q: 'b' });
266
+ * sel(value);
267
+ * // => { q: 1 }
268
+ * ```
269
+ */
270
+ selectAtWith<P extends Path.Get<T>, SL extends Selector<Path.Result<T, P>>>(path: P, selector: Selector.Shape<SL>): (source: T) => Selector.Result<Path.Result<T, P>, SL>;
271
+ }
272
+ /**
273
+ * Returns a curried API with a known target type. This can be useful for using the methods in
274
+ * contexts where the target type can be inferred from the usage.
275
+ * @typeparam T - the target type
276
+ * @example
277
+ * ```ts
278
+ * const s = { a: 1, b: { c: 'a', d: true }}
279
+ * const upd = Deep.withType<typeof s>().patchWith([{ d: (v) => !v }])
280
+ * upd(s)
281
+ * // => { a: 1, b: { c: 'a', d: false }}
282
+ * ```
283
+ */
284
+ export declare function withType<T>(): WithType<T>;