ast-monkey-traverse
examples
Table of Contents
Quick Take
import { strict as assert } from "assert";
import { traverse } from "ast-monkey-traverse";
const paths = [];
const source = {
a: {
foo: {
bar: [
{
foo: "c",
},
],
d: {
e: {
foo: "f",
},
},
},
},
};
traverse(source, (key, val, innerObj) => {
const current = val !== undefined ? val : key;
if (
val !== undefined &&
key === "foo"
) {
paths.push(innerObj.path);
}
return current;
});
assert.deepEqual(paths, [
"a.foo",
"a.foo.bar.0.foo",
"a.foo.d.e.foo",
]);
Compatible With object-path
import { strict as assert } from "assert";
import op from "object-path";
import { traverse } from "ast-monkey-traverse";
const input = { a: "1", b: [{ c: "2" }] };
Object.freeze(input);
const result1 = [];
traverse(input, (key1, val1, innerObj) => {
const current = val1 !== undefined ? val1 : key1;
result1.push(innerObj.path);
return current;
});
assert.deepEqual(result1, ["a", "b", "b.0", "b.0.c"]);
assert.deepEqual(op.get(input, "a"), "1");
assert.deepEqual(op.get(input, "b"), [{ c: "2" }]);
assert.deepEqual(op.get(input, "b.0"), { c: "2" });
assert.deepEqual(op.get(input, "b.0.c"), "2");
Stop
import { strict as assert } from "assert";
import { traverse } from "ast-monkey-traverse";
const input = { a: "1", b: { c: "2" } };
const result1 = [];
traverse(input, (key1, val1, innerObj) => {
const current = val1 !== undefined ? val1 : key1;
result1.push(innerObj.path);
return current;
});
assert.deepEqual(result1, ["a", "b", "b.c"]);
const result2 = [];
traverse(input, (key1, val1, innerObj, stop) => {
const current = val1 !== undefined ? val1 : key1;
result2.push(innerObj.path);
if (innerObj.path === "b") {
stop.now = true;
}
return current;
});
assert.deepEqual(result2, ["a", "b"]);