symbol-tree
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Symbol-tree is a JavaScript library that allows users to efficiently turn any collection of objects into its own tree or linked list using the ES6 Symbol. It has been specifically designed as an optimal backing data structure for DOM trees, but can also be utilized as a proficient linked list. The meta data is stored directly on the objects, ensuring performing any kind of insertion or deletion is carried out in constant time.
To use the symbol-tree package, you first need to install it via npm using npm install symbol-tree
. After that, you can include it in your JavaScript file using the require
statement. Here's how you create a linked list and a tree using symbol-tree
:
Linked List
const SymbolTree = require('symbol-tree');
const tree = new SymbolTree();
let a = {foo: 'bar'};
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);
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);
The detailed documentation of symbol-tree is included in the api.md
file in the repository. This provides instructions on how to use each method in the SymbolTree class. Additionally, complete API documentation is presented within the readme file of the GitHub repository at git+https://github.com/jsdom/js-symbol-tree.git
.