@rimbu/deep 2.0.1 → 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.
package/src/path.mts CHANGED
@@ -36,7 +36,7 @@ export namespace Path {
36
36
  T,
37
37
  Write extends boolean,
38
38
  Maybe extends boolean,
39
- First extends boolean = false
39
+ First extends boolean = false,
40
40
  > = `${IsAnyFunc<T> extends true
41
41
  ? // functions can not be further decomposed
42
42
  ''
@@ -54,20 +54,21 @@ export namespace Path {
54
54
  T,
55
55
  Write extends boolean,
56
56
  Maybe extends boolean,
57
- First extends boolean
58
- > = Path.Internal.IsOptional<T> extends true
59
- ? // the value T may be null or undefined, check whether further chaining is allowed
60
- Write extends false
61
- ? // path is not used to write to, so optional chaining is allowed
62
- Path.Internal.Generic<Exclude<T, undefined | null>, Write, true>
63
- : // path can be written to, no optional chaining allowed
64
- never
65
- : // determine separator, and continue with non-optional value
66
- `${Path.Internal.Separator<
67
- First,
68
- Maybe,
69
- IsArray<T>
70
- >}${Path.Internal.NonOptional<T, Write, Maybe>}`;
57
+ First extends boolean,
58
+ > =
59
+ Path.Internal.IsOptional<T> extends true
60
+ ? // the value T may be null or undefined, check whether further chaining is allowed
61
+ Write extends false
62
+ ? // path is not used to write to, so optional chaining is allowed
63
+ Path.Internal.Generic<Exclude<T, undefined | null>, Write, true>
64
+ : // path can be written to, no optional chaining allowed
65
+ never
66
+ : // determine separator, and continue with non-optional value
67
+ `${Path.Internal.Separator<
68
+ First,
69
+ Maybe,
70
+ IsArray<T>
71
+ >}${Path.Internal.NonOptional<T, Write, Maybe>}`;
71
72
 
72
73
  /**
73
74
  * Determines the allowed paths into a non-optional value of type `T`.
@@ -76,25 +77,22 @@ export namespace Path {
76
77
  * @typeparam Maybe - if true the value at the current path is optional
77
78
  * @typeparam First - if true this is the root call
78
79
  */
79
- export type NonOptional<
80
- T,
81
- Write extends boolean,
82
- Maybe extends boolean
83
- > = Tuple.IsTuple<T> extends true
84
- ? // determine allowed paths for tuple
85
- Path.Internal.Tup<T, Write, Maybe>
86
- : T extends readonly any[]
87
- ? // determine allowed paths for array
88
- Write extends false
89
- ? // path is not writable so arrays are allowed
90
- Path.Internal.Arr<T>
91
- : // path is writable, no arrays allowed
92
- never
93
- : IsPlainObj<T> extends true
94
- ? // determine allowed paths for object
95
- Path.Internal.Obj<T, Write, Maybe>
96
- : // no match
97
- never;
80
+ export type NonOptional<T, Write extends boolean, Maybe extends boolean> =
81
+ Tuple.IsTuple<T> extends true
82
+ ? // determine allowed paths for tuple
83
+ Path.Internal.Tup<T, Write, Maybe>
84
+ : T extends readonly any[]
85
+ ? // determine allowed paths for array
86
+ Write extends false
87
+ ? // path is not writable so arrays are allowed
88
+ Path.Internal.Arr<T>
89
+ : // path is writable, no arrays allowed
90
+ never
91
+ : IsPlainObj<T> extends true
92
+ ? // determine allowed paths for object
93
+ Path.Internal.Obj<T, Write, Maybe>
94
+ : // no match
95
+ never;
98
96
 
99
97
  /**
100
98
  * Determines the allowed paths for a tuple. Since tuples have fixed types, they do not
@@ -104,11 +102,7 @@ export namespace Path {
104
102
  * @typeparam Maybe - if true the value at the current path is optional
105
103
  */
106
104
  export type Tup<T, Write extends boolean, Maybe extends boolean> = {
107
- [K in Tuple.KeysOf<T>]: `[${K}]${Path.Internal.Generic<
108
- T[K],
109
- Write,
110
- Maybe
111
- >}`;
105
+ [K in Tuple.KeysOf<T>]: `[${K}]${Path.Internal.Generic<T[K], Write, Maybe>}`;
112
106
  }[Tuple.KeysOf<T>];
113
107
 
114
108
  /**
@@ -143,7 +137,7 @@ export namespace Path {
143
137
  export type Separator<
144
138
  First extends boolean,
145
139
  Maybe extends boolean,
146
- IsArray extends boolean
140
+ IsArray extends boolean,
147
141
  > = Maybe extends true
148
142
  ? First extends true
149
143
  ? // first optional value cannot have separator
@@ -151,13 +145,13 @@ export namespace Path {
151
145
  : // non-first optional value must have separator
152
146
  '?.'
153
147
  : First extends true
154
- ? // first non-optional value has empty separator
155
- ''
156
- : IsArray extends true
157
- ? // array selectors do not have separator
158
- ''
159
- : // normal separator
160
- '.';
148
+ ? // first non-optional value has empty separator
149
+ ''
150
+ : IsArray extends true
151
+ ? // array selectors do not have separator
152
+ ''
153
+ : // normal separator
154
+ '.';
161
155
 
162
156
  /**
163
157
  * Determines whether the given type `T` is optional, that is, whether it can be null or undefined.
@@ -169,10 +163,10 @@ export namespace Path {
169
163
  ? // is optional
170
164
  True
171
165
  : null extends T
172
- ? // is optional
173
- True
174
- : // not optional
175
- False;
166
+ ? // is optional
167
+ True
168
+ : // not optional
169
+ False;
176
170
 
177
171
  /**
178
172
  * Returns type `T` if `Maybe` is false, `T | undefined` otherwise.
@@ -190,7 +184,7 @@ export namespace Path {
190
184
  */
191
185
  export type AppendIfNotEmpty<
192
186
  A extends string[],
193
- T extends string
187
+ T extends string,
194
188
  > = T extends ''
195
189
  ? // empty string, do not add
196
190
  A
@@ -224,23 +218,23 @@ export namespace Path {
224
218
  export type For<
225
219
  T,
226
220
  Tokens,
227
- Maybe extends boolean = Path.Internal.IsOptional<T>
221
+ Maybe extends boolean = Path.Internal.IsOptional<T>,
228
222
  > = Tokens extends []
229
223
  ? // no more token
230
224
  Path.Internal.MaybeValue<T, Maybe>
231
225
  : Path.Internal.IsOptional<T> extends true
232
- ? // T can be null or undefined, so continue with Maybe set to true
233
- Path.Result.For<Exclude<T, undefined | null>, Tokens, Maybe>
234
- : Tokens extends ['?.', infer Key, ...infer Rest]
235
- ? // optional chaining, process first part and set Maybe to true
236
- Path.Result.For<Path.Result.Part<T, Key, Maybe>, Rest, true>
237
- : Tokens extends ['.', infer Key, ...infer Rest]
238
- ? // normal chaining, process first part and continue
239
- Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe>
240
- : Tokens extends [infer Key, ...infer Rest]
241
- ? // process first part, and continue
242
- Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe>
243
- : never;
226
+ ? // T can be null or undefined, so continue with Maybe set to true
227
+ Path.Result.For<Exclude<T, undefined | null>, Tokens, Maybe>
228
+ : Tokens extends ['?.', infer Key, ...infer Rest]
229
+ ? // optional chaining, process first part and set Maybe to true
230
+ Path.Result.For<Path.Result.Part<T, Key, Maybe>, Rest, true>
231
+ : Tokens extends ['.', infer Key, ...infer Rest]
232
+ ? // normal chaining, process first part and continue
233
+ Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe>
234
+ : Tokens extends [infer Key, ...infer Rest]
235
+ ? // process first part, and continue
236
+ Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe>
237
+ : never;
244
238
 
245
239
  /**
246
240
  * Determines the result of getting the property/index `K` from type `T`, taking into
@@ -249,15 +243,16 @@ export namespace Path {
249
243
  * @typeparam K - the key to get from the source type
250
244
  * @typeparam Maybe - if true indicates that the path may be undefined
251
245
  */
252
- export type Part<T, K, Maybe extends boolean> = IsArray<T> extends true
253
- ? // for arrays, Maybe needs to be set to true to force optional chaining
254
- // for tuples, Maybe should be false
255
- Path.Internal.MaybeValue<
256
- T[K & keyof T],
257
- Tuple.IsTuple<T> extends true ? Maybe : true
258
- >
259
- : // Return the type at the given key, and take `Maybe` into account
260
- Path.Internal.MaybeValue<T[K & keyof T], Maybe>;
246
+ export type Part<T, K, Maybe extends boolean> =
247
+ IsArray<T> extends true
248
+ ? // for arrays, Maybe needs to be set to true to force optional chaining
249
+ // for tuples, Maybe should be false
250
+ Path.Internal.MaybeValue<
251
+ T[K & keyof T],
252
+ Tuple.IsTuple<T> extends true ? Maybe : true
253
+ >
254
+ : // Return the type at the given key, and take `Maybe` into account
255
+ Path.Internal.MaybeValue<T[K & keyof T], Maybe>;
261
256
 
262
257
  /**
263
258
  * Converts a path string into separate tokens in a string array.
@@ -268,31 +263,35 @@ export namespace Path {
268
263
  export type Tokenize<
269
264
  P extends string,
270
265
  Token extends string = '',
271
- Res extends string[] = []
266
+ Res extends string[] = [],
272
267
  > = P extends ''
273
268
  ? // no more input to process, return result
274
269
  Path.Internal.AppendIfNotEmpty<Res, Token>
275
270
  : P extends `[${infer Index}]${infer Rest}`
276
- ? // input is an array selector, append index to tokens. Continue with new token
277
- Tokenize<
278
- Rest,
279
- '',
280
- [...Path.Internal.AppendIfNotEmpty<Res, Token>, Index]
281
- >
282
- : P extends `?.${infer Rest}`
283
- ? // optional chaining, append to tokens. Continue with new token
284
- Tokenize<
285
- Rest,
286
- '',
287
- [...Path.Internal.AppendIfNotEmpty<Res, Token>, '?.']
288
- >
289
- : P extends `.${infer Rest}`
290
- ? // normal chaining, append to tokens. Continue with new token
291
- Tokenize<Rest, '', [...Path.Internal.AppendIfNotEmpty<Res, Token>, '.']>
292
- : P extends `${infer First}${infer Rest}`
293
- ? // process next character
294
- Tokenize<Rest, `${Token}${First}`, Res>
295
- : never;
271
+ ? // input is an array selector, append index to tokens. Continue with new token
272
+ Tokenize<
273
+ Rest,
274
+ '',
275
+ [...Path.Internal.AppendIfNotEmpty<Res, Token>, Index]
276
+ >
277
+ : P extends `?.${infer Rest}`
278
+ ? // optional chaining, append to tokens. Continue with new token
279
+ Tokenize<
280
+ Rest,
281
+ '',
282
+ [...Path.Internal.AppendIfNotEmpty<Res, Token>, '?.']
283
+ >
284
+ : P extends `.${infer Rest}`
285
+ ? // normal chaining, append to tokens. Continue with new token
286
+ Tokenize<
287
+ Rest,
288
+ '',
289
+ [...Path.Internal.AppendIfNotEmpty<Res, Token>, '.']
290
+ >
291
+ : P extends `${infer First}${infer Rest}`
292
+ ? // process next character
293
+ Tokenize<Rest, `${Token}${First}`, Res>
294
+ : never;
296
295
  }
297
296
 
298
297
  /**
package/src/protected.mts CHANGED
@@ -11,20 +11,21 @@ import type { IsAny, IsPlainObj } from '@rimbu/base';
11
11
  * - Any other type will not be mapped
12
12
  * @typeparam T - the input type
13
13
  */
14
- export type Protected<T> = IsAny<T> extends true
15
- ? // to prevent infinite recursion, any will be any
16
- T
17
- : T extends readonly any[] & infer A
18
- ? // convert all keys to readonly and all values to `Protected`
19
- { readonly [K in keyof A]: Protected<A[K]> }
20
- : T extends Map<infer K, infer V>
21
- ? ReadonlyMap<Protected<K>, Protected<V>>
22
- : T extends Set<infer E>
23
- ? ReadonlySet<Protected<E>>
24
- : T extends Promise<infer E>
25
- ? Promise<Protected<E>>
26
- : IsPlainObj<T> extends true
27
- ? // convert all keys to readonly and all values to `Protected`
28
- { readonly [K in keyof T]: Protected<T[K]> }
29
- : // nothing to do, just return `T`
30
- T;
14
+ export type Protected<T> =
15
+ IsAny<T> extends true
16
+ ? // to prevent infinite recursion, any will be any
17
+ T
18
+ : T extends readonly any[] & infer A
19
+ ? // convert all keys to readonly and all values to `Protected`
20
+ { readonly [K in keyof A]: Protected<A[K]> }
21
+ : T extends Map<infer K, infer V>
22
+ ? ReadonlyMap<Protected<K>, Protected<V>>
23
+ : T extends Set<infer E>
24
+ ? ReadonlySet<Protected<E>>
25
+ : T extends Promise<infer E>
26
+ ? Promise<Protected<E>>
27
+ : IsPlainObj<T> extends true
28
+ ? // convert all keys to readonly and all values to `Protected`
29
+ { readonly [K in keyof T]: Protected<T[K]> }
30
+ : // nothing to do, just return `T`
31
+ T;
package/src/selector.mts CHANGED
@@ -22,32 +22,34 @@ export namespace Selector {
22
22
  * Type defining the shape of allowed selectors, used to improve compiler checking.
23
23
  * @typeparam SL - the selector type
24
24
  */
25
- export type Shape<SL> = IsAnyFunc<SL> extends true
26
- ? // functions are allowed, type provided by `Selector`
27
- SL
28
- : IsArray<SL> extends true
29
- ? // ensure tuple type is preserved
30
- readonly [...(SL extends readonly unknown[] ? SL : never)]
31
- : SL extends { readonly [key: string | number | symbol]: unknown }
32
- ? // ensure all object properties satisfy `Shape`
33
- { readonly [K in keyof SL]: Selector.Shape<SL[K]> }
34
- : // nothing to check
35
- SL;
25
+ export type Shape<SL> =
26
+ IsAnyFunc<SL> extends true
27
+ ? // functions are allowed, type provided by `Selector`
28
+ SL
29
+ : IsArray<SL> extends true
30
+ ? // ensure tuple type is preserved
31
+ readonly [...(SL extends readonly unknown[] ? SL : never)]
32
+ : SL extends { readonly [key: string | number | symbol]: unknown }
33
+ ? // ensure all object properties satisfy `Shape`
34
+ { readonly [K in keyof SL]: Selector.Shape<SL[K]> }
35
+ : // nothing to check
36
+ SL;
36
37
 
37
38
  /**
38
39
  * Type defining the result type of applying the SL selector type to the T value type.
39
40
  * @typeparam T - the source value type
40
41
  * @typeparam SL - the selector type
41
42
  */
42
- export type Result<T, SL> = Selector<T> extends SL
43
- ? never
44
- : SL extends (...args: any[]) => infer R
45
- ? R
46
- : SL extends string
47
- ? Path.Result<T, SL>
48
- : {
49
- readonly [K in keyof SL]: Selector.Result<T, SL[K]>;
50
- };
43
+ export type Result<T, SL> =
44
+ Selector<T> extends SL
45
+ ? never
46
+ : SL extends (...args: any[]) => infer R
47
+ ? R
48
+ : SL extends string
49
+ ? Path.Result<T, SL>
50
+ : {
51
+ readonly [K in keyof SL]: Selector.Result<T, SL[K]>;
52
+ };
51
53
  }
52
54
 
53
55
  /**
package/src/tuple.mts CHANGED
@@ -138,7 +138,7 @@ export namespace Tuple {
138
138
  */
139
139
  export function append<
140
140
  T extends Tuple.Source,
141
- V extends readonly [unknown, ...unknown[]]
141
+ V extends readonly [unknown, ...unknown[]],
142
142
  >(tuple: T, ...values: V): readonly [...T, ...V] {
143
143
  return [...tuple, ...values];
144
144
  }