@thi.ng/resolve-map 6.2.0 → 7.0.0
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/CHANGELOG.md +13 -1
- package/README.md +19 -7
- package/index.d.ts +26 -7
- package/index.js +26 -23
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-07-
|
|
3
|
+
- **Last updated**: 2022-07-07T13:36:28Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,18 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
# [7.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@7.0.0) (2022-07-07)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- add ResolveOpts, conditional unwrapping ([a23308b](https://github.com/thi-ng/umbrella/commit/a23308b))
|
|
17
|
+
- BREAKING CHANGE: update resolve() signature, use new `ResolveOpts`
|
|
18
|
+
- this change has only downstream impact on use cases requiring custom
|
|
19
|
+
prefixes to indicate lookup paths
|
|
20
|
+
- add new option to control value unwrapping in final result
|
|
21
|
+
- update docs/readme
|
|
22
|
+
- add new tests
|
|
23
|
+
|
|
12
24
|
## [6.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@6.2.0) (2022-07-07)
|
|
13
25
|
|
|
14
26
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -76,7 +76,7 @@ node --experimental-repl-await
|
|
|
76
76
|
> const resolveMap = await import("@thi.ng/resolve-map");
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
Package sizes (gzipped, pre-treeshake): ESM: 1.
|
|
79
|
+
Package sizes (gzipped, pre-treeshake): ESM: 1.11 KB
|
|
80
80
|
|
|
81
81
|
## Dependencies
|
|
82
82
|
|
|
@@ -237,7 +237,7 @@ resolve({ a: 1, b: { c: "@d", d: "@/a"} })
|
|
|
237
237
|
// { a: 1, b: { c: 1, d: 1 } }
|
|
238
238
|
|
|
239
239
|
// same with custom lookup prefix
|
|
240
|
-
resolve({ a: 1, b: { c: ">>>d", d: ">>>/a"} }, ">>>")
|
|
240
|
+
resolve({ a: 1, b: { c: ">>>d", d: ">>>/a"} }, { prefix: ">>>" })
|
|
241
241
|
// { a: 1, b: { c: 1, d: 1 } }
|
|
242
242
|
```
|
|
243
243
|
|
|
@@ -256,8 +256,8 @@ the 2nd (legacy) form. Also, since ES6 var names can't contain special
|
|
|
256
256
|
characters, destructured keys can ALWAYS only be looked up as siblings
|
|
257
257
|
of the currently processed key.
|
|
258
258
|
|
|
259
|
-
The `resolve` function provided as arg to the user function accepts a
|
|
260
|
-
|
|
259
|
+
The `resolve` function provided as arg to the user function accepts a path
|
|
260
|
+
(**without `@` (or custom) prefix**) to look up any other values in the root
|
|
261
261
|
object.
|
|
262
262
|
|
|
263
263
|
```ts
|
|
@@ -302,12 +302,24 @@ res.e(2);
|
|
|
302
302
|
|
|
303
303
|
Values can be protected from further resolution attempts by wrapping them via
|
|
304
304
|
[`resolved()`](https://docs.thi.ng/umbrella/resolve-map/modules.html#resolved).
|
|
305
|
-
|
|
306
|
-
result object/array will only contain the original, unwrapped values.
|
|
307
|
-
values will also be supplied to any lookup functions, no
|
|
305
|
+
By default, these wrapped values are only used during the resolution phase and
|
|
306
|
+
the final result object/array will only contain the original, unwrapped values.
|
|
307
|
+
Unwrapped values will also be supplied to any lookup functions, no
|
|
308
308
|
[`.deref()`](https://docs.thi.ng/umbrella/api/interfaces/IDeref.html) necessary
|
|
309
309
|
there.
|
|
310
310
|
|
|
311
|
+
```ts
|
|
312
|
+
resolve({ a: 42, b: ({a}) => resolved(a) });
|
|
313
|
+
// { a: 42, b: 42 }
|
|
314
|
+
|
|
315
|
+
const res = resolve({ a: 42, b: ({a}) => resolved(a) }, { unwrap: false });
|
|
316
|
+
// { a: 42, b: Resolved { _value: 42 } }
|
|
317
|
+
|
|
318
|
+
// obtain b's value via .deref()
|
|
319
|
+
res.b.deref()
|
|
320
|
+
// 42
|
|
321
|
+
```
|
|
322
|
+
|
|
311
323
|
## Authors
|
|
312
324
|
|
|
313
325
|
Karsten Schmidt
|
package/index.d.ts
CHANGED
|
@@ -4,6 +4,22 @@ export declare type Unresolved<T> = {
|
|
|
4
4
|
};
|
|
5
5
|
export declare type ResolveFn = (path: string) => any;
|
|
6
6
|
export declare type LookupPath = NumOrString[];
|
|
7
|
+
export interface ResolveOpts {
|
|
8
|
+
/**
|
|
9
|
+
* Prefix for auto-recognizing & interpreting embedded string values as
|
|
10
|
+
* lookup paths.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue `@`
|
|
13
|
+
*/
|
|
14
|
+
prefix: string;
|
|
15
|
+
/**
|
|
16
|
+
* If true (default), all known values wrapped using {@link resolved} will
|
|
17
|
+
* be unwrapped in the final result.
|
|
18
|
+
*
|
|
19
|
+
* @defaultValue true
|
|
20
|
+
*/
|
|
21
|
+
unwrap: boolean;
|
|
22
|
+
}
|
|
7
23
|
/**
|
|
8
24
|
* Visits all key-value pairs or array items in depth-first order, expands any
|
|
9
25
|
* reference values, mutates the original object and returns it. Cyclic
|
|
@@ -20,10 +36,13 @@ export declare type LookupPath = NumOrString[];
|
|
|
20
36
|
* level (the original object passed to this function).
|
|
21
37
|
*
|
|
22
38
|
* Values can be protected from further resolution attempts by wrapping them via
|
|
23
|
-
* {@link resolved}.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* functions, no `.deref()`
|
|
39
|
+
* {@link resolved}. By default (unless `unwrap` is set to `false`), these
|
|
40
|
+
* wrapped values are only used during the resolution phase and the final result
|
|
41
|
+
* object/array will only contain the original, unwrapped values. In any way,
|
|
42
|
+
* unwrapped values will also be supplied to any lookup functions, no `.deref()`
|
|
43
|
+
* necessary there.
|
|
44
|
+
*
|
|
45
|
+
* See {@link ResolveOpts} for further details.
|
|
27
46
|
*
|
|
28
47
|
* @example
|
|
29
48
|
* ```ts
|
|
@@ -89,10 +108,10 @@ export declare type LookupPath = NumOrString[];
|
|
|
89
108
|
* ```
|
|
90
109
|
*
|
|
91
110
|
* @param root -
|
|
92
|
-
* @param
|
|
111
|
+
* @param opts -
|
|
93
112
|
*/
|
|
94
|
-
export declare function resolve<T>(root: Unresolved<T>,
|
|
95
|
-
export declare function resolve<T>(root: Unresolved<T[]>,
|
|
113
|
+
export declare function resolve<T>(root: Unresolved<T>, opts?: Partial<ResolveOpts>): T;
|
|
114
|
+
export declare function resolve<T>(root: Unresolved<T[]>, opts?: Partial<ResolveOpts>): T[];
|
|
96
115
|
/**
|
|
97
116
|
* Takes the path for the current key and a lookup path string. Converts
|
|
98
117
|
* the possibly relative lookup path into its absolute form.
|
package/index.js
CHANGED
|
@@ -7,30 +7,33 @@ import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
|
7
7
|
import { mutInUnsafe } from "@thi.ng/paths/mut-in";
|
|
8
8
|
import { exists } from "@thi.ng/paths/path";
|
|
9
9
|
const RE_ARGS = /^(function\s+\w+)?\s*\(\{([\w\s,:]+)\}/;
|
|
10
|
-
export function resolve(root,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return root;
|
|
10
|
+
export function resolve(root, opts) {
|
|
11
|
+
const $opts = { prefix: "@", unwrap: true, ...opts };
|
|
12
|
+
return isPlainObject(root)
|
|
13
|
+
? resolveMap(root, $opts)
|
|
14
|
+
: isArray(root)
|
|
15
|
+
? resolveArray(root, $opts)
|
|
16
|
+
: root;
|
|
18
17
|
}
|
|
19
18
|
/** @internal */
|
|
20
|
-
const resolveMap = (obj,
|
|
19
|
+
const resolveMap = (obj, opts, root, path = [], resolved = {}, stack = []) => {
|
|
21
20
|
root = root || obj;
|
|
22
21
|
for (let k in obj) {
|
|
23
|
-
_resolve(root, [...path, k], resolved, stack,
|
|
22
|
+
_resolve(root, [...path, k], resolved, stack, opts);
|
|
24
23
|
}
|
|
25
|
-
return path.length
|
|
24
|
+
return !opts.unwrap || path.length
|
|
25
|
+
? obj
|
|
26
|
+
: unwrapResolved(obj, resolved);
|
|
26
27
|
};
|
|
27
28
|
/** @internal */
|
|
28
|
-
const resolveArray = (arr,
|
|
29
|
+
const resolveArray = (arr, opts, root, path = [], resolved = {}, stack = []) => {
|
|
29
30
|
root = root || arr;
|
|
30
31
|
for (let k = 0, n = arr.length; k < n; k++) {
|
|
31
|
-
_resolve(root, [...path, k], resolved, stack,
|
|
32
|
+
_resolve(root, [...path, k], resolved, stack, opts);
|
|
32
33
|
}
|
|
33
|
-
return path.length
|
|
34
|
+
return !opts.unwrap || path.length
|
|
35
|
+
? arr
|
|
36
|
+
: unwrapResolved(arr, resolved);
|
|
34
37
|
};
|
|
35
38
|
/**
|
|
36
39
|
* The actual recursive resolution mechanism. Takes root object, key
|
|
@@ -45,7 +48,7 @@ const resolveArray = (arr, prefix, root, path = [], resolved = {}, stack = []) =
|
|
|
45
48
|
*
|
|
46
49
|
* @internal
|
|
47
50
|
*/
|
|
48
|
-
const _resolve = (root, path, resolved, stack,
|
|
51
|
+
const _resolve = (root, path, resolved, stack, opts) => {
|
|
49
52
|
const pathID = path.join("/");
|
|
50
53
|
if (stack.indexOf(pathID) >= 0) {
|
|
51
54
|
illegalArgs(`cyclic references not allowed: ${pathID}`);
|
|
@@ -60,19 +63,19 @@ const _resolve = (root, path, resolved, stack, prefix) => {
|
|
|
60
63
|
let res = SEMAPHORE;
|
|
61
64
|
stack.push(pathID);
|
|
62
65
|
if (isPlainObject(v)) {
|
|
63
|
-
resolveMap(v,
|
|
66
|
+
resolveMap(v, { ...opts, unwrap: false }, root, path, resolved, stack);
|
|
64
67
|
}
|
|
65
68
|
else if (isArray(v)) {
|
|
66
|
-
resolveArray(v,
|
|
69
|
+
resolveArray(v, { ...opts, unwrap: false }, root, path, resolved, stack);
|
|
67
70
|
}
|
|
68
|
-
else if (isString(v) && v.startsWith(prefix)) {
|
|
69
|
-
res = _resolve(root, absPath(path, v, prefix.length), resolved, stack,
|
|
71
|
+
else if (isString(v) && v.startsWith(opts.prefix)) {
|
|
72
|
+
res = _resolve(root, absPath(path, v, opts.prefix.length), resolved, stack, opts);
|
|
70
73
|
}
|
|
71
74
|
else if (isFunction(v)) {
|
|
72
|
-
res = resolveFunction(v, (p) => _resolve(root, absPath(path, p, 0), resolved, stack,
|
|
75
|
+
res = resolveFunction(v, (p) => _resolve(root, absPath(path, p, 0), resolved, stack, opts), pathID, resolved);
|
|
73
76
|
}
|
|
74
77
|
else if (!exists(root, path)) {
|
|
75
|
-
v = resolvePath(root, path, resolved, stack,
|
|
78
|
+
v = resolvePath(root, path, resolved, stack, opts);
|
|
76
79
|
}
|
|
77
80
|
if (res !== SEMAPHORE) {
|
|
78
81
|
mutInUnsafe(root, path, res);
|
|
@@ -102,12 +105,12 @@ const _resolve = (root, path, resolved, stack, prefix) => {
|
|
|
102
105
|
*
|
|
103
106
|
* @internal
|
|
104
107
|
*/
|
|
105
|
-
const resolvePath = (root, path, resolved, stack,
|
|
108
|
+
const resolvePath = (root, path, resolved, stack, opts) => {
|
|
106
109
|
// temporarily remove current path to avoid cycle detection
|
|
107
110
|
let pathID = stack.pop();
|
|
108
111
|
let v;
|
|
109
112
|
for (let i = 1, n = path.length; i <= n; i++) {
|
|
110
|
-
v = _resolve(root, path.slice(0, i), resolved, stack,
|
|
113
|
+
v = _resolve(root, path.slice(0, i), resolved, stack, opts);
|
|
111
114
|
}
|
|
112
115
|
// restore
|
|
113
116
|
stack.push(pathID);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/resolve-map",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "DAG resolution of vanilla objects & arrays with internally linked values",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -76,5 +76,5 @@
|
|
|
76
76
|
],
|
|
77
77
|
"year": 2018
|
|
78
78
|
},
|
|
79
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "4c4aeb0672852c319408c92b56444a5e42826996\n"
|
|
80
80
|
}
|