npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ilib-tree-node

v2.0.0

Published

A package to build, construct, and deconstruct an arbitrary tree of nodes.

Downloads

23,290

Readme

iLib Tree Node

Simple class to build, construct, and deconstruct an arbitrary tree structure.

A node in the tree may have an arbitrary number of children. Each node must have a type property and can optionally have any number of other properties. There will also be a "children" property which will contain any possible children, as well as a "use" and a "parent" property, so the properties "children" and "use" and "parent" are reserved.

There is no tree structure per-se, only nodes. Any node can be considered to be the root of a tree. Even a single node is a tree of size 1.

Building a Tree from Scratch

To build a tree from scratch, you can use the following methods:

node.add() - add a child node to the current node

To create a tree, create a new Node instance and then add it as a child to another Node instance.

Example of building a tree from scratch:

import Node from 'ilib-tree-node';

let parent = new Node({
    type: 'x'
});

let child = new Node({
    type: 'text',
    value: "this is a string"
});

parent.add(child);

// add an anonymous node
parent.add(new Node({
    type: 'text',
    value: "another string"
}));

Deconstructing the Tree to an Array of Nodes

To deconstruct (flatten) the tree into an array of nodes, simply use:

node.toArray() - deconstruct the tree into an array of nodes

If a node has children, the toArray method will add a start node with the "use" property set to "start", followed by the children within that node, followed finally by an end node with the "use" property set to "end". This happens recursively for all the children such that the entire tree underneath the node is flattened as well.

The array can be reconstructed back into a tree using the information in the "use" property. Any node that does not have children will get added to the array as-is.

Reconstructing the Tree from an Array of Nodes

To reconstruct a tree from an array of nodes, use the following static method:

Node.fromArray(array) - reconstruct the tree from an array of nodes

This method will use the "use" property to rebuild a tree of varying depths.

You may add a "use" property to any nodes in your array, as long as the start and end nodes for any node type are balanced and well-formed. (ie. they are properly nested like XML open and closing tags.). If they are not well-formed, this method will create a tree with an unexpected structure. Any array that is a result of a toArray call is guaranteed to be well-formed.

Unist Trees

Note that this type of tree follows the unist interface for a tree, and therefore any of the unist utilities may be used to manipulate the tree.

To convert a regular unist tree into a ilib-tree-node tree, simply do the following:

import Node from 'ilib-tree-node';
import map from 'unist-utils-map';

let ast = X; // some unist tree, probably from a parser

let treeNode = map(ast, node => new Node(node));

License

Copyright © 2018-2024, JEDLSoft

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

Release Notes

v2.0.0

  • convert all unit tests from nodeunit to jest
  • export the es6 code as real es6 modules and old transpiled javascript for older packages to use (breaking change)

v1.3.0

  • implement the ability to add an array of nodes to the children at the same time with the new addChildren method