@@ -255,12 +255,12 @@ export default class Trie<T = unknown> {
255
255
) {
256
256
return this . _getAllStartingWith ( [ ...prefix ] , completeSequencesOnly ) ;
257
257
}
258
- getFarthestIn ( sequence : Iterable < T > = [ ] ) {
259
- const _sequence = [ ... sequence ] ;
258
+ getFarthestIn ( sequence : Iterable < T > = [ ] ) : Iterable < T > {
259
+ const _sequence = toArray ( sequence ) ;
260
260
return _sequence . slice ( 0 , this . root . getDeepestNodeIn ( _sequence ) . index + 1 ) ;
261
261
}
262
262
has ( sequence : Iterable < T > ) {
263
- return ! ! this . root . getChildPrefixEnd ( [ ... sequence ] ) ?. isSequenceBoundary ;
263
+ return ! ! this . root . getChildPrefixEnd ( toArray ( sequence ) ) ?. isSequenceBoundary ;
264
264
}
265
265
isSame ( trie : Trie < T > ) { return this === trie }
266
266
matches ( graph : Array < Iterable < T > > ) : boolean ;
@@ -275,7 +275,7 @@ export default class Trie<T = unknown> {
275
275
if ( isIterable ( g [ 0 ] ) ) {
276
276
const _g = new Array ( g . length ) ;
277
277
for ( let i = _g . length ; i -- ; ) {
278
- _g [ i ] = [ ... g [ i ] ] ;
278
+ _g [ i ] = toArray ( g [ i ] ) ;
279
279
}
280
280
g = _g ;
281
281
} else {
@@ -314,9 +314,9 @@ export default class Trie<T = unknown> {
314
314
* @param {Array<Iterable<T>> } data - Accepts sequences of items to remove from the trie.
315
315
* @returns {boolean } - true is successfully removed; false otherwise
316
316
*/
317
- remove ( data : Iterable < T > ) { return this . root . removeChild ( [ ... data ] ) }
317
+ remove ( data : Iterable < T > ) { return this . root . removeChild ( toArray ( data ) ) }
318
318
removeAllStartingWith ( prefix : Iterable < T > = [ ] ) {
319
- const suffixStartNode = this . root . getChildPrefixEnd ( [ ... prefix ] ) ;
319
+ const suffixStartNode = this . root . getChildPrefixEnd ( toArray ( prefix ) ) ;
320
320
suffixStartNode ?. parentNode . childNodes . remove ( suffixStartNode ) ;
321
321
}
322
322
/**
@@ -373,7 +373,7 @@ export class Node<T = unknown> {
373
373
this . _isSequenceBoundary = isSequenceBoundary ;
374
374
for ( let dLen = successorData . length , d = 0 ; d < dLen ; d ++ ) {
375
375
isIterable ( successorData [ d ] )
376
- ? this . addChild ( [ ...successorData [ d ] as Array < T > ] )
376
+ ? this . addChild ( [ ...successorData [ d ] as Iterable < T > ] )
377
377
: this . mergeTrieableNode ( successorData [ d ] as TrieableNode < T > ) ;
378
378
}
379
379
}
@@ -468,11 +468,9 @@ export class Node<T = unknown> {
468
468
isEqual ( graph : TrieableNode < T > ) : boolean ;
469
469
isEqual ( graph ) : boolean {
470
470
const arr = this . asArray ( ) ;
471
- const cArr = Array . isArray ( graph )
472
- ? [ ...graph ]
473
- : getDescriptor ( graph ) === NODE_DESC
474
- ? graph . asArray ( )
475
- : new Trie ( graph ) . asArray ( ) ;
471
+ const cArr = ! Array . isArray ( graph )
472
+ ? ( getDescriptor ( graph ) === NODE_DESC ? graph : new Trie ( graph ) ) . asArray ( )
473
+ : [ ...graph ] ;
476
474
if ( cArr . length !== arr . length ) { return false }
477
475
for ( let a = arr . length ; a -- ; ) {
478
476
const thisSequence = arr [ a ] ;
@@ -810,3 +808,7 @@ function isIterable( sequence ) {
810
808
const type = getDescriptor ( sequence ) ;
811
809
return type === 'Array' || type === 'String' ;
812
810
}
811
+
812
+ function toArray < T > ( sequence : Iterable < T > ) : Array < T > {
813
+ return Array . isArray ( sequence ) ? sequence : [ ...sequence ] ;
814
+ }
0 commit comments