1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
symbol-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
-----------------
<a name="module_symbol-tree"></a>
## symbol-tree
**Author**: Joris van der Wel <joris@jorisvanderwel.com>
* [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) ⇒ <code>Object</code>
* [.hasChildren(object)](#module_symbol-tree--SymbolTree+hasChildren) ⇒ <code>Boolean</code>
* [.firstChild(object)](#module_symbol-tree--SymbolTree+firstChild) ⇒ <code>Object</code>
* [.lastChild(object)](#module_symbol-tree--SymbolTree+lastChild) ⇒ <code>Object</code>
* [.previousSibling(object)](#module_symbol-tree--SymbolTree+previousSibling) ⇒ <code>Object</code>
* [.nextSibling(object)](#module_symbol-tree--SymbolTree+nextSibling) ⇒ <code>Object</code>
* [.parent(object)](#module_symbol-tree--SymbolTree+parent) ⇒ <code>Object</code>
* [.lastInclusiveDescendant(object)](#module_symbol-tree--SymbolTree+lastInclusiveDescendant) ⇒ <code>Object</code>
* [.preceding(object, [options])](#module_symbol-tree--SymbolTree+preceding) ⇒ <code>Object</code>
* [.following(object, [options])](#module_symbol-tree--SymbolTree+following) ⇒ <code>Object</code>
* [.childrenToArray(parent, [options])](#module_symbol-tree--SymbolTree+childrenToArray) ⇒ <code>Array.<Object></code>
* [.ancestorsToArray(object, [options])](#module_symbol-tree--SymbolTree+ancestorsToArray) ⇒ <code>Array.<Object></code>
* [.treeToArray(root, [options])](#module_symbol-tree--SymbolTree+treeToArray) ⇒ <code>Array.<Object></code>
* [.childrenIterator(parent, [options])](#module_symbol-tree--SymbolTree+childrenIterator) ⇒ <code>Object</code>
* [.previousSiblingsIterator(object)](#module_symbol-tree--SymbolTree+previousSiblingsIterator) ⇒ <code>Object</code>
* [.nextSiblingsIterator(object)](#module_symbol-tree--SymbolTree+nextSiblingsIterator) ⇒ <code>Object</code>
* [.ancestorsIterator(object)](#module_symbol-tree--SymbolTree+ancestorsIterator) ⇒ <code>Object</code>
* [.treeIterator(root, options)](#module_symbol-tree--SymbolTree+treeIterator) ⇒ <code>Object</code>
* [.index(child)](#module_symbol-tree--SymbolTree+index) ⇒ <code>Number</code>
* [.childrenCount(parent)](#module_symbol-tree--SymbolTree+childrenCount) ⇒ <code>Number</code>
* [.compareTreePosition(left, right)](#module_symbol-tree--SymbolTree+compareTreePosition) ⇒ <code>Number</code>
* [.remove(removeObject)](#module_symbol-tree--SymbolTree+remove) ⇒ <code>Object</code>
* [.insertBefore(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertBefore) ⇒ <code>Object</code>
* [.insertAfter(referenceObject, newObject)](#module_symbol-tree--SymbolTree+insertAfter) ⇒ <code>Object</code>
* [.prependChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+prependChild) ⇒ <code>Object</code>
* [.appendChild(referenceObject, newObject)](#module_symbol-tree--SymbolTree+appendChild) ⇒ <code>Object</code>
<a name="exp_module_symbol-tree--SymbolTree"></a>
### SymbolTree ⏏
**Kind**: Exported class
<a name="new_module_symbol-tree--SymbolTree_new"></a>
#### new SymbolTree([description])
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [description] | <code>string</code> | <code>"'SymbolTree data'"</code> | Description used for the Symbol |
<a name="module_symbol-tree--SymbolTree+initialize"></a>
#### symbolTree.initialize(object) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - object
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+hasChildren"></a>
#### symbolTree.hasChildren(object) ⇒ <code>Boolean</code>
Returns `true` if the object has any children. Otherwise it returns `false`.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+firstChild"></a>
#### symbolTree.firstChild(object) ⇒ <code>Object</code>
Returns the first child of the given object.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+lastChild"></a>
#### symbolTree.lastChild(object) ⇒ <code>Object</code>
Returns the last child of the given object.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+previousSibling"></a>
#### symbolTree.previousSibling(object) ⇒ <code>Object</code>
Returns the previous sibling of the given object.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+nextSibling"></a>
#### symbolTree.nextSibling(object) ⇒ <code>Object</code>
Returns the next sibling of the given object.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+parent"></a>
#### symbolTree.parent(object) ⇒ <code>Object</code>
Return the parent of the given object.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+lastInclusiveDescendant"></a>
#### symbolTree.lastInclusiveDescendant(object) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+preceding"></a>
#### symbolTree.preceding(object, [options]) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type | Description |
| --- | --- | --- |
| object | <code>Object</code> | |
| [options] | <code>Object</code> | |
| [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` |
<a name="module_symbol-tree--SymbolTree+following"></a>
#### symbolTree.following(object, [options]) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| object | <code>Object</code> | | |
| [options] | <code>Object</code> | | |
| [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` |
| [options.skipChildren] | <code>Boolean</code> | <code>false</code> | If set, ignore the children of `object` |
<a name="module_symbol-tree--SymbolTree+childrenToArray"></a>
#### symbolTree.childrenToArray(parent, [options]) ⇒ <code>Array.<Object></code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| parent | <code>Object</code> | | |
| [options] | <code>Object</code> | | |
| [options.array] | <code>Array.<Object></code> | <code>[]</code> | |
| [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. |
| [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
<a name="module_symbol-tree--SymbolTree+ancestorsToArray"></a>
#### symbolTree.ancestorsToArray(object, [options]) ⇒ <code>Array.<Object></code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| object | <code>Object</code> | | |
| [options] | <code>Object</code> | | |
| [options.array] | <code>Array.<Object></code> | <code>[]</code> | |
| [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. |
| [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
<a name="module_symbol-tree--SymbolTree+treeToArray"></a>
#### symbolTree.treeToArray(root, [options]) ⇒ <code>Array.<Object></code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| root | <code>Object</code> | | |
| [options] | <code>Object</code> | | |
| [options.array] | <code>Array.<Object></code> | <code>[]</code> | |
| [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. |
| [options.thisArg] | <code>\*</code> | | Value to use as `this` when executing `filter`. |
<a name="module_symbol-tree--SymbolTree+childrenIterator"></a>
#### symbolTree.childrenIterator(parent, [options]) ⇒ <code>Object</code>
Iterate over all children of the given object
* `O(1)` for a single iteration
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - An iterable iterator (ES6)
| Param | Type | Default |
| --- | --- | --- |
| parent | <code>Object</code> | |
| [options] | <code>Object</code> | |
| [options.reverse] | <code>Boolean</code> | <code>false</code> |
<a name="module_symbol-tree--SymbolTree+previousSiblingsIterator"></a>
#### symbolTree.previousSiblingsIterator(object) ⇒ <code>Object</code>
Iterate over all the previous siblings of the given object. (in reverse tree order)
* `O(1)` for a single iteration
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - An iterable iterator (ES6)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+nextSiblingsIterator"></a>
#### symbolTree.nextSiblingsIterator(object) ⇒ <code>Object</code>
Iterate over all the next siblings of the given object. (in tree order)
* `O(1)` for a single iteration
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - An iterable iterator (ES6)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+ancestorsIterator"></a>
#### symbolTree.ancestorsIterator(object) ⇒ <code>Object</code>
Iterate over all inclusive ancestors of the given object
* `O(1)` for a single iteration
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - An iterable iterator (ES6)
| Param | Type |
| --- | --- |
| object | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+treeIterator"></a>
#### symbolTree.treeIterator(root, options) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - An iterable iterator (ES6)
| Param | Type | Default |
| --- | --- | --- |
| root | <code>Object</code> | |
| options | <code>Object</code> | |
| [options.reverse] | <code>Boolean</code> | <code>false</code> |
<a name="module_symbol-tree--SymbolTree+index"></a>
#### symbolTree.index(child) ⇒ <code>Number</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Number</code> - The number of preceding siblings, or -1 if the object has no parent
| Param | Type |
| --- | --- |
| child | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+childrenCount"></a>
#### symbolTree.childrenCount(parent) ⇒ <code>Number</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| parent | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+compareTreePosition"></a>
#### symbolTree.compareTreePosition(left, right) ⇒ <code>Number</code>
Compare the position of an object relative to another object. A bit set is returned:
<ul>
<li>DISCONNECTED : 1</li>
<li>PRECEDING : 2</li>
<li>FOLLOWING : 4</li>
<li>CONTAINS : 8</li>
<li>CONTAINED_BY : 16</li>
</ul>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
| Param | Type |
| --- | --- |
| left | <code>Object</code> |
| right | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+remove"></a>
#### symbolTree.remove(removeObject) ⇒ <code>Object</code>
Remove the object from this tree.
Has no effect if already removed.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - removeObject
| Param | Type |
| --- | --- |
| removeObject | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+insertBefore"></a>
#### symbolTree.insertBefore(referenceObject, newObject) ⇒ <code>Object</code>
Insert the given object before the reference object.
`newObject` is now the previous sibling of `referenceObject`.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - newObject
**Throws**:
- <code>Error</code> If the newObject is already present in this SymbolTree
| Param | Type |
| --- | --- |
| referenceObject | <code>Object</code> |
| newObject | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+insertAfter"></a>
#### symbolTree.insertAfter(referenceObject, newObject) ⇒ <code>Object</code>
Insert the given object after the reference object.
`newObject` is now the next sibling of `referenceObject`.
* `O(1)`
**Kind**: instance method of [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - newObject
**Throws**:
- <code>Error</code> If the newObject is already present in this SymbolTree
| Param | Type |
| --- | --- |
| referenceObject | <code>Object</code> |
| newObject | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+prependChild"></a>
#### symbolTree.prependChild(referenceObject, newObject) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - newObject
**Throws**:
- <code>Error</code> If the newObject is already present in this SymbolTree
| Param | Type |
| --- | --- |
| referenceObject | <code>Object</code> |
| newObject | <code>Object</code> |
<a name="module_symbol-tree--SymbolTree+appendChild"></a>
#### symbolTree.appendChild(referenceObject, newObject) ⇒ <code>Object</code>
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 [<code>SymbolTree</code>](#exp_module_symbol-tree--SymbolTree)
**Returns**: <code>Object</code> - newObject
**Throws**:
- <code>Error</code> If the newObject is already present in this SymbolTree
| Param | Type |
| --- | --- |
| referenceObject | <code>Object</code> |
| newObject | <code>Object</code> |
|