Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
symbol-tree
Advanced tools
Turn any collection of objects into its own efficient tree or linked list using Symbol
The symbol-tree package is a utility library for managing an ordered tree structure with symbols, allowing you to efficiently traverse and manipulate the tree without polluting the objects that make up the nodes of the tree. It is particularly useful when you need to maintain a tree of objects in memory and perform various operations on it without exposing the tree metadata on the objects themselves.
Creating and managing a tree structure
This feature allows you to create a tree and manage its structure by appending children to parent nodes. The tree structure is maintained internally using symbols.
{"const SymbolTree = require('symbol-tree');\nconst tree = new SymbolTree();\nconst parent = {};\nconst child1 = {};\nconst child2 = {};\ntree.appendChild(parent, child1);\ntree.appendChild(parent, child2);\nconsole.log(tree.firstChild(parent) === child1); // true\nconsole.log(tree.lastChild(parent) === child2); // true"}
Traversing the tree
This feature provides methods to traverse the tree, such as iterating over the children of a node. It allows for efficient tree traversal without exposing the internal structure.
{"const SymbolTree = require('symbol-tree');\nconst tree = new SymbolTree();\nconst parent = {};\nconst child = {};\ntree.appendChild(parent, child);\nfor(const node of tree.childrenIterator(parent)) {\n console.log(node); // logs the `child` object\n}"}
Manipulating the tree
This feature includes methods to manipulate the tree, such as inserting nodes before or after a given node, which allows for dynamic changes to the tree structure.
{"const SymbolTree = require('symbol-tree');\nconst tree = new SymbolTree();\nconst parent = {};\nconst child = {};\nconst newParent = {};\ntree.appendChild(parent, child);\ntree.insertAfter(child, newParent);\nconsole.log(tree.nextSibling(child) === newParent); // true"}
The domutils package provides utilities for working with DOM trees. It offers functions for traversing, manipulating, and serializing DOM nodes. Compared to symbol-tree, domutils is specifically tailored for DOM operations and not for generic tree structures.
The treenode package is designed for creating and manipulating tree structures. It provides a TreeNode class with methods for adding children, searching, and traversing the tree. Unlike symbol-tree, treenode exposes the tree structure on the TreeNode instances themselves.
Arborist is a package that manages dependency trees in npm projects. While it deals with tree structures, its focus is on package dependency resolution and not on providing a generic tree manipulation API like symbol-tree.
Turn any collection of objects into its own efficient tree or linked list using Symbol
.
This library has been designed to provide an efficient backing data structure for DOM trees. You can also use this library as an efficient linked list. Any meta data is stored on your objects directly, which ensures any kind of insertion or deletion is performed in constant time. Because an ES6 Symbol
is used, the meta data does not interfere with your object in any way.
Only io.js is supported at the moment, however io.js and node.js will merge in the near future.
A linked list:
const SymbolTree = require('symbol-tree');
const tree = new SymbolTree();
let a = {foo: 'bar'}; // or `new Whatever()`
let b = {foo: 'baz'};
let c = {foo: 'qux'};
tree.insertBefore(b, a); // insert a before b
tree.insertAfter(b, c); // insert c after b
console.log(tree.nextSibling(a) === b);
console.log(tree.nextSibling(b) === c);
console.log(tree.previousSibling(c) === b);
tree.remove(b);
console.log(tree.nextSibling(a) === c);
A tree:
const SymbolTree = require('symbol-tree');
const tree = new SymbolTree();
let parent = {};
let a = {};
let b = {};
let c = {};
tree.prependChild(parent, a); // insert a as the first child
tree.appendChild(parent,c ); // insert c as the last child
tree.insertAfter(a, b); // insert b after a, it now has the same parent as a
console.log(tree.firstChild(parent) === a);
console.log(tree.nextSibling(tree.firstChild(parent)) === b);
console.log(tree.lastChild(parent) === c);
let grandparent = {};
tree.prependChild(grandparent, parent);
console.log(tree.firstChild(tree.firstChild(grandparent)) === a);
See api.md for more documentation.
Make sure you install the dependencies first:
npm install
You can now run the unit tests by executing:
npm test
The line and branch coverage should be 100%.
FAQs
Turn any collection of objects into its own efficient tree or linked list using Symbol
The npm package symbol-tree receives a total of 14,486,266 weekly downloads. As such, symbol-tree popularity was classified as popular.
We found that symbol-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.