-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
1341 lines (1288 loc) · 41.8 KB
/
main.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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************/
/**
* @file :test/main.js
* @desc Unit tests for index.js
* @author Lukasz A.J. Wrona (layv.net)
* @license MIT
*/
/******************************************************************************/
"use strict"
const F = require("../index")
const assert = require("assert")
const streamArray = require("stream-array")
/******************************************************************************/
/* global describe it */
Object.defineProperty(Promise.prototype, 'end', {
value(done) {
this.then(() => done(), done)
}
})
const resolve = Promise.resolve
Object.defineProperty(Promise, 'resolve', () => ({
get : () => resolve
}))
describe("F", () => {
it("enable currying when provided with a single function", () => {
const sum3 = F((a, b, c) => a + b + c)
assert.strictEqual(sum3(1)(2, 3), 6);
assert.strictEqual(sum3(1, 2, 3), 6);
assert.strictEqual(sum3(1)(2)(3), 6);
})
it("single param => create function extracting param", () => {
const extract = F("param")
assert.strictEqual(extract({ param : "param's value" }),
"param's value")
})
it("F param should return udefined if param isn't found", () => {
const extract = F("param")
assert.strictEqual(extract({ }), undefined)
})
it("combine multiple params for deep crawling", () => {
const extract = F(0, "param")
assert.strictEqual(extract([ { param : "param's value" } ]),
"param's value")
})
})
describe("compose", () => {
it("Simple function, pass in parameters", () => {
const sum = (a, b) => a + b
const result = F.compose(sum)(3, 5)
assert.strictEqual(result, 8)
})
it("Single function, wait for parameters", done => {
const sum = (a, b) => a + b
F.compose(sum)(3, Promise.resolve(5))
.then(result => assert.strictEqual(result, 8))
.end(done)
})
it("Chain forward multiple functions", () => {
const sum = a => b => a + b
const mul = a => b => a * b
const result = F.compose(sum(10), mul(2), sum(3))(10)
assert.strictEqual(result, 43)
})
it("Chain forward multiple functions, promise arguments", done => {
const sum = a => b => a + b
const mul = a => b => a * b
F.compose(sum(10), mul(2), sum(3))(Promise.resolve(10))
.then(result => assert.strictEqual(result, 43))
.end(done)
})
it("Chain forward multiple functions, promise function result", done => {
const sum = a => b => a + b
const mul = a => b => Promise.resolve(a * b)
F.compose(sum(10), mul(2), sum(3))(10)
.then(result => assert.strictEqual(result, 43))
.end(done)
})
it("If composed function throws, throw", () => {
const object = { }
let result
try {
F.compose(() => {
throw object
})()
} catch (error) {
result = error
}
assert.strictEqual(result, object)
})
it("Catcher function should catch synchronous errors.", () => {
const object = { }
const result = F.compose(result => {
if (isNaN(result)) {
throw object
}
},
x => -x,
F.catch(err => err))("not a number")
assert.strictEqual(result, object)
})
it("Catcher function should catch asynchronous errors.", () => {
const object = { }
F.compose(result => {
if (isNaN(result)) {
throw object
}
},
x => -x,
F.catch(err => err))(Promise.resolve("not a number"))
.then(result => assert.strictEqual(result, object))
})
})
describe("curry", () => {
it("synchronous arguments", () => {
const addMul = F.curry((a, b, c) => (a + b) * c)
const result = addMul(2)(3)(5)
assert.strictEqual(result, 25)
})
it("asynchronous arguments - detect and await, return promise", done => {
const addMul = F.curry((a, b, c) => (a + b) * c)
addMul(2, Promise.resolve(3))(5)
.then(result => assert.strictEqual(result, 25))
.end(done)
})
it("curry with placeholders", () => {
const push = F.curry((obj, arr) => [ ...arr, obj ])
const pushInto = push(F._, [ 1, 2, 3 ])
assert.deepEqual(pushInto(4), [ 1, 2, 3, 4 ])
})
})
describe("range", () => {
it("single parameter > 0 => range from 0 ascending", () => {
const result = [ ]
for (const i of F.range(5)) {
result.push(i)
}
assert.deepEqual(result, [ 0, 1, 2, 3, 4, 5 ])
})
it("single parameter < 0 => range from 0 descending", () => {
const result = [ ]
for (const i of F.range(-5)) {
result.push(i)
}
assert.deepEqual(result, [ 0, -1, -2, -3, -4, -5 ])
})
it("two parameters, range from min to max ascending", () => {
const result = [ ]
for (const i of F.range(5, 10)) {
result.push(i)
}
assert.deepEqual(result, [ 5, 6, 7, 8, 9, 10 ])
})
it("two parameters, range from max to min descending", () => {
const result = [ ]
for (const i of F.range(10, 5)) {
result.push(i)
}
assert.deepEqual(result, [ 10, 9, 8, 7, 6, 5 ])
})
})
describe("args", () => {
it("return passed in arguments", () => {
const args = F.args("one", "two", "three", "four")
assert.deepEqual(args, [ "one", "two", "three", "four" ])
})
it("return passed in arguments, wait for asynchronous ones", () => {
F.args("one", Promise.resolve("two"), "three", Promise.resolve("four"))
.then(args => assert.deepEqual(args, [ "one", "two", "three", "four" ]))
})
})
describe("param", () => {
it("return undefined if base object is undefined", () => {
assert.strictEqual(F.param("one")(undefined), undefined)
})
it("get argument from object by key", () => {
assert.strictEqual(F.param("one", { one : 1 }), 1)
})
it("get argument from object by numeric key", () => {
assert.strictEqual(F.param(3, [ "one", "two", "three", "four" ]), "four")
})
})
describe("ifElse", () => {
it("synchronous condition", () => {
const abs = F.ifElse(a => a >= 0, a => a, a => a * -1)
assert.strictEqual(abs(5), 5)
assert.strictEqual(abs(-3), 3)
})
it("synchronous condition, else optional", () => {
const first = F.ifElse(a => a && a.length, a => a[0])
assert.strictEqual(first([ 5, 4, 3 ]), 5)
assert.strictEqual(first(undefined), undefined)
})
it("condition if not function should apply to F.match rules", () => {
const startsWithWAT = F.ifElse(/^WAT/, "starts with WAT", "WATLess")
assert.strictEqual(startsWithWAT("WAT's up"), "starts with WAT")
assert.strictEqual(startsWithWAT("nah"), "WATLess")
})
it("asyncrhonous condition", done => {
const abs = F.ifElse(a => Promise.resolve(a >= 0), a => a, a => a * -1)
abs(5).then(result => assert.strictEqual(result, 5))
.then(() => abs(-3).then(result => assert.strictEqual(result, 3)))
.end(done)
})
it("asynchronous condition, else optional", done => {
const first = F.ifElse(a => Promise.resolve(a && a.length), a => a[0])
first([ 5, 4, 3 ])
.then(result => assert.strictEqual(result, 5))
.then(() => first(undefined))
.then(result => assert.strictEqual(result, undefined))
.end(done)
})
it("if then elseif then else then", () => {
const sgn = F.ifElse(a => a > 0,
() => 1,
a => a < 0,
() => -1,
() => 0)
assert.strictEqual(sgn(5), 1)
assert.strictEqual(sgn(-3), -1)
assert.strictEqual(sgn(NaN), 0)
})
it("if then elseif then else then asynchronous", done => {
const sgn = F.ifElse(a => Promise.resolve(a > 0),
() => 1,
a => Promise.resolve(a < 0),
() => -1,
() => 0)
sgn(5).then(result => assert.strictEqual(result, 1))
.then(() => sgn(-3)).then(result => assert.strictEqual(result, -1))
.then(() => sgn(NaN)).then(result => assert.strictEqual(result, 0))
.end(done)
})
it("if then elseif then else then asynchronous into synchronous", done => {
const sgn = F.ifElse(a => Promise.resolve(a > 0),
() => 1,
a => a < 0,
() => -1,
() => 0)
sgn(5).then(result => assert.strictEqual(result, 1))
.then(() => sgn(-3)).then(result => assert.strictEqual(result, -1))
.then(() => sgn(NaN)).then(result => assert.strictEqual(result, 0))
.end(done)
})
it("ifElse should return then/else if then is not a function", () => {
const sgn = F.ifElse(a => a > 0, 1,
a => a < 0, -1,
0)
assert.strictEqual(sgn(5), 1)
assert.strictEqual(sgn(-3), -1)
assert.strictEqual(sgn("Bunny"), 0)
})
})
describe("forEach", () => {
it("iterate over an array", () => {
const arr = [ 7, 6, 8, 9 ]
const result = [ ]
F.forEach((value, i, arr) => result.push({ value, i, arr }), arr)
assert.deepEqual(result, [
{ value : 7, i : 0, arr },
{ value : 6, i : 1, arr },
{ value : 8, i : 2, arr },
{ value : 9, i : 3, arr }
])
})
it("iterate over a range", () => {
const range = F.range(5, 1)
const result = [ ]
F.forEach((value, i, range) => result.push({ value, i, range }), range)
assert.deepEqual(result, [
{ value : 5, i : 0, range },
{ value : 4, i : 1, range },
{ value : 3, i : 2, range },
{ value : 2, i : 3, range },
{ value : 1, i : 4, range }
])
})
it("iterate over an object", () => {
const object = { one : "Uno", two : "Dos", three : "Tres" }
const result = [ ]
F.forEach((value, key, object) => result.push({ value, key, object }), object)
assert.deepEqual(result, [
{ value : "Uno", key : "one", object },
{ value : "Dos", key : "two", object },
{ value : "Tres", key : "three", object },
])
})
it("iterate over stream", done => {
const stream = streamArray([ "one", "two", "three", "four" ])
const result = [ ]
F.forEach(chunk => result.push(chunk), stream)
// forEach is side-effect function, we don't care when it finishes
setTimeout(() => {
try {
assert.deepEqual(result, [
"one",
"two",
"three",
"four",
])
done()
} catch (err) {
done(err)
}
}, 250)
})
})
describe("filter", () => {
it("filter array by value", () => {
const arr = [ 7, -1, 6, -2, 8, 9, -4 ]
const result = F.filter(value => value < 0, arr)
assert.deepEqual(result, [ -1, -2, -4 ])
})
it("filter array by key", () => {
const arr = [ 7, -1, 6, -2, 8, 9, -4 ]
const result = F.filter((value, key) => key % 2, arr)
assert.deepEqual(result, [ -1, -2, 9 ])
})
it("filter array by match", () => {
const arr = [ "one", "two", "three", "four", "five" ]
const result = F.filter(/o/g, arr)
assert.deepEqual(result, [ "one", "two", "four" ])
})
it("filter object by value", () => {
const object = { one : "Uno", two : "Dos", three : "Tres" }
const result = F.filter(value => value.indexOf("s") >= 0, object)
assert.deepEqual(result, { two : "Dos", three : "Tres" })
})
it("filter object by key", () => {
const object = { one : "Uno", two : "Dos", three : "Tres" }
const result = F.filter((value, key) => key.indexOf("o") >= 0, object)
assert.deepEqual(result, { one : "Uno", two : "Dos" })
})
it("filter array asynchronously", () => {
const arr = [ 7, -1, 6, -2, 8, 9, -4 ]
F.filter(value => Promise.resolve(value < 0), arr)
.then(result => assert.deepEqual(result, [ -1, -2, -4 ]))
})
it("filter array asynchronously, not all conditions asynchronous", () => {
const arr = [ 7, -1, 6, -2, 8, 9, -4 ]
F.filter(value => value < 0 || Promise.resolve(false), arr)
.then(result => assert.deepEqual(result, [ -1, -2, -4 ]))
})
it("filter object asynchronously", () => {
const object = { one : "Uno", two : "Dos", three : "Tres" }
F.filter(value => Promise.resolve(value.indexOf("s") >= 0), object)
.then(result => assert.deepEqual(result, { two : "Dos", three : "Tres" }))
})
it("filter ES6 set synchronously", () => {
const set = new Set([ 1, -1, 2, -2, 3, -3 ])
const result = F.filter(value => value >= 0, set)
assert.deepEqual(result, new Set([ 1, 2, 3 ]))
})
it("filter ES6 set asynchronously", done => {
const set = new Set([ 1, -1, 2, -2, 3, -3 ])
F.filter(value => Promise.resolve(value >= 0), set)
.then(result => assert.deepEqual(result, new Set([ 1, 2, 3 ])))
.end(done)
})
it("filter ES6 map synchronously", () => {
const set = new Map([
[ "one", 1 ],
[ "minusOne", -1 ],
[ "two", 2 ],
[ "minusTwo", -2 ],
[ "three", 3 ],
[ "minusThree", -3 ]
])
const result = F.filter(value => value >= 0, set)
assert.ok(result instanceof Map, "fiilter over Map should return Map")
assert.deepEqual(result, new Map([
[ "one", 1 ],
[ "two", 2 ],
[ "three", 3 ],
]))
})
it("filter ES6 map asynchronously", done => {
const set = new Map([
[ "one", 1 ],
[ "minusOne", -1 ],
[ "two", 2 ],
[ "minusTwo", -2 ],
[ "three", 3 ],
[ "minusThree", -3 ]
])
F.filter(value => Promise.resolve(value >= 0), set)
.then(result => {
assert.ok(result instanceof Map, "fiilter over Map should return Map")
assert.deepEqual(result, new Map([
[ "one", 1 ],
[ "two", 2 ],
[ "three", 3 ],
]))
})
.end(done)
})
it("filter stream", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.filter(x => (parseInt(x.toString()) + 1) % 2))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(result => assert.deepEqual(result, [ "2", "4", "6" ]))
.then(() => done(), done)
})
it("re-throw stream filter error", done => {
const object = { }
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.filter(x => {
if (parseInt(x) > 4) {
throw object
}
return x
}))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(() => done(new Error("Should have thrown")),
error => {
assert.strictEqual(error, object)
done()
}
)
.catch(done)
})
it("filter async callback", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.filter(x => Promise.resolve((parseInt(x.toString()) + 1) % 2)))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(result => assert.deepEqual(result, [ "2", "4", "6" ]))
.then(() => done(), done)
})
})
describe("map", () => {
it("map array by value", () => {
const result = F.map(x => x * 2, [ 1, 2, 3, 4, 5 ])
assert.deepEqual(result, [ 2, 4, 6, 8, 10 ])
})
it("map array by key", () => {
const result = F.map((value, key) => value * key, [ 1, 2, 3, 4, 5 ])
assert.deepEqual(result, [ 0, 2, 6, 12, 20 ])
})
it("map object by value", () => {
const result = F.map(x => x * 2, { one : 1, two : 2, three : 3 })
assert.deepEqual(result, { one : 2, two : 4, three : 6 })
})
it("map object by key", () => {
const result = F.map((value, key) => key, { one : 1, two : 2, three : 3 })
assert.deepEqual(result, { one : "one", two : "two", three : "three" })
})
it("array + asynchronous callback, synchronize automatically", done => {
F.map(x => Promise.resolve(x * 2), [ 1, 2, 3, 4, 5 ])
.then(result => assert.deepEqual(result, [ 2, 4, 6, 8, 10 ]))
.end(done)
})
it("object + asynchronous callback, synchronize automatically", done => {
F.map(x => Promise.resolve(x * 2), { one : 1, two : 2, three : 3 })
.then(result => assert.deepEqual(result, { one : 2, two : 4, three : 6 }))
.end(done)
})
it("map over ES6 Set synchronously", () => {
const result = F.map(x => x * 2, new Set([ 1, 2, 3, 4, 5 ]))
assert.deepEqual(result, new Set([ 2, 4, 6, 8, 10 ]))
})
it("map over ES6 Set asynchronously", done => {
F.map(x => Promise.resolve(x * 2), new Set([ 1, 2, 3, 4, 5 ]))
.then(result => assert.deepEqual(result, new Set([ 2, 4, 6, 8, 10 ])))
.end(done)
})
it("map over ES6 Map synchronously", () => {
const result = F.map(x => x * 2, new Map([
[ "one", 1 ],
[ "two", 2 ],
[ "three", 3 ]
]))
assert.deepEqual(result, new Map([
[ "one", 2 ],
[ "two", 4 ],
[ "three", 6 ]
]))
})
it("map over ES6 Map asynchronously", done => {
F.map(x => Promise.resolve(x * 2), new Map([
[ "one", 1 ],
[ "two", 2 ],
[ "three", 3 ]
]))
.then(result => assert.deepEqual(result, new Map([
[ "one", 2 ],
[ "two", 4 ],
[ "three", 6 ]
])))
.end(done)
})
it("map stream", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.map(x => (parseInt(x.toString()) * 2).toString()))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(result => assert.deepEqual(result, [ "2", "4", "6", "8", "10", "12" ]))
.then(() => done(), done)
})
it("map stream", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.map(x => Promise.resolve((parseInt(x.toString()) * 2).toString())))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(result => assert.deepEqual(result, [ "2", "4", "6", "8", "10", "12" ]))
.then(() => done(), done)
})
it("map stream error handling", done => {
const object = {}
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.map(() => Promise.reject(object)))
.then(F.reduce((arr, x) => {
arr.push(x.toString())
return arr
}, [ ]))
.then(() => done(new Error("Should have rejected")))
.catch(error => {
assert.strictEqual(error, object)
done()
})
.catch(done)
})
})
describe("reduce", () => {
it("sum range", () => {
const result = F.reduce((prev, cur) => prev + cur, 0, F.range(1, 10))
assert.strictEqual(result, 55)
})
it("key value to object", () => {
const result = F.reduce((object, arr) => {
object[arr[0]] = arr[1];
return object
},
{ },
[
[ 'one', 1 ],
[ 'two', 2 ],
[ 'three', 3 ]
])
assert.deepEqual(result, { one : 1, two : 2, three : 3 })
})
it("object to key-value", () => {
const result = F.reduce((arr, value, key) => {
arr.push([ key, value ]);
return arr
},
[ ],
{
'one' : 1,
'two' : 2,
'three' : 3
})
assert.deepEqual(result, [
[ 'one', 1 ],
[ 'two', 2 ],
[ 'three', 3 ]
])
})
it("sum asynchronous range", done => {
F.reduce((prev, cur) => Promise.resolve(prev + cur), 0, F.range(1, 10))
.then(result => assert.strictEqual(result, 55))
.end(done)
})
it("sum object asynchronously", done => {
F.reduce((prev, cur) => Promise.resolve(prev + cur), 0,
{ one : 1, two : 2, three : 3 })
.then(result => assert.strictEqual(result, 6))
.end(done)
})
it("reduce stream", done => {
Promise.resolve(streamArray([ 1, 2, 5, 7 ]))
.then(F.reduce((count, x) => count + x, 0))
.then(result => assert.strictEqual(result, 15))
.then(() => done(), done)
})
it("reject if reductor rejects", done => {
const object = {}
Promise.resolve(streamArray([ 1, 2, 5, 7 ]))
.then(F.reduce(() => Promise.reject(object), ""))
.then(() => done(new Error("Should have rejected")), result => {
assert.strictEqual(result, object)
done()
})
.catch(done)
})
})
describe('find', () => {
it('find element in an array', () => {
const arr = [ 1, 2, 5, 7 ]
const result = F.find(a => a > 2, arr)
assert.strictEqual(result, 5)
})
it("find element in an array by key", () => {
const arr = [ 1, 2, 5, 7 ]
const result = F.find((a, key) => key === 1, arr)
assert.strictEqual(result, 2)
})
it("find element in array by match", () => {
const arr = [ 1, 2, 5, 7 ]
const result = F.find([ 0, 2, 3 ], arr)
assert.strictEqual(result, 2)
})
it("find element in object", () => {
const object = { one : "uno", two : "dos", three : "tres", four : "quatro" }
const result = F.find(val => val.indexOf('t') >= 0, object)
assert.strictEqual(result, 'tres')
})
it("element in object not found results in undefined", () => {
const object = { one : "uno", two : "dos", three : "tres", four : "quatro" }
const result = F.find(val => val.indexOf('x') >= 0, object)
assert.strictEqual(result, undefined)
})
it("find element in object by key", () => {
const object = { one : "uno", two : "dos", three : "tres", four : "quatro" }
const result = F.find((val, key) => key.indexOf('t') >= 0, object)
assert.strictEqual(result, 'dos')
})
it("find element in array with asynchronous callback", done => {
const arr = [ 1, 2, 5, 7 ]
F.find(a => Promise.resolve(a > 2), arr)
.then(result => assert.strictEqual(result, 5))
.end(done)
})
it("find element in array with asynchronous callback by key", done => {
const arr = [ 1, 2, 5, 7 ]
F.find((value, key) => Promise.resolve(key > 2), arr)
.then(result => assert.strictEqual(result, 7))
.end(done)
})
it("find element in object with asynchronous callback", done => {
const object = { one : "uno", two : "dos", three : "tres" }
F.find(val => Promise.resolve(val.indexOf('t') >= 0), object)
.then(result => assert.strictEqual(result, 'tres'))
.end(done)
})
it("find element in object by key asynchronously", done => {
const object = { one : "uno", two : "dos", four : "quatro" }
F.find((val, key) => Promise.resolve(key.indexOf('t') >= 0), object)
.then(result => assert.strictEqual(result, 'dos'))
.end(done)
})
it("find element in array sometimes asynchronously", done => {
const arr = [ 1, 2, 5, 7, 10, 11 ]
F.find((a, key) => key % 2
? a > 5
: Promise.resolve(a > 5), arr)
.then(result => assert.strictEqual(result, 7))
.end(done)
})
it("find first element in array asynchronously", done => {
const object = [ 0 ]
F.find(val => Promise.resolve(val === 0), object)
.then(result => assert.strictEqual(result, 0))
.end(done)
})
it("element not found should result in undefined", () => {
const object = [ 1, 2, 3, 4 ]
const result = F.find(val => val === 0, object)
assert.strictEqual(result, undefined)
})
it("element not found async should resolve in undefined", done => {
const object = [ 1, 2, 3, 4 ]
F.find(val => Promise.resolve(val === 0), object)
.then(result => assert.strictEqual(result, undefined))
.end(done)
})
it("find element in Map", () => {
const map = new Map([
[ "one", "uno" ],
[ "two", "dos" ],
[ "three", "tres" ],
[ "four", "quatro" ],
])
const result = F.find(val => val.indexOf('t') >= 0, map)
assert.strictEqual(result, 'tres')
})
it("element in Map not found results in undefined", () => {
const map = new Map([
[ "one", "uno" ],
[ "two", "dos" ],
[ "three", "tres" ],
[ "four", "quatro" ],
])
const result = F.find(val => val.indexOf('x') >= 0, map)
assert.strictEqual(result, undefined)
})
it("find element in Map by key", () => {
const map = new Map([
[ "one", "uno" ],
[ "two", "dos" ],
[ "three", "tres" ],
[ "four", "quatro" ],
])
const result = F.find((val, key) => key.indexOf('t') >= 0, map)
assert.strictEqual(result, 'dos')
})
it("find element in map with asynchronous callback", done => {
const map = new Map([
[ "one", "uno" ],
[ "two", "dos" ],
[ "three", "tres" ],
])
F.find(val => Promise.resolve(val.indexOf('t') >= 0), map)
.then(result => assert.strictEqual(result, 'tres'))
.end(done)
})
it('find element in set', () => {
const set = new Set([ 1, 2, 5, 7 ])
const result = F.find(a => a > 5, set)
assert.strictEqual(result, 7)
})
it("find in stream", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.find(x => parseInt(x.toString()) > 2.5))
.then(result => assert.strictEqual(result, "3"))
.then(() => done(), done)
})
it("find in stream with async callback", done => {
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.find(x => Promise.resolve(parseInt(x.toString()) > 2.5)))
.then(result => assert.strictEqual(result, "3"))
.then(() => done(), done)
})
it("error handling in stream find", done => {
const object = { }
Promise.resolve(streamArray([ "1", "2", "3", "4", "5", "6" ]))
.then(F.find(() => Promise.reject(object)))
.then(result => assert.strictEqual(result, "3"))
.then(() => done(new Error("Should have rejected")))
.catch(err => {
assert.strictEqual(err, object)
done()
})
.catch(done)
})
})
describe("some", () => {
it("no elements in the array", () => {
const arr = [ 1, 2, 5, 7 ]
const result = F.some(val => val < 0, arr)
assert.strictEqual(result, false)
})
it("some elements in the array fulfill condition", () => {
const arr = [ 1, 2, 5, 7, -1, 5 ]
const result = F.some(val => val < 0, arr)
assert.strictEqual(result, true)
})
it("some elements in the array do match", () => {
const arr = [
{ x : 1, y : 2 },
{ x : -1, y : 2 }
]
const result = F.some({
x : x => x < 0,
y : y => y > 0
}, arr)
assert.strictEqual(result, true)
})
it("some elements in array based on key", () => {
const arr = [ 1, 2, 3, 4, 5 ]
const result = F.some((val, key) => key === 4, arr)
assert.strictEqual(result, true)
})
it("some elements in array asynchronous asynchronously", done => {
const arr = [ 1, 2, 5, 7, -1, 10 ]
F.some(val => Promise.resolve(val < 0), arr)
.then(result => assert.strictEqual(result, true))
.end(done)
})
it("no elements in the object", () => {
const obj = { one : 1, two : 2, three : 3 }
const result = F.some(val => val < 0, obj)
assert.strictEqual(result, false)
})
it("some elements in the object", () => {
const obj = { one : 1, two : 2, three : 3, minusTwo : -2 }
const result = F.some(val => val < 0, obj)
assert.strictEqual(result, true)
})
it("some elements in the object based on key", () => {
const obj = { one : 1, two : 2, three : 3 }
const result = F.some((val, key) => key.indexOf("e") >= 0, obj)
assert.strictEqual(result, true)
})
it("some elements in the object asynchronously", done => {
const obj = { one : 1, two : 2, three : 3 }
F.some(val => Promise.resolve(val >= 0), obj)
.then(result => assert.strictEqual(result, true))
.end(done)
})
it("some elements in the object asynchronously, not found", done => {
const obj = { one : 1, two : 2, three : 3 }
F.some(val => Promise.resolve(val < 0), obj)
.then(result => assert.strictEqual(result, false))
.end(done)
})
it("some elements in the object asynchronously by key", done => {
const obj = { one : 1, two : 2, three : 3 }
F.some((val, key) => Promise.resolve(key.indexOf("h") >= 0), obj)
.then(result => assert.strictEqual(result, true))
.end(done)
})
})
describe("every", () => {
it("every element in the array synchronously", () => {
const arr = [ 1, 2, 5, 7, -1, 10 ]
const result = F.every(val => !isNaN(val), arr)
assert.strictEqual(result, true)
})
it("Not all elements in the array sunchronously", () => {
const arr = [ 1, 2, 5, 7, -1, NaN, 10 ]
const result = F.every(val => !isNaN(val), arr)
assert.strictEqual(result, false)
})
it("every elements in array by key", () => {
const arr = [ 1, 2, 5, 7, -1, NaN, 10 ]
const result = F.every((val, key) => key <= 6, arr)
assert.strictEqual(result, true)
})
it("Every element in object", () => {
const obj = { one : 1, two : 2, three : 3 }
const result = F.every(val => !isNaN(val), obj)
assert.strictEqual(result, true)
})
it("Every element in object by key", () => {
const obj = { one : 1, two : 2, three : 3 }
const result = F.every((val, key) => isNaN(key), obj)
assert.strictEqual(result, true)
})
it("every element in the array synchronously", done => {
const arr = [ 1, 2, 5, 7, -1, 10 ]
F.every(val => Promise.resolve(!isNaN(val)), arr)
.then(result => assert.strictEqual(result, true))
.end(done)
})
it("Not all elements in the array sunchronously", done => {
const arr = [ 1, 2, 5, 7, -1, NaN, 10 ]
F.every(val => Promise.resolve(!isNaN(val)), arr)
.then(result => assert.strictEqual(result, false))
.end(done)
})
it("every elements in array by key", done => {
const arr = [ 1, 2, 5, 7, -1, NaN, 10 ]
F.every((val, key) => Promise.resolve(key <= 6), arr)
.then(result => assert.strictEqual(result, true))
.end(done)
})
it("Every element in object", done => {
const obj = { one : 1, two : 2, three : 3 }
F.every(val => Promise.resolve(!isNaN(val)), obj)
.then(result => assert.strictEqual(result, true))
.end(done)
})
it("Every element in object by key", done => {
const obj = { one : 1, two : 2, three : 3 }
F.every((val, key) => Promise.resolve(isNaN(key)), obj)
.then(result => assert.strictEqual(result, true))
.end(done)
})
})
describe("match", () => {
it("match regex", () => {
const match = F.match(/.json$/)
assert.strictEqual(match("file.txt"), false)
assert.strictEqual(match("file.json"), true)
})
it("match string", () => {
const match = F.match("two")
assert.strictEqual(match("one"), false)
assert.strictEqual(match("two"), true)
})
it("match against array of strings", () => {
const match = F.match([ "three", "two" ])
assert.strictEqual(match("one"), false)
assert.strictEqual(match("two"), true)
})
it("match against array of regexes", () => {
const match = F.match([ /\.json$/, /\.txt$/ ])
assert.strictEqual(match("file.txt"), true)
assert.strictEqual(match("file.json"), true)
assert.strictEqual(match("file.jpg"), false)
})
it("match against a custom function", () => {
const match = F.match(value => value > 0 && value < 100)
assert.strictEqual(match("file.txt"), false)
assert.strictEqual(match(-0.0001), false)
assert.strictEqual(match(Infinity), false)
assert.strictEqual(match(50), true)
})
it("match against a constructor of the object", () => {
const match = F.match(Array)
assert.strictEqual(match(Set), false)
assert.strictEqual(match({ length : 0 }), false)
assert.strictEqual(match([ Infinity ]), true)
})
it("match against String constructor", () => {
const match = F.match(String)
assert.strictEqual(match(new Set()), false, "set is not a string")
assert.strictEqual(match("random string"), true, "string is a string")
assert.strictEqual(match(NaN), false, "NaN is not a string")
})
it("match against Number constructor", () => {
const match = F.match(Number)
assert.strictEqual(match("string"), false, "string is not a number")
assert.strictEqual(match(0), true, "zero is a number")
assert.strictEqual(match([ ]), false, "empty array is not a number")
})
it("match against object of properties", () => {
const match = F.match({
id : Number,
email : String,
dank : [ true, false ],
})
assert.strictEqual(match(undefined), false, "undefined is not an object")
assert.strictEqual(match({ }), false, "Missing properties")
assert.strictEqual(match({
id : 3,
email : "admin@example.com",
dank : true,
extra : 'whatever'
}), false, "Extra properties")
assert.strictEqual(match({
id : 3,
email : "admin@example.com",
dank : true,
}), true)
assert.strictEqual(match({
id : 5,
email : "admin@example.com",
dank : "cat",
}), false)
})
it("match against boolean constructor", () => {
const match = F.match(Boolean)
assert.strictEqual(match("string"), false, "string is not a boolean")
assert.strictEqual(match(false), true, "false is a aboolean")
assert.strictEqual(match(true), true, "true is a boolean")
assert.strictEqual(match(1), false, "1 is not a boolean")
})
it(`match against asynchronous predicate function should return promise
resolving to the result`, done => {
const match = F.match(x => Promise.resolve(x > 0))
match(5)