Giter VIP home page Giter VIP logo

tree-updater's Introduction

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

tree-updater's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.