Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
var OrderedMap;
15
16
/**
17
* Shared, reusable objects.
18
*/
19
var hasEmptyStringKey = {
20
'thisKeyIsFine': {data: []},
21
'': {thisShouldCauseAFailure: []},
22
'thisKeyIsAlsoFine': {data: []}
23
};
24
25
/**
26
* Used as map/forEach callback.
27
*/
28
var duplicate = function(itm, key, count) {
29
return {
30
uniqueID: itm.uniqueID,
31
val: itm.val + key + count + this.justToTestScope
32
};
33
};
34
35
// Should not be allowed - because then null/'null' become impossible to
36
// distinguish. Every key MUST be a string period!
37
var hasNullAndUndefStringKey = [
38
{uniqueID: 'undefined', val: 'thisIsUndefined'},
39
{uniqueID: 'null', val: 'thisIsNull'}
40
];
41
var hasNullKey = [
42
{uniqueID: 'thisKeyIsFine', data: []},
43
{uniqueID: 'thisKeyIsAlsoFine', data: []},
44
{uniqueID: null, data: []}
45
];
46
47
var hasObjectKey = [
48
{uniqueID: 'thisKeyIsFine', data: []},
49
{uniqueID: 'thisKeyIsAlsoFine', data: []},
50
{uniqueID: {}, data: []}
51
];
52
53
var hasArrayKey = [
54
{uniqueID: 'thisKeyIsFine', data: []},
55
{uniqueID: 'thisKeyIsAlsoFine', data: []},
56
{uniqueID: [], data: []}
57
];
58
59
// This should be allowed
60
var hasNullStringKey = [
61
{uniqueID: 'thisKeyIsFine', data: []},
62
{uniqueID: 'thisKeyIsAlsoFine', data: []},
63
{uniqueID: 'null', data: []}
64
];
65
66
var hasUndefinedKey = [
67
{uniqueID: 'thisKeyIsFine', data: []},
68
{uniqueID: 'thisKeyIsAlsoFine', data: []},
69
{uniqueID: undefined, data: []}
70
];
71
72
var hasUndefinedStringKey = [
73
{uniqueID: 'thisKeyIsFine', data: []},
74
{uniqueID: 'thisKeyIsAlsoFine', data: []},
75
{uniqueID: 'undefined', data: []}
76
];
77
78
var hasPositiveNumericKey = [
79
{uniqueID: 'notANumber', data: []},
80
{uniqueID: '5', data: []},
81
{uniqueID: 'notAnotherNumber',data: []}
82
];
83
84
var hasZeroStringKey = [
85
{uniqueID: 'greg', data: 'grego'},
86
{uniqueID: '0', data: '0o'},
87
{uniqueID: 'tom', data: 'tomo'}
88
];
89
90
var hasZeroNumberKey = [
91
{uniqueID: 'greg', data: 'grego'},
92
{uniqueID: 0, data: '0o'},
93
{uniqueID: 'tom', data: 'tomo'}
94
];
95
96
var hasAllNumericStringKeys = [
97
{uniqueID: '0', name: 'Gregory'},
98
{uniqueID: '2', name: 'James'},
99
{uniqueID: '1', name: 'Tom'}
100
];
101
102
var hasAllNumericKeys = [
103
{uniqueID: 0, name: 'Gregory'},
104
{uniqueID: 2, name: 'James'},
105
{uniqueID: 1, name: 'Tom'}
106
];
107
108
var hasAllValidKeys = [
109
{uniqueID: 'keyOne', value: 'valueOne'},
110
{uniqueID: 'keyTwo', value: 'valueTwo'}
111
];
112
113
var hasDuplicateKeys = [
114
{uniqueID: 'keyOne', value: 'valueOne'},
115
{uniqueID: 'keyTwo', value: 'valueTwo'},
116
{uniqueID: 'keyOne', value: 'valueThree'}
117
];
118
119
var idEntities = [
120
{uniqueID: 'greg', name: 'Gregory'},
121
{uniqueID: 'james', name: 'James'},
122
{uniqueID: 'tom', name: 'Tom'}
123
];
124
125
var hasEmptyKey = [
126
{uniqueID: 'greg', name: 'Gregory'},
127
{uniqueID: '', name: 'James'},
128
{uniqueID: 'tom', name: 'Tom'}
129
];
130
131
var extractUniqueID = function(entity) {
132
return entity.uniqueID;
133
};
134
135
describe('OrderedMap', function() {
136
beforeEach(function() {
137
require('mock-modules').dumpCache();
138
OrderedMap = require('OrderedMap');
139
});
140
141
it('should create according to simple object with keys', function() {
142
OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);
143
// Iterate over and ensure key order.
144
});
145
146
it('should create from array when providing an identity CB', function() {
147
expect(function() {
148
OrderedMap.fromArray(idEntities, extractUniqueID);
149
}).not.toThrow();
150
});
151
152
it('should throw if constructing from Array without identity CB', function() {
153
OrderedMap.fromArray(idEntities, extractUniqueID);
154
// Iterate and ensure key order
155
});
156
157
it('should not throw when fromArray extracts a numeric key', function() {
158
expect(function() {
159
OrderedMap.fromArray(hasPositiveNumericKey, extractUniqueID);
160
}).not.toThrow();
161
162
});
163
164
it('should throw when any key is the empty string', function() {
165
expect(function() {
166
OrderedMap.fromArray(hasEmptyKey, extractUniqueID);
167
}).toThrow();
168
});
169
170
it('should not throw when a key is the string "undefined" or "null"',
171
function() {
172
var om = OrderedMap.fromArray(hasNullAndUndefStringKey, extractUniqueID);
173
expect(om.length).toBe(2);
174
expect(om.indexOfKey('undefined')).toBe(0);
175
expect(om.indexOfKey('null')).toBe(1);
176
expect(om.keyAfter('undefined')).toBe('null');
177
expect(om.keyAfter('null')).toBe(undefined);
178
expect(om.keyBefore('undefined')).toBe(undefined);
179
expect(om.has('undefined')).toBe(true);
180
expect(om.has('null')).toBe(true);
181
expect(om.get('undefined').val).toBe('thisIsUndefined');
182
expect(om.get('null').val).toBe('thisIsNull');
183
});
184
185
186
/**
187
* Numeric keys are cast to strings.
188
*/
189
it('should not throw when a key is the number zero', function() {
190
var om = OrderedMap.fromArray(hasZeroNumberKey, extractUniqueID);
191
expect(om.length).toBe(3);
192
expect(om.indexOfKey('0')).toBe(1);
193
expect(om.indexOfKey(0)).toBe(1);
194
});
195
196
it('should throw when any key is falsey', function() {
197
expect(function() {
198
OrderedMap.fromArray(hasEmptyStringKey, extractUniqueID);
199
}).toThrow();
200
201
expect(function() {
202
OrderedMap.fromArray(hasNullKey, extractUniqueID);
203
}).toThrow();
204
205
expect(function() {
206
OrderedMap.fromArray(hasUndefinedKey, extractUniqueID);
207
}).toThrow();
208
});
209
210
it('should not throw on string keys "undefined/null"', function() {
211
expect(function() {
212
OrderedMap.fromArray(hasNullStringKey, extractUniqueID);
213
}).not.toThrow();
214
215
expect(function() {
216
OrderedMap.fromArray(hasUndefinedStringKey, extractUniqueID);
217
}).not.toThrow();
218
});
219
220
it('should throw on extracting keys that are not strings/nums', function() {
221
expect(function() {
222
OrderedMap.fromArray(hasObjectKey, extractUniqueID);
223
}).toThrow();
224
225
expect(function() {
226
OrderedMap.fromArray(hasArrayKey, extractUniqueID);
227
}).toThrow();
228
});
229
230
it('should throw if instantiating with duplicate key', function() {
231
expect(function() {
232
OrderedMap.fromArray(hasDuplicateKeys, extractUniqueID);
233
}).toThrow();
234
});
235
236
it('should not throw when a key is the string "0"', function() {
237
var verifyOM = function(om) {
238
expect(om.length).toBe(3);
239
expect(om.indexOfKey('greg')).toBe(0);
240
expect(om.indexOfKey('0')).toBe(1);
241
expect(om.indexOfKey(0)).toBe(1); // Casts on writes and reads.
242
expect(om.indexOfKey('tom')).toBe(2);
243
expect(om.keyAfter('greg')).toBe('0');
244
expect(om.keyAfter('0')).toBe('tom');
245
expect(om.keyAfter(0)).toBe('tom');
246
expect(om.keyAfter('tom')).toBe(undefined);
247
expect(om.keyBefore('greg')).toBe(undefined);
248
expect(om.keyBefore(0)).toBe('greg');
249
expect(om.keyBefore('0')).toBe('greg');
250
expect(om.keyBefore('tom')).toBe('0');
251
expect(om.has('undefined')).toBe(false);
252
expect(om.has(0)).toBe(true);
253
expect(om.has('0')).toBe(true);
254
};
255
var om = OrderedMap.fromArray(hasZeroStringKey, extractUniqueID);
256
verifyOM(om);
257
om = OrderedMap.fromArray(hasZeroNumberKey, extractUniqueID);
258
verifyOM(om);
259
});
260
261
it('should throw when getting invalid public key', function() {
262
var om = OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);
263
expect(function() {
264
om.has(undefined);
265
}).toThrow();
266
expect(function() {
267
om.get(undefined);
268
}).toThrow();
269
expect(function() {
270
om.has(null);
271
}).toThrow();
272
expect(function() {
273
om.get(null);
274
}).toThrow();
275
expect(function() {
276
om.has('');
277
}).toThrow();
278
expect(function() {
279
om.get('');
280
}).toThrow();
281
});
282
283
it('should throw when any key is falsey', function() {
284
expect(function() {
285
OrderedMap.fromArray(hasEmptyStringKey, extractUniqueID);
286
}).toThrow();
287
288
expect(function() {
289
OrderedMap.fromArray(hasNullKey, extractUniqueID);
290
}).toThrow();
291
292
expect(function() {
293
OrderedMap.fromArray(hasUndefinedKey, extractUniqueID);
294
}).toThrow();
295
});
296
297
298
it('should throw when fromArray is passed crazy args', function() {
299
// Test passing another OrderedMap (when it expects a plain object.)
300
// This is probably not what you meant to do! We should error.
301
var validOM = OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);
302
expect(function() {
303
OrderedMap.fromArray({uniqueID: 'asdf'}, extractUniqueID);
304
}).toThrow();
305
expect(function() {
306
OrderedMap.fromArray(validOM, extractUniqueID);
307
}).toThrow();
308
});
309
310
it('should throw when fromArray is passed crazy things', function() {
311
expect(function() {
312
OrderedMap.fromArray(null, extractUniqueID);
313
}).toThrow();
314
expect(function() {
315
OrderedMap.fromArray('stringgg', extractUniqueID);
316
}).toThrow();
317
expect(function() {
318
OrderedMap.fromArray(undefined, extractUniqueID);
319
}).toThrow();
320
expect(function() {
321
OrderedMap.fromArray(new Date(), extractUniqueID);
322
}).toThrow();
323
expect(function() {
324
OrderedMap.fromArray({}, extractUniqueID);
325
}).toThrow();
326
327
// Test failure without extractor
328
expect(function() {
329
OrderedMap.fromArray(idEntities);
330
}).toThrow();
331
expect(function() {
332
OrderedMap.fromArray(idEntities, extractUniqueID);
333
}).not.toThrow();
334
});
335
336
// Testing methods that accept other `OrderedMap`s.
337
it('should throw when from/merge is passed an non-OrderedMap.', function() {
338
// Test passing an array to construction.
339
expect(function() {
340
OrderedMap.from(idEntities, extractUniqueID);
341
}).toThrow();
342
343
// Test passing an array to merge.
344
expect(function() {
345
OrderedMap.fromArray(idEntities, extractUniqueID)
346
.merge(idEntities, extractUniqueID);
347
}).toThrow();
348
349
350
// Test passing a plain object to merge.
351
expect(function() {
352
OrderedMap.fromArray(
353
idEntities,
354
extractUniqueID
355
).merge({blah: 'willFail'});
356
}).toThrow();
357
});
358
359
it('should throw when accessing key before/after of non-key', function() {
360
var om = OrderedMap.fromArray([
361
{uniqueID: 'first'},
362
{uniqueID: 'two'}], extractUniqueID
363
);
364
expect(function() {
365
om.keyBefore('dog');
366
}).toThrow();
367
expect(function() {
368
om.keyAfter('cat');
369
}).toThrow();
370
expect(function() {
371
om.keyAfter(null);
372
}).toThrow();
373
expect(function() {
374
om.keyAfter(undefined);
375
}).toThrow();
376
});
377
378
it('should throw passing invalid/not-present-keys to before/after',
379
function() {
380
var om = OrderedMap.fromArray([
381
{uniqueID: 'one', val: 'first'},
382
{uniqueID: 'two', val: 'second'},
383
{uniqueID: 'three', val: 'third'},
384
{uniqueID: 'four', val: 'fourth'}
385
], extractUniqueID);
386
387
expect(function() {
388
om.keyBefore('');
389
}).toThrow();
390
expect(function() {
391
om.keyBefore(null);
392
}).toThrow();
393
expect(function() {
394
om.keyBefore(undefined);
395
}).toThrow();
396
expect(function() {
397
om.keyBefore('notInTheOrderedMap!');
398
}).toThrow();
399
400
expect(function() {
401
om.keyAfter('');
402
}).toThrow();
403
expect(function() {
404
om.keyAfter(null);
405
}).toThrow();
406
expect(function() {
407
om.keyAfter(undefined);
408
}).toThrow();
409
expect(function() {
410
om.keyAfter('notInTheOrderedMap!');
411
}).toThrow();
412
413
expect(function() {
414
om.nthKeyAfter('', 1);
415
}).toThrow();
416
expect(function() {
417
om.nthKeyAfter(null, 1);
418
}).toThrow();
419
expect(function() {
420
om.nthKeyAfter(undefined, 1);
421
}).toThrow();
422
expect(function() {
423
om.nthKeyAfter('notInTheOrderedMap!', 1);
424
}).toThrow();
425
426
expect(function() {
427
om.nthKeyBefore('', 1);
428
}).toThrow();
429
expect(function() {
430
om.nthKeyBefore(null, 1);
431
}).toThrow();
432
expect(function() {
433
om.nthKeyBefore(undefined, 1);
434
}).toThrow();
435
expect(function() {
436
om.nthKeyBefore('notInTheOrderedMap!', 1);
437
}).toThrow();
438
});
439
440
it('should correctly determine the nth key after before', function() {
441
var om = OrderedMap.fromArray([
442
{uniqueID: 'one', val: 'first'},
443
{uniqueID: 'two', val: 'second'},
444
{uniqueID: 'three', val: 'third'},
445
{uniqueID: 'four', val: 'fourth'}
446
], extractUniqueID);
447
expect(om.keyBefore('one')).toBe(undefined); // first key
448
expect(om.keyBefore('two')).toBe('one');
449
expect(om.keyBefore('three')).toBe('two');
450
expect(om.keyBefore('four')).toBe('three');
451
452
expect(om.keyAfter('one')).toBe('two'); // first key
453
expect(om.keyAfter('two')).toBe('three');
454
expect(om.keyAfter('three')).toBe('four');
455
expect(om.keyAfter('four')).toBe(undefined);
456
457
expect(om.nthKeyBefore('one', 0)).toBe('one'); // first key
458
expect(om.nthKeyBefore('one', 1)).toBe(undefined);
459
expect(om.nthKeyBefore('one', 2)).toBe(undefined);
460
expect(om.nthKeyBefore('two', 0)).toBe('two');
461
expect(om.nthKeyBefore('two', 1)).toBe('one');
462
expect(om.nthKeyBefore('four', 0)).toBe('four');
463
expect(om.nthKeyBefore('four', 1)).toBe('three');
464
465
expect(om.nthKeyAfter('one', 0)).toBe('one');
466
expect(om.nthKeyAfter('one', 1)).toBe('two');
467
expect(om.nthKeyAfter('one', 2)).toBe('three');
468
expect(om.nthKeyAfter('two', 0)).toBe('two');
469
expect(om.nthKeyAfter('two', 1)).toBe('three');
470
expect(om.nthKeyAfter('four', 0)).toBe('four');
471
expect(om.nthKeyAfter('four', 1)).toBe(undefined);
472
});
473
474
it('should compute key indices correctly', function() {
475
var om = OrderedMap.fromArray([
476
{uniqueID: 'one', val: 'first'},
477
{uniqueID: 'two', val: 'second'}
478
], extractUniqueID);
479
expect(om.keyAtIndex(0)).toBe('one');
480
expect(om.keyAtIndex(1)).toBe('two');
481
expect(om.keyAtIndex(2)).toBe(undefined);
482
expect(om.indexOfKey('one')).toBe(0);
483
expect(om.indexOfKey('two')).toBe(1);
484
expect(om.indexOfKey('nope')).toBe(undefined);
485
expect(function() {
486
om.indexOfKey(null);
487
}).toThrow();
488
expect(function() {
489
om.indexOfKey(undefined);
490
}).toThrow();
491
expect(function() {
492
om.indexOfKey(''); // Empty key is not allowed
493
}).toThrow();
494
});
495
496
it('should compute indices on array that extracted numeric ids', function() {
497
var om = OrderedMap.fromArray(hasZeroStringKey, extractUniqueID);
498
expect(om.keyAtIndex(0)).toBe('greg');
499
expect(om.keyAtIndex(1)).toBe('0');
500
expect(om.keyAtIndex(2)).toBe('tom');
501
expect(om.indexOfKey('greg')).toBe(0);
502
expect(om.indexOfKey('0')).toBe(1);
503
expect(om.indexOfKey('tom')).toBe(2);
504
505
506
var verifyNumericKeys = function(om) {
507
expect(om.keyAtIndex(0)).toBe('0');
508
expect(om.keyAtIndex(1)).toBe('2');
509
expect(om.keyAtIndex(2)).toBe('1');
510
expect(om.indexOfKey('0')).toBe(0);
511
expect(om.indexOfKey('2')).toBe(1); // Proove these are not ordered by
512
expect(om.indexOfKey('1')).toBe(2); // their keys
513
};
514
var omStringNumberKeys =
515
OrderedMap.fromArray(hasAllNumericStringKeys, extractUniqueID);
516
verifyNumericKeys(omStringNumberKeys);
517
var omNumericKeys =
518
OrderedMap.fromArray(hasAllNumericKeys, extractUniqueID);
519
verifyNumericKeys(omNumericKeys);
520
});
521
522
it('should compute indices on mutually exclusive merge', function() {
523
var om = OrderedMap.fromArray([
524
{uniqueID: 'one', val: 'first'},
525
{uniqueID: 'two', val: 'second'}
526
], extractUniqueID);
527
var om2 = OrderedMap.fromArray([
528
{uniqueID: 'three', val: 'third'}
529
], extractUniqueID);
530
var res = om.merge(om2);
531
532
expect(res.length).toBe(3);
533
534
expect(res.keyAtIndex(0)).toBe('one');
535
expect(res.keyAtIndex(1)).toBe('two');
536
expect(res.keyAtIndex(2)).toBe('three');
537
expect(res.keyAtIndex(3)).toBe(undefined);
538
539
expect(res.indexOfKey('one')).toBe(0);
540
expect(res.indexOfKey('two')).toBe(1);
541
expect(res.indexOfKey('three')).toBe(2);
542
expect(res.indexOfKey('dog')).toBe(undefined);
543
544
expect(res.has('one')).toBe(true);
545
expect(res.has('two')).toBe(true);
546
expect(res.has('three')).toBe(true);
547
expect(res.has('dog')).toBe(false);
548
549
expect(res.get('one').val).toBe('first');
550
expect(res.get('two').val).toBe('second');
551
expect(res.get('three').val).toBe('third');
552
expect(res.get('dog')).toBe(undefined);
553
});
554
555
it('should compute indices on intersected merge', function() {
556
var oneTwo = OrderedMap.fromArray([
557
{uniqueID: 'one', val: 'first'},
558
{uniqueID: 'two', val: 'secondOM1'}
559
], extractUniqueID);
560
561
var testOneTwoMergedWithTwoThree = function(res) {
562
expect(res.length).toBe(3);
563
expect(res.keyAtIndex(0)).toBe('one');
564
expect(res.keyAtIndex(1)).toBe('two');
565
expect(res.keyAtIndex(2)).toBe('three');
566
expect(res.keyAtIndex(3)).toBe(undefined);
567
expect(res.indexOfKey('one')).toBe(0);
568
expect(res.indexOfKey('two')).toBe(1);
569
expect(res.indexOfKey('three')).toBe(2);
570
expect(res.indexOfKey('dog')).toBe(undefined);
571
expect(res.has('one')).toBe(true);
572
expect(res.has('two')).toBe(true);
573
expect(res.has('three')).toBe(true);
574
expect(res.has('dog')).toBe(false);
575
expect(res.get('one').val).toBe('first');
576
expect(res.get('two').val).toBe('secondOM2');
577
expect(res.get('three').val).toBe('third');
578
expect(res.get('dog')).toBe(undefined);
579
};
580
581
var result =
582
oneTwo.merge(OrderedMap.fromArray([
583
{uniqueID: 'two', val: 'secondOM2'},
584
{uniqueID: 'three', val: 'third'}
585
], extractUniqueID));
586
testOneTwoMergedWithTwoThree(result);
587
588
// Everything should be exactly as before, since the ordering of `two` was
589
// already determined by `om`.
590
result = oneTwo.merge(
591
OrderedMap.fromArray([
592
{uniqueID: 'three', val: 'third'},
593
{uniqueID: 'two', val:'secondOM2'}
594
], extractUniqueID)
595
);
596
testOneTwoMergedWithTwoThree(result);
597
598
599
var testTwoThreeMergedWithOneTwo = function(res) {
600
expect(res.length).toBe(3);
601
expect(res.keyAtIndex(0)).toBe('two');
602
expect(res.keyAtIndex(1)).toBe('three');
603
expect(res.keyAtIndex(2)).toBe('one');
604
expect(res.keyAtIndex(3)).toBe(undefined);
605
expect(res.indexOfKey('two')).toBe(0);
606
expect(res.indexOfKey('three')).toBe(1);
607
expect(res.indexOfKey('one')).toBe(2);
608
expect(res.indexOfKey('cat')).toBe(undefined);
609
expect(res.has('two')).toBe(true);
610
expect(res.has('three')).toBe(true);
611
expect(res.has('one')).toBe(true);
612
expect(res.has('dog')).toBe(false);
613
expect(res.get('one').val).toBe('first');
614
expect(res.get('two').val).toBe('secondOM1');
615
expect(res.get('three').val).toBe('third');
616
expect(res.get('dog')).toBe(undefined);
617
};
618
result = OrderedMap.fromArray([
619
{uniqueID: 'two', val: 'secondOM2'},
620
{uniqueID: 'three', val: 'third'}
621
], extractUniqueID).merge(oneTwo);
622
testTwoThreeMergedWithOneTwo(result);
623
624
});
625
626
it('should merge mutually exclusive keys to the end.', function() {
627
var om = OrderedMap.fromArray([
628
{uniqueID: 'one', val: 'first'},
629
{uniqueID: 'two', val: 'second'}
630
], extractUniqueID);
631
var om2 = OrderedMap.fromArray([
632
{uniqueID: 'three', val: 'first'},
633
{uniqueID: 'four', val: 'second'}
634
], extractUniqueID);
635
var res = om.merge(om2);
636
expect(res.length).toBe(4);
637
638
});
639
640
it('should map correctly', function() {
641
var om = OrderedMap.fromArray([
642
{uniqueID: 'x', val: 'xx'},
643
{uniqueID: 'y', val: 'yy'},
644
{uniqueID: 'z', val: 'zz'}
645
], extractUniqueID);
646
var scope = {justToTestScope: 'justTestingScope'};
647
var verifyResult = function(omResult) {
648
expect(omResult.length).toBe(3);
649
expect(omResult.keyAtIndex(0)).toBe('x');
650
expect(omResult.keyAtIndex(1)).toBe('y');
651
expect(omResult.keyAtIndex(2)).toBe('z');
652
expect(omResult.get('x').val).toBe('xxx0justTestingScope');
653
expect(omResult.get('y').val).toBe('yyy1justTestingScope');
654
expect(omResult.get('z').val).toBe('zzz2justTestingScope');
655
};
656
var resultOM = om.map(function(itm, key, count) {
657
return {
658
uniqueID: itm.uniqueID,
659
val: itm.val + key + count + this.justToTestScope
660
};
661
}, scope);
662
verifyResult(resultOM);
663
664
var resArray = [];
665
om.forEach(function(itm, key, count) {
666
resArray.push({
667
uniqueID: itm.uniqueID,
668
val: itm.val + key + count + this.justToTestScope
669
});
670
}, scope);
671
resultOM = OrderedMap.fromArray(resArray, extractUniqueID);
672
verifyResult(resultOM);
673
});
674
675
it('should filter correctly', function() {
676
var om = OrderedMap.fromArray([
677
{uniqueID: 'x', val: 'xx'},
678
{uniqueID: 'y', val: 'yy'},
679
{uniqueID: 'z', val: 'zz'}
680
], extractUniqueID);
681
var scope = {justToTestScope: 'justTestingScope'};
682
683
var filteringCallback = function(item, key, indexInOriginal) {
684
expect(this).toBe(scope);
685
expect(key === 'x' || key === 'y' || key === 'z').toBe(true);
686
if (key === 'x') {
687
expect(item.val).toBe('xx');
688
expect(indexInOriginal).toBe(0);
689
return false;
690
} else if (key === 'y') {
691
expect(item.val).toBe('yy');
692
expect(indexInOriginal).toBe(1);
693
return true;
694
} else {
695
expect(item.val).toBe('zz');
696
expect(indexInOriginal).toBe(2);
697
return true;
698
}
699
};
700
701
var verifyResult = function(omResult) {
702
expect(omResult.length).toBe(2);
703
expect(omResult.keyAtIndex(0)).toBe('y');
704
expect(omResult.keyAtIndex(1)).toBe('z');
705
expect(omResult.has('x')).toBe(false);
706
expect(omResult.has('z')).toBe(true);
707
expect(omResult.get('z').val).toBe('zz');
708
expect(omResult.has('y')).toBe(true);
709
expect(omResult.get('y').val).toBe('yy');
710
};
711
712
var resultOM = om.filter(filteringCallback, scope);
713
verifyResult(resultOM);
714
});
715
716
it('should throw when providing invalid ranges to ranging', function() {
717
var om = OrderedMap.fromArray([
718
{uniqueID: 'x', val: 'xx'},
719
{uniqueID: 'y', val: 'yy'},
720
{uniqueID: 'z', val: 'zz'}
721
], extractUniqueID);
722
var scope = {justToTestScope: 'justTestingScope'};
723
724
expect(function() {
725
om.mapRange(duplicate, 0, 3, scope);
726
}).not.toThrow();
727
expect(function() {
728
om.filterRange(duplicate, 0, 3, scope);
729
}).not.toThrow();
730
expect(function() {
731
om.forEachRange(duplicate, 0, 3, scope);
732
}).not.toThrow();
733
expect(function() {
734
om.mapKeyRange(duplicate, 'x' , 3, scope);
735
}).toThrow(
736
'Invariant Violation: mapKeyRange must be given keys ' +
737
'that are present.'
738
);
739
expect(function() {
740
om.forEachKeyRange(duplicate, 'x', 3, scope);
741
}).toThrow(
742
'Invariant Violation: forEachKeyRange must be given keys ' +
743
'that are present.'
744
);
745
746
expect(function() {
747
om.mapRange(duplicate, 0, 4, scope);
748
}).toThrow();
749
expect(function() {
750
om.filterRange(duplicate, 0, 4, scope);
751
}).toThrow();
752
expect(function() {
753
om.forEachRange(duplicate, 0, 4, scope);
754
}).toThrow();
755
expect(function() {
756
om.mapKeyRange(duplicate, 'x', null, scope);
757
}).toThrow();
758
expect(function() {
759
om.forEachKeyRange(duplicate, 'x', null, scope);
760
}).toThrow();
761
762
expect(function() {
763
om.mapRange(duplicate, -1, 1, scope);
764
}).toThrow();
765
expect(function() {
766
om.filterRange(duplicate, -1, 1, scope);
767
}).toThrow();
768
expect(function() {
769
om.forEachRange(duplicate, -1, 1, scope);
770
}).toThrow();
771
expect(function() {
772
om.mapKeyRange(duplicate, null, 'y', scope);
773
}).toThrow();
774
expect(function() {
775
om.forEachKeyRange(duplicate, null, 'y', scope);
776
}).toThrow();
777
778
expect(function() {
779
om.mapRange(duplicate, 0, 0, scope);
780
}).not.toThrow();
781
expect(function() {
782
om.filterRange(duplicate, 0, 0, scope);
783
}).not.toThrow();
784
expect(function() {
785
om.forEachRange(duplicate, 0, 0, scope);
786
}).not.toThrow();
787
expect(function() {
788
om.mapKeyRange(duplicate, 'x', 'x', scope);
789
}).not.toThrow();
790
expect(function() {
791
om.forEachKeyRange(duplicate, 'x', 'x', scope);
792
}).not.toThrow();
793
794
expect(function() {
795
om.mapRange(duplicate, 0, -1, scope);
796
}).toThrow();
797
expect(function() {
798
om.filterRange(duplicate, 0, -1, scope);
799
}).toThrow();
800
expect(function() {
801
om.forEachRange(duplicate, 0, -1, scope);
802
}).toThrow();
803
expect(function() {
804
om.mapKeyRange(duplicate, 'x', null, scope);
805
}).toThrow();
806
expect(function() {
807
om.forEachKeyRange(duplicate, 'x', null, scope);
808
}).toThrow();
809
810
expect(function() {
811
om.mapRange(duplicate, 2, 1, scope);
812
}).not.toThrow();
813
expect(function() {
814
om.filterRange(duplicate, 2, 1, scope);
815
}).not.toThrow();
816
expect(function() {
817
om.forEachRange(duplicate, 2, 1, scope);
818
}).not.toThrow();
819
expect(function() {
820
om.mapKeyRange(duplicate, 'z', 'z', scope);
821
}).not.toThrow();
822
expect(function() {
823
om.forEachKeyRange(duplicate, 'z', 'z', scope);
824
}).not.toThrow();
825
826
expect(function() {
827
om.mapRange(duplicate, 2, 2, scope);
828
}).toThrow();
829
expect(function() {
830
om.filterRange(duplicate, 2, 2, scope);
831
}).toThrow();
832
expect(function() {
833
om.forEachRange(duplicate, 2, 2, scope);
834
}).toThrow();
835
expect(function() {
836
om.mapKeyRange(duplicate, 'z', null, scope);
837
}).toThrow();
838
expect(function() {
839
om.forEachKeyRange(duplicate, 'z', null, scope);
840
}).toThrow();
841
842
// Provide keys in reverse order - should throw.
843
expect(function() {
844
om.mapKeyRange(duplicate, 'y', 'x', scope);
845
}).toThrow();
846
expect(function() {
847
om.forEachKeyRange(duplicate, 'y', 'x', scope);
848
}).toThrow();
849
});
850
851
// TEST length zero map, or keyrange start===end
852
853
it('should map range correctly', function() {
854
var om = OrderedMap.fromArray([
855
{uniqueID: 'x', val: 'xx'},
856
{uniqueID: 'y', val: 'yy'},
857
{uniqueID: 'z', val: 'zz'}
858
], extractUniqueID);
859
var scope = {justToTestScope: 'justTestingScope'};
860
var verifyThreeItems = function(omResult) {
861
expect(omResult.length).toBe(3);
862
expect(omResult.keyAtIndex(0)).toBe('x');
863
expect(omResult.keyAtIndex(1)).toBe('y');
864
expect(omResult.keyAtIndex(2)).toBe('z');
865
expect(omResult.get('x').val).toBe('xxx0justTestingScope');
866
expect(omResult.get('y').val).toBe('yyy1justTestingScope');
867
expect(omResult.get('z').val).toBe('zzz2justTestingScope');
868
};
869
var verifyFirstTwoItems = function(omResult) {
870
expect(omResult.length).toBe(2);
871
expect(omResult.keyAtIndex(0)).toBe('x');
872
expect(omResult.keyAtIndex(1)).toBe('y');
873
expect(omResult.get('x').val).toBe('xxx0justTestingScope');
874
expect(omResult.get('y').val).toBe('yyy1justTestingScope');
875
};
876
877
var verifyLastTwoItems = function(omResult) {
878
expect(omResult.length).toBe(2);
879
expect(omResult.keyAtIndex(0)).toBe('y');
880
expect(omResult.keyAtIndex(1)).toBe('z');
881
expect(omResult.get('y').val).toBe('yyy1justTestingScope');
882
expect(omResult.get('z').val).toBe('zzz2justTestingScope');
883
};
884
885
var verifyMiddleItem = function(omResult) {
886
expect(omResult.length).toBe(1);
887
expect(omResult.keyAtIndex(0)).toBe('y');
888
expect(omResult.get('y').val).toBe('yyy1justTestingScope');
889
};
890
891
var verifyEmpty = function(omResult) {
892
expect(omResult.length).toBe(0);
893
};
894
895
var omResultThree = om.mapRange(duplicate, 0, 3, scope);
896
verifyThreeItems(omResultThree);
897
var resArray = [];
898
var pushToResArray = function(itm, key, count) {
899
resArray.push({
900
uniqueID: itm.uniqueID,
901
val: itm.val + key + count + this.justToTestScope
902
});
903
};
904
905
om.forEachRange(pushToResArray, 0, 3, scope);
906
omResultThree = OrderedMap.fromArray(resArray, extractUniqueID);
907
verifyThreeItems(omResultThree);
908
909
var omResultFirstTwo = om.mapRange(duplicate, 0, 2, scope);
910
verifyFirstTwoItems(omResultFirstTwo);
911
resArray = [];
912
om.forEachRange(pushToResArray, 0, 2, scope);
913
omResultFirstTwo = OrderedMap.fromArray(resArray, extractUniqueID);
914
verifyFirstTwoItems(omResultFirstTwo);
915
916
var omResultLastTwo = om.mapRange(duplicate, 1, 2, scope);
917
verifyLastTwoItems(omResultLastTwo);
918
resArray = [];
919
om.forEachRange(pushToResArray, 1, 2, scope);
920
omResultLastTwo = OrderedMap.fromArray(resArray, extractUniqueID);
921
verifyLastTwoItems(omResultLastTwo);
922
923
var omResultMiddle = om.mapRange(duplicate, 1, 1, scope);
924
verifyMiddleItem(omResultMiddle);
925
resArray = [];
926
om.forEachRange(pushToResArray, 1, 1, scope);
927
omResultMiddle = OrderedMap.fromArray(resArray, extractUniqueID);
928
verifyMiddleItem(omResultMiddle);
929
930
var omResultNone = om.mapRange(duplicate, 1, 0, scope);
931
verifyEmpty(omResultNone);
932
});
933
934
it('should extract the original array correctly', function() {
935
var sourceArray = [
936
{uniqueID: 'x', val: 'xx'},
937
{uniqueID: 'y', val: 'yy'},
938
{uniqueID: 'z', val: 'zz'}
939
];
940
var om = OrderedMap.fromArray(sourceArray, extractUniqueID);
941
expect(om.toArray()).toEqual(sourceArray);
942
});
943
});
944
945