-
Notifications
You must be signed in to change notification settings - Fork 14
/
spec.js
921 lines (693 loc) · 27.8 KB
/
spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
var fjs = require("./functional.js");
describe("functional", function() {
it("should have a global fjs object", function() {
expect(fjs).toBeDefined();
});
it("should throw an error calling fjs.curry with non function or expression", function() {
var result1 = function () {
fjs.curry();
};
var result2 = function () {
fjs.curry(1);
};
expect(result1).toThrow("fjs Error: Invalid function");
expect(result2).toThrow("fjs Error: Invalid function");
});
it("should fjs.curry a string concatenation function", function() {
var concatenate = fjs.curry(function(word1, word2) {
return word1 + " " + word2;
});
var concatenateHello = concatenate("Hello");
var result = concatenateHello("World");
expect(result).toEqual("Hello World");
});
it("should fjs.curry a string concatenation expression", function() {
var concatenate = fjs.curry("a, b => a + b");
var concatenateHello = concatenate("Hello");
var result = concatenateHello("World");
expect(result).toEqual("HelloWorld");
});
it("should fjs.curry an addition function with multiple args and fjs.curry the fjs.curry", function() {
var add = fjs.curry(function(arg1, arg2, arg3) {
return arg1 + arg2 + arg3;
});
var add3 = add(3),
add5 = add3(2);
expect(add(3)(2)(1)).toEqual(6);
expect(add3(2, 1)).toEqual(6);
expect(add3(2)(1)).toEqual(6);
expect(add5(1)).toEqual(6);
});
it("should extend the arity using fjs.curry", function() {
var add = fjs.curry(function(arg1, arg2) {
return arg1 + arg2;
});
var add3 = add(3);
expect(add(1, 2, 3)).toEqual(6);
expect(add3(1, 2, 3, 4, 5)).toEqual(18);
});
it("should extend the arity using expression", function() {
var add = fjs.curry("a, b => a + b");
var add3 = add(3);
expect(add(1, 2, 3)).toEqual(6);
expect(add3(1, 2, 3, 4, 5)).toEqual(18);
});
it("should be able to add items to an array using fjs.each", function() {
var result = [],
items = ["f", "u", "n", "c"];
var addTo = function (item) {
return result.push(item);
};
fjs.each(addTo, items);
expect(result).toEqual(items);
});
it("should be able to fjs.curry fjs.each", function() {
var result = [],
items = ["f", "u", "n", "c"];
var addTo = function (item) {
return result.push(item);
};
var addToResult = fjs.each(addTo);
expect(fjs.isFunction(addToResult)).toBeTruthy();
addToResult(items);
expect(result).toEqual(["f", "u", "n", "c"]);
});
it("should handle null param to fjs.each", function() {
var nothing = function (item) {
return item;
};
var doNothing = fjs.each(nothing);
var result = function () {
doNothing(null);
};
expect(result).not.toThrow();
});
it("should pass the current index when using fjs.each", function() {
var result = [],
items = ["f", "u", "n", "c"];
fjs.each(function (item, i) {
return result.push(i);
}, items);
expect(result).toEqual([0, 1, 2, 3]);
});
it("should allow bind using fjs.each", function() {
var result = [],
items = ["f", "u", "n", "c"];
var iterator = function (val, item, i) {
return result.push(val + item + i);
};
fjs.each(iterator.bind(this, "val"), items);
expect(result).toEqual(["valf0", "valu1", "valn2", "valc3"]);
});
it("should be able to double numbers in an array using fjs.map", function() {
var items = [1, 2, 3];
var doubleUp = function (number) {
return number * 2;
};
var result = fjs.map(doubleUp, items);
expect(result).toEqual([2, 4, 6]);
});
it("should be able to fjs.curry fjs.map", function() {
var items = [1, 2, 3];
var doubleUp = function (number) {
return number * 2;
};
var doubleMap = fjs.map(doubleUp);
expect(fjs.isFunction(doubleMap)).toBeTruthy();
var result = doubleMap(items);
expect(result).toEqual([2, 4, 6]);
});
it("should be able to fjs.curry fjs.map expression", function() {
var items = [1, 2, 3];
var doubleMap = fjs.map("n => n * 2");
var result = doubleMap(items);
expect(result).toEqual([2, 4, 6]);
});
it("should be able to use current index with fjs.map", function() {
var items = [1, 2, 3];
var timesIndex = function (number, i) {
return number * i;
};
var result = fjs.map(timesIndex, items);
expect(result).toEqual([0, 2, 6]);
});
it("should be able to use fjs.reduce, fjs.reducel or fjs.foldll", function() {
expect(fjs.reduce).toEqual(fjs.reducel);
expect(fjs.reduce).toEqual(fjs.foldll);
});
it("should be able to cumulate an array of numbers using fjs.reduce", function() {
var items = [1, 2, 3];
var add = function (arg1, arg2) {
return arg1 + arg2;
};
var result = fjs.reduce(add, items);
expect(result).toEqual(6);
});
it("should be able to cumulate an array of strings using fjs.reduce", function() {
var items = ["f", "u", "n", "c"];
var concatenate = function (arg1, arg2) {
return arg1 + arg2;
};
var result = fjs.reduce(concatenate, items);
expect(result).toEqual("func");
});
it("should be able use current index with fjs.reduce", function() {
var items = [1, 2, 3, 4];
var add = function (arg1, arg2, i) {
return arg1 + arg2 + i;
};
var result = fjs.reduce(add, items);
expect(result).toEqual(13);
});
it("should be able to fjs.curry fjs.reduce", function() {
var items = [1, 2, 3];
var multiply = function (arg1, arg2) {
return arg1 * arg2;
};
var multiplyReduce = fjs.reduce(multiply);
expect(fjs.isFunction(multiplyReduce)).toBeTruthy();
var result = multiplyReduce(items);
expect(result).toEqual(6);
});
it("should be able to fjs.curry fjs.reduce expression", function() {
var items = [1, 2, 3];
var multiply = function (arg1, arg2) {
return arg1 * arg2;
};
var multiplyReduce = fjs.reduce("a, b => a * b");
var result = multiplyReduce(items);
expect(result).toEqual(6);
});
it("should be able to use fjs.fold or fjs.foldl", function() {
expect(fjs.fold).toEqual(fjs.foldl);
});
it("should be able to fjs.curry fjs.fold", function() {
var items = [1, 2, 3];
var multiply = function (arg1, arg2) {
return arg1 * arg2;
};
var multiplyFoldFrom10 = fjs.fold(multiply, 10);
expect(fjs.isFunction(multiplyFoldFrom10)).toBeTruthy();
var result = multiplyFoldFrom10(items);
expect(result).toEqual(60);
});
it("should be able to fjs.curry fjs.fold expression", function() {
var items = [1, 2, 3];
var multiplyFoldFrom10 = fjs.fold("a, b => a * b", 10);
var result = multiplyFoldFrom10(items);
expect(result).toEqual(60);
});
it("should be able to fjs.curry fjs.best", function() {
var items = [1, -4, 2, 3];
var biggest = function (arg1, arg2) {
return arg1 > arg2;
};
var smallest = function (arg1, arg2) {
return arg1 < arg2;
};
var biggestAndBest = fjs.best(biggest);
var bestSmallest = fjs.best(smallest);
expect(fjs.isFunction(biggestAndBest)).toBeTruthy();
expect(fjs.isFunction(bestSmallest)).toBeTruthy();
expect(biggestAndBest(items)).toEqual(3);
expect(bestSmallest(items)).toEqual(-4);
});
it("should be able to fjs.curry fjs.best to get the longest word", function() {
var words = ["simply", "the", "best"];
var longest = fjs.best(function (arg1, arg2) {
return arg1.length > arg2.length;
});
expect(fjs.isFunction(longest)).toBeTruthy();
expect(longest(words)).toEqual("simply");
});
it("should be able to fjs.curry fjs.best expression", function() {
var words = ["simply", "the", "best"];
var longest = fjs.best("a, b => a.length > b.length");
expect(longest(words)).toEqual("simply");
});
it("should be able to fjs.curry fjs.while to get even numbers until odd", function() {
var even = function (item) {
return item % 2 === 0;
};
var whileEven = fjs.while(even);
expect(whileEven([2])).toEqual([2]);
expect(whileEven([2, 4, 5, 6])).toEqual([2, 4]);
expect(whileEven([1, 4, 6, 8])).toEqual([]);
});
it("should be able to fjs.curry fjs.while expression", function() {
var whileEven = fjs.while("n => n % 2 === 0");
expect(whileEven([2])).toEqual([2]);
expect(whileEven([2, 4, 5, 6])).toEqual([2, 4]);
expect(whileEven([1, 4, 6, 8])).toEqual([]);
});
it("should be able to use fjs.any or fjs.contains", function() {
expect(fjs.any).toEqual(fjs.contains);
});
it("should be able to fjs.curry fjs.any", function() {
var items1 = [1, 2, 3],
items2 = [1, 3, 5];
var even = function (item) {
return item % 2 === 0;
};
var anyEven = fjs.any(even);
var containsEven = fjs.contains(even);
expect(anyEven(items1)).toBeTruthy();
expect(containsEven(items1)).toBeTruthy();
expect(anyEven(items2)).not.toBeTruthy();
expect(containsEven(items2)).not.toBeTruthy();
});
it("should be able to fjs.curry fjs.any or fjs.contains expression", function() {
var items1 = [1, 2, 3],
items2 = [1, 3, 5];
var anyEven = fjs.any("n => n % 2 === 0");
var containsEven = fjs.contains("n => n % 2 === 0");
expect(anyEven(items1)).toBeTruthy();
expect(containsEven(items1)).toBeTruthy();
expect(anyEven(items2)).not.toBeTruthy();
expect(containsEven(items2)).not.toBeTruthy();
});
it("should be able to fjs.curry fjs.select", function() {
var items = [1, 2, 3, 4, 5];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var selectEven = fjs.select(even);
var selectOdd = fjs.select(odd);
expect(selectEven(items)).toEqual([2, 4]);
expect(selectOdd(items)).toEqual([1, 3, 5]);
});
it("should be able to fjs.curry fjs.select expression", function() {
var items = [1, 2, 3, 4, 5];
var selectEven = fjs.select("n => n % 2 === 0");
var selectOdd = fjs.select("n => n % 2 !== 0");
expect(selectEven(items)).toEqual([2, 4]);
expect(selectOdd(items)).toEqual([1, 3, 5]);
});
it("should be able to fjs.clone an array and keep independence", function() {
var items = [5, 4, 3, 2, 1];
var clonedItems = fjs.clone(items);
expect(clonedItems).toEqual(items);
items = [];
expect(clonedItems).not.toEqual(items);
});
it("should be able to use fjs.first, fjs.head or fjs.take", function() {
expect(fjs.first).toEqual(fjs.head);
expect(fjs.first).toEqual(fjs.take);
});
it("should be able to fjs.curry fjs.first", function() {
var items = [5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var firstEven = fjs.first(even);
var firstOdd = fjs.first(odd);
expect(firstEven(items)).toEqual(4);
expect(firstOdd(items)).toEqual(5);
});
it("should be able to fjs.curry fjs.first expression", function() {
var items = [5, 4, 3, 2, 1];
var firstEven = fjs.first("n => n % 2 === 0");
var firstOdd = fjs.first("n => n % 2 !== 0");
var first = fjs.first("n => n");
expect(firstEven(items)).toEqual(4);
expect(firstOdd(items)).toEqual(5);
expect(first(items)).toEqual(5);
});
it("should be able to use fjs.rest, fjs.tail or fjs.drop", function() {
expect(fjs.rest).toEqual(fjs.tail);
expect(fjs.rest).toEqual(fjs.drop);
});
it("should be able to fjs.curry fjs.rest", function() {
var items = [5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var restEven = fjs.rest(even);
var restOdd = fjs.rest(odd);
expect(restEven(items)).toEqual([2]);
expect(restOdd(items)).toEqual([3, 1]);
});
it("should be able to fjs.curry fjs.rest expression", function() {
var items = [5, 4, 3, 2, 1];
var restEven = fjs.rest("n => n % 2 === 0");
var restOdd = fjs.rest("n => n % 2 !== 0");
var rest = fjs.rest("n => n");
expect(restEven(items)).toEqual([2]);
expect(restOdd(items)).toEqual([3, 1]);
expect(rest(items)).toEqual([4, 3, 2, 1]);
});
it("should be able to fjs.curry fjs.last", function() {
var items = [5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var lastEven = fjs.last(even);
var lastOdd = fjs.last(odd);
expect(lastEven(items)).toEqual(2);
expect(lastOdd(items)).toEqual(1);
});
it("should be able to fjs.curry fjs.last expression", function() {
var items = [5, 4, 3, 2, 1];
var lastEven = fjs.last("n => n % 2 === 0");
var lastOdd = fjs.last("n => n % 2 !== 0");
var last = fjs.last("n => n");
expect(lastEven(items)).toEqual(2);
expect(lastOdd(items)).toEqual(1);
expect(last(items)).toEqual(1);
});
it("should be able to fjs.curry fjs.every", function() {
var items = [2, 4, 6, 8];
var even = function (item) {
return item % 2 === 0;
};
var odd = function (item) {
return item % 2 !== 0;
};
var everyEven = fjs.every(even);
var everyOdd = fjs.every(odd);
expect(everyEven(items)).toEqual(true);
expect(everyOdd(items)).toEqual(false);
});
it("should be able to fjs.curry fjs.every expression", function() {
var items = [2, 4, 6, 8];
var everyEven = fjs.every("n => n % 2 === 0");
var everyOdd = fjs.every("n => n % 2 !== 0");
expect(everyEven(items)).toEqual(true);
expect(everyOdd(items)).toEqual(false);
});
it("should be able to use fjs.every or fjs.all", function() {
expect(fjs.every).toEqual(fjs.all);
});
it("should throw an error attempting to fjs.compose anything that isn't a function", function() {
var f = function (a) {
return "hello " + a;
};
var g = 1;
var result = function () {
fjs.compose(f, g);
};
expect(result).toThrow("fjs Error: Invalid function to compose");
});
it("should be able to fjs.compose two functions", function() {
var f = function (a) {
return "hello " + a;
};
var g = function (a) {
return a + 1;
};
var composed = fjs.compose(f, g);
expect(composed(1)).toEqual("hello 2");
});
it("should be able to fjs.compose multiple functions", function() {
var e = function (a) {
return "hello " + a;
};
var f = function (a) {
return a + 1;
};
var g = function (a) {
return a * 100;
};
var composed = fjs.compose(e, f, g);
expect(composed(2)).toEqual("hello 201");
});
it("fjs.compose should return the same value when called with the same argument", function() {
var f = function (a) {
return "hello " + a;
};
var g = function (a) {
return a + 1;
};
var composed = fjs.compose(f, g);
expect(composed(1)).toEqual(composed(1));
});
it("should be able to fjs.partition an array of odd and even numbers", function() {
var items = [1, 2, 3, 4, 5, 6, 7];
var even = function (item) {
return item % 2 === 0;
};
var result = fjs.partition(even, items);
expect(result).toEqual([[2, 4, 6], [1, 3, 5, 7]]);
});
it("should be able to fjs.curry fjs.partition", function() {
var items = [7, 6, 5, 4, 3, 2, 1];
var even = function (item) {
return item % 2 === 0;
};
var partitionEven = fjs.partition(even);
var result = partitionEven(items);
expect(result).toEqual([[6, 4, 2], [7, 5, 3, 1]]);
});
it("should be able to fjs.curry fjs.partition expression", function() {
var items = [7, 6, 5, 4, 3, 2, 1];
var partitionEven = fjs.partition("n => n % 2 === 0");
var result = partitionEven(items);
expect(result).toEqual([[6, 4, 2], [7, 5, 3, 1]]);
});
it("should be able to fjs.curry fjs.group", function() {
var items = ["Lee", "Ryan", "Leona", "Sarah", "Rob", "Liam"];
var firstLetter = function (item) {
return item.charAt(0);
};
var groupFirstLetter = fjs.group(firstLetter);
var result = groupFirstLetter(items);
expect(result).toEqual({"L": [ "Lee", "Leona", "Liam" ],
"R": [ "Ryan", "Rob" ], "S": [ "Sarah" ]});
});
it("should be able to fjs.curry fjs.group", function() {
var items = ["Lee", "Ryan", "Leona", "Sarah", "Rob", "Liam"];
var groupFirstLetter = fjs.group("a => a.charAt(0)");
var result = groupFirstLetter(items);
expect(result).toEqual({"L": [ "Lee", "Leona", "Liam" ],
"R": [ "Ryan", "Rob" ], "S": [ "Sarah" ]});
});
it("should be able to fjs.curry fjs.pluck", function() {
var items = [{
"p1": "abc",
"p2": false,
"p3": 123
}, {
"p1": "cab",
"p2": true,
"p3": 312
},{
"p1": "bca",
"p2": false,
"p3": 231
}];
var pluck1 = fjs.pluck("p1");
var result1 = pluck1(items);
var pluck2 = fjs.pluck("p2");
var result2 = pluck2(items);
expect(result1).toEqual(["abc", "cab", "bca"]);
expect(result2).toEqual([false, true, false]);
});
it("should convert an object to an array", function() {
var obj = {
"p1": "abc",
"p2": false,
"p3": null
};
var result = fjs.toArray(obj);
expect(result).toEqual([["p1", "abc"], ["p2", false], ["p3", null]]);
expect(fjs.isArray(obj)).toBeFalsy();
expect(fjs.isArray(result)).toBeTruthy();
});
it("should be able to fjs.curry fjs.apply", function() {
var items = ["Hello", "World"];
var applyCase = fjs.apply("toUpperCase");
var result = applyCase(items);
expect(result).toEqual(["HELLO", "WORLD"]);
});
it("should be able to fjs.curry fjs.apply with additional argument", function() {
var items = ["Hello", "World"];
var applyIndexOf = fjs.apply(["indexOf", "o"]);
var result = applyIndexOf(items);
expect(result).toEqual([4, 1]);
});
it("should be able to fjs.curry fjs.apply with multiple arguments", function() {
var items = ["Hello", "World"];
var applyIndexOf = fjs.apply(["substring", "1", "4"]);
var result = applyIndexOf(items);
expect(result).toEqual(["ell", "orl"]);
});
it("should be able to use fjs.assign or fjs.extend", function() {
expect(fjs.assign).toEqual(fjs.extend);
});
it("should be able to do a basic fjs.assign", function() {
var obj1 = {
prop1: "obj1prop1",
prop2: "obj1prop2"
};
var obj2 = {
prop2: "obj2prop2",
prop3: "obj2prop3"
};
var result = fjs.assign(obj1, obj2);
expect(result).toEqual({
prop1: "obj1prop1",
prop2: "obj1prop2",
prop3: "obj2prop3"
});
});
it("should be able to fjs.curry fjs.assign and extend the arity", function() {
var obj1 = {
prop1: "obj1prop1",
prop2: "obj1prop2"
};
var obj2 = {
prop2: "obj2prop2",
prop3: "obj2prop3",
prop4: "obj2prop4"
};
var obj3 = {
prop4: "obj3prop4",
prop5: "obj3prop5"
};
var assignToObj1 = fjs.assign(obj1);
var result1 = assignToObj1(obj2, obj3);
var result2 = fjs.assign(obj1, obj2, obj3);
expect(result1).toEqual({
prop1: "obj1prop1",
prop2: "obj1prop2",
prop3: "obj2prop3",
prop4: "obj2prop4",
prop5: "obj3prop5"
});
expect(result1).toEqual(result2);
});
it("should be able to use fjs.nub, fjs.unique or fjs.distinct", function() {
expect(fjs.nub).toEqual(fjs.unique);
expect(fjs.nub).toEqual(fjs.distinct);
});
it("should remove duplicate elements from a list with fjs.nub", function() {
var base = ["John", "Jane", "Jane", "Jane", "Joe", "John", "Joe"];
var expected = ["John", "Jane", "Joe"];
var actual = fjs.nub(function (arg1, arg2) {
return arg1 === arg2;
}, base);
expect(actual).toEqual(expected);
});
it("should return one item when fjs.nub list of one", function() {
var base = [1];
var actual = fjs.nub(function (arg1, arg2) {
return arg1 === arg2;
}, base);
expect(actual).toEqual(base);
});
it("should return empty when fjs.nub of empty list", function() {
var empty = [];
var actual = fjs.nub(function (arg1, arg2) {
return arg1 === arg2;
}, empty);
expect(actual).toEqual(empty);
});
it("should be able to fjs.curry fjs.nub expression", function() {
var base = [1,2,4,5,5,5,6,7,8,8];
var expected = [1,2,4,5,6,7,8];
var nubByEquality = fjs.nub(function (arg1, arg2) {
return arg1 === arg2;
});
var actual = nubByEquality(base);
expect(actual).toEqual(expected);
});
it("should be able to fjs.nub with a complex comparator", function() {
var base = [{value1: 1, value2: 2},{value1: 9, value2: 2},
{value1: 3, value2: 1},{value1: 1, value2: 4},{value1: 44, value2: 4}];
var expected = [{value1: 1, value2: 2},{value1: 3, value2: 1},{value1: 1, value2: 4},];
var actual = fjs.nub(function (arg1, arg2) {
return arg1.value2 === arg2.value2;
}, base);
expect(actual).toEqual(expected);
});
it("should have correct return values for fjs.exists", function() {
expect(fjs.exists(undefined)).toBeFalsy();
expect(fjs.exists(null)).toBeFalsy();
expect(fjs.exists(1)).toBeTruthy();
expect(fjs.exists(-1)).toBeTruthy();
expect(fjs.exists(0)).toBeTruthy();
expect(fjs.exists("abc")).toBeTruthy();
expect(fjs.exists("")).toBeTruthy();
expect(fjs.exists(Number.MAX_VALUE)).toBeTruthy();
expect(fjs.exists(Number.MIN_VALUE)).toBeTruthy();
expect(fjs.exists(NaN)).toBeTruthy();
expect(fjs.exists(0144)).toBeTruthy();
expect(fjs.exists(0xFF)).toBeTruthy();
expect(fjs.exists(0.1)).toBeTruthy();
expect(fjs.exists(-0.1)).toBeTruthy();
expect(fjs.exists(3e5)).toBeTruthy();
expect(fjs.exists(true)).toBeTruthy();
expect(fjs.exists(false)).toBeTruthy();
expect(fjs.exists(Infinity)).toBeTruthy();
expect(fjs.exists(Number.POSITIVE_INFINITY)).toBeTruthy();
expect(fjs.exists(Number.NEGATIVE_INFINITY)).toBeTruthy();
expect(fjs.exists(new Date())).toBeTruthy();
expect(fjs.exists([])).toBeTruthy();
expect(fjs.exists({})).toBeTruthy();
expect(fjs.exists(function() { })).toBeTruthy();
});
it("should have correct return values for fjs.truthy", function() {
expect(fjs.truthy(undefined)).toBeFalsy();
expect(fjs.truthy(null)).toBeFalsy();
expect(fjs.truthy(false)).toBeFalsy();
expect(fjs.truthy(1)).toBeTruthy();
expect(fjs.truthy(-1)).toBeTruthy();
expect(fjs.truthy(0)).toBeTruthy();
expect(fjs.truthy("abc")).toBeTruthy();
expect(fjs.truthy("")).toBeTruthy();
expect(fjs.truthy(Number.MAX_VALUE)).toBeTruthy();
expect(fjs.truthy(Number.MIN_VALUE)).toBeTruthy();
expect(fjs.truthy(NaN)).toBeTruthy();
expect(fjs.truthy(0144)).toBeTruthy();
expect(fjs.truthy(0xFF)).toBeTruthy();
expect(fjs.truthy(0.1)).toBeTruthy();
expect(fjs.truthy(-0.1)).toBeTruthy();
expect(fjs.truthy(3e5)).toBeTruthy();
expect(fjs.truthy(true)).toBeTruthy();
expect(fjs.truthy(Infinity)).toBeTruthy();
expect(fjs.truthy(Number.POSITIVE_INFINITY)).toBeTruthy();
expect(fjs.truthy(Number.NEGATIVE_INFINITY)).toBeTruthy();
expect(fjs.truthy(new Date())).toBeTruthy();
expect(fjs.truthy([])).toBeTruthy();
expect(fjs.truthy({})).toBeTruthy();
expect(fjs.truthy(function() { })).toBeTruthy();
});
it("should have correct return values for fjs.falsy", function() {
expect(fjs.falsy(undefined)).toBeTruthy();
expect(fjs.falsy(null)).toBeTruthy();
expect(fjs.falsy(false)).toBeTruthy();
expect(fjs.falsy(1)).toBeFalsy();
expect(fjs.falsy(-1)).toBeFalsy();
expect(fjs.falsy(0)).toBeFalsy();
expect(fjs.falsy("abc")).toBeFalsy();
expect(fjs.falsy("")).toBeFalsy();
expect(fjs.falsy(Number.MAX_VALUE)).toBeFalsy();
expect(fjs.falsy(Number.MIN_VALUE)).toBeFalsy();
expect(fjs.falsy(NaN)).toBeFalsy();
expect(fjs.falsy(0144)).toBeFalsy();
expect(fjs.falsy(0xFF)).toBeFalsy();
expect(fjs.falsy(0.1)).toBeFalsy();
expect(fjs.falsy(-0.1)).toBeFalsy();
expect(fjs.falsy(3e5)).toBeFalsy();
expect(fjs.falsy(true)).toBeFalsy();
expect(fjs.falsy(Infinity)).toBeFalsy();
expect(fjs.falsy(Number.POSITIVE_INFINITY)).toBeFalsy();
expect(fjs.falsy(Number.NEGATIVE_INFINITY)).toBeFalsy();
expect(fjs.falsy(new Date())).toBeFalsy();
expect(fjs.falsy([])).toBeFalsy();
expect(fjs.falsy({})).toBeFalsy();
expect(fjs.falsy(function() { })).toBeFalsy();
});
});