Skip to content

Latest commit

 

History

History
130 lines (110 loc) · 2.04 KB

README.md

File metadata and controls

130 lines (110 loc) · 2.04 KB

Tree updater

Function to recursively update a given node and its children inside a tree structure (incl. frozen objects)


Tech stack

  • Node.js - JavaScript runtime built on Chrome's V8 JavaScript engine
  • Ava - JavaScript toolkit for tests
  • Deep freeze - Recursively Object.freeze() objects (for tests)

Requirements

Tested successfully on Node.js v5.6+


Installation

$ cd tree-updater
$ npm install

Usage

$ 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: [],
    }
}

Testing

$ npm test

License

MIT