Skip to content

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

Notifications You must be signed in to change notification settings

paratagas/tree-updater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages