1
1
import { assert } from "chai"
2
2
import ArrayStringMap from "./arrayStringMap.js"
3
3
4
- const sampleArray1 = [ 1 , 2 ]
5
- const sampleArray2 = [ 1 , 2 ]
6
- const sampleArray3 = [ 1 , 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 ]
7
9
const sampleValue1 = 1
8
10
const sampleValue2 = 2
9
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
+
10
20
describe ( "Empty map" , ( ) => {
11
- const arrayStringMap = new ArrayStringMap ( ) ;
21
+ const arrayStringMap = new ArrayStringMap < TwoNumberArray , number > ( ) ;
12
22
it ( "Empty map size is 0" , ( ) => {
13
23
assert ( arrayStringMap . size === 0 ) ;
14
24
} )
@@ -21,4 +31,192 @@ describe("Empty map", () => {
21
31
it ( "Empty map entries returns empty array" , ( ) => {
22
32
assert ( [ ...arrayStringMap . entries ( ) ] . length === 0 ) ;
23
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 ) ;
53
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray1 ) ;
54
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue1 ) ;
55
+ } )
56
+ it ( "Map keys returns array with one object" , ( ) => {
57
+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 1 ) ;
58
+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray1 ) ;
59
+ } )
60
+ it ( "Map values returns array with one value" , ( ) => {
61
+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 ) ;
62
+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === 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" ) ) ;
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 ) ;
71
+ } )
72
+ it ( "Clearing map removes object" , ( ) => {
73
+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
74
+ copiedMap . clear ( ) ;
75
+ assert ( copiedMap . size === 0 ) ;
76
+ assert ( copiedMap . get ( sampleArray1 ) === undefined ) ;
77
+ assert ( ! copiedMap . has ( sampleArray1 ) ) ;
78
+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 ) ;
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 ) ;
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 ) ;
85
+ } )
86
+ it ( "Deleting entry from map removes object" , ( ) => {
87
+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
88
+ copiedMap . delete ( sampleArray1 ) ;
89
+ assert ( copiedMap . size === 0 ) ;
90
+ assert ( copiedMap . get ( sampleArray1 ) === undefined ) ;
91
+ assert ( ! copiedMap . has ( sampleArray1 ) ) ;
92
+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 ) ;
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 ) ;
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 ) ;
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 ( ":" ) ) ;
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 ) ;
130
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray2 ) ;
131
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue2 ) ;
132
+ } )
133
+ it ( "Map keys returns array with one object" , ( ) => {
134
+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 1 ) ;
135
+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray2 ) ;
136
+ } )
137
+ it ( "Map values returns array with one value" , ( ) => {
138
+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 ) ;
139
+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === 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 ) ;
152
+ assert ( arrayStringMap . get ( sampleArray3 ) === sampleValue2 ) ;
153
+ } )
154
+ it ( "Alternate array with same values returns same value" , ( ) => {
155
+ assert ( arrayStringMap . get ( sampleArray2 ) === sampleValue1 ) ;
156
+ assert ( arrayStringMap . get ( sampleArray3 ) !== sampleValue1 ) ;
157
+ } )
158
+ it ( "Map has returns true" , ( ) => {
159
+ assert ( arrayStringMap . has ( sampleArray1 ) ) ;
160
+ assert ( arrayStringMap . has ( sampleArray2 ) ) ;
161
+ } )
162
+ it ( "Map entries returns array with two objects" , ( ) => {
163
+ assert ( [ ...arrayStringMap . entries ( ) ] . length === 2 ) ;
164
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 0 ] === sampleArray1 ) ;
165
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 0 ] [ 1 ] === sampleValue1 ) ;
166
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 1 ] [ 0 ] === sampleArray3 ) ;
167
+ assert ( [ ...arrayStringMap . entries ( ) ] [ 1 ] [ 1 ] === sampleValue2 ) ;
168
+ } )
169
+ it ( "Map keys returns array with two objects" , ( ) => {
170
+ assert ( [ ...arrayStringMap . keys ( ) ] . length === 2 ) ;
171
+ assert ( [ ...arrayStringMap . keys ( ) ] [ 0 ] === sampleArray1 ) ;
172
+ assert ( [ ...arrayStringMap . keys ( ) ] [ 1 ] === sampleArray3 ) ;
173
+ } )
174
+ it ( "Map values returns array with two values" , ( ) => {
175
+ assert ( [ ...arrayStringMap . values ( ) ] . length === 1 ) ;
176
+ assert ( [ ...arrayStringMap . values ( ) ] [ 0 ] === 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" ) ) ;
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" ) ) ;
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 ) ;
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 ) ;
191
+ } )
192
+ it ( "Clearing map removes all objects" , ( ) => {
193
+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
194
+ copiedMap . clear ( ) ;
195
+ assert ( copiedMap . size === 0 ) ;
196
+ assert ( copiedMap . get ( sampleArray1 ) === undefined ) ;
197
+ assert ( ! copiedMap . has ( sampleArray1 ) ) ;
198
+ assert ( [ ...copiedMap . entries ( ) ] . length === 0 ) ;
199
+ // @ts -ignore - this is a test, and we need to make sure the underlying map
200
+ // works as expected
201
+ assert ( copiedMap . _map . size === 0 ) ;
202
+ // @ts -ignore - this is a test, and we need to make sure the underlying encoding map
203
+ // works as expected
204
+ assert ( copiedMap . _converterInfo . size === 0 ) ;
205
+ } )
206
+ it ( "Deleting entry from map removes object but keeps other object" , ( ) => {
207
+ const copiedMap = copyArrayStringMap ( arrayStringMap ) ;
208
+ copiedMap . delete ( sampleArray1 ) ;
209
+ assert ( copiedMap . size === 1 ) ;
210
+ assert ( copiedMap . get ( sampleArray1 ) === undefined ) ;
211
+ assert ( copiedMap . get ( sampleArray3 ) === sampleValue2 ) ;
212
+ assert ( ! copiedMap . has ( sampleArray1 ) ) ;
213
+ assert ( copiedMap . has ( sampleArray3 ) ) ;
214
+ assert ( [ ...copiedMap . entries ( ) ] . length === 1 ) ;
215
+ // @ts -ignore - this is a test, and we need to make sure the underlying map
216
+ // works as expected
217
+ assert ( copiedMap . _map . size === 1 ) ;
218
+ // @ts -ignore - this is a test, and we need to make sure the underlying encoding map
219
+ // works as expected
220
+ assert ( copiedMap . _converterInfo . size === 1 ) ;
221
+ } )
24
222
} )
0 commit comments