Skip to content

Commit 84c204a

Browse files
authored
Merge pull request #10 from webKrafters/arr-to-iter
Minor touchups and refactorings.
2 parents 7b5dcfb + 608e11b commit 84c204a

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@
5959
"test:watch": "jest --updateSnapshot --watchAll"
6060
},
6161
"types": "dist/index.d.ts",
62-
"version": "0.5.0"
62+
"version": "0.5.1"
6363
}

src/main.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ export default class Trie<T = unknown> {
255255
) {
256256
return this._getAllStartingWith([ ...prefix ], completeSequencesOnly );
257257
}
258-
getFarthestIn( sequence : Iterable<T> = [] ) {
259-
const _sequence = [ ...sequence ];
258+
getFarthestIn( sequence : Iterable<T> = [] ) : Iterable<T> {
259+
const _sequence = toArray( sequence );
260260
return _sequence.slice( 0, this.root.getDeepestNodeIn( _sequence ).index + 1 );
261261
}
262262
has( sequence : Iterable<T> ) {
263-
return !!this.root.getChildPrefixEnd([ ...sequence ])?.isSequenceBoundary;
263+
return !!this.root.getChildPrefixEnd( toArray( sequence ) )?.isSequenceBoundary;
264264
}
265265
isSame( trie : Trie<T> ) { return this === trie }
266266
matches( graph : Array<Iterable<T>> ) : boolean;
@@ -275,7 +275,7 @@ export default class Trie<T = unknown> {
275275
if( isIterable( g[ 0 ] ) ) {
276276
const _g = new Array( g.length );
277277
for( let i = _g.length; i--; ) {
278-
_g[ i ] = [ ...g[ i ] ];
278+
_g[ i ] = toArray( g[ i ] );
279279
}
280280
g = _g;
281281
} else {
@@ -314,9 +314,9 @@ export default class Trie<T = unknown> {
314314
* @param {Array<Iterable<T>>} data - Accepts sequences of items to remove from the trie.
315315
* @returns {boolean} - true is successfully removed; false otherwise
316316
*/
317-
remove( data : Iterable<T> ) { return this.root.removeChild([ ...data ]) }
317+
remove( data : Iterable<T> ) { return this.root.removeChild( toArray( data ) ) }
318318
removeAllStartingWith( prefix : Iterable<T> = [] ) {
319-
const suffixStartNode = this.root.getChildPrefixEnd([ ...prefix ]);
319+
const suffixStartNode = this.root.getChildPrefixEnd( toArray( prefix ) );
320320
suffixStartNode?.parentNode.childNodes.remove( suffixStartNode );
321321
}
322322
/**
@@ -373,7 +373,7 @@ export class Node<T = unknown> {
373373
this._isSequenceBoundary = isSequenceBoundary;
374374
for( let dLen = successorData.length, d = 0; d < dLen; d++ ) {
375375
isIterable( successorData[ d ] )
376-
? this.addChild([ ...successorData[ d ] as Array<T> ])
376+
? this.addChild([ ...successorData[ d ] as Iterable<T> ])
377377
: this.mergeTrieableNode( successorData[ d ] as TrieableNode<T> );
378378
}
379379
}
@@ -468,11 +468,9 @@ export class Node<T = unknown> {
468468
isEqual( graph : TrieableNode<T> ) : boolean;
469469
isEqual( graph ) : boolean {
470470
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 ];
476474
if( cArr.length !== arr.length ) { return false }
477475
for( let a = arr.length; a--; ) {
478476
const thisSequence = arr[ a ];
@@ -810,3 +808,7 @@ function isIterable( sequence ) {
810808
const type = getDescriptor( sequence );
811809
return type === 'Array' || type === 'String';
812810
}
811+
812+
function toArray<T>( sequence : Iterable<T> ) : Array<T> {
813+
return Array.isArray( sequence ) ? sequence : [ ...sequence ];
814+
}

0 commit comments

Comments
 (0)