11import { build } from './trie' ;
22
3+ interface ICharTrieData {
4+ [ prefix : string ] : ICharTrieData ;
5+ }
6+
7+ function convertObjectToTrie ( trieData : ICharTrieData ) : ICharTrie {
8+ const trieDataPairs = Object . entries ( trieData ) ;
9+
10+ const trie = trieDataPairs . reduce (
11+ ( map , [ prefix , suffix ] ) => map . set ( prefix , convertObjectToTrie ( suffix ) ) ,
12+ new Map ( )
13+ ) ;
14+
15+ return trie as ICharTrie ;
16+ }
17+
318describe ( 'build' , ( ) => {
419 it ( 'returns empty trie when no words' , ( ) => {
5- expect ( build ( [ ] ) ) . toEqual ( { } ) ;
20+ const expectedResult = convertObjectToTrie ( { } ) ;
21+ expect ( build ( [ ] ) ) . toEqual ( expectedResult ) ;
622 } ) ;
723
824 it ( 'returns the entirety of words when no common head' , ( ) => {
925 const words = [ 'bar' , 'foo' ] ;
10- expect ( build ( words ) ) . toEqual ( {
26+ const expectedResult = convertObjectToTrie ( {
1127 bar : { } ,
1228 foo : { } ,
1329 } ) ;
30+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
1431 } ) ;
1532
1633 it ( 'returns the entirety of repeated equal words' , ( ) => {
1734 const words = [ 'foo' , 'foo' , 'foo' , 'foo' ] ;
18- expect ( build ( words ) ) . toEqual ( {
35+ const expectedResult = convertObjectToTrie ( {
1936 foo : { } ,
2037 } ) ;
38+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
2139 } ) ;
2240
2341 it ( 'returns the entirety of multiple, repeated equal words' , ( ) => {
2442 const words = [ 'bar' , 'bar' , 'bar' , 'bar' , 'foo' , 'foo' , 'foo' , 'foo' ] ;
25- expect ( build ( words ) ) . toEqual ( {
43+ const expectedResult = convertObjectToTrie ( {
2644 bar : { } ,
2745 foo : { } ,
2846 } ) ;
47+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
2948 } ) ;
3049
3150 it ( 'returns an empty string for partial words' , ( ) => {
3251 const words = [ 'foo' , 'foobar' ] ;
33- expect ( build ( words ) ) . toEqual ( {
52+ const expectedResult = convertObjectToTrie ( {
3453 foo : { '' : { } , bar : { } } ,
3554 } ) ;
55+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
3656 } ) ;
3757
3858 it ( 'returns the common head of two words' , ( ) => {
3959 const words = [ 'bar' , 'baz' ] ;
40- expect ( build ( words ) ) . toEqual ( {
60+ const expectedResult = convertObjectToTrie ( {
4161 ba : { r : { } , z : { } } ,
4262 } ) ;
63+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
4364 } ) ;
4465
4566 it ( 'returns multiple depths of partial words' , ( ) => {
4667 const words = [ 'foo' , 'foobar' , 'foobaz' ] ;
47- expect ( build ( words ) ) . toEqual ( {
68+ const expectedResult = convertObjectToTrie ( {
4869 foo : { '' : { } , ba : { r : { } , z : { } } } ,
4970 } ) ;
71+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
5072 } ) ;
5173
5274 it ( 'returns the common head of multiple words' , ( ) => {
5375 const words = [ 'bar' , 'baz' , 'quux' , 'quuz' ] ;
54- expect ( build ( words ) ) . toEqual ( {
76+ const expectedResult = convertObjectToTrie ( {
5577 ba : { r : { } , z : { } } ,
5678 quu : { x : { } , z : { } } ,
5779 } ) ;
80+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
5881 } ) ;
5982
6083 it ( 'preserves leading whitespace' , ( ) => {
6184 const words = [ ' foo' , 'foo' ] ;
62- expect ( build ( words ) ) . toEqual ( {
85+ const expectedResult = convertObjectToTrie ( {
6386 ' foo' : { } ,
6487 foo : { } ,
6588 } ) ;
89+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
6690 } ) ;
6791
6892 it ( 'preserves trailing whitespace' , ( ) => {
6993 const words = [ 'foo ' , 'foo' ] ;
70- expect ( build ( words ) ) . toEqual ( {
94+ const expectedResult = convertObjectToTrie ( {
7195 foo : { '' : { } , ' ' : { } } ,
7296 } ) ;
97+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
7398 } ) ;
7499
75100 it ( 'preserves mid-word whitespace' , ( ) => {
76101 const words = [ 'foo bar' , 'foobar' ] ;
77- expect ( build ( words ) ) . toEqual ( {
102+ const expectedResult = convertObjectToTrie ( {
78103 foo : { ' bar' : { } , bar : { } } ,
79104 } ) ;
105+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
80106 } ) ;
81107
82108 it ( 'is case-sensitive with string heads' , ( ) => {
83109 const words = [ 'Foo' , 'foo' ] ;
84- expect ( build ( words ) ) . toEqual ( {
110+ const expectedResult = convertObjectToTrie ( {
85111 Foo : { } ,
86112 foo : { } ,
87113 } ) ;
114+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
88115 } ) ;
89116
90117 it ( 'is case-sensitive with string tails' , ( ) => {
91118 const words = [ 'foo' , 'foO' ] ;
92- expect ( build ( words ) ) . toEqual ( {
119+ const expectedResult = convertObjectToTrie ( {
93120 fo : { o : { } , O : { } } ,
94121 } ) ;
122+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
95123 } ) ;
96124
97125 it ( 'handles complex words' , ( ) => {
@@ -116,7 +144,7 @@ describe('build', () => {
116144 'Oklahoma' ,
117145 'Oregon' ,
118146 ] ;
119- expect ( build ( words ) ) . toEqual ( {
147+ const expectedResult = convertObjectToTrie ( {
120148 M : {
121149 a : { ine : { } , ryland : { } , ssachusetts : { } } ,
122150 i : { chigan : { } , nnesota : { } , ss : { issippi : { } , ouri : { } } } ,
@@ -132,5 +160,6 @@ describe('build', () => {
132160 } ,
133161 O : { hio : { } , klahoma : { } , regon : { } } ,
134162 } ) ;
163+ expect ( build ( words ) ) . toEqual ( expectedResult ) ;
135164 } ) ;
136165} ) ;
0 commit comments