Package Exports
- @jondotsoy/utils-js
- @jondotsoy/utils-js/get
- @jondotsoy/utils-js/visit
Readme
utils-js
Some utilities to js
Visit
A generator function that recursively visits nodes in an object, yielding each node that passes the provided test.
Syntax
visit(node);
visit(node, test);Arguments:
node<unknown>: The starting node to visit.test<(node: unknown) => boolean>: An optional function that takes a node as input and returns a boolean. If true, the node will be yielded
Example:
import { visit } from "@jondotsoy/utils-js/visit";
const v = visit([1, 2, 3]);
v.next().value; // [1, 2, 3]
v.next().value; // 1
v.next().value; // 2
v.next().value; // 3
const v = visit(
{
children: [
{ type: "span", value: "foo" },
{ type: "block", children: [{ type: "span", value: "var" }] },
],
},
(node) => node.type === "span",
);
v.next().value; // {type:"span",value:"foo"}
v.next().value; // {type:"span",value:"var"}Get
Recursively retrieves a property from an object by following the specified path.
Syntax:
get(obj); // => unknown | undefined
get(obj, ...paths); // => unknown | undefined
// Alternative get value only if is of a type
get.string(obj); // => string | undefined
get.string(obj, ...paths); // => string | undefined
get.number(obj); // => number | undefined
get.number(obj, ...paths); // => number | undefined
get.boolean(obj); // => boolean | undefined
get.boolean(obj, ...paths); // => boolean | undefined
get.function(obj); // => function | undefined
get.function(obj, ...paths); // => function | undefined
get.bigint(obj); // => bigint | undefined
get.bigint(obj, ...paths); // => bigint | undefined
get.symbol(obj); // => symbol | undefined
get.symbol(obj, ...paths); // => symbol | undefined
get.array(obj); // => Array<unknown> | undefined
get.array(obj, ...paths); // => Array<unknown> | undefined
get.date(obj); // => Date | string | number | undefined
get.date(obj, ...paths); // => Date | string | number | undefined
get.record(obj); // => Record | undefined
get.record(obj, ...paths); // => Record | undefinedArguments:
obj<unknown>: The object to retrieve the property from.paths<Array<string | number | symbol>>: The path(s) of properties to follow. If no paths are provided, returns the entire object.
Return:
If a value is found on this path, the results will be an unknown other side will return a undefined value.
The function check returns only the type defined.
Example:
const obj = { key: { key: { a: 1, b: 2 } } };
get(obj, "key", "key"); // <unknown> { a: 1, b: 2 }
const obj = { key: { key: { a: 1, b: 2 } } };
get.record(obj, "key", "key"); // <Record<unknown, unknown>> { a: 1, b: 2 }