pax_global_header00006660000000000000000000000064135002376650014521gustar00rootroot0000000000000052 comment=693669568538887e75181628c392668f8ff4a6d6 js-symbol-tree-3.2.4/000077500000000000000000000000001350023766500144035ustar00rootroot00000000000000js-symbol-tree-3.2.4/.editorconfig000066400000000000000000000002231350023766500170550ustar00rootroot00000000000000root = true [*] end_of_line = lf insert_final_newline = true charset = utf-8 indent_style = space indent_size = 8 trim_trailing_whitespace = true js-symbol-tree-3.2.4/.eslintrc.json000066400000000000000000000210061350023766500171760ustar00rootroot00000000000000{ "env": { "commonjs": true, "es6": true, "node": true }, "globals": {}, "parser": "babel-eslint", "parserOptions": { "ecmaVersion": 7, "sourceType": "script" }, "plugins": [ "import" ], "rules": { "array-bracket-spacing": [ "error", "never" ], "array-callback-return": "error", "arrow-parens": "off", "arrow-spacing": "error", "brace-style": [ "error", "stroustrup", { "allowSingleLine": true } ], "camelcase": [ "error", { "properties": "always" } ], "comma-dangle": [ "error", "always-multiline" ], "comma-spacing": [ "error", { "after": true } ], "comma-style": [ "error", "last" ], "complexity": "warn", "consistent-return": "error", "constructor-super": "error", "curly": "error", "dot-location": [ "error", "property" ], "dot-notation": [ "error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" } ], "eol-last": "error", "eqeqeq": "error", "generator-star-spacing": [ "error", { "after": true, "before": false } ], "global-require": "error", "import/default": "error", "import/export": "error", "import/first": "error", "import/named": "error", "import/namespace": "error", "import/newline-after-import": "error", "import/no-absolute-path": "error", "import/no-extraneous-dependencies": [ "error", { "devDependencies": [ "test/**", "webpack.config.js" ] } ], "import/no-mutable-exports": "error", "import/no-named-default": "error", "import/no-unassigned-import": "error", "import/no-webpack-loader-syntax": "error", "import/order": [ "error", { "groups": [ ["builtin", "external"], ["index","sibling","parent","internal"] ], "newlines-between": "always" } ], "indent": [ "error", 8, { "SwitchCase": 1 } ], "key-spacing": [ "error", { "afterColon": true, "beforeColon": false, "mode": "minimum" } ], "keyword-spacing": "error", "linebreak-style": [ "error", "unix" ], "max-len": [ "error", 140 ], "max-nested-callbacks": [ "error", 4 ], "max-params": [ "error", 5 ], "new-cap": [ "error", { "capIsNewExceptions": [ "Array", "Boolean", "Error", "Number", "PageMod", "Object", "QueryInterface", "String", "Symbol" ] } ], "no-array-constructor": "error", "no-caller": "error", "no-case-declarations": "error", "no-catch-shadow": "error", "no-class-assign": "error", "no-cond-assign": [ "error", "except-parens" ], "no-confusing-arrow": [ "error", { "allowParens": true } ], "no-const-assign": "error", "no-div-regex": "error", "no-dupe-args": "error", "no-dupe-class-members": "error", "no-dupe-keys": "error", "no-duplicate-case": "error", "no-else-return": "error", "no-empty-character-class": "error", "no-empty-pattern": "error", "no-eval": "error", "no-ex-assign": "error", "no-fallthrough": "error", "no-floating-decimal": "error", "no-func-assign": "error", "no-implicit-coercion": [ "error", { "boolean": true, "number": true, "string": true } ], "no-implied-eval": "error", "no-inner-declarations": "error", "no-invalid-regexp": "error", "no-iterator": "error", "no-lonely-if": "error", "no-loop-func": "error", "no-multi-str": "error", "no-native-reassign": "error", "no-negated-in-lhs": "error", "no-new-func": "error", "no-new-object": "error", "no-new-symbol": "error", "no-new-wrappers": "error", "no-obj-calls": "error", "no-octal": "error", "no-octal-escape": "error", "no-param-reassign": "error", "no-proto": "error", "no-prototype-builtins": "error", "no-regex-spaces": "error", "no-return-assign": "error", "no-self-assign": "error", "no-self-compare": "error", "no-spaced-func": "error", "no-sparse-arrays": "error", "no-tabs": "error", "no-this-before-super": "error", "no-throw-literal": "error", "no-trailing-spaces": "error", "no-undef": "error", "no-unexpected-multiline": "error", "no-unmodified-loop-condition": "error", "no-unreachable": "error", "no-unsafe-finally": "error", "no-unsafe-negation": "error", "no-unused-vars": [ "error", { "args": "none" } ], "no-useless-call": "error", "no-useless-computed-key": "error", "no-var": "error", "no-void": "error", "no-with": "error", "object-curly-spacing": [ "error", "never" ], "one-var": [ "error", "never" ], "prefer-arrow-callback": "error", "prefer-const": "error", "prefer-rest-params": "error", "prefer-spread": "error", "quote-props": [ "error", "as-needed" ], "quotes": [ "error", "single", { "allowTemplateLiterals": true } ], "radix": "error", "rest-spread-spacing": "error", "semi": [ "error", "always" ], "semi-spacing": [ "error", { "after": true, "before": false } ], "sort-keys": [ "error", "asc", { "caseSensitive": true, "natural": true } ], "space-before-blocks": [ "error", "always" ], "space-before-function-paren": [ "error", { "anonymous": "ignore", "named": "never" } ], "space-in-parens": [ "error", "never" ], "space-infix-ops": "error", "space-unary-ops": [ "error", { "nonwords": false, "words": false } ], "spaced-comment": [ "error", "always" ], "strict": "error", "unicode-bom": "error", "use-isnan": "error", "valid-jsdoc": [ "error", { "prefer": { "arg": "param", "argument": "param", "class": "constructor", "returns": "return", "virtual": "abstract" }, "requireParamDescription": false, "requireReturn": false, "requireReturnDescription": false } ], "valid-typeof": [ "error", { "requireStringLiterals": true } ], "wrap-iife": "error", "yoda": [ "error", "never" ] } } js-symbol-tree-3.2.4/.gitignore000066400000000000000000000001311350023766500163660ustar00rootroot00000000000000/node_modules /jsdoc /coverage /.idea /symbol-tree.iml /npm-debug.log /package-lock.json js-symbol-tree-3.2.4/.travis.yml000066400000000000000000000001361350023766500165140ustar00rootroot00000000000000language: node_js node_js: - "node" install: - npm install script: npm run ci js-symbol-tree-3.2.4/LICENSE000066400000000000000000000020741350023766500154130ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Joris van der Wel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. js-symbol-tree-3.2.4/README.md000066400000000000000000000510501350023766500156630ustar00rootroot00000000000000symbol-tree =========== [![Travis CI Build Status](https://api.travis-ci.org/jsdom/js-symbol-tree.svg?branch=master)](https://travis-ci.org/jsdom/js-symbol-tree) [![Coverage Status](https://coveralls.io/repos/github/jsdom/js-symbol-tree/badge.svg?branch=master)](https://coveralls.io/github/jsdom/js-symbol-tree?branch=master) 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: ```javascript 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: ```javascript 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](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 * [symbol-tree](#module_symbol-tree) * [SymbolTree](#exp_module_symbol-tree--SymbolTree) ⏏ * [new SymbolTree([description])](#new_module_symbol-tree--SymbolTree_new) * [.initialize(object)](#module_symbol-tree--SymbolTree+initialize) ⇒ Object * [.hasChildren(object)](#module_symbol-tree--SymbolTree+hasChildren) ⇒ Boolean * [.firstChild(object)](#module_symbol-tree--SymbolTree+firstChild) ⇒ Object * [.lastChild(object)](#module_symbol-tree--SymbolTree+lastChild) ⇒ Object * [.previousSibling(object)](#module_symbol-tree--SymbolTree+previousSibling) ⇒ Object * [.nextSibling(object)](#module_symbol-tree--SymbolTree+nextSibling) ⇒ Object * [.parent(object)](#module_symbol-tree--SymbolTree+parent) ⇒ Object * [.lastInclusiveDescendant(object)](#module_symbol-tree--SymbolTree+lastInclusiveDescendant) ⇒ Object * [.preceding(object, [options])](#module_symbol-tree--SymbolTree+preceding) ⇒ Object * [.following(object, [options])](#module_symbol-tree--SymbolTree+following) ⇒ Object * [.childrenToArray(parent, [options])](#module_symbol-tree--SymbolTree+childrenToArray) ⇒ Array.<Object> * [.ancestorsToArray(object, [options])](#module_symbol-tree--SymbolTree+ancestorsToArray) ⇒ Array.<Object> * [.treeToArray(root, [options])](#module_symbol-tree--SymbolTree+treeToArray) ⇒ Array.<Object> * [.childrenIterator(parent, [options])](#module_symbol-tree--SymbolTree+childrenIterator) ⇒ Object * [.previousSiblingsIterator(object)](#module_symbol-tree--SymbolTree+previousSiblingsIterator) ⇒ Object * [.nextSiblingsIterator(object)](#module_symbol-tree--SymbolTree+nextSiblingsIterator) ⇒ Object * [.ancestorsIterator(object)](#module_symbol-tree--SymbolTree+ancestorsIterator) ⇒ Object * [.treeIterator(root, options)](#module_symbol-tree--SymbolTree+treeIterator) ⇒ Object * [.index(child)](#module_symbol-tree--SymbolTree+index) ⇒ Number * [.childrenCount(parent)](#module_symbol-tree--SymbolTree+childrenCount) ⇒ Number * [.compareTreePosition(left, right)](#module_symbol-tree--SymbolTree+compareTreePosition) ⇒ Number * [.remove(removeObject)](#module_symbol-tree--SymbolTree+remove) ⇒ Object * [.insertBefore(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertBefore) ⇒ Object * [.insertAfter(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertAfter) ⇒ Object * [.prependChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+prependChild) ⇒ Object * [.appendChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+appendChild) ⇒ Object ### SymbolTree ⏏ **Kind**: Exported class #### new SymbolTree([description]) | Param | Type | Default | Description | | --- | --- | --- | --- | | [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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - object | Param | Type | | --- | --- | | object | Object | #### symbolTree.hasChildren(object) ⇒ Boolean Returns `true` if the object has any children. Otherwise it returns `false`. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### symbolTree.firstChild(object) ⇒ Object Returns the first child of the given object. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### symbolTree.lastChild(object) ⇒ Object Returns the last child of the given object. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### symbolTree.previousSibling(object) ⇒ Object Returns the previous sibling of the given object. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### symbolTree.nextSibling(object) ⇒ Object Returns the next sibling of the given object. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### symbolTree.parent(object) ⇒ Object Return the parent of the given object. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | object | Object | #### 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | Description | | --- | --- | --- | | object | Object | | | [options] | Object | | | [options.root] | Object | If 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | Default | Description | | --- | --- | --- | --- | | object | Object | | | | [options] | Object | | | | [options.root] | Object | | If 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] | Boolean | false | If 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | Default | Description | | --- | --- | --- | --- | | parent | Object | | | | [options] | Object | | | | [options.array] | Array.<Object> | [] | | | [options.filter] | function | | Function 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | Default | Description | | --- | --- | --- | --- | | object | Object | | | | [options] | Object | | | | [options.array] | Array.<Object> | [] | | | [options.filter] | function | | Function 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | Default | Description | | --- | --- | --- | --- | | root | Object | | | | [options] | Object | | | | [options.array] | Array.<Object> | [] | | | [options.filter] | function | | Function 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - An iterable iterator (ES6) | Param | Type | Default | | --- | --- | --- | | parent | Object | | | [options] | Object | | | [options.reverse] | Boolean | false | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - An iterable iterator (ES6) | Param | Type | | --- | --- | | object | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - An iterable iterator (ES6) | Param | Type | | --- | --- | | object | Object | #### symbolTree.ancestorsIterator(object) ⇒ Object Iterate over all inclusive ancestors of the given object * `O(1)` for a single iteration **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - An iterable iterator (ES6) | Param | Type | | --- | --- | | object | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - An iterable iterator (ES6) | Param | Type | Default | | --- | --- | --- | | root | Object | | | options | Object | | | [options.reverse] | Boolean | false | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Number - The number of preceding siblings, or -1 if the object has no parent | Param | Type | | --- | --- | | child | Object | #### 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | parent | Object | #### symbolTree.compareTreePosition(left, right) ⇒ Number Compare the position of an object relative to another object. A bit set is returned: 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](#exp_module_symbol-tree--SymbolTree) | Param | Type | | --- | --- | | left | Object | | right | Object | #### symbolTree.remove(removeObject) ⇒ Object Remove the object from this tree. Has no effect if already removed. * `O(1)` **Kind**: instance method of [SymbolTree](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - removeObject | Param | Type | | --- | --- | | removeObject | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - newObject **Throws**: - Error If the newObject is already present in this SymbolTree | Param | Type | | --- | --- | | referenceObject | Object | | newObject | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - newObject **Throws**: - Error If the newObject is already present in this SymbolTree | Param | Type | | --- | --- | | referenceObject | Object | | newObject | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - newObject **Throws**: - Error If the newObject is already present in this SymbolTree | Param | Type | | --- | --- | | referenceObject | Object | | newObject | Object | #### 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](#exp_module_symbol-tree--SymbolTree) **Returns**: Object - newObject **Throws**: - Error If the newObject is already present in this SymbolTree | Param | Type | | --- | --- | | referenceObject | Object | | newObject | Object | js-symbol-tree-3.2.4/lib/000077500000000000000000000000001350023766500151515ustar00rootroot00000000000000js-symbol-tree-3.2.4/lib/SymbolTree.js000066400000000000000000000715061350023766500176050ustar00rootroot00000000000000'use strict'; /** * @module symbol-tree * @author Joris van der Wel */ const SymbolTreeNode = require('./SymbolTreeNode'); const TreePosition = require('./TreePosition'); const TreeIterator = require('./TreeIterator'); function returnTrue() { return true; } function reverseArrayIndex(array, reverseIndex) { return array[array.length - 1 - reverseIndex]; // no need to check `index >= 0` } class SymbolTree { /** * @constructor * @alias module:symbol-tree * @param {string} [description='SymbolTree data'] Description used for the Symbol */ constructor(description) { this.symbol = Symbol(description || 'SymbolTree data'); } /** * 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)` * * @method * @alias module:symbol-tree#initialize * @param {Object} object * @return {Object} object */ initialize(object) { this._node(object); return object; } _node(object) { if (!object) { return null; } const node = object[this.symbol]; if (node) { return node; } return (object[this.symbol] = new SymbolTreeNode()); } /** * Returns `true` if the object has any children. Otherwise it returns `false`. * * * `O(1)` * * @method hasChildren * @memberOf module:symbol-tree# * @param {Object} object * @return {Boolean} */ hasChildren(object) { return this._node(object).hasChildren; } /** * Returns the first child of the given object. * * * `O(1)` * * @method firstChild * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ firstChild(object) { return this._node(object).firstChild; } /** * Returns the last child of the given object. * * * `O(1)` * * @method lastChild * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ lastChild(object) { return this._node(object).lastChild; } /** * Returns the previous sibling of the given object. * * * `O(1)` * * @method previousSibling * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ previousSibling(object) { return this._node(object).previousSibling; } /** * Returns the next sibling of the given object. * * * `O(1)` * * @method nextSibling * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ nextSibling(object) { return this._node(object).nextSibling; } /** * Return the parent of the given object. * * * `O(1)` * * @method parent * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ parent(object) { return this._node(object).parent; } /** * 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` * * @method lastInclusiveDescendant * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} */ lastInclusiveDescendant(object) { let lastChild; let current = object; while ((lastChild = this._node(current).lastChild)) { current = lastChild; } return current; } /** * 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) * * @method preceding * @memberOf module:symbol-tree# * @param {Object} object * @param {Object} [options] * @param {Object} [options.root] If 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` * @return {?Object} */ preceding(object, options) { const treeRoot = options && options.root; if (object === treeRoot) { return null; } const previousSibling = this._node(object).previousSibling; if (previousSibling) { return this.lastInclusiveDescendant(previousSibling); } // if there is no previous sibling return the parent (might be null) return this._node(object).parent; } /** * 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) * * @method following * @memberOf module:symbol-tree# * @param {!Object} object * @param {Object} [options] * @param {Object} [options.root] If 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` * @param {Boolean} [options.skipChildren=false] If set, ignore the children of `object` * @return {?Object} */ following(object, options) { const treeRoot = options && options.root; const skipChildren = options && options.skipChildren; const firstChild = !skipChildren && this._node(object).firstChild; if (firstChild) { return firstChild; } let current = object; do { if (current === treeRoot) { return null; } const nextSibling = this._node(current).nextSibling; if (nextSibling) { return nextSibling; } current = this._node(current).parent; } while (current); return null; } /** * Append all children of the given object to an array. * * * `O(n)` where `n` is the amount of children of the given `parent` * * @method childrenToArray * @memberOf module:symbol-tree# * @param {Object} parent * @param {Object} [options] * @param {Object[]} [options.array=[]] * @param {Function} [options.filter] Function 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. * @param {*} [options.thisArg] Value to use as `this` when executing `filter`. * @return {Object[]} */ childrenToArray(parent, options) { const array = (options && options.array) || []; const filter = (options && options.filter) || returnTrue; const thisArg = (options && options.thisArg) || undefined; const parentNode = this._node(parent); let object = parentNode.firstChild; let index = 0; while (object) { const node = this._node(object); node.setCachedIndex(parentNode, index); if (filter.call(thisArg, object)) { array.push(object); } object = node.nextSibling; ++index; } return array; } /** * Append all inclusive ancestors of the given object to an array. * * * `O(n)` where `n` is the amount of ancestors of the given `object` * * @method ancestorsToArray * @memberOf module:symbol-tree# * @param {Object} object * @param {Object} [options] * @param {Object[]} [options.array=[]] * @param {Function} [options.filter] Function 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. * @param {*} [options.thisArg] Value to use as `this` when executing `filter`. * @return {Object[]} */ ancestorsToArray(object, options) { const array = (options && options.array) || []; const filter = (options && options.filter) || returnTrue; const thisArg = (options && options.thisArg) || undefined; let ancestor = object; while (ancestor) { if (filter.call(thisArg, ancestor)) { array.push(ancestor); } ancestor = this._node(ancestor).parent; } return array; } /** * 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` * * @method treeToArray * @memberOf module:symbol-tree# * @param {Object} root * @param {Object} [options] * @param {Object[]} [options.array=[]] * @param {Function} [options.filter] Function 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. * @param {*} [options.thisArg] Value to use as `this` when executing `filter`. * @return {Object[]} */ treeToArray(root, options) { const array = (options && options.array) || []; const filter = (options && options.filter) || returnTrue; const thisArg = (options && options.thisArg) || undefined; let object = root; while (object) { if (filter.call(thisArg, object)) { array.push(object); } object = this.following(object, {root: root}); } return array; } /** * Iterate over all children of the given object * * * `O(1)` for a single iteration * * @method childrenIterator * @memberOf module:symbol-tree# * @param {Object} parent * @param {Object} [options] * @param {Boolean} [options.reverse=false] * @return {Object} An iterable iterator (ES6) */ childrenIterator(parent, options) { const reverse = options && options.reverse; const parentNode = this._node(parent); return new TreeIterator( this, parent, reverse ? parentNode.lastChild : parentNode.firstChild, reverse ? TreeIterator.PREV : TreeIterator.NEXT ); } /** * Iterate over all the previous siblings of the given object. (in reverse tree order) * * * `O(1)` for a single iteration * * @method previousSiblingsIterator * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} An iterable iterator (ES6) */ previousSiblingsIterator(object) { return new TreeIterator( this, object, this._node(object).previousSibling, TreeIterator.PREV ); } /** * Iterate over all the next siblings of the given object. (in tree order) * * * `O(1)` for a single iteration * * @method nextSiblingsIterator * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} An iterable iterator (ES6) */ nextSiblingsIterator(object) { return new TreeIterator( this, object, this._node(object).nextSibling, TreeIterator.NEXT ); } /** * Iterate over all inclusive ancestors of the given object * * * `O(1)` for a single iteration * * @method ancestorsIterator * @memberOf module:symbol-tree# * @param {Object} object * @return {Object} An iterable iterator (ES6) */ ancestorsIterator(object) { return new TreeIterator( this, object, object, TreeIterator.PARENT ); } /** * 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) * * @method treeIterator * @memberOf module:symbol-tree# * @param {Object} root * @param {Object} options * @param {Boolean} [options.reverse=false] * @return {Object} An iterable iterator (ES6) */ treeIterator(root, options) { const reverse = options && options.reverse; return new TreeIterator( this, root, reverse ? this.lastInclusiveDescendant(root) : root, reverse ? TreeIterator.PRECEDING : TreeIterator.FOLLOWING ); } /** * 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) * * @method index * @memberOf module:symbol-tree# * @param {Object} child * @return {Number} The number of preceding siblings, or -1 if the object has no parent */ index(child) { const childNode = this._node(child); const parentNode = this._node(childNode.parent); if (!parentNode) { // In principal, you could also find out the number of preceding siblings // for objects that do not have a parent. This method limits itself only to // objects that have a parent because that lets us optimize more. return -1; } let currentIndex = childNode.getCachedIndex(parentNode); if (currentIndex >= 0) { return currentIndex; } currentIndex = 0; let object = parentNode.firstChild; if (parentNode.childIndexCachedUpTo) { const cachedUpToNode = this._node(parentNode.childIndexCachedUpTo); object = cachedUpToNode.nextSibling; currentIndex = cachedUpToNode.getCachedIndex(parentNode) + 1; } while (object) { const node = this._node(object); node.setCachedIndex(parentNode, currentIndex); if (object === child) { break; } ++currentIndex; object = node.nextSibling; } parentNode.childIndexCachedUpTo = child; return currentIndex; } /** * Calculate the number of children. * * * `O(n)` where `n` is the amount of children * * `O(1)` (amortized, if the tree is not modified) * * @method childrenCount * @memberOf module:symbol-tree# * @param {Object} parent * @return {Number} */ childrenCount(parent) { const parentNode = this._node(parent); if (!parentNode.lastChild) { return 0; } return this.index(parentNode.lastChild) + 1; } /** * Compare the position of an object relative to another object. A bit set is returned: * * * * 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) * * @method compareTreePosition * @memberOf module:symbol-tree# * @param {Object} left * @param {Object} right * @return {Number} */ compareTreePosition(left, right) { // In DOM terms: // left = reference / context object // right = other if (left === right) { return 0; } /* jshint -W016 */ const leftAncestors = []; { // inclusive let leftAncestor = left; while (leftAncestor) { if (leftAncestor === right) { return TreePosition.CONTAINS | TreePosition.PRECEDING; // other is ancestor of reference } leftAncestors.push(leftAncestor); leftAncestor = this.parent(leftAncestor); } } const rightAncestors = []; { let rightAncestor = right; while (rightAncestor) { if (rightAncestor === left) { return TreePosition.CONTAINED_BY | TreePosition.FOLLOWING; } rightAncestors.push(rightAncestor); rightAncestor = this.parent(rightAncestor); } } const root = reverseArrayIndex(leftAncestors, 0); if (!root || root !== reverseArrayIndex(rightAncestors, 0)) { // note: unlike DOM, preceding / following is not set here return TreePosition.DISCONNECTED; } // find the lowest common ancestor let commonAncestorIndex = 0; const ancestorsMinLength = Math.min(leftAncestors.length, rightAncestors.length); for (let i = 0; i < ancestorsMinLength; ++i) { const leftAncestor = reverseArrayIndex(leftAncestors, i); const rightAncestor = reverseArrayIndex(rightAncestors, i); if (leftAncestor !== rightAncestor) { break; } commonAncestorIndex = i; } // indexes within the common ancestor const leftIndex = this.index(reverseArrayIndex(leftAncestors, commonAncestorIndex + 1)); const rightIndex = this.index(reverseArrayIndex(rightAncestors, commonAncestorIndex + 1)); return rightIndex < leftIndex ? TreePosition.PRECEDING : TreePosition.FOLLOWING; } /** * Remove the object from this tree. * Has no effect if already removed. * * * `O(1)` * * @method remove * @memberOf module:symbol-tree# * @param {Object} removeObject * @return {Object} removeObject */ remove(removeObject) { const removeNode = this._node(removeObject); const parentNode = this._node(removeNode.parent); const prevNode = this._node(removeNode.previousSibling); const nextNode = this._node(removeNode.nextSibling); if (parentNode) { if (parentNode.firstChild === removeObject) { parentNode.firstChild = removeNode.nextSibling; } if (parentNode.lastChild === removeObject) { parentNode.lastChild = removeNode.previousSibling; } } if (prevNode) { prevNode.nextSibling = removeNode.nextSibling; } if (nextNode) { nextNode.previousSibling = removeNode.previousSibling; } removeNode.parent = null; removeNode.previousSibling = null; removeNode.nextSibling = null; removeNode.cachedIndex = -1; removeNode.cachedIndexVersion = NaN; if (parentNode) { parentNode.childrenChanged(); } return removeObject; } /** * Insert the given object before the reference object. * `newObject` is now the previous sibling of `referenceObject`. * * * `O(1)` * * @method insertBefore * @memberOf module:symbol-tree# * @param {Object} referenceObject * @param {Object} newObject * @throws {Error} If the newObject is already present in this SymbolTree * @return {Object} newObject */ insertBefore(referenceObject, newObject) { const referenceNode = this._node(referenceObject); const prevNode = this._node(referenceNode.previousSibling); const newNode = this._node(newObject); const parentNode = this._node(referenceNode.parent); if (newNode.isAttached) { throw Error('Given object is already present in this SymbolTree, remove it first'); } newNode.parent = referenceNode.parent; newNode.previousSibling = referenceNode.previousSibling; newNode.nextSibling = referenceObject; referenceNode.previousSibling = newObject; if (prevNode) { prevNode.nextSibling = newObject; } if (parentNode && parentNode.firstChild === referenceObject) { parentNode.firstChild = newObject; } if (parentNode) { parentNode.childrenChanged(); } return newObject; } /** * Insert the given object after the reference object. * `newObject` is now the next sibling of `referenceObject`. * * * `O(1)` * * @method insertAfter * @memberOf module:symbol-tree# * @param {Object} referenceObject * @param {Object} newObject * @throws {Error} If the newObject is already present in this SymbolTree * @return {Object} newObject */ insertAfter(referenceObject, newObject) { const referenceNode = this._node(referenceObject); const nextNode = this._node(referenceNode.nextSibling); const newNode = this._node(newObject); const parentNode = this._node(referenceNode.parent); if (newNode.isAttached) { throw Error('Given object is already present in this SymbolTree, remove it first'); } newNode.parent = referenceNode.parent; newNode.previousSibling = referenceObject; newNode.nextSibling = referenceNode.nextSibling; referenceNode.nextSibling = newObject; if (nextNode) { nextNode.previousSibling = newObject; } if (parentNode && parentNode.lastChild === referenceObject) { parentNode.lastChild = newObject; } if (parentNode) { parentNode.childrenChanged(); } return newObject; } /** * Insert the given object as the first child of the given reference object. * `newObject` is now the first child of `referenceObject`. * * * `O(1)` * * @method prependChild * @memberOf module:symbol-tree# * @param {Object} referenceObject * @param {Object} newObject * @throws {Error} If the newObject is already present in this SymbolTree * @return {Object} newObject */ prependChild(referenceObject, newObject) { const referenceNode = this._node(referenceObject); const newNode = this._node(newObject); if (newNode.isAttached) { throw Error('Given object is already present in this SymbolTree, remove it first'); } if (referenceNode.hasChildren) { this.insertBefore(referenceNode.firstChild, newObject); } else { newNode.parent = referenceObject; referenceNode.firstChild = newObject; referenceNode.lastChild = newObject; referenceNode.childrenChanged(); } return newObject; } /** * Insert the given object as the last child of the given reference object. * `newObject` is now the last child of `referenceObject`. * * * `O(1)` * * @method appendChild * @memberOf module:symbol-tree# * @param {Object} referenceObject * @param {Object} newObject * @throws {Error} If the newObject is already present in this SymbolTree * @return {Object} newObject */ appendChild(referenceObject, newObject) { const referenceNode = this._node(referenceObject); const newNode = this._node(newObject); if (newNode.isAttached) { throw Error('Given object is already present in this SymbolTree, remove it first'); } if (referenceNode.hasChildren) { this.insertAfter(referenceNode.lastChild, newObject); } else { newNode.parent = referenceObject; referenceNode.firstChild = newObject; referenceNode.lastChild = newObject; referenceNode.childrenChanged(); } return newObject; } } module.exports = SymbolTree; SymbolTree.TreePosition = TreePosition; js-symbol-tree-3.2.4/lib/SymbolTreeNode.js000066400000000000000000000036171350023766500204110ustar00rootroot00000000000000'use strict'; module.exports = class SymbolTreeNode { constructor() { this.parent = null; this.previousSibling = null; this.nextSibling = null; this.firstChild = null; this.lastChild = null; /** This value is incremented anytime a children is added or removed */ this.childrenVersion = 0; /** The last child object which has a cached index */ this.childIndexCachedUpTo = null; /** This value represents the cached node index, as long as * cachedIndexVersion matches with the childrenVersion of the parent */ this.cachedIndex = -1; this.cachedIndexVersion = NaN; // NaN is never equal to anything } get isAttached() { return Boolean(this.parent || this.previousSibling || this.nextSibling); } get hasChildren() { return Boolean(this.firstChild); } childrenChanged() { /* jshint -W016 */ // integer wrap around this.childrenVersion = (this.childrenVersion + 1) & 0xFFFFFFFF; this.childIndexCachedUpTo = null; } getCachedIndex(parentNode) { // (assumes parentNode is actually the parent) if (this.cachedIndexVersion !== parentNode.childrenVersion) { this.cachedIndexVersion = NaN; // cachedIndex is no longer valid return -1; } return this.cachedIndex; // -1 if not cached } setCachedIndex(parentNode, index) { // (assumes parentNode is actually the parent) this.cachedIndexVersion = parentNode.childrenVersion; this.cachedIndex = index; } }; js-symbol-tree-3.2.4/lib/TreeIterator.js000066400000000000000000000036671350023766500201340ustar00rootroot00000000000000'use strict'; const TREE = Symbol(); const ROOT = Symbol(); const NEXT = Symbol(); const ITERATE_FUNC = Symbol(); class TreeIterator { constructor(tree, root, firstResult, iterateFunction) { this[TREE] = tree; this[ROOT] = root; this[NEXT] = firstResult; this[ITERATE_FUNC] = iterateFunction; } next() { const tree = this[TREE]; const iterateFunc = this[ITERATE_FUNC]; const root = this[ROOT]; if (!this[NEXT]) { return { done: true, value: root, }; } const value = this[NEXT]; if (iterateFunc === 1) { this[NEXT] = tree._node(value).previousSibling; } else if (iterateFunc === 2) { this[NEXT] = tree._node(value).nextSibling; } else if (iterateFunc === 3) { this[NEXT] = tree._node(value).parent; } else if (iterateFunc === 4) { this[NEXT] = tree.preceding(value, {root: root}); } else /* if (iterateFunc === 5)*/ { this[NEXT] = tree.following(value, {root: root}); } return { done: false, value: value, }; } } Object.defineProperty(TreeIterator.prototype, Symbol.iterator, { value: function() { return this; }, writable: false, }); TreeIterator.PREV = 1; TreeIterator.NEXT = 2; TreeIterator.PARENT = 3; TreeIterator.PRECEDING = 4; TreeIterator.FOLLOWING = 5; Object.freeze(TreeIterator); Object.freeze(TreeIterator.prototype); module.exports = TreeIterator; js-symbol-tree-3.2.4/lib/TreePosition.js000066400000000000000000000003611350023766500201330ustar00rootroot00000000000000'use strict'; /* eslint-disable sort-keys */ module.exports = Object.freeze({ // same as DOM DOCUMENT_POSITION_ DISCONNECTED: 1, PRECEDING: 2, FOLLOWING: 4, CONTAINS: 8, CONTAINED_BY: 16, }); js-symbol-tree-3.2.4/package.json000066400000000000000000000024201350023766500166670ustar00rootroot00000000000000{ "name": "symbol-tree", "version": "3.2.4", "description": "Turn any collection of objects into its own efficient tree or linked list using Symbol", "main": "lib/SymbolTree.js", "scripts": { "lint": "eslint lib test", "test": "istanbul cover test/SymbolTree.js", "posttest": "npm run lint", "ci": "istanbul cover test/SymbolTree.js --report lcovonly && cat ./coverage/lcov.info | coveralls", "postci": "npm run posttest", "predocumentation": "cp readme-header.md README.md", "documentation": "jsdoc2md --files lib/SymbolTree.js >> README.md" }, "repository": { "type": "git", "url": "https://github.com/jsdom/js-symbol-tree.git" }, "keywords": [ "list", "queue", "stack", "linked-list", "tree", "es6", "dom", "symbol" ], "files": [ "lib" ], "author": "Joris van der Wel ", "license": "MIT", "bugs": { "url": "https://github.com/jsdom/js-symbol-tree/issues" }, "homepage": "https://github.com/jsdom/js-symbol-tree#symbol-tree", "devDependencies": { "babel-eslint": "^10.0.1", "coveralls": "^3.0.0", "eslint": "^5.16.0", "eslint-plugin-import": "^2.2.0", "istanbul": "^0.4.5", "jsdoc-to-markdown": "^5.0.0", "tape": "^4.0.0" } } js-symbol-tree-3.2.4/readme-header.md000066400000000000000000000042771350023766500174220ustar00rootroot00000000000000symbol-tree =========== [![Travis CI Build Status](https://api.travis-ci.org/jsdom/js-symbol-tree.svg?branch=master)](https://travis-ci.org/jsdom/js-symbol-tree) [![Coverage Status](https://coveralls.io/repos/github/jsdom/js-symbol-tree/badge.svg?branch=master)](https://coveralls.io/github/jsdom/js-symbol-tree?branch=master) 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: ```javascript 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: ```javascript 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](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 ----------------- js-symbol-tree-3.2.4/test/000077500000000000000000000000001350023766500153625ustar00rootroot00000000000000js-symbol-tree-3.2.4/test/SymbolTree.js000066400000000000000000000777201350023766500200220ustar00rootroot00000000000000'use strict'; const test = require('tape'); const SymbolTree = require('..'); function o() { // return an object that is unique in a deepEqual check return { unique: Symbol(), }; } test('test case internal prerequisite', (t) => { const a = o(); t.notDeepEqual([o()], [o()]); t.deepEqual([a], [a]); t.end(); }); test('initialize', (t) => { const tree = new SymbolTree(); const obj = {foo: 'bar'}; t.equal(obj, tree.initialize(obj)); t.deepEqual(['foo'], Object.getOwnPropertyNames(obj), 'initialize() should not introduce any enumerable properties'); t.end(); }); test('unassociated object', (t) => { const tree = new SymbolTree(); const a = o(); t.equal(false, tree.hasChildren(a)); t.equal(null, tree.firstChild(a)); t.equal(null, tree.lastChild(a)); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(a)); t.equal(null, tree.parent(a)); t.end(); }); test('insertBefore without parent or siblings', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); t.equal(a, tree.insertBefore(b, a)); t.equal(false, tree.hasChildren(a)); t.equal(null, tree.firstChild(a)); t.equal(null, tree.lastChild(a)); t.equal(null, tree.parent(a)); t.equal(false, tree.hasChildren(b)); t.equal(null, tree.firstChild(b)); t.equal(null, tree.lastChild(b)); t.equal(null, tree.parent(b)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('insertAfter without parent or siblings', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); t.equal(b, tree.insertAfter(a, b)); t.equal(false, tree.hasChildren(a)); t.equal(null, tree.firstChild(a)); t.equal(null, tree.lastChild(a)); t.equal(null, tree.parent(a)); t.equal(false, tree.hasChildren(b)); t.equal(null, tree.firstChild(b)); t.equal(null, tree.lastChild(b)); t.equal(null, tree.parent(b)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('prependChild without children', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); t.equal(a, tree.prependChild(parent, a)); t.equal(false, tree.hasChildren(a)); t.equal(null, tree.firstChild(a)); t.equal(null, tree.lastChild(a)); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(a)); t.equal(parent, tree.parent(a)); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(a, tree.lastChild(parent)); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(parent)); t.equal(null, tree.parent(parent)); t.end(); }); test('appendChild without children', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); t.equal(a, tree.appendChild(parent, a)); t.equal(false, tree.hasChildren(a)); t.equal(null, tree.firstChild(a)); t.equal(null, tree.lastChild(a)); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(a)); t.equal(parent, tree.parent(a)); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(a, tree.lastChild(parent)); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(parent)); t.equal(null, tree.parent(parent)); t.end(); }); test('prependChild with children', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); const b = o(); tree.prependChild(parent, b); tree.prependChild(parent, a); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(b, tree.lastChild(parent)); t.equal(parent, tree.parent(a)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(parent, tree.parent(b)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('appendChild with children', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); const b = o(); tree.appendChild(parent, a); tree.appendChild(parent, b); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(b, tree.lastChild(parent)); t.equal(parent, tree.parent(a)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(parent, tree.parent(b)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('insertBefore with parent', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); const b = o(); tree.prependChild(parent, b); tree.insertBefore(b, a); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(b, tree.lastChild(parent)); t.equal(parent, tree.parent(a)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(parent, tree.parent(b)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('insertAfter with parent', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); const b = o(); tree.appendChild(parent, a); tree.insertAfter(a, b); t.equal(true, tree.hasChildren(parent)); t.equal(a, tree.firstChild(parent)); t.equal(b, tree.lastChild(parent)); t.equal(parent, tree.parent(a)); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(parent, tree.parent(b)); t.equal(a, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.end(); }); test('insertBefore with siblings', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); const c = o(); tree.insertBefore(c, a); tree.insertBefore(c, b); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(a, tree.previousSibling(b)); t.equal(c, tree.nextSibling(b)); t.equal(b, tree.previousSibling(c)); t.equal(null, tree.nextSibling(c)); t.end(); }); test('insertAfter with siblings', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); const c = o(); tree.insertAfter(a, c); tree.insertAfter(a, b); t.equal(null, tree.previousSibling(a)); t.equal(b, tree.nextSibling(a)); t.equal(a, tree.previousSibling(b)); t.equal(c, tree.nextSibling(b)); t.equal(b, tree.previousSibling(c)); t.equal(null, tree.nextSibling(c)); t.end(); }); test('remove with previous sibling', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); tree.insertAfter(a, b); tree.remove(b); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(a)); t.equal(null, tree.parent(a)); t.equal(null, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.equal(null, tree.parent(b)); t.end(); }); test('remove with next sibling', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); tree.insertAfter(a, b); tree.remove(a); t.equal(null, tree.previousSibling(a)); t.equal(null, tree.nextSibling(a)); t.equal(null, tree.parent(a)); t.equal(null, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.equal(null, tree.parent(b)); t.end(); }); test('remove with siblings', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); const c = o(); tree.insertAfter(a, b); tree.insertAfter(b, c); tree.remove(b); t.equal(null, tree.previousSibling(a)); t.equal(c, tree.nextSibling(a)); t.equal(null, tree.parent(a)); t.equal(null, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.equal(null, tree.parent(b)); t.equal(a, tree.previousSibling(c)); t.equal(null, tree.nextSibling(c)); t.equal(null, tree.parent(c)); t.end(); }); test('remove with parent', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); tree.prependChild(parent, a); tree.remove(a); t.equal(null, tree.parent(a)); t.equal(null, tree.firstChild(parent)); t.equal(null, tree.lastChild(parent)); t.end(); }); test('remove with children', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); tree.prependChild(parent, a); tree.remove(parent); t.equal(parent, tree.parent(a)); t.equal(a, tree.firstChild(parent)); t.equal(a, tree.lastChild(parent)); t.end(); }); test('remove with parent and siblings', (t) => { const tree = new SymbolTree(); const parent = o(); const a = o(); const b = o(); const c = o(); tree.prependChild(parent, a); tree.insertAfter(a, b); tree.insertAfter(b, c); tree.remove(b); t.equal(a, tree.firstChild(parent)); t.equal(c, tree.lastChild(parent)); t.equal(null, tree.previousSibling(a)); t.equal(c, tree.nextSibling(a)); t.equal(parent, tree.parent(a)); t.equal(null, tree.previousSibling(b)); t.equal(null, tree.nextSibling(b)); t.equal(null, tree.parent(b)); t.equal(a, tree.previousSibling(c)); t.equal(null, tree.nextSibling(c)); t.equal(parent, tree.parent(c)); t.end(); }); test('inserting an already associated object should fail', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); tree.insertBefore(b, a); // jscs:disable requireBlocksOnNewline // `nextSibling` check t.throws(() => { tree.insertBefore(b, a); }, /already present/); t.throws(() => { tree.insertAfter(b, a); }, /already present/); t.throws(() => { tree.prependChild(b, a); }, /already present/); t.throws(() => { tree.appendChild(b, a); }, /already present/); // `previousSibling` check t.throws(() => { tree.insertBefore(a, b); }, /already present/); t.throws(() => { tree.insertAfter(a, b); }, /already present/); t.throws(() => { tree.prependChild(a, b); }, /already present/); t.throws(() => { tree.appendChild(a, b); }, /already present/); tree.remove(a); tree.prependChild(b, a); // `parent` check t.throws(() => { tree.insertBefore(b, a); }, /already present/); t.throws(() => { tree.insertAfter(b, a); }, /already present/); t.throws(() => { tree.prependChild(b, a); }, /already present/); t.throws(() => { tree.appendChild(b, a); }, /already present/); // jscs:enable requireBlocksOnNewline t.end(); }); test('Multiple SymbolTree instances should not conflict', (t) => { const tree1 = new SymbolTree(); const tree2 = new SymbolTree(); const a = o(); const b = o(); tree1.insertBefore(b, a); tree2.insertBefore(a, b); t.equal(null, tree1.previousSibling(a)); t.equal(b, tree1.nextSibling(a)); t.equal(a, tree1.previousSibling(b)); t.equal(null, tree1.nextSibling(b)); t.equal(null, tree2.previousSibling(b)); t.equal(a, tree2.nextSibling(b)); t.equal(b, tree2.previousSibling(a)); t.equal(null, tree2.nextSibling(a)); t.end(); }); test('lastInclusiveDescendant', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); t.equal(abaa, tree.lastInclusiveDescendant(a)); t.end(); }); test('look up preceding with a previous sibling', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); tree.insertAfter(a, b); t.equal(null, tree.preceding(a)); t.equal(a, tree.preceding(b)); t.end(); }); test('look up preceding with a previous sibling with a child', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.insertAfter(a, b); t.equal(null, tree.preceding(a)); t.equal(a, tree.preceding(aa)); t.equal(aa, tree.preceding(ab)); t.equal(ab, tree.preceding(b)); t.end(); }); test('look up preceding with a previous sibling with a descendants', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); t.equal(abaa, tree.preceding(b)); t.end(); }); test('look up preceding using a specified root', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); tree.appendChild(a, aa); t.equal(null, tree.preceding(a, {root: a})); t.equal(a, tree.preceding(aa, {root: a})); t.equal(null, tree.preceding(aa, {root: aa})); t.end(); }); test('following with a child', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); tree.appendChild(a, aa); t.equal(aa, tree.following(a)); t.equal(null, tree.following(aa)); t.end(); }); test('following with a nextSibling sibling', (t) => { const tree = new SymbolTree(); const a = o(); const b = o(); tree.insertAfter(a, b); t.equal(b, tree.following(a)); t.equal(null, tree.following(b)); t.end(); }); test('following with sibling of parent', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const b = o(); tree.appendChild(a, aa); tree.insertAfter(a, b); t.equal(b, tree.following(aa)); t.end(); }); test('following with sibling of grandparent', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const aaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(aa, aaa); tree.insertAfter(a, b); t.equal(b, tree.following(aaa)); t.end(); }); test('following using a specified root', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const aaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(aa, aaa); tree.insertAfter(a, b); t.equal(null, tree.following(aaa, {root: aaa})); t.equal(null, tree.following(aaa, {root: aa})); t.equal(null, tree.following(aaa, {root: a})); t.equal(aa, tree.following(a, {root: a})); t.equal(aaa, tree.following(aa, {root: a})); t.end(); }); test('following with skipChildren', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const b = o(); tree.appendChild(a, aa); tree.insertAfter(a, b); t.equal(b, tree.following(a, {skipChildren: true})); t.end(); }); test('childrenToArray', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); t.deepEqual([aa, ab, ac], tree.childrenToArray(a)); const arr = ['a', 5]; tree.childrenToArray(a, {array: arr}); t.deepEqual(['a', 5, aa, ab, ac], arr); t.end(); }); test('childrenToArray with filter', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); const filter = function(object) { t.equal(this, undefined); return object !== ab; }; t.deepEqual([aa, ac], tree.childrenToArray(a, {filter: filter})); const thisArg = {a: 123}; const filterThis = function(object) { t.equal(this, thisArg); return object !== ab; }; t.deepEqual([aa, ac], tree.childrenToArray(a, {filter: filterThis, thisArg: thisArg})); t.end(); }); test('children iterator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); const results = []; for (const object of tree.childrenIterator(a)) { results.push(object); } t.deepEqual([aa, ab, ac], results); t.end(); }); test('children iterator reverse', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); const results = []; for (const object of tree.childrenIterator(a, {reverse: true})) { results.push(object); } t.deepEqual([ac, ab, aa], results); t.end(); }); test('children iterator return value using a generator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const ac = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(a, ac); function* generator(it) { const returnValue = yield* it; t.equal(a, returnValue); } const results = []; for (const object of generator(tree.childrenIterator(a))) { results.push(object); } t.deepEqual([aa, ab, ac], results); t.end(); }); test('previous sibling iterator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const ad = o(); const ae = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.appendChild(a, ad); tree.appendChild(a, ae); tree.insertAfter(a, b); const results = []; for (const object of tree.previousSiblingsIterator(ad)) { results.push(object); } t.deepEqual([ac, ab, aa], results); t.end(); }); test('nextSibling sibling iterator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const ad = o(); const ae = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.appendChild(a, ad); tree.appendChild(a, ae); tree.insertAfter(a, b); const results = []; for (const object of tree.nextSiblingsIterator(ab)) { results.push(object); } t.deepEqual([ac, ad, ae], results); t.end(); }); test('ancestorsToArray', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); t.deepEqual([abaa, aba, ab, a], tree.ancestorsToArray(abaa)); t.deepEqual([aba, ab, a], tree.ancestorsToArray(aba)); t.deepEqual([b], tree.ancestorsToArray(b)); const arr = ['a', 5]; tree.ancestorsToArray(abaa, {array: arr}); t.deepEqual(['a', 5, abaa, aba, ab, a], arr); t.end(); }); test('ancestorsToArray with filter', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); const thisArg = {foo: 'bar'}; const filter = function(object) { t.equal(this, thisArg); return object !== abaa && object !== ab; }; t.deepEqual([aba, a], tree.ancestorsToArray(abaa, {filter: filter, thisArg: thisArg})); t.end(); }); test('ancestors iterator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); const results = []; const iterator = tree.ancestorsIterator(abaa); for (const object of iterator) { results.push(object); } t.deepEqual([abaa, aba, ab, a], results); t.deepEqual({done: true, value: abaa}, iterator.next()); t.deepEqual({done: true, value: abaa}, iterator.next()); // should keep returning done: true t.end(); }); test('treeToArray', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); t.deepEqual([a, aa, ab, aba, abaa], tree.treeToArray(a)); const arr = ['a', 5]; tree.treeToArray(a, {array: arr}); t.deepEqual(['a', 5, a, aa, ab, aba, abaa], arr); t.end(); }); test('treeToArray with filter', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.insertAfter(a, b); const filter = function(object) { t.equal(this, undefined); return object !== a && object !== aba; }; t.deepEqual([aa, ab, abaa], tree.treeToArray(a, {filter: filter})); const thisArg = {foo: 'bar'}; const filterThis = function(object) { t.equal(this, thisArg); return object !== a && object !== aba; }; t.deepEqual([aa, ab, abaa], tree.treeToArray(a, {filter: filterThis, thisArg: thisArg})); t.end(); }); test('tree iterator', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.appendChild(a, ac); tree.insertAfter(a, b); const results = []; const iterator = tree.treeIterator(a); for (const object of iterator) { results.push(object); } t.deepEqual([a, aa, ab, aba, abaa, ac], results); t.deepEqual({done: true, value: a}, iterator.next()); t.deepEqual({done: true, value: a}, iterator.next()); // should keep returning done: true t.end(); }); test('tree iterator reverse', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const abaa = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.appendChild(a, ac); tree.insertAfter(a, b); const results = []; const iterator = tree.treeIterator(a, {reverse: true}); for (const object of iterator) { results.push(object); } t.deepEqual([ac, abaa, aba, ab, aa, a], results); t.deepEqual({done: true, value: a}, iterator.next()); t.deepEqual({done: true, value: a}, iterator.next()); // should keep returning done: true t.end(); }); test('look up the index of an object', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); t.equal(-1, tree.index(a), 'should return -1 if an object has no parent'); t.equal(0, tree.index(aa)); t.equal(1, tree.index(ab)); t.equal(0, tree.index(aba)); t.equal(2, tree.index(ac)); t.equal(-1, tree.index(b)); t.end(); }); test('cached index', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); // looking up ac, will also set the cached index for aa and ab, so check that those are valid t.equal(2, tree.index(ac)); t.equal(1, tree.index(ab)); t.equal(0, tree.index(aa)); // removing something should invalidate the cache tree.remove(ab); t.equal(1, tree.index(ac)); t.equal(-1, tree.index(ab)); t.equal(0, tree.index(aa)); // insertAfter should invalidate tree.insertAfter(aa, ab); t.equal(0, tree.index(aa)); t.equal(1, tree.index(ab)); t.equal(2, tree.index(ac)); // insertBefore should invalidate const foo = o(); tree.insertBefore(ab, foo); t.equal(0, tree.index(aa)); t.equal(2, tree.index(ab)); t.equal(3, tree.index(ac)); t.end(); }); test('cached index warmed up by childrenToArray', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); tree.childrenToArray(a); t.equal(0, tree.index(aa)); t.equal(1, tree.index(ab)); t.equal(2, tree.index(ac)); tree.appendChild(a, o()); t.equal(2, tree.index(ac)); tree.childrenToArray(a); t.equal(2, tree.index(ac)); t.end(); }); test('regression test: remove() should invalidate the child index cache', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const b = o(); const ba = o(); const bb = o(); tree.appendChild(a, aa); tree.appendChild(b, ba); tree.appendChild(b, bb); t.equal(0, tree.index(ba)); tree.remove(ba); t.equal(-1, tree.index(ba)); tree.appendChild(a, ba); t.equal(1, tree.index(ba)); t.end(); }); test('children count', (t) => { // no need to test the caching since we already tested for that in childrenCount const tree = new SymbolTree(); const a = o(); const aa = o(); const ab = o(); const aba = o(); const ac = o(); const b = o(); tree.appendChild(a, aa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(a, ac); tree.insertAfter(a, b); t.equal(3, tree.childrenCount(a), 'foo'); t.equal(0, tree.childrenCount(aa)); t.equal(1, tree.childrenCount(ab)); t.equal(0, tree.childrenCount(b)); t.end(); }); test('compare tree position', (t) => { const tree = new SymbolTree(); const a = o(); const aa = o(); const aaa = o(); const ab = o(); const aba = o(); const abaa = o(); const ac = o(); const b = o(); const ba = o(); tree.appendChild(a, aa); tree.appendChild(aa, aaa); tree.appendChild(a, ab); tree.appendChild(ab, aba); tree.appendChild(aba, abaa); tree.appendChild(a, ac); tree.insertAfter(a, b); tree.appendChild(b, ba); t.equal(0, tree.compareTreePosition(a, a), 'object equal'); t.equal(1, tree.compareTreePosition(a, o()), 'object disconnected'); t.equal(1, tree.compareTreePosition(a, b), 'object disconnected'); t.equal(20, tree.compareTreePosition(a, aa), 'contained by & following'); t.equal(10, tree.compareTreePosition(aa, a), 'contains & preceding'); t.equal(20, tree.compareTreePosition(a, abaa), 'contained by & following'); t.equal(10, tree.compareTreePosition(abaa, a), 'contains & preceding'); t.equal(4, tree.compareTreePosition(aa, ab), 'following'); t.equal(2, tree.compareTreePosition(ab, aa), 'preceding'); t.equal(4, tree.compareTreePosition(aa, aba), 'following'); t.equal(2, tree.compareTreePosition(aba, aa), 'preceding'); t.equal(4, tree.compareTreePosition(aa, abaa), 'following'); t.equal(2, tree.compareTreePosition(abaa, aa), 'preceding'); t.equal(4, tree.compareTreePosition(aaa, abaa), 'following'); t.equal(2, tree.compareTreePosition(abaa, aaa), 'preceding'); t.end(); });