@thi.ng/resolve-map 7.1.74 → 7.1.76
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 +7 -1
- package/README.md +2 -2
- package/index.d.ts +17 -9
- package/index.js +29 -29
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
3
|
+
- **Last updated**: 2024-06-29T09:28:36Z
|
|
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,12 @@ 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.1.75](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@7.1.75) (2024-06-21)
|
|
13
|
+
|
|
14
|
+
#### ♻️ Refactoring
|
|
15
|
+
|
|
16
|
+
- enforce uniform naming convention of internal functions ([56992b2](https://github.com/thi-ng/umbrella/commit/56992b2))
|
|
17
|
+
|
|
12
18
|
### [7.1.41](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@7.1.41) (2023-11-09)
|
|
13
19
|
|
|
14
20
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
10
|
+
> This is one of 189 standalone projects, maintained as part
|
|
11
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
12
|
> and anti-framework.
|
|
13
13
|
>
|
|
@@ -84,7 +84,7 @@ For Node.js REPL:
|
|
|
84
84
|
const rm = await import("@thi.ng/resolve-map");
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 1.
|
|
87
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.02 KB
|
|
88
88
|
|
|
89
89
|
## Dependencies
|
|
90
90
|
|
package/index.d.ts
CHANGED
|
@@ -53,12 +53,14 @@ export interface ResolveOpts {
|
|
|
53
53
|
* See {@link ResolveOpts} and package readme for further details.
|
|
54
54
|
*
|
|
55
55
|
* @example
|
|
56
|
-
* ```ts
|
|
56
|
+
* ```ts tangle:../export/resolve.ts
|
|
57
57
|
* import { resolve } from "@thi.ng/resolve-map";
|
|
58
58
|
*
|
|
59
59
|
* // `c` references sibling `d`
|
|
60
60
|
* // `d` references parent `a`
|
|
61
|
-
*
|
|
61
|
+
* console.log(
|
|
62
|
+
* resolve({ a: 1, b: { c: "@d", d: "@/a" } })
|
|
63
|
+
* );
|
|
62
64
|
* // { a: 1, b: { c: 1, d: 1 } }
|
|
63
65
|
* ```
|
|
64
66
|
*
|
|
@@ -79,16 +81,20 @@ export interface ResolveOpts {
|
|
|
79
81
|
* path (**without `@` prefix**) to look up any other values in the root
|
|
80
82
|
* object.
|
|
81
83
|
*
|
|
82
|
-
* ```ts
|
|
84
|
+
* ```ts tangle:../export/resolve-2.ts
|
|
83
85
|
* import { resolve } from "@thi.ng/resolve-map";
|
|
84
86
|
*
|
|
85
87
|
* // `c` uses ES6 destructuring form to look up `a` & `b` values
|
|
86
88
|
* // `d` uses provided resolve fn arg `$` to look up `c`
|
|
87
|
-
*
|
|
89
|
+
* console.log(
|
|
90
|
+
* resolve({ a: 1, b: 2, c: ({ a, b }) => a + b, d: ($) => $("c") })
|
|
91
|
+
* );
|
|
88
92
|
* // { a: 1, b: 2, c: 3, d: 3 }
|
|
89
93
|
*
|
|
90
94
|
* // last item references item @ index = 2
|
|
91
|
-
*
|
|
95
|
+
* console.log(
|
|
96
|
+
* resolve([1, 2, ($) => $("0") + $("1"), "@2"])
|
|
97
|
+
* );
|
|
92
98
|
* // [1, 2, 3, 3]
|
|
93
99
|
* ```
|
|
94
100
|
*
|
|
@@ -102,22 +108,24 @@ export interface ResolveOpts {
|
|
|
102
108
|
* start with `@`, it needs to be wrapped in a function (see `f` key
|
|
103
109
|
* below).
|
|
104
110
|
*
|
|
105
|
-
* ```ts
|
|
111
|
+
* ```ts tangle:../export/resolve-3.ts
|
|
106
112
|
* import { resolve } from "@thi.ng/resolve-map";
|
|
107
113
|
*
|
|
108
114
|
* // `a` is derived from 1st array element in `b.d`
|
|
109
115
|
* // `b.c` is looked up from `b.d[0]`
|
|
110
116
|
* // `b.d[1]` is derived from calling `e(2)`
|
|
111
117
|
* // `e` is a wrapped function
|
|
112
|
-
* res = resolve({
|
|
118
|
+
* const res = resolve({
|
|
113
119
|
* a: ($) => $("b/c") * 100,
|
|
114
120
|
* b: { c: "@d/0", d: [2, ($) => $("../../e")(2) ] },
|
|
115
121
|
* e: () => (x) => x * 10,
|
|
116
122
|
* f: () => "@foo",
|
|
117
|
-
* })
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* console.log(res);
|
|
118
126
|
* // { a: 200, b: { c: 2, d: [ 2, 20 ] }, e: [Function], f: "@foo" }
|
|
119
127
|
*
|
|
120
|
-
* res.e(2);
|
|
128
|
+
* console.log(res.e(2));
|
|
121
129
|
* // 20
|
|
122
130
|
* ```
|
|
123
131
|
*
|
package/index.js
CHANGED
|
@@ -9,28 +9,28 @@ import { exists } from "@thi.ng/paths/path";
|
|
|
9
9
|
const RE_ARGS = /^(function\s+\w+)?\s*\(\{([\w\s,:]+)\}/;
|
|
10
10
|
function resolve(root, opts) {
|
|
11
11
|
const $opts = { prefix: "@", unwrap: true, ...opts };
|
|
12
|
-
return isPlainObject(root) ?
|
|
12
|
+
return isPlainObject(root) ? __resolveMap(root, $opts) : isArray(root) ? __resolveArray(root, $opts) : root;
|
|
13
13
|
}
|
|
14
|
-
const
|
|
14
|
+
const __resolveMap = (obj, opts, root, path = [], resolved2 = {}, stack = []) => {
|
|
15
15
|
root = root || obj;
|
|
16
16
|
for (let k in obj) {
|
|
17
|
-
|
|
17
|
+
__resolve(root, [...path, k], resolved2, stack, opts);
|
|
18
18
|
}
|
|
19
|
-
return !opts.unwrap || path.length ? obj :
|
|
19
|
+
return !opts.unwrap || path.length ? obj : __unwrapResolved(obj, resolved2);
|
|
20
20
|
};
|
|
21
|
-
const
|
|
21
|
+
const __resolveArray = (arr, opts, root, path = [], resolved2 = {}, stack = []) => {
|
|
22
22
|
root = root || arr;
|
|
23
23
|
for (let k = 0, n = arr.length; k < n; k++) {
|
|
24
|
-
|
|
24
|
+
__resolve(root, [...path, k], resolved2, stack, opts);
|
|
25
25
|
}
|
|
26
|
-
return !opts.unwrap || path.length ? arr :
|
|
26
|
+
return !opts.unwrap || path.length ? arr : __unwrapResolved(arr, resolved2);
|
|
27
27
|
};
|
|
28
|
-
const
|
|
28
|
+
const __resolve = (root, path, resolved2, stack, opts) => {
|
|
29
29
|
const pathID = path.join("/");
|
|
30
30
|
if (stack.indexOf(pathID) >= 0) {
|
|
31
31
|
illegalArgs(`cyclic references not allowed: ${pathID}`);
|
|
32
32
|
}
|
|
33
|
-
let [v, isResolved] =
|
|
33
|
+
let [v, isResolved] = __getInUnsafe(root, path);
|
|
34
34
|
if (!resolved2[pathID]) {
|
|
35
35
|
if (isResolved) {
|
|
36
36
|
resolved2[pathID] = true;
|
|
@@ -39,7 +39,7 @@ const _resolve = (root, path, resolved2, stack, opts) => {
|
|
|
39
39
|
let res = SEMAPHORE;
|
|
40
40
|
stack.push(pathID);
|
|
41
41
|
if (isPlainObject(v)) {
|
|
42
|
-
|
|
42
|
+
__resolveMap(
|
|
43
43
|
v,
|
|
44
44
|
{ ...opts, unwrap: false },
|
|
45
45
|
root,
|
|
@@ -48,7 +48,7 @@ const _resolve = (root, path, resolved2, stack, opts) => {
|
|
|
48
48
|
stack
|
|
49
49
|
);
|
|
50
50
|
} else if (isArray(v)) {
|
|
51
|
-
|
|
51
|
+
__resolveArray(
|
|
52
52
|
v,
|
|
53
53
|
{ ...opts, unwrap: false },
|
|
54
54
|
root,
|
|
@@ -57,7 +57,7 @@ const _resolve = (root, path, resolved2, stack, opts) => {
|
|
|
57
57
|
stack
|
|
58
58
|
);
|
|
59
59
|
} else if (!opts.onlyFnRefs && isString(v) && v.startsWith(opts.prefix)) {
|
|
60
|
-
res =
|
|
60
|
+
res = __resolve(
|
|
61
61
|
root,
|
|
62
62
|
absPath(path, v, opts.prefix.length),
|
|
63
63
|
resolved2,
|
|
@@ -65,14 +65,14 @@ const _resolve = (root, path, resolved2, stack, opts) => {
|
|
|
65
65
|
opts
|
|
66
66
|
);
|
|
67
67
|
} else if (isFunction(v)) {
|
|
68
|
-
res =
|
|
68
|
+
res = __resolveFunction(
|
|
69
69
|
v,
|
|
70
|
-
(p) =>
|
|
70
|
+
(p) => __resolve(root, absPath(path, p, 0), resolved2, stack, opts),
|
|
71
71
|
pathID,
|
|
72
72
|
resolved2
|
|
73
73
|
);
|
|
74
74
|
} else if (!exists(root, path)) {
|
|
75
|
-
v =
|
|
75
|
+
v = __resolvePath(root, path, resolved2, stack, opts);
|
|
76
76
|
}
|
|
77
77
|
if (res !== SEMAPHORE) {
|
|
78
78
|
mutInUnsafe(root, path, res);
|
|
@@ -83,16 +83,16 @@ const _resolve = (root, path, resolved2, stack, opts) => {
|
|
|
83
83
|
}
|
|
84
84
|
return v;
|
|
85
85
|
};
|
|
86
|
-
const
|
|
86
|
+
const __resolvePath = (root, path, resolved2, stack, opts) => {
|
|
87
87
|
let pathID = stack.pop();
|
|
88
88
|
let v;
|
|
89
89
|
for (let i = 1, n = path.length; i <= n; i++) {
|
|
90
|
-
v =
|
|
90
|
+
v = __resolve(root, path.slice(0, i), resolved2, stack, opts);
|
|
91
91
|
}
|
|
92
92
|
stack.push(pathID);
|
|
93
93
|
return v;
|
|
94
94
|
};
|
|
95
|
-
const
|
|
95
|
+
const __resolveFunction = (fn, resolve2, pathID, resolved2) => {
|
|
96
96
|
const match = RE_ARGS.exec(fn.toString());
|
|
97
97
|
let res;
|
|
98
98
|
if (match) {
|
|
@@ -101,31 +101,31 @@ const resolveFunction = (fn, resolve2, pathID, resolved2) => {
|
|
|
101
101
|
} else {
|
|
102
102
|
res = fn(resolve2);
|
|
103
103
|
}
|
|
104
|
-
|
|
104
|
+
__markResolved(res, pathID, resolved2);
|
|
105
105
|
return res;
|
|
106
106
|
};
|
|
107
|
-
const
|
|
107
|
+
const __markResolved = (v, path, resolved2) => {
|
|
108
108
|
resolved2[path] = true;
|
|
109
109
|
if (isPlainObject(v)) {
|
|
110
|
-
|
|
110
|
+
__markObjResolved(v, path, resolved2);
|
|
111
111
|
} else if (isArray(v)) {
|
|
112
|
-
|
|
112
|
+
__markArrayResolved(v, path, resolved2);
|
|
113
113
|
}
|
|
114
114
|
};
|
|
115
|
-
const
|
|
115
|
+
const __markObjResolved = (obj, path, resolved2) => {
|
|
116
116
|
let v, p;
|
|
117
117
|
for (let k in obj) {
|
|
118
118
|
v = obj[k];
|
|
119
119
|
p = path + "/" + k;
|
|
120
|
-
|
|
120
|
+
__markResolved(v, p, resolved2);
|
|
121
121
|
}
|
|
122
122
|
};
|
|
123
|
-
const
|
|
123
|
+
const __markArrayResolved = (arr, path, resolved2) => {
|
|
124
124
|
let v, p;
|
|
125
125
|
for (let i = 0, n = arr.length; i < n; i++) {
|
|
126
126
|
v = arr[i];
|
|
127
127
|
p = path + "/" + i;
|
|
128
|
-
|
|
128
|
+
__markResolved(v, p, resolved2);
|
|
129
129
|
}
|
|
130
130
|
};
|
|
131
131
|
const absPath = (curr, path, idx = 1) => {
|
|
@@ -154,7 +154,7 @@ class Resolved {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
const resolved = (val) => new Resolved(val);
|
|
157
|
-
const
|
|
157
|
+
const __getInUnsafe = (obj, path) => {
|
|
158
158
|
const n = path.length - 1;
|
|
159
159
|
let res = obj;
|
|
160
160
|
let isResolved = obj instanceof Resolved;
|
|
@@ -167,10 +167,10 @@ const getInUnsafe = (obj, path) => {
|
|
|
167
167
|
}
|
|
168
168
|
return [res, isResolved];
|
|
169
169
|
};
|
|
170
|
-
const
|
|
170
|
+
const __unwrapResolved = (root, resolved2) => {
|
|
171
171
|
for (let path in resolved2) {
|
|
172
172
|
const $path = path.split("/");
|
|
173
|
-
const val =
|
|
173
|
+
const val = __getInUnsafe(root, $path);
|
|
174
174
|
val[1] && mutInUnsafe(root, $path, val[0]);
|
|
175
175
|
}
|
|
176
176
|
return root;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/resolve-map",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.76",
|
|
4
4
|
"description": "DAG resolution of vanilla objects & arrays with internally linked values",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://
|
|
13
|
+
"homepage": "https://thi.ng/resolve-map",
|
|
14
14
|
"funding": [
|
|
15
15
|
{
|
|
16
16
|
"type": "github",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.11.
|
|
40
|
-
"@thi.ng/checks": "^3.6.
|
|
41
|
-
"@thi.ng/errors": "^2.5.
|
|
42
|
-
"@thi.ng/paths": "^5.1.
|
|
39
|
+
"@thi.ng/api": "^8.11.4",
|
|
40
|
+
"@thi.ng/checks": "^3.6.6",
|
|
41
|
+
"@thi.ng/errors": "^2.5.9",
|
|
42
|
+
"@thi.ng/paths": "^5.1.83"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@microsoft/api-extractor": "^7.
|
|
46
|
-
"esbuild": "^0.21.
|
|
45
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
46
|
+
"esbuild": "^0.21.5",
|
|
47
47
|
"typedoc": "^0.25.13",
|
|
48
|
-
"typescript": "^5.
|
|
48
|
+
"typescript": "^5.5.2"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"configuration",
|
|
@@ -76,5 +76,5 @@
|
|
|
76
76
|
],
|
|
77
77
|
"year": 2018
|
|
78
78
|
},
|
|
79
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "7b950c112fba0b2e7c450765b15624c3382f1354\n"
|
|
80
80
|
}
|