@@ -200,6 +200,9 @@ describe('utils spec', function () {
200
200
201
201
expect ( sm . get ( 'two' ) ) . to . be . undefined ( )
202
202
expect ( sm . size ) . to . eql ( 2 )
203
+
204
+ sm . delete ( 'two' )
205
+ expect ( sm . size ) . to . eql ( 2 )
203
206
} )
204
207
205
208
it ( 'clear' , ( ) => {
@@ -235,26 +238,100 @@ describe('utils spec', function () {
235
238
expect ( collected ) . to . eql ( [ ...sm ] )
236
239
} )
237
240
238
- it ( 'custom order' , ( ) => {
239
- const sm = new SortedMap ( [ ] , ( a , b ) => b [ 1 ] . priority - a [ 1 ] . priority )
240
-
241
- const data1 = { k : 'v1' , priority : 1 }
242
- const data2 = { k : 'v2' , priority : 3 }
243
- const data3 = { k : 'v3' , priority : 2 }
244
- sm . set ( 'one' , data1 )
245
- sm . set ( 'two' , data2 )
246
- sm . set ( 'three' , data3 )
247
-
248
- expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'two' , 'three' , 'one' ] )
249
- expect ( [ ...sm . values ( ) ] . map ( v => v . k ) ) . to . eql ( [ 'v2' , 'v3' , 'v1' ] )
250
-
251
- // After changing data that affects the sort order, need to call update
252
- // to actually trigger the sort
253
- data3 . priority = 5
254
- sm . update ( 'three' )
255
-
256
- expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'three' , 'two' , 'one' ] )
257
- expect ( [ ...sm . values ( ) ] . map ( v => v . k ) ) . to . eql ( [ 'v3' , 'v2' , 'v1' ] )
241
+ describe ( 'custom order' , ( ) => {
242
+ const prioritySort = ( a , b ) => b [ 1 ] . priority - a [ 1 ] . priority
243
+
244
+ it ( 'forward' , ( ) => {
245
+ const sm = new SortedMap ( [
246
+ [ 'low' , { priority : 1 } ] ,
247
+ [ 'high' , { priority : 2 } ]
248
+ ] , prioritySort )
249
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'low' ] )
250
+ } )
251
+
252
+ it ( 'backward' , ( ) => {
253
+ const sm = new SortedMap ( [
254
+ [ 'high' , { priority : 2 } ] ,
255
+ [ 'low' , { priority : 1 } ]
256
+ ] , prioritySort )
257
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'low' ] )
258
+ } )
259
+
260
+ it ( 'insert start' , ( ) => {
261
+ const sm = new SortedMap ( [
262
+ [ 'mid' , { priority : 2 } ] ,
263
+ [ 'low' , { priority : 1 } ] ,
264
+ [ 'high' , { priority : 3 } ]
265
+ ] , prioritySort )
266
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'mid' , 'low' ] )
267
+ } )
268
+
269
+ it ( 'insert end' , ( ) => {
270
+ const sm = new SortedMap ( [
271
+ [ 'low' , { priority : 1 } ] ,
272
+ [ 'mid' , { priority : 2 } ] ,
273
+ [ 'high' , { priority : 3 } ]
274
+ ] , prioritySort )
275
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'mid' , 'low' ] )
276
+ } )
277
+
278
+ it ( 'insert middle' , ( ) => {
279
+ const sm = new SortedMap ( [
280
+ [ 'low' , { priority : 1 } ] ,
281
+ [ 'high' , { priority : 3 } ] ,
282
+ [ 'mid' , { priority : 2 } ]
283
+ ] , prioritySort )
284
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'mid' , 'low' ] )
285
+ } )
286
+
287
+ it ( 'insert same priority start' , ( ) => {
288
+ const sm = new SortedMap ( [
289
+ [ 'low' , { priority : 1 } ] ,
290
+ [ 'high-a' , { priority : 3 } ] ,
291
+ [ 'high-b' , { priority : 3 } ]
292
+ ] , prioritySort )
293
+ expect ( [ ...sm . keys ( ) ] . map ( s => s . substring ( 0 , 4 ) ) ) . to . eql ( [ 'high' , 'high' , 'low' ] )
294
+ } )
295
+
296
+ it ( 'insert same priority end' , ( ) => {
297
+ const sm = new SortedMap ( [
298
+ [ 'hi' , { priority : 3 } ] ,
299
+ [ 'low-a' , { priority : 1 } ] ,
300
+ [ 'low-b' , { priority : 1 } ]
301
+ ] , prioritySort )
302
+ expect ( [ ...sm . keys ( ) ] . map ( s => s . substring ( 0 , 3 ) ) ) . to . eql ( [ 'hi' , 'low' , 'low' ] )
303
+ } )
304
+
305
+ it ( 'insert same key' , ( ) => {
306
+ const sm = new SortedMap ( [
307
+ [ 'low' , { priority : 1 } ] ,
308
+ [ 'high' , { priority : 3 } ] ,
309
+ [ 'high' , { priority : 4 } ]
310
+ ] , prioritySort )
311
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'high' , 'low' ] )
312
+ } )
313
+
314
+ it ( 'update' , ( ) => {
315
+ const sm = new SortedMap ( [ ] , prioritySort )
316
+
317
+ const data1 = { k : 'v1' , priority : 1 }
318
+ const data2 = { k : 'v2' , priority : 3 }
319
+ const data3 = { k : 'v3' , priority : 2 }
320
+ sm . set ( 'one' , data1 )
321
+ sm . set ( 'two' , data2 )
322
+ sm . set ( 'three' , data3 )
323
+
324
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'two' , 'three' , 'one' ] )
325
+ expect ( [ ...sm . values ( ) ] . map ( v => v . k ) ) . to . eql ( [ 'v2' , 'v3' , 'v1' ] )
326
+
327
+ // After changing data that affects the sort order, need to call update
328
+ // to actually trigger the sort
329
+ data3 . priority = 5
330
+ sm . update ( 'three' )
331
+
332
+ expect ( [ ...sm . keys ( ) ] ) . to . eql ( [ 'three' , 'two' , 'one' ] )
333
+ expect ( [ ...sm . values ( ) ] . map ( v => v . k ) ) . to . eql ( [ 'v3' , 'v2' , 'v1' ] )
334
+ } )
258
335
} )
259
336
} )
260
337
} )
0 commit comments