1.0.3 • Published 6 years ago
object-graph v1.0.3
Object Graph
Simpler library to walk into objects as graph.
Installation
Using npm:
$ npm i -g npm
$ npm i object-graph
Note: add --save if you are using npm < 5.0.0
In Node.js:
// Load the walker.
var Graph = require('object-graph');
var object = {
Root: {
BasicNode1: {
BasicNode2: {
BasicNode3: {}
}
},
ArrayNode: ["ArrayElement", 16384, { BasicNode4: {} }],
String: "StringElement",
Integer: 42,
Double: 3.1415926589792,
Set: new Set([1, 2, 3]), // Set no has keys, then its not iterable,
Function() {
return null;
},
AnonimousFunction: () => null
}
}
// Walk an breadth search walk once with function mode
console.log("Breadth walk");
const breadth = Array.from(Graph.BreadthFirstSearchFunction(object));
breadth.forEach(({ path }) => {
console.log(path.reverse());
});
// Walk an depth search walk multiple times with class mode
console.log("Depth walk");
const depthClass = new Graph.DepthFirstSearch(object);
const depth = Array.from(depthClass);
depth.forEach(({ path }) => {
console.log(path.reverse());
});
// or
for (let { path } of depthClass) {
console.log(path.reverse());
}
Examples
See some examples of usage in examples.