Skip to content

Commit

Permalink
feat(resolve-map): resolve each ref only once, re-use resolved results
Browse files Browse the repository at this point in the history
- this also fixes function value resolution,
  e.g. {a: () => ()=> 1, b: "->a" } only unwraps `a` once now
  • Loading branch information
postspectacular committed Apr 16, 2018
1 parent 4c1bd85 commit 6992e82
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
63 changes: 32 additions & 31 deletions packages/resolve-map/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Path, getIn, toPath } from "@thi.ng/paths";
import { isArray } from "@thi.ng/checks/is-array";
import { isFunction } from "@thi.ng/checks/is-function";
import { isPlainObject } from "@thi.ng/checks/is-plain-object";
import { isString } from "@thi.ng/checks/is-string";
import { getIn, mutIn, toPath } from "@thi.ng/paths";

const SEMAPHORE = Symbol("SEMAPHORE");

/**
* Visits all key-value pairs in depth-first order for given object and
Expand Down Expand Up @@ -45,46 +47,45 @@ import { isString } from "@thi.ng/checks/is-string";
* @param obj
* @param root
*/
export function resolveMap(obj: any, root?: any) {
export const resolveMap = (obj: any, root?: any, path: PropertyKey[] = [], resolved: any = {}) => {
root = root || obj;
for (let k in obj) {
obj[k] = _resolve(k, obj, root);
_resolve(root, [...path, k], resolved);
}
// console.log("resolved:::", resolved);
return obj;
}

/**
* Same as `resolveMap`, but for arrays.
*
* @param arr
* @param root
*/
export function resolveArray(arr: any[], root?: any) {
for (let i = 0, n = arr.length; i < n; i++) {
arr[i] = _resolve(i, arr, root);
export const resolveArray = (arr: any[], root?: any, path: PropertyKey[] = [], resolved: any = {}) => {
root = root || arr;
for (let k = 0, n = arr.length; k < n; k++) {
_resolve(root, [...path, k], resolved);
}
return arr;
}

function _resolve(k: Path, obj: any, root?: any, makeAbs = false) {
root = root || obj;
const v = getIn(obj, k);
if (isString(v) && v.indexOf("->") === 0) {
if (v.charAt(2) === "/") {
return _resolve(v.substr(3), root, root);
} else {
if (makeAbs) {
const path = toPath(k);
return _resolve([...path.slice(0, path.length - 1), ...toPath(v.substr(2))], root);
const _resolve = (root: any, path: PropertyKey[], resolved: any) => {
let v = getIn(root, path), rv = SEMAPHORE;
const pp = path.join(".");
if (!resolved[pp]) {
if (isString(v) && v.indexOf("->") === 0) {
if (v.charAt(2) === "/") {
rv = _resolve(root, toPath(v.substr(3)), resolved);
} else {
rv = _resolve(root, [...path.slice(0, path.length - 1), ...toPath(v.substr(2))], resolved);
}
return _resolve(v.substr(2), obj, root);
} else if (isPlainObject(v)) {
resolveMap(v, root, path, resolved);
} else if (isArray(v)) {
resolveArray(v, root, path, resolved);
} else if (isFunction(v)) {
rv = v((p) => _resolve(root, toPath(p), resolved));
}
if (rv !== SEMAPHORE) {
mutIn(root, path, rv);
v = rv;
}
} else if (isPlainObject(v)) {
return resolveMap(v, root);
} else if (isArray(v)) {
return resolveArray(v, root);
} else if (isFunction(v)) {
return v((x) => _resolve(x, root, root, true));
} else {
return v;
resolved[pp] = true;
}
return v;
}
6 changes: 6 additions & 0 deletions packages/resolve-map/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("resolve-map", () => {
it("cycles", () => {
assert.throws(() => resolveMap({ a: "->a" }));
assert.throws(() => resolveMap({ a: { b: "->b" } }));
assert.throws(() => resolveMap({ a: { b: "->/a" } }));
assert.throws(() => resolveMap({ a: { b: "->/a.b" } }));
assert.throws(() => resolveMap({ a: "->b", b: "->a" }));
});
Expand All @@ -43,5 +44,10 @@ describe("resolve-map", () => {
resolveMap({ a: (x) => x("b.c") * 10, b: { c: "->d", d: "->/e" }, e: () => 1 }),
{ a: 10, b: { c: 1, d: 1 }, e: 1 }
);
const res = resolveMap({ a: (x) => x("b.c")() * 10, b: { c: "->d", d: "->/e" }, e: () => () => 1 });
assert.equal(res.a, 10);
assert.strictEqual(res.b.c, res.e);
assert.strictEqual(res.b.d, res.e);
assert.strictEqual(res.e(), 1);
});
});

0 comments on commit 6992e82

Please sign in to comment.