symbol-tree

中文文档
3.2.4发布于 2019/06/13MITCommonJS零依赖近 30 天下载 3.2亿

Turn any collection of objects into its own efficient tree or linked list using Symbol

原始使用文档
文档目录5 节
  1. Example
  2. Testing
  3. API Documentation
  4. symbol-tree
  5. SymbolTree ⏏

symbol-tree

Travis CI Build Status Coverage Status

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.

Node.js 4+, io.js and modern browsers are supported.

Example

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.

Testing

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

API Documentation

symbol-tree

Author: Joris van der Wel [email protected]

SymbolTree ⏏

Kind: Exported class

new SymbolTree([description])

ParamTypeDefaultDescription
[description]string"'SymbolTree data'"Description used for the Symbol

symbolTree.initialize(object) ⇒ Object

You can use this function to (optionally) initialize an object right after its creation, to take advantage of V8's fast properties. Also useful if you would like to freeze your object.

O(1)

Kind: instance method of SymbolTree
Returns: Object - object

ParamType
objectObject

symbolTree.hasChildren(object) ⇒ Boolean

Returns true if the object has any children. Otherwise it returns false.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.firstChild(object) ⇒ Object

Returns the first child of the given object.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.lastChild(object) ⇒ Object

Returns the last child of the given object.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.previousSibling(object) ⇒ Object

Returns the previous sibling of the given object.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.nextSibling(object) ⇒ Object

Returns the next sibling of the given object.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.parent(object) ⇒ Object

Return the parent of the given object.

  • O(1)

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.lastInclusiveDescendant(object) ⇒ Object

Find the inclusive descendant that is last in tree order of the given object.

  • O(n) (worst case) where n is the depth of the subtree of object

Kind: instance method of SymbolTree

ParamType
objectObject

symbolTree.preceding(object, [options]) ⇒ Object

Find the preceding object (A) of the given object (B). An object A is preceding an object B if A and B are in the same tree and A comes before B in tree order.

  • O(n) (worst case)
  • O(1) (amortized when walking the entire tree)

Kind: instance method of SymbolTree

ParamTypeDescription
objectObject
[options]Object
[options.root]ObjectIf set, root must be an inclusive ancestor of the return value (or else null is returned). This check assumes that root is also an inclusive ancestor of the given object

symbolTree.following(object, [options]) ⇒ Object

Find the following object (A) of the given object (B). An object A is following an object B if A and B are in the same tree and A comes after B in tree order.

  • O(n) (worst case) where n is the amount of objects in the entire tree
  • O(1) (amortized when walking the entire tree)

Kind: instance method of SymbolTree

ParamTypeDefaultDescription
objectObject
[options]Object
[options.root]ObjectIf set, root must be an inclusive ancestor of the return value (or else null is returned). This check assumes that root is also an inclusive ancestor of the given object
[options.skipChildren]BooleanfalseIf set, ignore the children of object

symbolTree.childrenToArray(parent, [options]) ⇒ Array.<Object>

Append all children of the given object to an array.

  • O(n) where n is the amount of children of the given parent

Kind: instance method of SymbolTree

ParamTypeDefaultDescription
parentObject
[options]Object
[options.array]Array.<Object>[]
[options.filter]functionFunction to test each object before it is added to the array. Invoked with arguments (object). Should return true if an object is to be included.
[options.thisArg]*Value to use as this when executing filter.

symbolTree.ancestorsToArray(object, [options]) ⇒ Array.<Object>

Append all inclusive ancestors of the given object to an array.

  • O(n) where n is the amount of ancestors of the given object

Kind: instance method of SymbolTree

ParamTypeDefaultDescription
objectObject
[options]Object
[options.array]Array.<Object>[]
[options.filter]functionFunction to test each object before it is added to the array. Invoked with arguments (object). Should return true if an object is to be included.
[options.thisArg]*Value to use as this when executing filter.

symbolTree.treeToArray(root, [options]) ⇒ Array.<Object>

Append all descendants of the given object to an array (in tree order).

  • O(n) where n is the amount of objects in the sub-tree of the given object

Kind: instance method of SymbolTree

ParamTypeDefaultDescription
rootObject
[options]Object
[options.array]Array.<Object>[]
[options.filter]functionFunction to test each object before it is added to the array. Invoked with arguments (object). Should return true if an object is to be included.
[options.thisArg]*Value to use as this when executing filter.

symbolTree.childrenIterator(parent, [options]) ⇒ Object

Iterate over all children of the given object

  • O(1) for a single iteration

Kind: instance method of SymbolTree
Returns: Object - An iterable iterator (ES6)

ParamTypeDefault
parentObject
[options]Object
[options.reverse]Booleanfalse

symbolTree.previousSiblingsIterator(object) ⇒ Object

Iterate over all the previous siblings of the given object. (in reverse tree order)

  • O(1) for a single iteration

Kind: instance method of SymbolTree
Returns: Object - An iterable iterator (ES6)

ParamType
objectObject

symbolTree.nextSiblingsIterator(object) ⇒ Object

Iterate over all the next siblings of the given object. (in tree order)

  • O(1) for a single iteration

Kind: instance method of SymbolTree
Returns: Object - An iterable iterator (ES6)

ParamType
objectObject

symbolTree.ancestorsIterator(object) ⇒ Object

Iterate over all inclusive ancestors of the given object

  • O(1) for a single iteration

Kind: instance method of SymbolTree
Returns: Object - An iterable iterator (ES6)

ParamType
objectObject

symbolTree.treeIterator(root, options) ⇒ Object

Iterate over all descendants of the given object (in tree order).

Where n is the amount of objects in the sub-tree of the given root:

  • O(n) (worst case for a single iteration)
  • O(n) (amortized, when completing the iterator)

Kind: instance method of SymbolTree
Returns: Object - An iterable iterator (ES6)

ParamTypeDefault
rootObject
optionsObject
[options.reverse]Booleanfalse

symbolTree.index(child) ⇒ Number

Find the index of the given object (the number of preceding siblings).

  • O(n) where n is the amount of preceding siblings
  • O(1) (amortized, if the tree is not modified)

Kind: instance method of SymbolTree
Returns: Number - The number of preceding siblings, or -1 if the object has no parent

ParamType
childObject

symbolTree.childrenCount(parent) ⇒ Number

Calculate the number of children.

  • O(n) where n is the amount of children
  • O(1) (amortized, if the tree is not modified)

Kind: instance method of SymbolTree

ParamType
parentObject

symbolTree.compareTreePosition(left, right) ⇒ Number

Compare the position of an object relative to another object. A bit set is returned:

  • DISCONNECTED : 1
  • PRECEDING : 2
  • FOLLOWING : 4
  • CONTAINS : 8
  • CONTAINED_BY : 16

The semantics are the same as compareDocumentPosition in DOM, with the exception that DISCONNECTED never occurs with any other bit.

where n and m are the amount of ancestors of left and right; where o is the amount of children of the lowest common ancestor of left and right:

  • O(n + m + o) (worst case)
  • O(n + m) (amortized, if the tree is not modified)

Kind: instance method of SymbolTree

ParamType
leftObject
rightObject

symbolTree.remove(removeObject) ⇒ Object

Remove the object from this tree. Has no effect if already removed.

  • O(1)

Kind: instance method of SymbolTree
Returns: Object - removeObject

ParamType
removeObjectObject

symbolTree.insertBefore(referenceObject, newObject) ⇒ Object

Insert the given object before the reference object. newObject is now the previous sibling of referenceObject.

  • O(1)

Kind: instance method of SymbolTree
Returns: Object - newObject
Throws:

  • Error If the newObject is already present in this SymbolTree
ParamType
referenceObjectObject
newObjectObject

symbolTree.insertAfter(referenceObject, newObject) ⇒ Object

Insert the given object after the reference object. newObject is now the next sibling of referenceObject.

  • O(1)

Kind: instance method of SymbolTree
Returns: Object - newObject
Throws:

  • Error If the newObject is already present in this SymbolTree
ParamType
referenceObjectObject
newObjectObject

symbolTree.prependChild(referenceObject, newObject) ⇒ Object

Insert the given object as the first child of the given reference object. newObject is now the first child of referenceObject.

  • O(1)

Kind: instance method of SymbolTree
Returns: Object - newObject
Throws:

  • Error If the newObject is already present in this SymbolTree
ParamType
referenceObjectObject
newObjectObject

symbolTree.appendChild(referenceObject, newObject) ⇒ Object

Insert the given object as the last child of the given reference object. newObject is now the last child of referenceObject.

  • O(1)

Kind: instance method of SymbolTree
Returns: Object - newObject
Throws:

  • Error If the newObject is already present in this SymbolTree
ParamType
referenceObjectObject
newObjectObject

版本与发布记录

symbol-tree 共发布 19 个版本,首次发布于 2015/07/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.2.4latest2019/06/137 年前
3.2.32019/06/137 年前
3.2.22017/02/129 年前
3.2.12016/12/199 年前
3.2.02016/12/119 年前
3.1.42015/11/0910 年前

关于 symbol-tree 的常见问题

symbol-tree 是什么?

Turn any collection of objects into its own efficient tree or linked list using Symbol

如何安装 symbol-tree?

在项目目录运行 npm install symbol-tree,或使用 pnpm add / yarn add / bun add。

symbol-tree 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/symbol-tree 是否存在。

symbol-tree 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

symbol-tree 可以商用吗?

symbol-tree 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

symbol-tree 的下载量有多少?

根据 npm 官方下载统计,symbol-tree 在 2026/10/05 – 2026/11/03 这 30 天内被下载 321,917,778 次,日均约 10,730,593 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

symbol-tree 还在维护吗?

最新版本 3.2.4 发布于 2019/06/13(7 年前);至今共发布 19 个版本,其中过去 12 个月发布了 0 个;首次发布于 2015/07/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。