@thi.ng/resolve-map 5.1.3 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2021-12-13T10:26:00Z
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,21 @@ 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
+
21
+ ### [5.1.5](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@5.1.5) (2022-04-07)
22
+
23
+ #### ♻️ Refactoring
24
+
25
+ - replace deprecated .substr() w/ .substring() ([0710509](https://github.com/thi-ng/umbrella/commit/0710509))
26
+
12
27
  ## [5.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/resolve-map@5.1.0) (2021-11-17)
13
28
 
14
29
  #### 🚀 Features
package/README.md CHANGED
@@ -75,7 +75,7 @@ node --experimental-repl-await
75
75
  > const resolveMap = await import("@thi.ng/resolve-map");
76
76
  ```
77
77
 
78
- Package sizes (gzipped, pre-treeshake): ESM: 920 bytes
78
+ Package sizes (gzipped, pre-treeshake): ESM: 922 bytes
79
79
 
80
80
  ## Dependencies
81
81
 
@@ -314,4 +314,4 @@ If this project contributes to an academic publication, please cite it as:
314
314
 
315
315
  ## License
316
316
 
317
- © 2018 - 2021 Karsten Schmidt // Apache Software License 2.0
317
+ © 2018 - 2022 Karsten Schmidt // Apache Software License 2.0
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 const resolve: (root: any, prefix?: string) => any;
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) {
@@ -254,10 +174,10 @@ const markArrayResolved = (arr, path, resolved) => {
254
174
  */
255
175
  export const absPath = (curr, path, idx = 1) => {
256
176
  if (path.charAt(idx) === "/") {
257
- return path.substr(idx + 1).split("/");
177
+ return path.substring(idx + 1).split("/");
258
178
  }
259
179
  curr = curr.slice(0, curr.length - 1);
260
- const sub = path.substr(idx).split("/");
180
+ const sub = path.substring(idx).split("/");
261
181
  for (let i = 0, n = sub.length; i < n; i++) {
262
182
  if (sub[i] === "..") {
263
183
  !curr.length && illegalArgs(`invalid lookup path: ${path}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/resolve-map",
3
- "version": "5.1.3",
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",
@@ -34,18 +34,18 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.3",
38
- "@thi.ng/checks": "^3.1.3",
39
- "@thi.ng/errors": "^2.1.3",
40
- "@thi.ng/paths": "^5.1.3"
37
+ "@thi.ng/api": "^8.3.5",
38
+ "@thi.ng/checks": "^3.1.5",
39
+ "@thi.ng/errors": "^2.1.5",
40
+ "@thi.ng/paths": "^5.1.5"
41
41
  },
42
42
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.19.2",
44
- "@thi.ng/testament": "^0.2.3",
43
+ "@microsoft/api-extractor": "^7.19.4",
44
+ "@thi.ng/testament": "^0.2.5",
45
45
  "rimraf": "^3.0.2",
46
46
  "tools": "^0.0.1",
47
- "typedoc": "^0.22.10",
48
- "typescript": "^4.5.3"
47
+ "typedoc": "^0.22.13",
48
+ "typescript": "^4.6.2"
49
49
  },
50
50
  "keywords": [
51
51
  "configuration",
@@ -66,7 +66,7 @@
66
66
  ],
67
67
  "exports": {
68
68
  ".": {
69
- "import": "./index.js"
69
+ "default": "./index.js"
70
70
  }
71
71
  },
72
72
  "thi.ng": {
@@ -76,5 +76,5 @@
76
76
  ],
77
77
  "year": 2018
78
78
  },
79
- "gitHead": "2db9dd34c0c2c60cbfde3dad0bca352b20292f5c\n"
79
+ "gitHead": "8329baf8b824e76f196f67740993d92d9ddcafa8\n"
80
80
  }