@thi.ng/resolve-map 5.1.5 → 6.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 +10 -1
- package/index.d.ts +7 -2
- package/index.js +2 -82
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-
|
|
3
|
+
- **Last updated**: 2022-05-02T11:57:05Z
|
|
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,15 @@ 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
|
+
# [6.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@6.0.0) (2022-05-02)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- add `Unresolved` type & type checking ([a997fd2](https://github.com/thi-ng/umbrella/commit/a997fd2))
|
|
17
|
+
- BREAKING CHANGE: add type checking to `resolve()`.
|
|
18
|
+
This MIGHT require additional type generics (of the result object type)
|
|
19
|
+
to be added to any call sites. See tests for examples.
|
|
20
|
+
|
|
12
21
|
### [5.1.5](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@5.1.5) (2022-04-07)
|
|
13
22
|
|
|
14
23
|
#### ♻️ Refactoring
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import type { NumOrString } from "@thi.ng/api";
|
|
1
|
+
import type { Fn, NumOrString } from "@thi.ng/api";
|
|
2
|
+
export declare type Unresolved<T> = {
|
|
3
|
+
[K in keyof T]: Unresolved<T[K]> | Fn<T, T[K]> | Fn<ResolveFn, T[K]> | Function | string;
|
|
4
|
+
};
|
|
2
5
|
export declare type ResolveFn = (path: string) => any;
|
|
3
6
|
export declare type LookupPath = NumOrString[];
|
|
4
7
|
/**
|
|
@@ -7,6 +10,7 @@ export declare type LookupPath = NumOrString[];
|
|
|
7
10
|
* references are not allowed and will throw an error. However, refs pointing to
|
|
8
11
|
* other refs are recursively resolved (again, provided there are no cycles).
|
|
9
12
|
*
|
|
13
|
+
* @remarks
|
|
10
14
|
* Reference values are special strings representing lookup paths of other
|
|
11
15
|
* values in the object and are prefixed with given `prefix` string (default:
|
|
12
16
|
* `@`) for relative refs or `@/` for absolute refs and both using `/` as path
|
|
@@ -81,7 +85,8 @@ export declare type LookupPath = NumOrString[];
|
|
|
81
85
|
* @param root -
|
|
82
86
|
* @param prefix -
|
|
83
87
|
*/
|
|
84
|
-
export declare
|
|
88
|
+
export declare function resolve<T>(root: Unresolved<T>, prefix?: string): T;
|
|
89
|
+
export declare function resolve<T>(root: Unresolved<T[]>, prefix?: string): T[];
|
|
85
90
|
/**
|
|
86
91
|
* Takes the path for the current key and a lookup path string. Converts
|
|
87
92
|
* the possibly relative lookup path into its absolute form.
|
package/index.js
CHANGED
|
@@ -8,87 +8,7 @@ import { getInUnsafe } from "@thi.ng/paths/get-in";
|
|
|
8
8
|
import { mutInUnsafe } from "@thi.ng/paths/mut-in";
|
|
9
9
|
import { exists } from "@thi.ng/paths/path";
|
|
10
10
|
const RE_ARGS = /^(function\s+\w+)?\s*\(\{([\w\s,:]+)\}/;
|
|
11
|
-
|
|
12
|
-
* Visits all key-value pairs or array items in depth-first order, expands any
|
|
13
|
-
* reference values, mutates the original object and returns it. Cyclic
|
|
14
|
-
* references are not allowed and will throw an error. However, refs pointing to
|
|
15
|
-
* other refs are recursively resolved (again, provided there are no cycles).
|
|
16
|
-
*
|
|
17
|
-
* Reference values are special strings representing lookup paths of other
|
|
18
|
-
* values in the object and are prefixed with given `prefix` string (default:
|
|
19
|
-
* `@`) for relative refs or `@/` for absolute refs and both using `/` as path
|
|
20
|
-
* separator (Note: trailing slashes are NOT allowed!). Relative refs are
|
|
21
|
-
* resolved from the currently visited object and support "../" prefixes to
|
|
22
|
-
* access any parent levels. Absolute refs are always resolved from the root
|
|
23
|
-
* level (the original object passed to this function).
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* // `c` references sibling `d`
|
|
28
|
-
* // `d` references parent `a`
|
|
29
|
-
* resolve({ a: 1, b: { c: "@d", d: "@/a" } })
|
|
30
|
-
* // { a: 1, b: { c: 1, d: 1 } }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* Any function values are called using two possible conventions:
|
|
34
|
-
*
|
|
35
|
-
* 1) If the user function uses ES6 object destructuring for its first
|
|
36
|
-
* argument, the given object keys are resolved prior to calling the
|
|
37
|
-
* function and the resolved values provided as first argument
|
|
38
|
-
* (object) and a general `resolve` function as second argument.
|
|
39
|
-
* 2) If no de-structure form is found in the function's arguments, the
|
|
40
|
-
* function is only called with `resolve` as argument.
|
|
41
|
-
*
|
|
42
|
-
* **Important:** Since ES6 var names can't contain special characters,
|
|
43
|
-
* destructured keys can ALWAYS only be looked up as siblings of the
|
|
44
|
-
* currently processed key.
|
|
45
|
-
*
|
|
46
|
-
* The `resolve` function provided as arg to the user function accepts a
|
|
47
|
-
* path (**without `@` prefix**) to look up any other values in the root
|
|
48
|
-
* object.
|
|
49
|
-
*
|
|
50
|
-
* ```
|
|
51
|
-
* // `c` uses ES6 destructuring form to look up `a` & `b` values
|
|
52
|
-
* // `d` uses provided resolve fn arg `$` to look up `c`
|
|
53
|
-
* resolve({ a: 1, b: 2, c: ({ a, b }) => a + b, d: ($) => $("c") })
|
|
54
|
-
* // { a: 1, b: 2, c: 3, d: 3 }
|
|
55
|
-
*
|
|
56
|
-
* // last item references item @ index = 2
|
|
57
|
-
* resolve([1, 2, ($) => $("0") + $("1"), "@2"])
|
|
58
|
-
* // [1, 2, 3, 3]
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* The return value of the user provided function is used as final value
|
|
62
|
-
* for that key in the object. This mechanism can be used to compute
|
|
63
|
-
* derived values of other values stored anywhere in the root object.
|
|
64
|
-
* **Function values will always be called only once.** Therefore, in
|
|
65
|
-
* order to associate a function as final value to a key, it MUST be
|
|
66
|
-
* wrapped with an additional function, as shown for the `e` key in the
|
|
67
|
-
* example below. Similarly, if an actual string value should happen to
|
|
68
|
-
* start with `@`, it needs to be wrapped in a function (see `f` key
|
|
69
|
-
* below).
|
|
70
|
-
*
|
|
71
|
-
* ```
|
|
72
|
-
* // `a` is derived from 1st array element in `b.d`
|
|
73
|
-
* // `b.c` is looked up from `b.d[0]`
|
|
74
|
-
* // `b.d[1]` is derived from calling `e(2)`
|
|
75
|
-
* // `e` is a wrapped function
|
|
76
|
-
* res = resolve({
|
|
77
|
-
* a: ($) => $("b/c") * 100,
|
|
78
|
-
* b: { c: "@d/0", d: [2, ($) => $("../../e")(2) ] },
|
|
79
|
-
* e: () => (x) => x * 10,
|
|
80
|
-
* f: () => "@foo",
|
|
81
|
-
* })
|
|
82
|
-
* // { a: 200, b: { c: 2, d: [ 2, 20 ] }, e: [Function], f: "@foo" }
|
|
83
|
-
*
|
|
84
|
-
* res.e(2);
|
|
85
|
-
* // 20
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* @param root -
|
|
89
|
-
* @param prefix -
|
|
90
|
-
*/
|
|
91
|
-
export const resolve = (root, prefix = "@") => {
|
|
11
|
+
export function resolve(root, prefix = "@") {
|
|
92
12
|
if (isPlainObject(root)) {
|
|
93
13
|
return resolveMap(root, prefix);
|
|
94
14
|
}
|
|
@@ -96,7 +16,7 @@ export const resolve = (root, prefix = "@") => {
|
|
|
96
16
|
return resolveArray(root, prefix);
|
|
97
17
|
}
|
|
98
18
|
return root;
|
|
99
|
-
}
|
|
19
|
+
}
|
|
100
20
|
const resolveMap = (obj, prefix, root, path = [], resolved = {}, stack = []) => {
|
|
101
21
|
root = root || obj;
|
|
102
22
|
for (let k in obj) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/resolve-map",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.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": "8329baf8b824e76f196f67740993d92d9ddcafa8\n"
|
|
80
80
|
}
|