You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

545 lines
20 KiB

  1. symbol-tree
  2. ===========
  3. [![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)
  4. Turn any collection of objects into its own efficient tree or linked list using `Symbol`.
  5. 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.
  6. Node.js 4+, io.js and modern browsers are supported.
  7. Example
  8. -------
  9. A linked list:
  10. ```javascript
  11. const SymbolTree = require('symbol-tree');
  12. const tree = new SymbolTree();
  13. let a = {foo: 'bar'}; // or `new Whatever()`
  14. let b = {foo: 'baz'};
  15. let c = {foo: 'qux'};
  16. tree.insertBefore(b, a); // insert a before b
  17. tree.insertAfter(b, c); // insert c after b
  18. console.log(tree.nextSibling(a) === b);
  19. console.log(tree.nextSibling(b) === c);
  20. console.log(tree.previousSibling(c) === b);
  21. tree.remove(b);
  22. console.log(tree.nextSibling(a) === c);
  23. ```
  24. A tree:
  25. ```javascript
  26. const SymbolTree = require('symbol-tree');
  27. const tree = new SymbolTree();
  28. let parent = {};
  29. let a = {};
  30. let b = {};
  31. let c = {};
  32. tree.prependChild(parent, a); // insert a as the first child
  33. tree.appendChild(parent,c ); // insert c as the last child
  34. tree.insertAfter(a, b); // insert b after a, it now has the same parent as a
  35. console.log(tree.firstChild(parent) === a);
  36. console.log(tree.nextSibling(tree.firstChild(parent)) === b);
  37. console.log(tree.lastChild(parent) === c);
  38. let grandparent = {};
  39. tree.prependChild(grandparent, parent);
  40. console.log(tree.firstChild(tree.firstChild(grandparent)) === a);
  41. ```
  42. See [api.md](api.md) for more documentation.
  43. Testing
  44. -------
  45. Make sure you install the dependencies first:
  46. npm install
  47. You can now run the unit tests by executing:
  48. npm test
  49. The line and branch coverage should be 100%.
  50. API Documentation
  51. -----------------
  52. <a name="module_symbol-tree"></a>
  53. ## symbol-tree
  54. **Author**: Joris van der Wel <joris@jorisvanderwel.com>
  55. * [symbol-tree](#module_symbol-tree)
  56. * [SymbolTree](#exp_module_symbol-tree--SymbolTree) ⏏
  57. * [new SymbolTree([description])](#new_module_symbol-tree--SymbolTree_new)
  58. * [.initialize(object)](#module_symbol-tree--SymbolTree+initialize) ⇒ <code>Object</code>
  59. * [.hasChildren(object)](#module_symbol-tree--SymbolTree+hasChildren) ⇒ <code>Boolean</code>
  60. * [.firstChild(object)](#module_symbol-tree--SymbolTree+firstChild) ⇒ <code>Object</code>
  61. * [.lastChild(object)](#module_symbol-tree--SymbolTree+lastChild) ⇒ <code>Object</code>
  62. * [.previousSibling(object)](#module_symbol-tree--SymbolTree+previousSibling) ⇒ <code>Object</code>
  63. * [.nextSibling(object)](#module_symbol-tree--SymbolTree+nextSibling) ⇒ <code>Object</code>
  64. * [.parent(object)](#module_symbol-tree--SymbolTree+parent) ⇒ <code>Object</code>
  65. * [.lastInclusiveDescendant(object)](#module_symbol-tree--SymbolTree+lastInclusiveDescendant) ⇒ <code>Object</code>
  66. * [.preceding(object, [options])](#module_symbol-tree--SymbolTree+preceding) ⇒ <code>Object</code>
  67. * [.following(object, [options])](#module_symbol-tree--SymbolTree+following) ⇒ <code>Object</code>
  68. * [.childrenToArray(parent, [options])](#module_symbol-tree--SymbolTree+childrenToArray) ⇒ <code>Array.&lt;Object&gt;</code>
  69. * [.ancestorsToArray(object, [options])](#module_symbol-tree--SymbolTree+ancestorsToArray) ⇒ <code>Array.&lt;Object&gt;</code>
  70. * [.treeToArray(root, [options])](#module_symbol-tree--SymbolTree+treeToArray) ⇒ <code>Array.&lt;Object&gt;</code>
  71. * [.childrenIterator(parent, [options])](#module_symbol-tree--SymbolTree+childrenIterator) ⇒ <code>Object</code>
  72. * [.previousSiblingsIterator(object)](#module_symbol-tree--SymbolTree+previousSiblingsIterator) ⇒ <code>Object</code>
  73. * [.nextSiblingsIterator(object)](#module_symbol-tree--SymbolTree+nextSiblingsIterator) ⇒ <code>Object</code>
  74. * [.ancestorsIterator(object)](#module_symbol-tree--SymbolTree+ancestorsIterator) ⇒ <code>Object</code>
  75. * [.treeIterator(root, options)](#module_symbol-tree--SymbolTree+treeIterator) ⇒ <code>Object</code>
  76. * [.index(child)](#module_symbol-tree--SymbolTree+index) ⇒ <code>Number</code>
  77. * [.childrenCount(parent)](#module_symbol-tree--SymbolTree+childrenCount) ⇒ <code>Number</code>
  78. * [.compareTreePosition(left, right)](#module_symbol-tree--SymbolTree+compareTreePosition) ⇒ <code>Number</code>
  79. * [.remove(removeObject)](#module_symbol-tree--SymbolTree+remove) ⇒ <code>Object</code>
  80. * [.insertBefore(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertBefore) ⇒ <code>Object</code>
  81. * [.insertAfter(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertAfter) ⇒ <code>Object</code>
  82. * [.prependChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+prependChild) ⇒ <code>Object</code>
  83. * [.appendChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+appendChild) ⇒ <code>Object</code>
  84. <a name="exp_module_symbol-tree--SymbolTree"></a>
  85. ### SymbolTree ⏏
  86. **Kind**: Exported class
  87. <a name="new_module_symbol-tree--SymbolTree_new"></a>
  88. #### new SymbolTree([description])
  89. | Param | Type | Default | Description |
  90. | --- | --- | --- | --- |
  91. | [description] | <code>string</code> | <code>&quot;&#x27;SymbolTree data&#x27;&quot;</code> | Description used for the Symbol |
  92. <a name="module_symbol-tree--SymbolTree+initialize"></a>
  93. #### symbolTree.initialize(object) ⇒ <code>Object</code>
  94. You can use this function to (optionally) initialize an object right after its creation,
  95. to take advantage of V8's fast properties. Also useful if you would like to
  96. freeze your object.
  97. `O(1)`
  98. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  99. **Returns**: <code>Object</code> - object
  100. | Param | Type |
  101. | --- | --- |
  102. | object | <code>Object</code> |
  103. <a name="module_symbol-tree--SymbolTree+hasChildren"></a>
  104. #### symbolTree.hasChildren(object) ⇒ <code>Boolean</code>
  105. Returns `true` if the object has any children. Otherwise it returns `false`.
  106. * `O(1)`
  107. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  108. | Param | Type |
  109. | --- | --- |
  110. | object | <code>Object</code> |
  111. <a name="module_symbol-tree--SymbolTree+firstChild"></a>
  112. #### symbolTree.firstChild(object) ⇒ <code>Object</code>
  113. Returns the first child of the given object.
  114. * `O(1)`
  115. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  116. | Param | Type |
  117. | --- | --- |
  118. | object | <code>Object</code> |
  119. <a name="module_symbol-tree--SymbolTree+lastChild"></a>
  120. #### symbolTree.lastChild(object) ⇒ <code>Object</code>
  121. Returns the last child of the given object.
  122. * `O(1)`
  123. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  124. | Param | Type |
  125. | --- | --- |
  126. | object | <code>Object</code> |
  127. <a name="module_symbol-tree--SymbolTree+previousSibling"></a>
  128. #### symbolTree.previousSibling(object) ⇒ <code>Object</code>
  129. Returns the previous sibling of the given object.
  130. * `O(1)`
  131. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  132. | Param | Type |
  133. | --- | --- |
  134. | object | <code>Object</code> |
  135. <a name="module_symbol-tree--SymbolTree+nextSibling"></a>
  136. #### symbolTree.nextSibling(object) ⇒ <code>Object</code>
  137. Returns the next sibling of the given object.
  138. * `O(1)`
  139. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  140. | Param | Type |
  141. | --- | --- |
  142. | object | <code>Object</code> |
  143. <a name="module_symbol-tree--SymbolTree+parent"></a>
  144. #### symbolTree.parent(object) ⇒ <code>Object</code>
  145. Return the parent of the given object.
  146. * `O(1)`
  147. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  148. | Param | Type |
  149. | --- | --- |
  150. | object | <code>Object</code> |
  151. <a name="module_symbol-tree--SymbolTree+lastInclusiveDescendant"></a>
  152. #### symbolTree.lastInclusiveDescendant(object) ⇒ <code>Object</code>
  153. Find the inclusive descendant that is last in tree order of the given object.
  154. * `O(n)` (worst case) where `n` is the depth of the subtree of `object`
  155. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  156. | Param | Type |
  157. | --- | --- |
  158. | object | <code>Object</code> |
  159. <a name="module_symbol-tree--SymbolTree+preceding"></a>
  160. #### symbolTree.preceding(object, [options]) ⇒ <code>Object</code>
  161. Find the preceding object (A) of the given object (B).
  162. An object A is preceding an object B if A and B are in the same tree
  163. and A comes before B in tree order.
  164. * `O(n)` (worst case)
  165. * `O(1)` (amortized when walking the entire tree)
  166. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  167. | Param | Type | Description |
  168. | --- | --- | --- |
  169. | object | <code>Object</code> | |
  170. | [options] | <code>Object</code> | |
  171. | [options.root] | <code>Object</code> | 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` |
  172. <a name="module_symbol-tree--SymbolTree+following"></a>
  173. #### symbolTree.following(object, [options]) ⇒ <code>Object</code>
  174. Find the following object (A) of the given object (B).
  175. An object A is following an object B if A and B are in the same tree
  176. and A comes after B in tree order.
  177. * `O(n)` (worst case) where `n` is the amount of objects in the entire tree
  178. * `O(1)` (amortized when walking the entire tree)
  179. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  180. | Param | Type | Default | Description |
  181. | --- | --- | --- | --- |
  182. | object | <code>Object</code> | | |
  183. | [options] | <code>Object</code> | | |
  184. | [options.root] | <code>Object</code> | | 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` |
  185. | [options.skipChildren] | <code>Boolean</code> | <code>false</code> | If set, ignore the children of `object` |
  186. <a name="module_symbol-tree--SymbolTree+childrenToArray"></a>
  187. #### symbolTree.childrenToArray(parent, [options]) ⇒ <code>Array.&lt;Object&gt;</code>
  188. Append all children of the given object to an array.
  189. * `O(n)` where `n` is the amount of children of the given `parent`
  190. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  191. | Param | Type | Default | Description |
  192. | --- | --- | --- | --- |
  193. | parent | <code>Object</code> | | |
  194. | [options] | <code>Object</code> | | |
  195. | [options.array] | <code>Array.&lt;Object&gt;</code> | <code>[]</code> | |
  196. | [options.filter] | <code>function</code> | | 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. |
  197. | [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
  198. <a name="module_symbol-tree--SymbolTree+ancestorsToArray"></a>
  199. #### symbolTree.ancestorsToArray(object, [options]) ⇒ <code>Array.&lt;Object&gt;</code>
  200. Append all inclusive ancestors of the given object to an array.
  201. * `O(n)` where `n` is the amount of ancestors of the given `object`
  202. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  203. | Param | Type | Default | Description |
  204. | --- | --- | --- | --- |
  205. | object | <code>Object</code> | | |
  206. | [options] | <code>Object</code> | | |
  207. | [options.array] | <code>Array.&lt;Object&gt;</code> | <code>[]</code> | |
  208. | [options.filter] | <code>function</code> | | 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. |
  209. | [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
  210. <a name="module_symbol-tree--SymbolTree+treeToArray"></a>
  211. #### symbolTree.treeToArray(root, [options]) ⇒ <code>Array.&lt;Object&gt;</code>
  212. Append all descendants of the given object to an array (in tree order).
  213. * `O(n)` where `n` is the amount of objects in the sub-tree of the given `object`
  214. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  215. | Param | Type | Default | Description |
  216. | --- | --- | --- | --- |
  217. | root | <code>Object</code> | | |
  218. | [options] | <code>Object</code> | | |
  219. | [options.array] | <code>Array.&lt;Object&gt;</code> | <code>[]</code> | |
  220. | [options.filter] | <code>function</code> | | 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. |
  221. | [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
  222. <a name="module_symbol-tree--SymbolTree+childrenIterator"></a>
  223. #### symbolTree.childrenIterator(parent, [options]) ⇒ <code>Object</code>
  224. Iterate over all children of the given object
  225. * `O(1)` for a single iteration
  226. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  227. **Returns**: <code>Object</code> - An iterable iterator (ES6)
  228. | Param | Type | Default |
  229. | --- | --- | --- |
  230. | parent | <code>Object</code> | |
  231. | [options] | <code>Object</code> | |
  232. | [options.reverse] | <code>Boolean</code> | <code>false</code> |
  233. <a name="module_symbol-tree--SymbolTree+previousSiblingsIterator"></a>
  234. #### symbolTree.previousSiblingsIterator(object) ⇒ <code>Object</code>
  235. Iterate over all the previous siblings of the given object. (in reverse tree order)
  236. * `O(1)` for a single iteration
  237. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  238. **Returns**: <code>Object</code> - An iterable iterator (ES6)
  239. | Param | Type |
  240. | --- | --- |
  241. | object | <code>Object</code> |
  242. <a name="module_symbol-tree--SymbolTree+nextSiblingsIterator"></a>
  243. #### symbolTree.nextSiblingsIterator(object) ⇒ <code>Object</code>
  244. Iterate over all the next siblings of the given object. (in tree order)
  245. * `O(1)` for a single iteration
  246. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  247. **Returns**: <code>Object</code> - An iterable iterator (ES6)
  248. | Param | Type |
  249. | --- | --- |
  250. | object | <code>Object</code> |
  251. <a name="module_symbol-tree--SymbolTree+ancestorsIterator"></a>
  252. #### symbolTree.ancestorsIterator(object) ⇒ <code>Object</code>
  253. Iterate over all inclusive ancestors of the given object
  254. * `O(1)` for a single iteration
  255. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  256. **Returns**: <code>Object</code> - An iterable iterator (ES6)
  257. | Param | Type |
  258. | --- | --- |
  259. | object | <code>Object</code> |
  260. <a name="module_symbol-tree--SymbolTree+treeIterator"></a>
  261. #### symbolTree.treeIterator(root, options) ⇒ <code>Object</code>
  262. Iterate over all descendants of the given object (in tree order).
  263. Where `n` is the amount of objects in the sub-tree of the given `root`:
  264. * `O(n)` (worst case for a single iteration)
  265. * `O(n)` (amortized, when completing the iterator)
  266. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  267. **Returns**: <code>Object</code> - An iterable iterator (ES6)
  268. | Param | Type | Default |
  269. | --- | --- | --- |
  270. | root | <code>Object</code> | |
  271. | options | <code>Object</code> | |
  272. | [options.reverse] | <code>Boolean</code> | <code>false</code> |
  273. <a name="module_symbol-tree--SymbolTree+index"></a>
  274. #### symbolTree.index(child) ⇒ <code>Number</code>
  275. Find the index of the given object (the number of preceding siblings).
  276. * `O(n)` where `n` is the amount of preceding siblings
  277. * `O(1)` (amortized, if the tree is not modified)
  278. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  279. **Returns**: <code>Number</code> - The number of preceding siblings, or -1 if the object has no parent
  280. | Param | Type |
  281. | --- | --- |
  282. | child | <code>Object</code> |
  283. <a name="module_symbol-tree--SymbolTree+childrenCount"></a>
  284. #### symbolTree.childrenCount(parent) ⇒ <code>Number</code>
  285. Calculate the number of children.
  286. * `O(n)` where `n` is the amount of children
  287. * `O(1)` (amortized, if the tree is not modified)
  288. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  289. | Param | Type |
  290. | --- | --- |
  291. | parent | <code>Object</code> |
  292. <a name="module_symbol-tree--SymbolTree+compareTreePosition"></a>
  293. #### symbolTree.compareTreePosition(left, right) ⇒ <code>Number</code>
  294. Compare the position of an object relative to another object. A bit set is returned:
  295. <ul>
  296. <li>DISCONNECTED : 1</li>
  297. <li>PRECEDING : 2</li>
  298. <li>FOLLOWING : 4</li>
  299. <li>CONTAINS : 8</li>
  300. <li>CONTAINED_BY : 16</li>
  301. </ul>
  302. The semantics are the same as compareDocumentPosition in DOM, with the exception that
  303. DISCONNECTED never occurs with any other bit.
  304. where `n` and `m` are the amount of ancestors of `left` and `right`;
  305. where `o` is the amount of children of the lowest common ancestor of `left` and `right`:
  306. * `O(n + m + o)` (worst case)
  307. * `O(n + m)` (amortized, if the tree is not modified)
  308. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  309. | Param | Type |
  310. | --- | --- |
  311. | left | <code>Object</code> |
  312. | right | <code>Object</code> |
  313. <a name="module_symbol-tree--SymbolTree+remove"></a>
  314. #### symbolTree.remove(removeObject) ⇒ <code>Object</code>
  315. Remove the object from this tree.
  316. Has no effect if already removed.
  317. * `O(1)`
  318. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  319. **Returns**: <code>Object</code> - removeObject
  320. | Param | Type |
  321. | --- | --- |
  322. | removeObject | <code>Object</code> |
  323. <a name="module_symbol-tree--SymbolTree+insertBefore"></a>
  324. #### symbolTree.insertBefore(referenceObject, newObject) ⇒ <code>Object</code>
  325. Insert the given object before the reference object.
  326. `newObject` is now the previous sibling of `referenceObject`.
  327. * `O(1)`
  328. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  329. **Returns**: <code>Object</code> - newObject
  330. **Throws**:
  331. - <code>Error</code> If the newObject is already present in this SymbolTree
  332. | Param | Type |
  333. | --- | --- |
  334. | referenceObject | <code>Object</code> |
  335. | newObject | <code>Object</code> |
  336. <a name="module_symbol-tree--SymbolTree+insertAfter"></a>
  337. #### symbolTree.insertAfter(referenceObject, newObject) ⇒ <code>Object</code>
  338. Insert the given object after the reference object.
  339. `newObject` is now the next sibling of `referenceObject`.
  340. * `O(1)`
  341. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  342. **Returns**: <code>Object</code> - newObject
  343. **Throws**:
  344. - <code>Error</code> If the newObject is already present in this SymbolTree
  345. | Param | Type |
  346. | --- | --- |
  347. | referenceObject | <code>Object</code> |
  348. | newObject | <code>Object</code> |
  349. <a name="module_symbol-tree--SymbolTree+prependChild"></a>
  350. #### symbolTree.prependChild(referenceObject, newObject) ⇒ <code>Object</code>
  351. Insert the given object as the first child of the given reference object.
  352. `newObject` is now the first child of `referenceObject`.
  353. * `O(1)`
  354. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  355. **Returns**: <code>Object</code> - newObject
  356. **Throws**:
  357. - <code>Error</code> If the newObject is already present in this SymbolTree
  358. | Param | Type |
  359. | --- | --- |
  360. | referenceObject | <code>Object</code> |
  361. | newObject | <code>Object</code> |
  362. <a name="module_symbol-tree--SymbolTree+appendChild"></a>
  363. #### symbolTree.appendChild(referenceObject, newObject) ⇒ <code>Object</code>
  364. Insert the given object as the last child of the given reference object.
  365. `newObject` is now the last child of `referenceObject`.
  366. * `O(1)`
  367. **Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
  368. **Returns**: <code>Object</code> - newObject
  369. **Throws**:
  370. - <code>Error</code> If the newObject is already present in this SymbolTree
  371. | Param | Type |
  372. | --- | --- |
  373. | referenceObject | <code>Object</code> |
  374. | newObject | <code>Object</code> |