Coder Social home page Coder Social logo

ts-tree-structure's Introduction

ts-tree-structure

Manipulate and traverse tree-like structures in TypeScript.

Inspiration from tree-model.js.

Build Status

Installation

Node

Tree is available as an npm module so you can install it with npm install ts-tree-structure and use it in your script:

ES Module

import Tree from 'ts-tree-structure';

const tree = new Tree();
const root = tree.parse({ id: 1, name: 'foo', children: [{ id: 11, name: 'bar' }]});

Common.js

const Tree = require('ts-tree-structure').default;

const tree = new Tree();
const root = tree.parse({ id: 1, name: 'foo', children: [{ id: 11, name: 'bar' }]});

UMD

<script src="https://unpkg.com/ts-tree-structure/umd/ts-tree-structure.min.js"></script>
<script>
  var Tree = Tree.default;
  var tree = new Tree();
  var root = tree.parse({ id: 1, name: 'foo', children: [{ id: 11, name: 'bar' }]});
</script>

TypeScript

Type definitions are already bundled with the package, which should just work with npm install.

You can maually find the definition files in the src folder.

API Reference

Create a new Tree

Create a new Tree with the given options.

const tree = new Tree()

Parse the hierarchy object

Parse the given user defined model and return the root Node object.

tree.parse(model): Node

Is Root?

Return true if this Node is the root, false otherwise.

node.isRoot(): boolean

Has Children?

Return true if this Node has one or more children, false otherwise.

node.hasChildren(): boolean

Add a child

Add the given node as child of this one. Return the child Node.

parentNode.addChild(childNode): Node

Add a child at a given index

Add the given node as child of this one at the given index. Return the child Node.

parentNode.addChildAtIndex(childNode, index): Node

Set the index of a node among its siblings

Sets the index of the node among its siblings to the given value. Return the node itself.

node.setIndex(index): Node

Get the index of a node among its siblings

Gets the index of the node relative to its siblings. Return the index value.

node.getIndex(): number

Get the node path

Get the array of Nodes representing the path from the root to this Node (inclusive).

node.getPath(): Node[]

Delete a node from the tree

Drop the subtree starting at this node. Returns the node itself, which is now a root node.

node.drop(): Node

Warning - Dropping a node while walking the tree is not supported. You must first collect the nodes to drop using one of the traversal functions and then drop them. Example:

root.all( /* predicate */ ).forEach((node) => {
  node.drop();
});

Find a node

Starting from this node, find the first Node that matches the predicate and return it. The predicate is a function wich receives the visited Node and returns true if the Node should be picked and false otherwise.

node.first(predicate): Node

Find all nodes

Starting from this node, find all Nodes that match the predicate and return these.

node.all(predicate): Node[]

Walk the tree

Starting from this node, traverse the subtree calling the action for each visited node. The action is a function which receives the visited Node as argument. The traversal can be halted by returning false from the action.

node.walk([options], action): void

Note - first, all and walk can optionally receive as first argument an object with traversal options. Currently the only supported option is the traversal strategy which can be any of the following:

  • {strategy: 'pre'} - Depth-first pre-order [default];
  • {strategy: 'post'} - Depth-first post-order;
  • {strategy: 'breadth'} - Breadth-first.

These functions can also take, as the last parameter, the context on which the action will be called.

Contributing

Setup

Fork this repository and run npm install on the project root folder to make sure you have all project dependencies installed.

Code Linting

Run npm run lint

This will check both source and tests for code correctness and style compliance.

Running Tests

Run npm test

License

MIT

ts-tree-structure's People

Contributors

gentamura 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.