@rimbu/deep 1.1.0 → 2.0.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/dist/cjs/deep.cjs +197 -576
- package/dist/cjs/deep.cjs.map +1 -0
- package/dist/cjs/deep.d.cts +284 -0
- package/dist/cjs/index.cjs +18 -662
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +18 -0
- package/dist/cjs/internal.cjs +8 -582
- package/dist/cjs/internal.cjs.map +1 -0
- package/dist/cjs/internal.d.cts +7 -0
- package/dist/cjs/match.cjs +251 -343
- package/dist/cjs/match.cjs.map +1 -0
- package/dist/cjs/match.d.cts +140 -0
- package/dist/cjs/patch.cjs +121 -93
- package/dist/cjs/patch.cjs.map +1 -0
- package/dist/cjs/patch.d.cts +89 -0
- package/dist/cjs/path.cjs +114 -573
- package/dist/cjs/path.cjs.map +1 -0
- package/dist/cjs/path.d.cts +196 -0
- package/dist/cjs/protected.cjs +2 -17
- package/dist/cjs/protected.cjs.map +1 -0
- package/dist/cjs/selector.cjs +33 -574
- package/dist/cjs/selector.cjs.map +1 -0
- package/dist/cjs/selector.d.cts +47 -0
- package/dist/cjs/tuple.cjs +162 -71
- package/dist/cjs/tuple.cjs.map +1 -0
- package/dist/esm/match.mjs.map +1 -1
- package/dist/esm/patch.mjs.map +1 -1
- package/dist/esm/path.mjs.map +1 -1
- package/dist/esm/protected.d.mts +17 -0
- package/dist/esm/selector.mjs.map +1 -1
- package/dist/esm/tuple.d.mts +142 -0
- package/package.json +21 -15
- /package/dist/{types/protected.d.mts → cjs/protected.d.cts} +0 -0
- /package/dist/{types/tuple.d.mts → cjs/tuple.d.cts} +0 -0
- /package/dist/{types → esm}/deep.d.mts +0 -0
- /package/dist/{types → esm}/index.d.mts +0 -0
- /package/dist/{types → esm}/internal.d.mts +0 -0
- /package/dist/{types → esm}/match.d.mts +0 -0
- /package/dist/{types → esm}/patch.d.mts +0 -0
- /package/dist/{types → esm}/path.d.mts +0 -0
- /package/dist/{types → esm}/selector.d.mts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.cjs","sourceRoot":"","sources":["../../_cjs_prepare/path.cts"],"names":[],"mappings":";;;;AACA,+CAAkD;AAGlD,IAAiB,IAAI,CAsTpB;AAtTD,WAAiB,IAAI;IAqSnB;;OAEG;IACU,qBAAgB,GAAG,gBAAgB,CAAC;IAOjD;;;OAGG;IACH,SAAgB,WAAW,CAAC,IAAY;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;IAC3C,CAAC;IAFe,gBAAW,cAE1B,CAAA;AACH,CAAC,EAtTgB,IAAI,oBAAJ,IAAI,QAsTpB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,KAAK,CACnB,MAAS,EACT,IAAO;;IAEP,IAAI,IAAI,KAAK,EAAE,EAAE,CAAC;QAChB,kDAAkD;QAClD,OAAO,MAAa,CAAC;IACvB,CAAC;IAED,IAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAErC,sCAAsC;IACtC,IAAI,MAAM,GAAG,MAAa,CAAC;;QAE3B,KAAmB,IAAA,UAAA,iBAAA,KAAK,CAAA,4BAAA,+CAAE,CAAC;YAAtB,IAAM,IAAI,kBAAA;YACb,IAAI,SAAS,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;gBACtD,0BAA0B;gBAC1B,SAAS;YACX,CAAC;YAED,IAAI,SAAS,KAAK,MAAM,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC5C,2FAA2F;gBAC3F,OAAO,SAAgB,CAAC;YAC1B,CAAC;YAED,sCAAsC;YACtC,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;;;;;;;;;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AA9BD,sB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0 +1,196 @@
|
|
|
1
|
+
import type { IsAnyFunc, IsArray, IsPlainObj } from '@rimbu/base';
|
|
2
|
+
import { type Patch } from './internal.cjs';
|
|
3
|
+
import type { Tuple } from './tuple.cjs';
|
|
4
|
+
export declare namespace Path {
|
|
5
|
+
/**
|
|
6
|
+
* A string representing a path into an (nested) object of type T.
|
|
7
|
+
* @typeparam T - the object type to select in
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const p: Path.Get<{ a: { b: { c : 5 } } }> = 'a.b'
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
type Get<T> = Path.Internal.Generic<T, false, false, true>;
|
|
14
|
+
/**
|
|
15
|
+
* A string representing a path into an (nested) object of type T.
|
|
16
|
+
* @typeparam T - the object type to select in
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const p: Path.Set<{ a: { b: { c : 5 } } }> = 'a.b'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
type Set<T> = Path.Internal.Generic<T, true, false, true>;
|
|
23
|
+
namespace Internal {
|
|
24
|
+
/**
|
|
25
|
+
* Determines the allowed paths into a value of type `T`.
|
|
26
|
+
* @typeparam T - the source type
|
|
27
|
+
* @typeparam Write - if true the path should be writable (no optional chaining)
|
|
28
|
+
* @typeparam Maybe - if true the value at the current path is optional
|
|
29
|
+
* @typeparam First - if true this is the root call
|
|
30
|
+
* @note type is mapped as template literal to prevent non-string types to leak through
|
|
31
|
+
*/
|
|
32
|
+
type Generic<T, Write extends boolean, Maybe extends boolean, First extends boolean = false> = `${IsAnyFunc<T> extends true ? '' : // empty string is always an option
|
|
33
|
+
'' | Path.Internal.NonEmpty<T, Write, Maybe, First>}`;
|
|
34
|
+
/**
|
|
35
|
+
* Determines the allowed non-empty paths into a value of type `T`.
|
|
36
|
+
* @typeparam T - the source type
|
|
37
|
+
* @typeparam Write - if true the path should be writable (no optional chaining)
|
|
38
|
+
* @typeparam Maybe - if true the value at the current path is optional
|
|
39
|
+
* @typeparam First - if true this is the root call
|
|
40
|
+
*/
|
|
41
|
+
type NonEmpty<T, Write extends boolean, Maybe extends boolean, First extends boolean> = Path.Internal.IsOptional<T> extends true ? Write extends false ? Path.Internal.Generic<Exclude<T, undefined | null>, Write, true> : never : `${Path.Internal.Separator<First, Maybe, IsArray<T>>}${Path.Internal.NonOptional<T, Write, Maybe>}`;
|
|
42
|
+
/**
|
|
43
|
+
* Determines the allowed paths into a non-optional value of type `T`.
|
|
44
|
+
* @typeparam T - the source type
|
|
45
|
+
* @typeparam Write - if true the path should be writable (no optional chaining)
|
|
46
|
+
* @typeparam Maybe - if true the value at the current path is optional
|
|
47
|
+
* @typeparam First - if true this is the root call
|
|
48
|
+
*/
|
|
49
|
+
type NonOptional<T, Write extends boolean, Maybe extends boolean> = Tuple.IsTuple<T> extends true ? Path.Internal.Tup<T, Write, Maybe> : T extends readonly any[] ? Write extends false ? Path.Internal.Arr<T> : never : IsPlainObj<T> extends true ? Path.Internal.Obj<T, Write, Maybe> : never;
|
|
50
|
+
/**
|
|
51
|
+
* Determines the allowed paths for a tuple. Since tuples have fixed types, they do not
|
|
52
|
+
* need to be optional, in contrast to arrays.
|
|
53
|
+
* @typeparam T - the input tuple type
|
|
54
|
+
* @typeparam Write - if true the path should be writable (no optional chaining)
|
|
55
|
+
* @typeparam Maybe - if true the value at the current path is optional
|
|
56
|
+
*/
|
|
57
|
+
type Tup<T, Write extends boolean, Maybe extends boolean> = {
|
|
58
|
+
[K in Tuple.KeysOf<T>]: `[${K}]${Path.Internal.Generic<T[K], Write, Maybe>}`;
|
|
59
|
+
}[Tuple.KeysOf<T>];
|
|
60
|
+
/**
|
|
61
|
+
* Determines the allowed paths for an array.
|
|
62
|
+
* @typeparam T - the input array type
|
|
63
|
+
*/
|
|
64
|
+
type Arr<T extends readonly any[]> = `[${number}]${Path.Internal.Generic<T[number], false, true>}`;
|
|
65
|
+
/**
|
|
66
|
+
* Determines the allowed paths for an object.
|
|
67
|
+
* @typeparam T - the input object type
|
|
68
|
+
* @typeparam Write - if true the path should be writable (no optional chaining)
|
|
69
|
+
* @typeparam Maybe - if true the value at the current path is optional
|
|
70
|
+
*/
|
|
71
|
+
type Obj<T, Write extends boolean, Maybe extends boolean> = {
|
|
72
|
+
[K in keyof T]: `${K & string}${Path.Internal.Generic<T[K], Write, Write extends true ? false : Path.Internal.IsOptional<T[K], true, Maybe>>}`;
|
|
73
|
+
}[keyof T];
|
|
74
|
+
/**
|
|
75
|
+
* Determines the allowed path part seperator based on the input types.
|
|
76
|
+
* @typeparam First - if true, this is the first call
|
|
77
|
+
* @typeparam Maybe - if true, the value is optional
|
|
78
|
+
* @typeparam IsArray - if true, the value is an array
|
|
79
|
+
*/
|
|
80
|
+
type Separator<First extends boolean, Maybe extends boolean, IsArray extends boolean> = Maybe extends true ? First extends true ? never : '?.' : First extends true ? '' : IsArray extends true ? '' : '.';
|
|
81
|
+
/**
|
|
82
|
+
* Determines whether the given type `T` is optional, that is, whether it can be null or undefined.
|
|
83
|
+
* @typeparam T - the input type
|
|
84
|
+
* @typeparam True - the value to return if `T` is optional
|
|
85
|
+
* @typeparam False - the value to return if `T` is mandatory
|
|
86
|
+
*/
|
|
87
|
+
type IsOptional<T, True = true, False = false> = undefined extends T ? True : null extends T ? True : False;
|
|
88
|
+
/**
|
|
89
|
+
* Returns type `T` if `Maybe` is false, `T | undefined` otherwise.
|
|
90
|
+
* @typeparam T - the input type
|
|
91
|
+
* @typeparam Maybe - if true, the return type value should be optional
|
|
92
|
+
*/
|
|
93
|
+
type MaybeValue<T, Maybe extends boolean> = Maybe extends true ? T | undefined : T;
|
|
94
|
+
/**
|
|
95
|
+
* Utility type to only add non-empty string types to a string array.
|
|
96
|
+
* @typeparma A - the input string array
|
|
97
|
+
* @typeparam T - the string value to optionally add
|
|
98
|
+
*/
|
|
99
|
+
type AppendIfNotEmpty<A extends string[], T extends string> = T extends '' ? A : [
|
|
100
|
+
...A,
|
|
101
|
+
T
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The result type when selecting from object type T a path with type P.
|
|
106
|
+
* @typeparam T - the object type to select in
|
|
107
|
+
* @typeparam P - a Path in object type T
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* let r!: Path.Result<{ a: { b: { c: number } } }, 'a.b'>;
|
|
111
|
+
* // => type of r: { c: number }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
type Result<T, P extends string> = Path.Result.For<T, Path.Result.Tokenize<P>, false>;
|
|
115
|
+
namespace Result {
|
|
116
|
+
/**
|
|
117
|
+
* Determines the result type for an array of tokens representing subpaths in type `T`.
|
|
118
|
+
* @typeparam T - the current source type
|
|
119
|
+
* @typeparam Tokens - an array of elements indicating a path into the source type
|
|
120
|
+
* @typeparam Maybe - if true indicates that the path may be undefined
|
|
121
|
+
*/
|
|
122
|
+
type For<T, Tokens, Maybe extends boolean = Path.Internal.IsOptional<T>> = Tokens extends [] ? Path.Internal.MaybeValue<T, Maybe> : Path.Internal.IsOptional<T> extends true ? Path.Result.For<Exclude<T, undefined | null>, Tokens, Maybe> : Tokens extends ['?.', infer Key, ...infer Rest] ? Path.Result.For<Path.Result.Part<T, Key, Maybe>, Rest, true> : Tokens extends ['.', infer Key, ...infer Rest] ? Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe> : Tokens extends [infer Key, ...infer Rest] ? Path.Result.For<Path.Result.Part<T, Key, false>, Rest, Maybe> : never;
|
|
123
|
+
/**
|
|
124
|
+
* Determines the result of getting the property/index `K` from type `T`, taking into
|
|
125
|
+
* account that the value may be optional.
|
|
126
|
+
* @typeparam T - the current source type
|
|
127
|
+
* @typeparam K - the key to get from the source type
|
|
128
|
+
* @typeparam Maybe - if true indicates that the path may be undefined
|
|
129
|
+
*/
|
|
130
|
+
type Part<T, K, Maybe extends boolean> = IsArray<T> extends true ? Path.Internal.MaybeValue<T[K & keyof T], Tuple.IsTuple<T> extends true ? Maybe : true> : Path.Internal.MaybeValue<T[K & keyof T], Maybe>;
|
|
131
|
+
/**
|
|
132
|
+
* Converts a path string into separate tokens in a string array.
|
|
133
|
+
* @typeparam P - the literal string path type
|
|
134
|
+
* @typeparam Token - the token currently being produced
|
|
135
|
+
* @typeparam Res - the resulting literal string token array
|
|
136
|
+
*/
|
|
137
|
+
type Tokenize<P extends string, Token extends string = '', Res extends string[] = []> = P extends '' ? Path.Internal.AppendIfNotEmpty<Res, Token> : P extends `[${infer Index}]${infer Rest}` ? Tokenize<Rest, '', [
|
|
138
|
+
...Path.Internal.AppendIfNotEmpty<Res, Token>,
|
|
139
|
+
Index
|
|
140
|
+
]> : P extends `?.${infer Rest}` ? Tokenize<Rest, '', [
|
|
141
|
+
...Path.Internal.AppendIfNotEmpty<Res, Token>,
|
|
142
|
+
'?.'
|
|
143
|
+
]> : P extends `.${infer Rest}` ? Tokenize<Rest, '', [...Path.Internal.AppendIfNotEmpty<Res, Token>, '.']> : P extends `${infer First}${infer Rest}` ? Tokenize<Rest, `${Token}${First}`, Res> : never;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Regular expression used to split a path string into tokens.
|
|
147
|
+
*/
|
|
148
|
+
const stringSplitRegex: RegExp;
|
|
149
|
+
/**
|
|
150
|
+
* The allowed values of a split path.
|
|
151
|
+
*/
|
|
152
|
+
type StringSplit = (string | number | undefined)[];
|
|
153
|
+
/**
|
|
154
|
+
* Return the given `path` string split into an array of subpaths.
|
|
155
|
+
* @param path - the input string path
|
|
156
|
+
*/
|
|
157
|
+
function stringSplit(path: string): Path.StringSplit;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns the value resulting from selecting the given `path` in the given `source` object.
|
|
161
|
+
* It supports optional chaining for nullable values or values that may be undefined, and also
|
|
162
|
+
* for accessing objects inside an array.
|
|
163
|
+
* There is currently no support for forcing non-null (the `!` operator).
|
|
164
|
+
* @typeparam T - the object type to select in
|
|
165
|
+
* @typeparam P - a Path in object type T
|
|
166
|
+
* @param source - the object to select in
|
|
167
|
+
* @param path - the path into the object
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* const value = { a: { b: { c: [{ d: 5 }, { d: 6 }] } } }
|
|
171
|
+
* Deep.getAt(value, 'a.b');
|
|
172
|
+
* // => { c: 5 }
|
|
173
|
+
* Deep.getAt(value, 'a.b.c');
|
|
174
|
+
* // => [{ d: 5 }, { d: 5 }]
|
|
175
|
+
* Deep.getAt(value, 'a.b.c[1]');
|
|
176
|
+
* // => { d: 6 }
|
|
177
|
+
* Deep.getAt(value, 'a.b.c[1]?.d');
|
|
178
|
+
* // => 6
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export declare function getAt<T, P extends Path.Get<T>>(source: T, path: P): Path.Result<T, P>;
|
|
182
|
+
/**
|
|
183
|
+
* Patches the value at the given path in the source to the given value.
|
|
184
|
+
* Because the path to update must exist in the `source` object, optional
|
|
185
|
+
* chaining and array indexing is not allowed.
|
|
186
|
+
* @param source - the object to update
|
|
187
|
+
* @param path - the path in the object to update
|
|
188
|
+
* @param patchItem - the patch for the value at the given path
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* const value = { a: { b: { c: 5 } } };
|
|
192
|
+
* Deep.patchAt(value, 'a.b.c', v => v + 5);
|
|
193
|
+
* // => { a: { b: { c: 6 } } }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare function patchAt<T, P extends Path.Set<T>, C = Path.Result<T, P>>(source: T, path: P, patchItem: Patch<Path.Result<T, P>, Path.Result<T, P> & C>): T;
|
package/dist/cjs/protected.cjs
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
|
|
16
|
-
// src/protected.mts
|
|
17
|
-
var protected_exports = {};
|
|
18
|
-
module.exports = __toCommonJS(protected_exports);
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
//# sourceMappingURL=protected.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protected.cjs","sourceRoot":"","sources":["../../_cjs_prepare/protected.cts"],"names":[],"mappings":""}
|