Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hiccup): update derefContext() to only apply to selected keys
- Loading branch information
1 parent
d279751
commit 749925f
Showing
2 changed files
with
20 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { implementsFunction } from "@thi.ng/checks/implements-function"; | ||
|
||
/** | ||
* Takes an arbitrary `ctx` object and array of `keys`. Attempts to call | ||
* `.deref()` on all given keys' values and stores result values instead | ||
* of original. Returns updated copy of `ctx` or original if `ctx` is | ||
* `null` or no keys were given. | ||
* | ||
* @param ctx | ||
* @param keys | ||
*/ | ||
export const derefContext = | ||
(ctx: any) => { | ||
if (ctx == null) return ctx; | ||
const res = {}; | ||
for (let k in ctx) { | ||
const v = ctx[k]; | ||
res[k] = implementsFunction(v, "deref") ? | ||
v.deref() : | ||
v; | ||
(ctx: any, keys: PropertyKey[]) => { | ||
if (ctx == null || !keys || !keys.length) return ctx; | ||
const res = { ...ctx }; | ||
for (let k of keys) { | ||
const v = res[k]; | ||
implementsFunction(v, "deref") && (res[k] = v.deref()); | ||
} | ||
return res; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters