forked from lukasbuenger/immutable-treeutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1179 lines (1139 loc) · 24.1 KB
/
index.js
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Immutable = require('immutable')
var Seq = Immutable.Seq
var List = Immutable.List
var Stack = Immutable.Stack || List
function isV4() {
return typeof Seq.of === 'undefined'
}
function exists(value) {
return (
value !== null &&
typeof value !== 'undefined'
)
}
var NONE = undefined
/**
* @id TreeUtils
* @lookup TreeUtils
*
*
* ### *class* TreeUtils
*
* A collection of functional tree traversal helper functions for >ImmutableJS data structures.
*
* **Example**
*
* ```js
* var treeUtils = new TreeUtils(Immutable.Seq(['path', 'to', 'tree']));
* ```
*
* **With custom key accessors**
*
* ```js
* var treeUtils = new TreeUtils(Immutable.Seq(['path', 'to', 'tree']), '__id', '__children');
* ```
*
* **With custom *no result*-default**
*
* ```js
* var treeUtils = new TreeUtils(Immutable.Seq(['path', 'to', 'tree']), 'id', 'children', false);
* ```
*
* **Note**
* The first argument of every method of a `TreeUtils` object is the state you want to analyse. I won't mention / explain it again in method descriptions bellow. The argument `idOrKeyPath` also appears in most signatures, its purpose is thoroughly explained in the docs of >byArbitrary.
*
*
* ###### Signature:
* ```js
* new TreeUtils(
* rootPath?: immutable.Seq,
* idKey?: string,
* childNodesKey?: string,
* nonValue?: any
* )
* ```
*
* ###### Arguments:
* * `rootPath` - The path to the substate of your >ImmutableJS state that represents the root node of your tree. Default: `Immutable.Seq()`.
* * `idKey` - The name of the key that points at unique identifiers of all nodes in your tree . Default: `'id'`.
* * `childNodesKey` - The name of the key at which child nodes can be found. Default: `'childNodes'`.
* * `noneValue` - The value that will get returned if a query doesn't return any results. Default: `undefined`.
*
* ###### Returns:
* * A new `TreeUtils` object
*/
function TreeUtils(
rootPath,
idKey,
childNodesKey,
none
) {
this.rootPath = rootPath || Seq()
this.idKey = idKey || 'id'
this.childNodesKey =
childNodesKey || 'childNodes'
this.none =
typeof none !== 'undefined'
? none
: NONE
}
/**
* @id TreeUtils-walk
* @lookup walk
*
* #### *method* walk()
*
* Main traversal algorithm. Lets you walk over all nodes in the tree **in no particular order**.
*
* ###### Signature:
* ```js
* walk(
* state: Immutable.Iterable,
* iterator: (
* accumulator: any,
* keyPath: Immutable.Seq<string|number>
* stop: (
* value: any
* ): any
* ): any,
* path?: Immutable.Seq<string|number>
* ): any
* ```
*
* ###### Arguments:
* * `iterator` - A function that gets passed an accumulator, the current key path and a stop function:
* * If the iterator returns a value, this value will be kept as reduction and passed as accumulator to further iterations.
* * If the iterator returns a `stop` call, the walk operation will return immediately, giving back any value you passed to the `stop` function.
* * `path` - The key path that points at the root of the (sub)tree you want to walk over. Default: The `TreeUtils` object's `rootPath`.
*
* ###### Returns:
* The result of the walk operation.
*/
TreeUtils.prototype.walk = function(
state,
iterator,
path
) {
var childNodesKey = this.childNodesKey
var stack = Stack.of(
path || this.rootPath
)
var reduction = this.rootPath
var stopped = false
var stop = function(value) {
stopped = true
return value
}
while (!stopped && stack.size > 0) {
var keyPath = stack.first()
reduction = iterator(
reduction,
keyPath,
stop
)
stack = stack.shift()
var childNodes = state.getIn(
keyPath.concat(childNodesKey)
)
if (
childNodes &&
childNodes.size > 0
) {
childNodes
.keySeq()
.forEach(function(i) {
stack = stack.unshift(
keyPath.concat(
childNodesKey,
i
)
)
})
}
}
return reduction
}
/**
* @id TreeUtils-nodes
* @lookup nodes
*
* #### *method* nodes()
*
* ```js
* treeUtils.nodes(state).forEach(
* keyPath =>
* console.log(treeUtils.id(state, keyPath));
* )
* ```
*
* ###### Signature:
* ```
* nodes(
* state: Immutable.Iterable,
* path?: Immutable.Seq<string|number>
* ): Immutable.List<Immutable.Seq<string|number>>
* ```
*
* ###### Arguments:
* * `path` - The key path that points at the root of the (sub)tree whose descendants you want to iterate. Default: The `TreeUtils` object's `rootPath`.
*
* ###### Returns:
* An **unordered** >Immutable.List of all key paths that point to nodes in the tree, including the root of the (sub)tree..
*/
TreeUtils.prototype.nodes = function(
state,
path
) {
return this.walk(
state,
function(acc, keyPath) {
return List.isList(acc)
? acc.push(keyPath)
: List.of(keyPath)
},
path
)
}
/**
* @id TreeUtils-find
* @lookup find
*
* #### *method* find()
*
* Returns the key path to the first node for which `compatator` returns `true`. Uses >nodes internally and as >nodes is an **unordered** List, you should probably use this to find unique occurences of data.
* ```js
* treeUtils.find(state, node => node.get('name') === 'Me in Paris');
* // Seq ["childNodes", 0, "childNodes", 0]
* ```
*
* ###### Signature:
* ```js
* find(
* state: Immutable.Iterable,
* comparator: (
* node: Immutable.Iterable,
* keyPath: Immutable.Seq<string|number>
* ): boolean,
* path?: Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
*
* ###### Arguments:
* * `comparator` - A function that gets passed a `node` and its `keyPath` and should return whether it fits the criteria or not.
* * `path?` - An optional key path to the (sub)state you want to analyse: Default: The `TreeUtils` object's `rootPath`.
*
* ###### Returns:
* The key path to the first node for which `comparator` returned `true`.
*/
TreeUtils.prototype.find = function(
state,
comparator,
path
) {
var self = this
return this.walk(
state,
function(acc, keyPath, stop) {
if (
comparator(
state.getIn(keyPath),
keyPath
)
) {
return stop(keyPath)
}
return self.none
},
path
)
}
/**
* @id TreeUtils-filter
* @lookup filter
*
* #### *method* filter()
*
* Returns an >Immutable.List of key paths pointing at the nodes for which `comparator` returned `true`.
* ```js
* treeUtils.filter(state, node => node.get('type') === 'folder');
* //List [ Seq[], Seq["childNodes", 0], Seq["childNodes", 1] ]
* ```
*
* ###### Signature:
* ```js
* filter(
* state: Immutable.Iterable,
* comparator: (
* node: Immutable.Iterable,
* keyPath: Immutable.Seq<string|number>
* ): boolean,
* path?: Immutable.Seq<string|number>
* ): List<Immutable.Seq<string|number>>
* ```
*
* ###### Arguments:
* * `comparator` - A function that gets passed a `node` and its `keyPath` and should return whether it fits the criteria or not.
* * `path?` - An optional key path to the (sub)state you want to analyse: Default: The `TreeUtils` object's `rootPath`.
*
*
* ###### Returns:
* A >Immutable.List of all the key paths that point at nodes for which `comparator` returned `true`.
*/
TreeUtils.prototype.filter = function(
state,
comparator,
path
) {
return this.walk(
state,
function(acc, keyPath) {
var res = List.isList(acc)
? acc
: List()
if (
comparator(
state.getIn(keyPath),
keyPath
)
) {
return res.push(keyPath)
}
return res
},
path
)
}
/**
* @id TreeUtils-byId
* @lookup byId
*
* #### *method* byId()
*
* Returns the key path to the node with id === `id`.
*
* ###### Signature:
* ```js
* id(
* state: Immutable.Iterable,
* id: string
* ): Immutable.Seq<string|number>
* ```
*
* ###### Arguments:
* * `id` - A unique identifier
*
* ###### Returns:
* The key path to the node with id === `id`.
*/
TreeUtils.prototype.byId = function(
state,
id
) {
var idKey = this.idKey
return this.find(state, function(
item
) {
return item.get(idKey) === id
})
}
/**
* @id TreeUtils-byArbitrary
* @lookup byArbitrary
*
* #### *method* byArbitrary()
*
* Returns `idOrKeyPath` if it is a >Immutable.Seq, else returns the result of >byId for `idOrKeyPath`. This is used in all other functions that work on a unique identifiers in order to reduce the number of lookup operations.
*
* ###### Signature:
* ```js
* byArbitrary(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
* ###### Returns:
* The key path pointing at the node found for id === `idOrKeyPath` or, if is a >Immutable.Seq, the `idOrKeyPath` itself.
*
*/
TreeUtils.prototype.byArbitrary = function(
state,
idOrKeyPath
) {
return Seq.isSeq(idOrKeyPath)
? idOrKeyPath
: this.byId(state, idOrKeyPath)
}
/**
* @id TreeUtils-id
* @lookup id
*
* #### *method* id()
*
* Returns the id for the node at `keyPath`. Most useful when you want to get the id of the result of a previous tree query:
* ```js
* treeUtils.id(state, treeUtils.parent(state, 'node-3'));
* // 'node-1'
* ```
*
* ###### Signature:
* ```js
* id(
* state: Immutable.Iterable,
* keyPath: Immutable.Seq<string|number>
* ): string
* ```
*
* ###### Arguments:
* * `keyPath` - The absolute key path to the substate / node whose id you want to retrieve
*
* ###### Returns:
* The unique identifier of the node at the given key path.
*
*/
TreeUtils.prototype.id = function(
state,
keyPath
) {
return state.getIn(
keyPath.concat(this.idKey)
)
}
/**
* @id TreeUtils-nextSibling
* @lookup nextSibling
*
* #### *method* nextSibling()
*
* ###### Signature:
* ```js
* nextSibling(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the next sibling node of the node at `idOrKeyPath`
*/
TreeUtils.prototype.nextSibling = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
var index = Number(keyPath.last())
var nextSiblingPath = keyPath
.skipLast(1)
.concat(index + 1)
if (state.hasIn(nextSiblingPath)) {
return nextSiblingPath
}
return this.none
}
/**
* @id TreeUtils-previousSibling
* @lookup previousSibling
*
* #### *method* previousSibling()
*
* ###### Signature:
* ```js
* previousSibling(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the previous sibling node of the node at `idOrKeyPath`
*/
TreeUtils.prototype.previousSibling = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
var index = Number(keyPath.last())
if (index > 0) {
return keyPath
.skipLast(1)
.concat(index - 1)
}
return this.none
}
/**
* @id TreeUtils-firstChild
* @lookup firstChild
*
* #### *method* firstChild()
*
* ###### Signature:
* ```js
* firstChild(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the first child node of the node at `idOrKeyPath`
*/
TreeUtils.prototype.firstChild = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat([this.childNodesKey, 0])
if (state.hasIn(keyPath)) {
return keyPath
}
return this.none
}
/**
* @id TreeUtils-lastChild
* @lookup lastChild
*
* #### *method* lastChild()
*
* ###### Signature:
* ```js
* lastChild(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the last child node of the node at `idOrKeyPath`
*/
TreeUtils.prototype.lastChild = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat([this.childNodesKey])
var item = state.getIn(keyPath)
if (item && item.size > 0) {
return keyPath.concat([
item.size - 1
])
}
return this.none
}
/**
* @id TreeUtils-siblings
* @lookup siblings
*
* #### *method* siblings()
*
* ###### Signature:
* ```js
* siblings(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.List<Immutable.Seq<string|number>>
* ```
*
* ###### Returns:
* Returns a >Immutable.List of key paths pointing at the sibling nodes of the node at `idOrKeyPath`
*/
TreeUtils.prototype.siblings = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
var index = Number(keyPath.last())
var parentChildren = keyPath.skipLast(
1
)
var item = state.getIn(parentChildren)
if (exists(item)) {
return item
.keySeq()
.reduce(function(result, i) {
return i !== index
? result.push(
parentChildren.concat(i)
)
: result
}, List())
}
return this.none
}
/**
* @id TreeUtils-childNodes
* @lookup childNodes
*
* #### *method* childNodes()
*
* ###### Signature:
* ```js
* childNodes(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>
* ): Immutable.List<Immutable.Seq<string|number>>
* ```
*
* ###### Returns:
* Returns a >Immutable.List of all child nodes of the node at `idOrKeyPath`
*/
TreeUtils.prototype.childNodes = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat(this.childNodesKey)
var item = state.getIn(keyPath)
if (exists(item)) {
var l = item.size
var result = List()
for (var i = 0; i < l; i += 1) {
result = result.push(
keyPath.concat(i)
)
}
return result
}
return this.none
}
/**
* @id TreeUtils-childAt
* @lookup childNodes
*
* #### *method* childAt()
*
* ###### Signature:
* ```js
* childAt(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* index: number
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the child node at position of `index` of the node at `idOrKeyPath`
*/
TreeUtils.prototype.childAt = function(
state,
idOrKeyPath,
index
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat(this.childNodesKey, index)
if (state.hasIn(keyPath)) {
return keyPath
}
return this.none
}
/**
* @id TreeUtils-descendants
* @lookup descendants
*
* #### *method* descendants()
*
* ###### Signature:
* ```js
* descendants(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): Immutable.List<Immutable.Seq<string|number>>
* ```
*
* ###### Returns:
* Returns a list of key paths pointing at all descendants of the node at `idOrKeyPath`
*/
TreeUtils.prototype.descendants = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
var self = this
return this.filter(
state,
function(item) {
return (
item.get(self.idKey) !==
self.id(state, keyPath)
)
},
keyPath
)
}
/**
* @id TreeUtils-childIndex
* @lookup childIndex
*
* #### *method* childIndex()
*
* ###### Signature:
* ```js
* childIndex(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): number
* ```
*
* ###### Returns:
* Returns the index at which the node at `idOrKeyPath` is positioned in its parent child nodes list.
*/
TreeUtils.prototype.childIndex = function(
state,
idOrKeyPath
) {
return Number(
this.byArbitrary(
state,
idOrKeyPath
).last()
)
}
/**
* @id TreeUtils-hasChildNodes
* @lookup hasChildNodes
*
* #### *method* hasChildNodes()
*
* ###### Signature:
* ```js
* hasChildNodes(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): boolean
* ```
*
* ###### Returns:
* Returns whether the node at `idOrKeyPath` has children.
*/
TreeUtils.prototype.hasChildNodes = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat(this.childNodesKey)
var item = state.getIn(keyPath)
return exists(item) && item.size > 0
}
/**
* @id TreeUtils-numChildNodes
* @lookup numChildNodes
*
* #### *method* numChildNodes()
*
* ###### Signature:
* ```js
* numChildNodes(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): number
* ```
*
* ###### Returns:
* Returns the number of child nodes the node at `idOrKeyPath` has.
*/
TreeUtils.prototype.numChildNodes = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
).concat(this.childNodesKey)
var item = state.getIn(keyPath)
return exists(item) ? item.size : 0
}
/**
* @id TreeUtils-parent
* @lookup parent
*
* #### *method* parent()
*
* ###### Signature:
* ```js
* parent(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the key path to the parent of the node at `idOrKeyPath`.
*/
TreeUtils.prototype.parent = function(
state,
idOrKeyPath
) {
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
if (keyPath && keyPath.size) {
return keyPath.slice(0, -2)
}
return this.none
}
/**
* @id TreeUtils-ancestors
* @lookup ancestors
*
* #### *method* ancestors()
*
* ###### Signature:
* ```js
* ancestors(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* An >Immutable.List of all key paths that point at direct ancestors of the node at `idOrKeyPath`.
*/
TreeUtils.prototype.ancestors = function(
state,
idOrKeyPath
) {
var self = this
return this.byArbitrary(
state,
idOrKeyPath
).reduceRight(function(
memo,
value,
index,
keyPath
) {
if (
(index - self.rootPath.size) %
2 ===
0 &&
index >= self.rootPath.size
) {
return memo.push(
isV4()
? keyPath.take(index)
: keyPath
.takeLast(index)
.reverse()
.toSetSeq()
)
}
return memo
},
List())
}
/**
* @id TreeUtils-depth
* @lookup depth
*
* #### *method* depth()
*
* ###### Signature:
* ```js
* depth(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): number
* ```
*
* ###### Returns:
* A numeric representation of the depth of the node at `idOrKeyPath`
*/
TreeUtils.prototype.depth = function(
state,
idOrKeyPath
) {
return Math.floor(
this.byArbitrary(
state,
idOrKeyPath
).skip(this.rootPath.size).size / 2
)
}
/**
* @id TreeUtils-position
* @lookup position
*
* #### *method* position()
*
* This method is a very naive attempt to calculate a unqiue numeric position descriptor that can be used to compare two nodes for their absolute position in the tree.
* ```js
* treeUtils.position(state, 'node-4') > treeUtils.position(state, 'node-3');
* // true
* ```
*
* Please note that `position` should not get used to do any comparison with the root node.
*
* ###### Signature:
* ```js
* position(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): number
* ```
*
* ###### Returns:
* Returns a unique numeric value that represents the absolute position of the node at `idOrKeyPath`.
*/
TreeUtils.prototype.position = function(
state,
idOrKeyPath
) {
var self = this
var order = this.byArbitrary(
state,
idOrKeyPath
).reduceRight(function(
memo,
value,
index
) {
if (
index >= self.rootPath.size &&
index % 2 === 0
) {
return value.toString() + memo
}
return memo
},
'')
return Number(
'1.'.concat(order.toString())
)
}
/**
* @id TreeUtils-right
* @lookup right
*
* #### *method* right()
*
* Returns the key path to the next node to the right. The next right node is either:
* * The first child node.
* * The next sibling.
* * The next sibling of the first ancestor that in fact has a next sibling.
* * The none value
*
* ```js
* var nodePath = treeUtils.byId(state, 'root');
* while (nodePath) {
* console.log(nodePath);
* nodePath = treeUtils.right(state, nodePath);
* }
* // 'root'
* // 'node-1'
* // 'node-2'
* // 'node-3'
* // 'node-4'
* // 'node-5'
* // 'node-6'
* ```
*
* ###### Signature:
* ```js
* right(
* state: Immutable.Iterable,
* idOrKeyPath: string|Immutable.Seq<string|number>,
* ): Immutable.Seq<string|number>
* ```
*
* ###### Returns:
* Returns the key path to the node to the right of the one at `idOrKeyPath`.
*/
TreeUtils.prototype.right = function(
state,
idOrKeyPath
) {
var l = this.rootPath.size
var keyPath = this.byArbitrary(
state,
idOrKeyPath
)
var firstChild = this.firstChild(
state,