@rimbu/deep 0.14.5 → 1.0.0-alpha.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/{src/deep.ts → dist/bun/deep.mts} +7 -7
- package/{src/index.ts → dist/bun/index.mts} +5 -5
- package/dist/bun/internal.mts +8 -0
- package/{src/match.ts → dist/bun/match.mts} +6 -6
- package/{src/patch.ts → dist/bun/patch.mts} +9 -4
- package/{src/path.ts → dist/bun/path.mts} +2 -2
- package/{src/selector.ts → dist/bun/selector.mts} +1 -1
- package/dist/cjs/deep.js +590 -0
- package/dist/cjs/index.js +663 -0
- package/dist/cjs/internal.js +583 -0
- package/dist/cjs/match.js +376 -0
- package/dist/cjs/patch.js +105 -0
- package/dist/cjs/path.js +585 -0
- package/dist/cjs/protected.js +18 -0
- package/dist/cjs/selector.js +581 -0
- package/dist/cjs/tuple.js +73 -0
- package/dist/{module/deep.js → esm/deep.mjs} +6 -6
- package/dist/esm/deep.mjs.map +1 -0
- package/dist/{module/index.js → esm/index.mjs} +5 -5
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/internal.mjs +5 -0
- package/dist/esm/internal.mjs.map +1 -0
- package/dist/{module/match.js → esm/match.mjs} +21 -21
- package/dist/esm/match.mjs.map +1 -0
- package/dist/{module/patch.js → esm/patch.mjs} +5 -5
- package/dist/esm/patch.mjs.map +1 -0
- package/dist/{module/path.js → esm/path.mjs} +2 -2
- package/dist/esm/path.mjs.map +1 -0
- package/dist/esm/protected.mjs +2 -0
- package/dist/esm/protected.mjs.map +1 -0
- package/dist/{module/selector.js → esm/selector.mjs} +2 -2
- package/dist/esm/selector.mjs.map +1 -0
- package/dist/{module/tuple.js → esm/tuple.mjs} +1 -1
- package/dist/esm/tuple.mjs.map +1 -0
- package/dist/types/{deep.d.ts → deep.d.mts} +6 -6
- package/dist/types/{index.d.ts → index.d.mts} +5 -5
- package/dist/types/internal.d.mts +7 -0
- package/dist/types/{match.d.ts → match.d.mts} +3 -3
- package/dist/types/{patch.d.ts → patch.d.mts} +3 -3
- package/dist/types/{path.d.ts → path.d.mts} +2 -2
- package/dist/types/{selector.d.ts → selector.d.mts} +1 -1
- package/package.json +29 -23
- package/src/deep.mts +364 -0
- package/src/index.mts +23 -0
- package/src/internal.mts +8 -0
- package/src/match.mts +700 -0
- package/src/patch.mts +262 -0
- package/src/path.mts +430 -0
- package/src/protected.mts +30 -0
- package/src/selector.mts +90 -0
- package/src/tuple.mts +197 -0
- package/dist/main/deep.js +0 -211
- package/dist/main/deep.js.map +0 -1
- package/dist/main/index.js +0 -19
- package/dist/main/index.js.map +0 -1
- package/dist/main/internal.js +0 -9
- package/dist/main/internal.js.map +0 -1
- package/dist/main/match.js +0 -284
- package/dist/main/match.js.map +0 -1
- package/dist/main/patch.js +0 -133
- package/dist/main/patch.js.map +0 -1
- package/dist/main/path.js +0 -126
- package/dist/main/path.js.map +0 -1
- package/dist/main/protected.js +0 -3
- package/dist/main/protected.js.map +0 -1
- package/dist/main/selector.js +0 -40
- package/dist/main/selector.js.map +0 -1
- package/dist/main/tuple.js +0 -164
- package/dist/main/tuple.js.map +0 -1
- package/dist/module/deep.js.map +0 -1
- package/dist/module/index.js.map +0 -1
- package/dist/module/internal.js +0 -4
- package/dist/module/internal.js.map +0 -1
- package/dist/module/match.js.map +0 -1
- package/dist/module/patch.js.map +0 -1
- package/dist/module/path.js.map +0 -1
- package/dist/module/protected.js +0 -2
- package/dist/module/protected.js.map +0 -1
- package/dist/module/selector.js.map +0 -1
- package/dist/module/tuple.js.map +0 -1
- package/dist/types/internal.d.ts +0 -7
- package/src/internal.ts +0 -8
- /package/{src/protected.ts → dist/bun/protected.mts} +0 -0
- /package/{src/tuple.ts → dist/bun/tuple.mts} +0 -0
- /package/dist/types/{protected.d.ts → protected.d.mts} +0 -0
- /package/dist/types/{tuple.d.ts → tuple.d.mts} +0 -0
package/src/deep.mts
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import type { Match, Patch, Path, Protected, Selector } from './internal.mjs';
|
|
2
|
+
import { Deep } from './internal.mjs';
|
|
3
|
+
|
|
4
|
+
export { match, type Match } from './match.mjs';
|
|
5
|
+
export { patch, type Patch } from './patch.mjs';
|
|
6
|
+
export { getAt, patchAt, type Path } from './path.mjs';
|
|
7
|
+
export type { Protected } from './protected.mjs';
|
|
8
|
+
export { select, type Selector } from './selector.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns the same value wrapped in the `Protected` type.
|
|
12
|
+
* @typeparam T - the source value type
|
|
13
|
+
* @param source - the value to wrap
|
|
14
|
+
* @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
|
|
15
|
+
* type to a value
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const obj = Deep.protect({ a: 1, b: { c: true, d: [1] } })
|
|
19
|
+
* obj.a = 2 // compiler error: a is readonly
|
|
20
|
+
* obj.b.c = false // compiler error: c is readonly
|
|
21
|
+
* obj.b.d.push(2) // compiler error: d is a readonly array
|
|
22
|
+
* (obj as any).b.d.push(2) // will actually mutate the object
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function protect<T>(source: T): Protected<T> {
|
|
26
|
+
return source as Protected<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns a function that gets the value at the given string `path` inside an object.
|
|
31
|
+
* @typeparam T - the input value type
|
|
32
|
+
* @typeparam P - the string literal path type in the object
|
|
33
|
+
* @param path - the string path in the object
|
|
34
|
+
* @param source - the value from which to extract the path value
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
38
|
+
* items.map(Deep.getAtWith('a.c'));
|
|
39
|
+
* // => ['a', 'b']
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function getAtWith<T, P extends Path.Get<T>>(
|
|
43
|
+
path: P
|
|
44
|
+
): (source: T) => Path.Result<T, P> {
|
|
45
|
+
return (source) => Deep.getAt(source, path);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns a function that patches a given `source` with the given `patchItems`.
|
|
50
|
+
* @typeparam T - the patch value type
|
|
51
|
+
* @typeparam TE - utility type
|
|
52
|
+
* @typeparam TT - utility type
|
|
53
|
+
* @param patchItem - the `Patch` definition to update the given value of type `T` with.
|
|
54
|
+
* @param source - the value to use the given `patchItem` on.
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }];
|
|
58
|
+
* items.map(Deep.patchWith([{ a: v => v + 1 }]));
|
|
59
|
+
* // => [{ a: 2, b: 'a' }, { a: 3, b: 'b' }]
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function patchWith<T, TE extends T = T, TT = T>(
|
|
63
|
+
patchItem: Patch<TT, TE>
|
|
64
|
+
): (source: TE) => T {
|
|
65
|
+
return (source) => Deep.patch(source, patchItem as any);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Returns a function that patches a given `value` with the given `patchItems` at the given `path`.
|
|
70
|
+
* @typeparam T - the patch value type
|
|
71
|
+
* @typeparam P - the string literal path type in the object
|
|
72
|
+
* @typeparam TE - utility type
|
|
73
|
+
* @typeparam TT - utility type
|
|
74
|
+
* @param path - the string path in the object
|
|
75
|
+
* @param patchItem - the `Patch` definition to update the value at the given `path` in `T` with.
|
|
76
|
+
* @param source - the value to use the given `patchItem` on at the given `path`.
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
80
|
+
* items.map(Deep.patchAtWith('a', [{ b: (v) => v + 1 }]));
|
|
81
|
+
* // => [{ a: { b: 2, c: 'a' } }, { a: { b: 3, c: 'b' } }]
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function patchAtWith<T, P extends Path.Set<T>, TE extends T = T, TT = T>(
|
|
85
|
+
path: P,
|
|
86
|
+
patchItem: Patch<Path.Result<TE, P>, Path.Result<TT, P>>
|
|
87
|
+
): (source: T) => T {
|
|
88
|
+
return (source) => Deep.patchAt(source, path, patchItem as any);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns a function that matches a given `value` with the given `matcher`.
|
|
93
|
+
* @typeparam T - the patch value type
|
|
94
|
+
* @param matcher - a matcher object that matches input values.
|
|
95
|
+
* @param source - the value to use the given `patchItem` on at the given `path`.
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }];
|
|
99
|
+
* items.filter(Deep.matchWith({ a: 2 }));
|
|
100
|
+
* // => [{ a: 2, b: 'b' }]
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function matchWith<T>(matcher: Match<T>): (source: T) => boolean {
|
|
104
|
+
return (source) => Deep.match(source, matcher);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns true if the given `value` object matches the given `matcher` at the given `path`, false otherwise.
|
|
109
|
+
* @typeparam T - the input value type
|
|
110
|
+
* @typeparam P - the string literal path type in the object
|
|
111
|
+
* @param source - the input value
|
|
112
|
+
* @param path - the string path in the object
|
|
113
|
+
* @param matcher - a matcher object or a function taking the matcher API and returning a match object
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const input = { a: 1, b: { c: true, d: 'a' } }
|
|
117
|
+
* Deep.matchAt(input, 'b', { c: true })
|
|
118
|
+
* // => true
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function matchAt<T, P extends Path.Get<T>>(
|
|
122
|
+
source: T,
|
|
123
|
+
path: P,
|
|
124
|
+
matcher: Match<Path.Result<T, P>>
|
|
125
|
+
): boolean {
|
|
126
|
+
return Deep.match(Deep.getAt(source, path), matcher);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Returns a function that matches a given `value` with the given `matcher` at the given string `path`.
|
|
131
|
+
* @typeparam T - the patch value type
|
|
132
|
+
* @typeparam P - the string literal path type in the object
|
|
133
|
+
* @typeparam TE - utility type
|
|
134
|
+
* @param path - the string path in the object
|
|
135
|
+
* @param matcher - a matcher object that matches input values.
|
|
136
|
+
* @param source - the value to use the given `matcher` on at the given `path`.
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
140
|
+
* items.filter(Deep.matchAtWith('a.b', 2));
|
|
141
|
+
* // => [{ a: 2, b: 'b' }]
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function matchAtWith<T, P extends Path.Get<T>, TE extends T = T>(
|
|
145
|
+
path: P,
|
|
146
|
+
matcher: Match<Path.Result<T & TE, P>>
|
|
147
|
+
): (source: T) => boolean {
|
|
148
|
+
return (source) => Deep.matchAt(source, path, matcher as any);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns a function that selects a certain shape from a given `value` with the given `selector`.
|
|
153
|
+
* @typeparam T - the patch value type
|
|
154
|
+
* @typeparam SL - the selector shape type
|
|
155
|
+
* @param selector - a shape indicating the selection from the source values
|
|
156
|
+
* @param source - the value to use the given `selector` on.
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
160
|
+
* items.map(Deep.selectWith({ q: 'a.c', z: ['a.b', v => v.a.b + 1] as const }));
|
|
161
|
+
* // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }]
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export function selectWith<T, SL extends Selector<T>>(
|
|
165
|
+
selector: Selector.Shape<SL>
|
|
166
|
+
): (source: T) => Selector.Result<T, SL> {
|
|
167
|
+
return (source) => Deep.select(source, selector);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns the result of applying the given `selector` shape to the given `source` value.
|
|
172
|
+
* @typeparam T - the patch value type
|
|
173
|
+
* @typeparam P - the string literal path type in the object
|
|
174
|
+
* @typeparam SL - the selector shape type
|
|
175
|
+
* @param source - the source value to select from
|
|
176
|
+
* @param path - the string path in the object
|
|
177
|
+
* @param selector - a shape indicating the selection from the source value at the given path
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const item = { a: { b: 1, c: 'a' } };
|
|
181
|
+
* Deep.selectAt(item, 'a', { q: 'c', z: ['b', v => v.b + 1] as const });
|
|
182
|
+
* // => { q: 'a', z: [1, 2] }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export function selectAt<
|
|
186
|
+
T,
|
|
187
|
+
P extends Path.Get<T>,
|
|
188
|
+
SL extends Selector<Path.Result<T, P>>
|
|
189
|
+
>(
|
|
190
|
+
source: T,
|
|
191
|
+
path: P,
|
|
192
|
+
selector: Selector.Shape<SL>
|
|
193
|
+
): Selector.Result<Path.Result<T, P>, SL> {
|
|
194
|
+
return Deep.select(Deep.getAt(source, path), selector);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Returns a function that selects a certain shape from a given `value` with the given `selector` at the given string `path`.
|
|
199
|
+
* @typeparam T - the patch value type
|
|
200
|
+
* @typeparam P - the string literal path type in the object
|
|
201
|
+
* @typeparam SL - the selector shape type
|
|
202
|
+
* @param path - the string path in the object
|
|
203
|
+
* @param selector - a shape indicating the selection from the source values
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
207
|
+
* items.map(Deep.selectAtWith('a', { q: 'c', z: ['b', v => v.b + 1] as const }));
|
|
208
|
+
* // => [{ q: 'a', z: [1, 2] }, { q: 'b', z: [2, 3] }]
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export function selectAtWith<
|
|
212
|
+
T,
|
|
213
|
+
P extends Path.Get<T>,
|
|
214
|
+
SL extends Selector<Path.Result<T, P>>
|
|
215
|
+
>(
|
|
216
|
+
path: P,
|
|
217
|
+
selector: Selector.Shape<SL>
|
|
218
|
+
): (source: T) => Selector.Result<Path.Result<T, P>, SL> {
|
|
219
|
+
return (source) => Deep.selectAt(source, path, selector);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Typed and curried Deep API, used in situations where the target type
|
|
224
|
+
* is known but the value will be applied later.
|
|
225
|
+
* @typeparam T - the target type
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* const s = { a: 1, b: { c: 'a', d: true }};
|
|
229
|
+
* const upd = Deep.withType<typeof s>().patchWith([{ a: (v) => v + 1 }]);
|
|
230
|
+
* upd(s);
|
|
231
|
+
* // => { a: 2, b: { c: 'a', d: true }}
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
export interface WithType<T> {
|
|
235
|
+
/**
|
|
236
|
+
* Returns a function that given an object returns the value at the given `path`.
|
|
237
|
+
* @typeparam P - a Path in object type T
|
|
238
|
+
* @param path - the path into the object
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* const value = { a: { b: { c: 5 } } }
|
|
242
|
+
* const getValue = Deep.withType<typeof value>().getAtWith('a.b.c');
|
|
243
|
+
* getValue(value);
|
|
244
|
+
* // => 5
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
getAtWith<P extends Path.Get<T>>(path: P): (source: T) => Path.Result<T, P>;
|
|
248
|
+
/**
|
|
249
|
+
* Returns a function that patches a given `value` with the given `patchItems`.
|
|
250
|
+
* @typeparam TT - utility type
|
|
251
|
+
* @param patchItem - the `Patch` definition to update the given value with
|
|
252
|
+
* @param source - the value to use the given `patchItem` on.
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* const value = { a: 1, b: 'a' };
|
|
256
|
+
* const upd = Deep.withType<typeof value>().patch([{ a: v => v + 1 }]);
|
|
257
|
+
* upd(value);
|
|
258
|
+
* // => { a: 2, b: 'a' }
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
patchWith<TE extends T = T, TT = T>(
|
|
262
|
+
patchItem: Patch<TT, TE>
|
|
263
|
+
): (source: TE) => T;
|
|
264
|
+
/**
|
|
265
|
+
* Returns a function that patches a given `value` with the given `patchItems` at the given `path`.
|
|
266
|
+
* @typeparam P - the string literal path type in the object
|
|
267
|
+
* @typeparam TT - utility type
|
|
268
|
+
* @param path - the string path in the object
|
|
269
|
+
* @param patchItem - the `Patch` definition to update the given value with
|
|
270
|
+
* @param source - the value to use the given `patchItem` on at the given `path`.
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* const value = { a: { b: 1, c: 'a' } };
|
|
274
|
+
* const upd = Deep.withType<typeof value>().patchAtWith('a', [{ b: (v) => v + 1 }])
|
|
275
|
+
* upd(value);
|
|
276
|
+
* // => { a: { b: 2, c: 'a' } }
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
patchAtWith<P extends Path.Set<T>, TT = T>(
|
|
280
|
+
path: P,
|
|
281
|
+
patchItem: Patch<Path.Result<T, P>, Path.Result<TT, P>>
|
|
282
|
+
): (source: T) => T;
|
|
283
|
+
/**
|
|
284
|
+
* Returns a function that matches a given `value` with the given `matcher`.
|
|
285
|
+
* @param matcher - a matcher object that matches input values.
|
|
286
|
+
* @param source - the value to use the given `matcher` on.
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* const value = { a: 1, b: 'a' };
|
|
290
|
+
* const m = Deep.withType<typeof value>().matchWith({ a: 1 });
|
|
291
|
+
* m(value);
|
|
292
|
+
* // => true
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
matchWith(matcher: Match<T>): (source: T) => boolean;
|
|
296
|
+
/**
|
|
297
|
+
* Returns a function that matches a given `value` with the given `matcher` at the given string `path`.
|
|
298
|
+
* @typeparam P - the string literal path type in the object
|
|
299
|
+
* @param path - the string path in the object
|
|
300
|
+
* @param matcher - a matcher object that matches input values.
|
|
301
|
+
* @param source - the value to use the given `matcher` on at the given `path`.
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* const items = [{ a: { b: 1, c: 'a' } }, { a: { b: 2, c: 'b' } }];
|
|
305
|
+
* items.filter(Deep.matchAtWith('a.b', 2));
|
|
306
|
+
* // => [{ a: 2, b: 'b' }]
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
matchAtWith<P extends Path.Get<T>>(
|
|
310
|
+
path: P,
|
|
311
|
+
matcher: Match<Path.Result<T, P>>
|
|
312
|
+
): (source: T) => boolean;
|
|
313
|
+
/**
|
|
314
|
+
* Returns a function that selects a certain shape from a given `value` with the given `selector`.
|
|
315
|
+
* @typeparam SL - the selector shape type
|
|
316
|
+
* @param selector - a shape indicating the selection from the source values
|
|
317
|
+
* @param source - the value to use the given `selector` on.
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* const value = { a: { b: 1, c: 'a' } };
|
|
321
|
+
* const sel = Deep.withType<typeof value>().selectWith({ q: 'a.b' });
|
|
322
|
+
* sel(value);
|
|
323
|
+
* // => { q: 1 }
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
selectWith<SL extends Selector<T>>(
|
|
327
|
+
selector: Selector.Shape<SL>
|
|
328
|
+
): (source: T) => Selector.Result<T, SL>;
|
|
329
|
+
/**
|
|
330
|
+
* Returns a function that selects a certain shape from a given `value` with the given `selector` at the given string `path`.
|
|
331
|
+
* @typeparam P - the string literal path type in the object
|
|
332
|
+
* @typeparam SL - the selector shape type
|
|
333
|
+
* @param path - the string path in the object
|
|
334
|
+
* @param selector - a shape indicating the selection from the source values
|
|
335
|
+
* @param source - the value to use the given `selector` on at the given `path`.
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* const value = { a: { b: 1, c: 'a' } };
|
|
339
|
+
* const sel = Deep.withType<typeof value>().selectAtWith('a', { q: 'b' });
|
|
340
|
+
* sel(value);
|
|
341
|
+
* // => { q: 1 }
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
selectAtWith<P extends Path.Get<T>, SL extends Selector<Path.Result<T, P>>>(
|
|
345
|
+
path: P,
|
|
346
|
+
selector: Selector.Shape<SL>
|
|
347
|
+
): (source: T) => Selector.Result<Path.Result<T, P>, SL>;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Returns a curried API with a known target type. This can be useful for using the methods in
|
|
352
|
+
* contexts where the target type can be inferred from the usage.
|
|
353
|
+
* @typeparam T - the target type
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* const s = { a: 1, b: { c: 'a', d: true }}
|
|
357
|
+
* const upd = Deep.withType<typeof s>().patchWith([{ d: (v) => !v }])
|
|
358
|
+
* upd(s)
|
|
359
|
+
* // => { a: 1, b: { c: 'a', d: false }}
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
export function withType<T>(): WithType<T> {
|
|
363
|
+
return Deep;
|
|
364
|
+
}
|
package/src/index.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* The `@rimbu/deep` package provides utilities to patch and match plain JavaScript objects.<br/>
|
|
5
|
+
* <br/>
|
|
6
|
+
* See the [Rimbu docs Deep overview page](/docs/deep/overview) for more information.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export { Tuple } from './tuple.mjs';
|
|
10
|
+
|
|
11
|
+
export type { Protected, Patch } from './internal.mjs';
|
|
12
|
+
export { Path, type Selector, type Match } from './internal.mjs';
|
|
13
|
+
|
|
14
|
+
export * from './deep.mjs';
|
|
15
|
+
|
|
16
|
+
import * as Deep from './deep.mjs';
|
|
17
|
+
export {
|
|
18
|
+
/**
|
|
19
|
+
* Convenience namespace offering access to most common functions used in the `@rimbu/deep` package.
|
|
20
|
+
* These are mainly utilities to patch and match plain JavaScript objects.
|
|
21
|
+
*/
|
|
22
|
+
Deep,
|
|
23
|
+
};
|
package/src/internal.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { Protected } from './protected.mjs';
|
|
2
|
+
export { Path } from './path.mjs';
|
|
3
|
+
export { type Match } from './match.mjs';
|
|
4
|
+
export type { Patch } from './patch.mjs';
|
|
5
|
+
export type { Selector } from './selector.mjs';
|
|
6
|
+
|
|
7
|
+
import * as Deep from './deep.mjs';
|
|
8
|
+
export { Deep };
|