1+ import { assert } from "chai"
2+ import ArrayStringMap from "./arrayStringMap.js"
3+
4+ type TwoNumberArray = [ number , number ]
5+
6+ const sampleArray1 :TwoNumberArray = [ 1 , 2 ]
7+ const sampleArray2 :TwoNumberArray = [ 1 , 2 ]
8+ const sampleArray3 :TwoNumberArray = [ 1 , 3 ]
9+ const sampleValue1 = 1
10+ const sampleValue2 = 2
11+
12+ function copyArrayStringMap < K extends any [ ] , V > ( map :ArrayStringMap < K , V > ) :ArrayStringMap < K , V > {
13+ const newMap = new ArrayStringMap < K , V > ( )
14+ for ( const [ key , value ] of map ) {
15+ newMap . set ( key , value )
16+ }
17+ return newMap
18+ }
19+
20+ describe ( "Empty map" , ( ) => {
21+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
22+ it ( "Empty map size is 0" , ( ) => {
23+ assert ( arrayStringMap . size === 0 ) ;
24+ } )
25+ it ( "Empty map get returns undefined" , ( ) => {
26+ assert ( arrayStringMap . get ( sampleArray1 ) === undefined ) ;
27+ } )
28+ it ( "Empty map has returns false" , ( ) => {
29+ assert ( ! arrayStringMap . has ( sampleArray1 ) ) ;
30+ } )
31+ it ( "Empty map entries returns empty array" , ( ) => {
32+ assert ( [ ...arrayStringMap . entries ( ) ] . length === 0 ) ;
33+ } )
34+ } )
35+
36+ describe ( "Map with one object" , ( ) => {
37+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
38+ arrayStringMap . set ( sampleArray1 , sampleValue1 ) ;
39+ it ( "Map size is 1" , ( ) => {
40+ assert ( arrayStringMap . size === 1 ) ;
41+ } )
42+ it ( "Map get returns value" , ( ) => {
43+ assert ( arrayStringMap . get ( sampleArray1 ) === sampleValue1 ) ;
44+ } )
45+ it ( "Alternate array with same values returns same value" , ( ) => {
46+ assert ( arrayStringMap . get ( sampleArray2 ) === sampleValue1 ) ;
47+ } )
48+ it ( "Map has returns true" , ( ) => {
49+ assert ( arrayStringMap . has ( sampleArray1 ) ) ;
50+ } )
51+ it ( "Map entries returns array with one object" , ( ) => {
52+ assert ( [ ...arrayStringMap . entries ( ) ] . length === 1 , "Array length is 1" ) ;
53+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray1 , "Array is sampleArray1" ) ;
54+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue1 , "Value is sampleValue1" ) ;
55+ } )
56+ it ( "Map keys returns array with one object" , ( ) => {
57+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 1 , "Array length is 1" ) ;
58+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray1 , "Array is sampleArray1" ) ;
59+ } )
60+ it ( "Map values returns array with one value" , ( ) => {
61+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 , "Array length is 1" ) ;
62+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === sampleValue1 , "Value is sampleValue1" ) ;
63+ } )
64+ it ( "Map uses proper separator underneath" , ( ) => {
65+ //@ts -ignore - this is a test, and we need to make sure the underlying map
66+ // works as expected
67+ assert ( [ ...arrayStringMap . _map . keys ( ) ] [ 0 ] . includes ( "\u200b" ) , "Separator is present" ) ;
68+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
69+ // works as expected
70+ assert ( [ ...arrayStringMap . _converterInfo . values ( ) ] [ 0 ] === sampleArray1 , "Converter info is sampleArray1" ) ;
71+ } )
72+ it ( "Clearing map removes object" , ( ) => {
73+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
74+ copiedMap . clear ( ) ;
75+ assert ( copiedMap . size === 0 , "Map size is 0" ) ;
76+ assert ( copiedMap . get ( sampleArray1 ) === undefined , "Map get returns undefined" ) ;
77+ assert ( ! copiedMap . has ( sampleArray1 ) , "Map has returns false" ) ;
78+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 , "Map entries returns empty array" ) ;
79+ //@ts -ignore - this is a test, and we need to make sure the underlying map
80+ // works as expected
81+ assert ( copiedMap . _map . size === 0 , "Data map size is 0" ) ;
82+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
83+ // works as expected
84+ assert ( copiedMap . _converterInfo . size === 0 , "Converter map size is 0" ) ;
85+ } )
86+ it ( "Deleting entry from map removes object" , ( ) => {
87+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
88+ copiedMap . delete ( sampleArray1 ) ;
89+ assert ( copiedMap . size === 0 , "Map size is 0" ) ;
90+ assert ( copiedMap . get ( sampleArray1 ) === undefined , "Map get returns undefined" ) ;
91+ assert ( ! copiedMap . has ( sampleArray1 ) , "Map has returns false" ) ;
92+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 , "Map entries returns empty array" ) ;
93+ //@ts -ignore - this is a test, and we need to make sure the underlying map
94+ // works as expected
95+ assert ( copiedMap . _map . size === 0 , "Data map size is 0" ) ;
96+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
97+ // works as expected
98+ assert ( copiedMap . _converterInfo . size === 0 , "Converter map size is 0" ) ;
99+ } )
100+ } )
101+
102+ describe ( "Map with one object and different separator" , ( ) => {
103+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
104+ arrayStringMap . set ( sampleArray1 , sampleValue1 ) ;
105+ it ( "Map uses proper encoding underneath" , ( ) => {
106+ //@ts -ignore - this is a test, and we need to make sure the underlying item
107+ // works as expected
108+ assert ( [ ...arrayStringMap . _map . keys ( ) ] [ 0 ] . includes ( ":" ) , "Separator is present" ) ;
109+ } )
110+ } )
111+
112+ describe ( "Map with one object and alternate array" , ( ) => {
113+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
114+ arrayStringMap . set ( sampleArray1 , sampleValue1 ) ;
115+ arrayStringMap . set ( sampleArray2 , sampleValue2 ) ;
116+ it ( "Map size is 1" , ( ) => {
117+ assert ( arrayStringMap . size === 1 ) ;
118+ } )
119+ it ( "Map get returns value" , ( ) => {
120+ assert ( arrayStringMap . get ( sampleArray2 ) === sampleValue2 ) ;
121+ } )
122+ it ( "Alternate array with same values returns same value" , ( ) => {
123+ assert ( arrayStringMap . get ( sampleArray1 ) === sampleValue2 ) ;
124+ } )
125+ it ( "Map has returns true" , ( ) => {
126+ assert ( arrayStringMap . has ( sampleArray2 ) ) ;
127+ } )
128+ it ( "Map entries returns array with one object" , ( ) => {
129+ assert ( [ ...arrayStringMap . entries ( ) ] . length === 1 , "Array length is 1" ) ;
130+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray2 , "Array is sampleArray2" ) ;
131+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue2 , "Value is sampleValue2" ) ;
132+ } )
133+ it ( "Map keys returns array with one object" , ( ) => {
134+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 1 , "Array length is 1" ) ;
135+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray2 , "Array is sampleArray2" ) ;
136+ } )
137+ it ( "Map values returns array with one value" , ( ) => {
138+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 , "Array length is 1" ) ;
139+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === sampleValue2 , "Value is sampleValue2" ) ;
140+ } )
141+ } )
142+
143+ describe ( "Map with two objects" , ( ) => {
144+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
145+ arrayStringMap . set ( sampleArray1 , sampleValue1 ) ;
146+ arrayStringMap . set ( sampleArray3 , sampleValue2 ) ;
147+ it ( "Map size is 2" , ( ) => {
148+ assert ( arrayStringMap . size === 2 ) ;
149+ } )
150+ it ( "Map get returns value" , ( ) => {
151+ assert ( arrayStringMap . get ( sampleArray1 ) === sampleValue1 , "Value is sampleValue1" ) ;
152+ assert ( arrayStringMap . get ( sampleArray3 ) === sampleValue2 , "Value is sampleValue2" ) ;
153+ } )
154+ it ( "Alternate array with same values returns same value" , ( ) => {
155+ assert ( arrayStringMap . get ( sampleArray2 ) === sampleValue1 , "Value is sampleValue1" ) ;
156+ assert ( arrayStringMap . get ( sampleArray3 ) !== sampleValue1 , "Value is not sampleValue1" ) ;
157+ } )
158+ it ( "Map has returns true" , ( ) => {
159+ assert ( arrayStringMap . has ( sampleArray1 ) , "Has for sampleArray1 returns true" ) ;
160+ assert ( arrayStringMap . has ( sampleArray2 ) , "Has for sampleArray2 returns true" ) ;
161+ } )
162+ it ( "Map entries returns array with two objects" , ( ) => {
163+ assert ( [ ...arrayStringMap . entries ( ) ] . length === 2 , "Array length is 2" ) ;
164+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray1 , "Array is sampleArray1" ) ;
165+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue1 , "Value is sampleValue1" ) ;
166+ assert ( [ ...arrayStringMap . entries ( ) ] [ 1 ] [ 0 ] === sampleArray3 , "Array is sampleArray3" ) ;
167+ assert ( [ ...arrayStringMap . entries ( ) ] [ 1 ] [ 1 ] === sampleValue2 , "Value is sampleValue2" ) ;
168+ } )
169+ it ( "Map keys returns array with two objects" , ( ) => {
170+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 2 , "Array length is 2" ) ;
171+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray1 , "Array is sampleArray1" ) ;
172+ assert ( [ ...arrayStringMap . keys ( ) ] [ 1 ] === sampleArray3 , "Array is sampleArray3" ) ;
173+ } )
174+ it ( "Map values returns array with two values" , ( ) => {
175+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 , "Array length is 1" ) ;
176+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === sampleValue1 , "Value is sampleValue1" ) ;
177+ } )
178+ it ( "Map uses proper separator underneath" , ( ) => {
179+ //@ts -ignore - this is a test, and we need to make sure the underlying map
180+ // works as expected
181+ assert ( [ ...arrayStringMap . _map . keys ( ) ] [ 0 ] . includes ( "\u200b" ) , "Separator is present in item 0" ) ;
182+ //@ts -ignore - this is a test, and we need to make sure the underlying map
183+ // works as expected
184+ assert ( [ ...arrayStringMap . _map . keys ( ) ] [ 1 ] . includes ( "\u200b" ) , "Separator is present in item 1" ) ;
185+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
186+ // works as expected
187+ assert ( [ ...arrayStringMap . _converterInfo . values ( ) ] [ 0 ] === sampleArray1 , "Converter info is sampleArray1 for item 0" ) ;
188+ //@ts -ignore - this is a test, and we need to make sure the underlying map
189+ // works as expected
190+ assert ( [ ...arrayStringMap . _converterInfo . values ( ) ] [ 1 ] === sampleArray3 , "Converter info is sampleArray3 for item 1" ) ;
191+ } )
192+ it ( "Clearing map removes all objects" , ( ) => {
193+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
194+ copiedMap . clear ( ) ;
195+ assert ( copiedMap . size === 0 , "Map size is 0" ) ;
196+ assert ( copiedMap . get ( sampleArray1 ) === undefined , "Map get returns undefined for sampleArray1" ) ;
197+ assert ( copiedMap . get ( sampleArray3 ) === undefined , "Map get returns undefined for sampleArray3" ) ;
198+ assert ( ! copiedMap . has ( sampleArray1 ) , "Map has returns false for sampleArray1" ) ;
199+ assert ( ! copiedMap . has ( sampleArray3 ) , "Map has returns false for sampleArray3" ) ;
200+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 , "Map entries returns empty array" ) ;
201+ //@ts -ignore - this is a test, and we need to make sure the underlying map
202+ // works as expected
203+ assert ( copiedMap . _map . size === 0 , "Data map size is 0" ) ;
204+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
205+ // works as expected
206+ assert ( copiedMap . _converterInfo . size === 0 , "Converter map size is 0" ) ;
207+ } )
208+ it ( "Deleting entry from map removes object but keeps other object" , ( ) => {
209+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
210+ copiedMap . delete ( sampleArray1 ) ;
211+ assert ( copiedMap . size === 1 , "Map size is 1" ) ;
212+ assert ( copiedMap . get ( sampleArray1 ) === undefined , "Map get returns undefined for sampleArray1" ) ;
213+ assert ( copiedMap . get ( sampleArray3 ) === sampleValue2 , "Map get returns sampleValue2 for sampleArray3" ) ;
214+ assert ( ! copiedMap . has ( sampleArray1 ) , "Map has returns false for sampleArray1" ) ;
215+ assert ( copiedMap . has ( sampleArray3 ) , "Map has returns true for sampleArray3" ) ;
216+ assert ( [ ...copiedMap . entries ( ) ] . length === 1 , "Map entries returns array with one object" ) ;
217+ //@ts -ignore - this is a test, and we need to make sure the underlying map
218+ // works as expected
219+ assert ( copiedMap . _map . size === 1 , "Data map size is 1" ) ;
220+ //@ts -ignore - this is a test, and we need to make sure the underlying encoding map
221+ // works as expected
222+ assert ( copiedMap . _converterInfo . size === 1 , "Converter map size is 1" ) ;
223+ } )
224+ } )