Function to recursively update a given node and its children inside a tree structure (incl. frozen objects)
- Node.js - JavaScript runtime built on Chrome's V8 JavaScript engine
- Ava - JavaScript toolkit for tests
- Deep freeze - Recursively Object.freeze() objects (for tests)
Tested successfully on Node.js v5.6+
- Download and extract the latest version of Tree updater
- Install the dependencies and devDependencies:
$ cd tree-updater
$ npm install
$ import updateTree from './index';
$ const tree = {
"1": {
id: "1",
value: "",
children: ["2", "3"],
},
"2": {
id: "2",
value: "",
children: ["4", "5"],
},
"3": {
id: "3",
value: "",
children: ["6"],
},
"4": {
id: "4",
value: "",
children: ["7"],
},
"5": {
id: "5",
value: "",
children: [],
},
"6": {
id: "6",
value: "",
children: [],
},
"7": {
id: "7",
value: "",
children: [],
}
};
$ const updatedTree = updateTree(tree, "2", { value: "Hello World" });
// updated tree will output to:
{
"1": {
id: "1",
value: "",
children: ["2", "3"],
},
"2": {
id: "2",
value: "Hello World",
children: ["4", "5"],
},
"3": {
id: "3",
value: "",
children: ["6"],
},
"4": {
id: "4",
value: "Hello World",
children: ["7"],
},
"5": {
id: "5",
value: "Hello World",
children: [],
},
"6": {
id: "6",
value: "",
children: [],
},
"7": {
id: "7",
value: "Hello World",
children: [],
}
}
$ npm test
MIT