-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathfunction.js
1108 lines (880 loc) · 36.7 KB
/
function.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
namespace('Function', function () {
'use strict';
var clock, u;
function getTimers(fn) {
return testGetPrivateProp(fn, 'timers');
}
function getThreeNoLength(args) {
var arr = [];
for (var i = 0; i < 3; i++) {
arr[i] = args[i];
}
return arr;
}
function getAllWithLength(args) {
var arr = [];
for (var i = 0; i < args.length; i++) {
arr[i] = args[i];
}
return arr;
}
function takesNone() {
return getThreeNoLength(arguments);
}
function takesOne(a) {
return getThreeNoLength(arguments);
}
function takesTwo(a, b) {
return getThreeNoLength(arguments);
}
function takesNoneReturnsVaried() {
return getAllWithLength(arguments);
}
function takesTwoReturnsVaried(a, b) {
return getAllWithLength(arguments);
}
setup(function() {
clock = sinon.useFakeTimers();
});
teardown(function() {
clock.restore();
});
method('delay', function() {
var fn, ret, count;
count = 0;
fn = function() {
count++;
}
run(fn, 'delay', []);
clock.tick(1);
equal(count, 1, 'no arguments should be equal to 1ms');
clock.reset();
count = 0;
fn = function(one, two) {
count++;
equal(this, fn, 'this object should be the function');
equal(one, 'one', 'first parameter');
equal(two, 'two', 'second parameter');
};
equal(getTimers(fn), undefined, 'timers object should not exist yet');
ret = run(fn, 'delay', [20, 'one', 'two']);
equal(typeof getTimers(fn), 'object', 'timers object should be exposed');
equal(typeof ret, 'function', 'returns the function');
equal(count, 0, 'should not have run yet');
clock.tick(20);
equal(count, 1, 'should have run once');
equal(getTimers(fn).length, 1, 'timers are not cleared after execution');
count = 0;
fn = function() {
count++;
}
run(fn, 'delay', []);
clock.tick(1);
equal(count, 1, 'no arguments should be equal to 1ms');
});
method('cancel', function() {
var fn, ref, count;
// Basic functionality
clock.reset();
count = 0;
fn = function() {
count++;
};
run(fn, 'delay', [20 / 4]);
run(fn, 'delay', [20 / 5]);
run(fn, 'delay', [20 / 6]);
ref = run(fn, 'cancel');
equal(ref, fn, 'returns a reference to the function');
clock.tick(20);
equal(count, 0, 'all functions should be canceled');
// Canceling after a delay
clock.reset();
count = 0;
fn = function() {
count++;
};
run(fn, 'delay', [50]);
run(fn, 'delay', [10]);
equal(count, 0, 'should not have been called yet');
equal(getTimers(fn).length, 2, 'should be 2 calls pending');
clock.tick(30);
run(fn, 'cancel');
equal(count, 1, 'should have called the function once');
equal(getTimers(fn).length, 0, 'should have no more calls pending');
clock.tick(30);
equal(count, 1, 'should still have only been called once');
equal(getTimers(fn).length, 0, 'should still be no more delays');
// Canceling after first call (Issue #346)
clock.reset();
count = 0;
fn = function() {
count++;
run(fn, 'cancel');
};
run(fn, 'delay', [5]);
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
equal(count, 0, 'should not have been called yet');
equal(getTimers(fn).length, 6, 'should be 6 calls pending');
clock.tick(50);
equal(count, 1, 'delays should have been canceled after 1');
equal(getTimers(fn).length, 0, 'should be no more pending calls');
// Canceling n functions in
clock.reset();
count = 0;
fn = function() {
count++;
if (count === 2) {
run(fn, 'cancel');
}
};
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
run(fn, 'delay', [2]);
run(fn, 'delay', [5]);
run(fn, 'delay', [20]);
run(fn, 'delay', [20]);
equal(count, 0, 'should not have been called yet');
equal(getTimers(fn).length, 6, 'should be 6 calls pending');
clock.tick(50);
equal(count, 2, 'should have been called twice');
equal(getTimers(fn).length, 0, 'should be no more pending calls');
});
method('lazy', function() {
var count, fn, expected;
// Default
clock.reset();
expected = [['maybe','a',1],['baby','b',2],['you lazy','c',3],['biotch','d',4]];
count = 0;
fn = run(function(one, two) {
equal([this.toString(), one, two], expected[count], 'scope and arguments are correct');
count++;
}, 'lazy');
fn.call('maybe', 'a', 1);
fn.call('baby', 'b', 2);
fn.call('you lazy', 'c', 3);
equal(count, 0, 'not immediate by default');
clock.tick(5);
equal(count, 3, 'was executed 3 times in 5ms');
fn.call('biotch', 'd', 4);
equal(count, 3, 'should not execute immediately on subsequent call');
clock.tick(5);
equal(count, 4, 'final call');
// Immediate execution
clock.reset();
count = 0;
expected = [['maybe','a',1],['baby','b',2],['you lazy','c',3],['biotch','d',4]];
fn = run(function(one, two) {
equal([this.toString(), one, two], expected[count], 'scope and arguments are correct');
count++;
}, 'lazy', [1, true]);
fn.call('maybe', 'a', 1);
fn.call('baby', 'b', 2);
fn.call('you lazy', 'c', 3);
equal(count, 1, 'immediately executed');
clock.tick(5);
equal(count, 3, 'was executed 3 times in 5ms');
fn.call('biotch', 'd', 4);
equal(count, 4, 'should execute immediately again');
clock.tick(5);
equal(count, 4, 'should still have executed 4 times');
// Canceling lazy functions
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy');
fn();
fn();
fn();
equal(count, 0, 'no calls made yet before cancel');
equal(getTimers(fn).length, 1, 'should have 1 pending call');
run(fn, 'cancel');
equal(count, 0, 'no calls made after cancel');
equal(getTimers(fn).length, 0, 'should have no more pending calls');
clock.tick(10);
equal(count, 0, 'lazy function should have been canceled');
equal(getTimers(fn).length, 0, 'should have no more pending calls');
// Cancelling immediate lazy functions
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [1, true]);
fn();
fn();
fn();
equal(count, 1, 'should have run once before cancel');
equal(getTimers(fn).length, 1, 'should have 1 pending call');
run(fn, 'cancel');
equal(count, 1, 'should still have only run once after cancel');
equal(getTimers(fn).length, 0, 'should have no more pending calls');
clock.tick(10);
equal(count, 1, 'should still have only run once after 10ms');
// Fractional ms values
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [0.1]);
for(var i = 0; i < 20; i++) {
fn();
}
equal(count, 0, 'no calls before tick');
clock.tick(2);
equal(count, 20, 'a fractional wait value will call multiple times in a single tick');
// Upper limit for calls
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [0.1, false, 10]);
for(var i = 0; i < 50; i++) {
fn();
}
clock.tick(5);
equal(count, 10, 'number of calls should be capped at 10');
// Upper limit for calls with immediate execution
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [0.1, true, 10]);
for(var i = 0; i < 50; i++) {
fn();
}
clock.tick(5);
equal(count, 10, 'number of calls should be capped at 10');
// Upper limit of 1
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [0.1, false, 1]);
for(var i = 0; i < 50; i++) {
fn();
}
clock.tick(5);
equal(count, 1, 'should have been called once');
// Upper limit of 1 with immediate execution
clock.reset();
count = 0;
fn = run(function() {
count++;
}, 'lazy', [0.1, true, 1]);
for(var i = 0; i < 50; i++) {
fn();
}
clock.tick(5);
equal(count, 1, 'should have been called once');
});
method('debounce', function() {
var fn, ret, count, expected;
// Basic debouncing
clock.reset();
count = 0;
expected = [['leia', 5], ['han solo', 7]];
fn = run(function(one){
equal([this.toString(), one], expected[count], 'scope and arguments are correct');
count++;
}, 'debounce', [20]);
ret = fn.call('3p0', 1);
fn.call('r2d2', 2);
clock.tick(5);
fn.call('chewie', 3);
clock.tick(10);
fn.call('leia', 5);
clock.tick(20);
equal(count, 1, 'should have fired after 30ms');
fn.call('luke', 6);
fn.call('han solo', 7);
equal(ret, undefined, 'calls to a debounced function return undefined');
clock.tick(40);
equal(count, 2, 'count should still be correct after another 10ms');
// Canceling debounced functions
count = 0;
fn = run(function() {
count++;
}, 'debounce', [50]);
fn();
fn();
fn();
run(fn, 'cancel');
equal(count, 0, 'canceled debounce function should not have been called yet');
clock.tick(50);
equal(count, 0, 'canceled debounce function should not have been called after 50ms');
});
method('throttle', function() {
var fn, ret, count, expected;
// Basic throttle functionality
clock.reset();
count = 0;
expected = [['3p0', 1], ['luke', 6]];
fn = run(function(one){
equal([this.toString(), one], expected[count], 'immediate execution | scope and arguments are correct');
count++;
return count;
}, 'throttle', [50]);
equal(fn.call('3p0', 1), 1, 'first run, gets value');
equal(fn.call('r2d2', 2), 1, 'second run, return value is caching');
equal(fn.call('chewie', 3), 1, 'third run, return value is caching');
equal(fn.call('vader', 4), 1, 'fourth run, return value is caching');
clock.tick(25);
equal(fn.call('leia', 5), 1, 'fifth run, return value is caching');
clock.tick(50);
equal(fn.call('luke', 6), 2, 'sixth run, gets value');
equal(fn.call('han solo', 7), 2, 'seventh run, return value is caching');
clock.tick(100);
equal(count, 2, 'count is correct');
// Throttle memoizing
clock.reset();
count = 1;
fn = run(function() {
return ++count;
}, 'throttle', [50]);
equal(fn(), 2, 'iteration 1');
equal(fn(), 2, 'iteration 2');
equal(fn(), 2, 'iteration 3');
clock.tick(200);
equal(fn(), 3, 'memoize | result expires after 200 ms');
});
method('every', function() {
var fn, count;
// Basic
clock.reset();
count = 0;
fn = function(one, two) {
equal(this, fn, 'this object should be the function');
equal(one, 'one', 'first argument should be curried');
equal(two, 'two', 'second argument should be curried');
count++;
};
run(fn, 'every' , [10, 'one', 'two']);
clock.tick(100);
equal(count, 10, 'should have been called 10 times');
// Issue #488
clock.reset();
count = 0;
fn = function(one, two) {
count++;
if (count === 5) {
run(fn, 'cancel');
}
};
run(fn, 'every' , [10]);
clock.tick(100);
equal(count, 5, 'should have been called 5 times');
});
method('after', function() {
var count = 0, expected = true;
var fn = function() { count++; };
var onceFn = run(fn, 'once', []);
var single = run(onceFn, 'after', [3]);
for (var i = 0; i < 10; i++) {
var target = i < 3 ? 0 : 1;
if (count !== target) {
expected = correct;
}
single();
}
equal(expected, true, 'works in conjunction with once to only be called a single time');
function assertCalledAfter(times, arg) {
var i = 0;
var fn = run(function() {
return true;
}, 'after', [arg]);
while(!fn()) {
i++;
};
equal(i + 1, times, 'should have fired after ' + times + ' executions');
}
function assertCalledOutOfTen(times, args) {
var count = 0;
var fn = run(function() {
count++;
}, 'after', args);
for (var i = 0; i < 10; i++) {
fn();
}
equal(count, times, 'should have fired ' + times + ' times out of 10');
}
var count = 0, i = 1;
var expectedArguments = [
[[1,'bop'], [2,'bop'], [3,'bop'], [4,'bop'], [5,'bop']],
[[1,'bop'], [2,'bop'], [3,'bop'], [4,'bop'], [5,'bop'], [6,'bop']],
[[1,'bop'], [2,'bop'], [3,'bop'], [4,'bop'], [5,'bop'], [6,'bop'], [7,'bop']],
[[1,'bop'], [2,'bop'], [3,'bop'], [4,'bop'], [5,'bop'], [6,'bop'], [7,'bop'], [8,'bop']]
];
var fn = run(function(args) {
equal(args, expectedArguments[count], 'collects arguments called');
equal(!!args[0].slice, true, 'arguments are converted to actual arrays');
count++;
return 'hooha';
}, 'after', [5]);
while(i <= 8) {
equal(fn(i, 'bop'), (i >= 5 ? 'hooha' : undefined), 'collects return value as well');
i++;
}
equal(count, 4, 'calls function every time after n calls');
assertCalledAfter(1, 0);
assertCalledAfter(3, 3);
assertCalledAfter(10, 10);
assertCalledAfter(1, 1.5);
assertCalledAfter(1, '0');
assertCalledAfter(3, '3');
assertCalledAfter(10, '10');
assertCalledAfter(1, null);
assertCalledAfter(1, undefined);
assertCalledAfter(1, NaN);
assertCalledAfter(1, false);
assertCalledAfter(1, true);
assertCalledAfter(1, []);
assertCalledAfter(1, {});
assertCalledOutOfTen(10, [0]);
assertCalledOutOfTen(10, [1]);
assertCalledOutOfTen(9, [2]);
assertCalledOutOfTen(8, [3]);
assertCalledOutOfTen(10, [0]);
assertCalledOutOfTen(10, [1]);
assertCalledOutOfTen(9, [2]);
assertCalledOutOfTen(8, [3]);
raisesError(function() { run(fn, 'after', [-1]); }, 'negative raises an error');
raisesError(function() { run(fn, 'after', ['-1']); }, 'negative string raises an error');
raisesError(function() { run(fn, 'after', [Infinity]); }, 'Infinity raises an error');
raisesError(function() { run(fn, 'after', [-Infinity]); }, '-Infinity raises an error');
var count = 0;
var fn = function() { count++; };
var single = run(run(fn, 'once', []), 'after', [3]);
for (var i = 0; i < 10; i++) {
single();
}
equal(count, 1, 'works in conjunction with once to only be called a single time');
});
method('once', function() {
var fn, count;
// Simple count
count = 0;
fn = run(function(one, two) {
count++;
}, 'once');
fn.call();
fn.call();
fn.call();
equal(count, 1, 'returning undefined will not affect the number of calls');
// Simple arguments
count = 0;
fn = run(function(n) {
count++;
return n + 1;
}, 'once');
equal(fn(3), 4, 'running with 3 should add 1');
equal(fn(4), 4, 'running with 4 should remain 4');
equal(fn(500), 4, 'running with 500 should still be 4');
// Runs
fn(1);
// Runs
fn(2);
// Cached
fn(3);
equal(count, 1, 'should have run once');
// Complex arguments
var obj = { foo: 'bar' };
count = 0;
fn = run(function(one, two) {
count++;
equal(this, obj, 'scope is properly set');
equal(one, 'one', 'first argument is passed');
equal(two, 'two', 'second argument is passed');
return count * 30;
}, 'once');
equal(fn.call(obj, 'one', 'two'), 30, 'first call calculates the result');
equal(fn.call(obj, 'one', 'two'), 30, 'second call memoizes the result');
equal(fn.call(obj, 'one', 'two'), 30, 'third call memoizes the result');
equal(fn.call(obj, 'one', 'two'), 30, 'fourth call memoizes the result');
equal(fn.call(obj, 'one', 'two'), 30, 'fifth call memoizes the result');
equal(count, 1, 'count is only incremented once');
});
method('memoize', function() {
// Simple memoization
var count = 0;
var fn = run(function(n) {
count++;
return n + 1;
}, 'memoize');
equal(fn(3), 4, 'running with 3 should add 1');
equal(fn(4), 5, 'running with 4 should still add 1');
equal(fn(500), 501, 'running with 500 should still add 1');
// Runs
fn(1);
// Runs
fn(2);
// Cached
fn(3);
equal(count, 5, 'should have run 5 times');
// Custom hash function.
var firstArgument = function(x) {
return x;
}
var count = 0;
var fn = run(function(n) {
count++;
return n + 1;
}, 'memoize', [firstArgument]);
equal(fn(1, 'a'), 2, 'first time should run');
equal(fn(1, 'a'), 2, 'second time should be cached');
equal(fn(2, 'a'), 3, 'different first argument should run');
equal(fn(2, 'b'), 3, 'different second argument should be cached');
equal(count, 2, 'should have run 2 times');
// Complex memoization
var foo1 = { foo: 'bar' };
var foo2 = { foo: 'bar' };
var foo3 = { foo: 'bar', moo: 'car' };
var count = 0;
var fn = run(function(n) {
count++;
}, 'memoize');
fn(foo1); // Should run
fn(foo1); // Should cache
fn(foo2); // Equal by value, should also cache
fn(foo3); // Not equal by value, should run
fn(foo2, 'c'); // Equal first argument, but different second so should run
fn(foo2, 'c'); // Identical to last, should cache
fn(foo1, 'c'); // Equivalent to last, should also cache
equal(count, 3, 'should have run 3 times');
// Dot operator can serve as a shortcut to the hashing function.
var p1 = { name: { first: 'Tom', last: 'Hanks' }};
var p2 = { name: { first: 'Jon', last: 'Voight' }};
var p3 = { name: { first: 'Tom', last: 'Cruise' }};
var p4 = { name: { first: 'Joe', last: 'Rogan' }};
var p5 = { name: { first: 'Tom', last: 'Waits' }};
var count = 0;
var fn = run(function(obj) {
count++;
return obj.name.first + ' ' + obj.name.last;
}, 'memoize', ['name.first']);
equal(fn(p1), 'Tom Hanks', 'first should run');
equal(fn(p2), 'Jon Voight', 'second should run');
equal(fn(p3), 'Tom Hanks', 'third should cache');
equal(fn(p4), 'Joe Rogan', 'fourth should run');
equal(fn(p5), 'Tom Hanks', 'fifth should run');
equal(count, 3, 'should have run 3 times');
// Limit argument
var count = 0;
var fn = run(function() { count++; }, 'memoize', [function() {}, 1]);
fn(1);
fn(2);
fn(3);
fn(4);
// Noop hash function always returns undefined, which means
// the function will be memoized indefinitely, so limit is ignored.
equal(count, 1, 'limit ignored with noop');
var count = 0;
var fn = run(function() { count++; }, 'memoize', [2]);
fn(1); // First hit, not cached. +1
fn(2); // First hit, not cached. +1
fn(1); // Second hit, cached. +0
fn(2); // Second hit, cached. +0
fn(3); // First hit, not cached. +1 - overflow so cache reset
fn(1); // Second hit, was reset. +1
fn(2); // Second hit, was reset. +1
fn(3); // Third hit, was reset. +1
fn(4); // First ,hit, not cached. +1
// Cache was cleared once, fn should have been called 7 times.
equal(count, 7, 'limit hit 7 times');
// Class instances
function Foo() {}
var o1 = new Foo;
var o2 = new Foo;
var count = 0;
var fn = run(function() { count++; }, 'memoize', []);
fn(o1);
fn(o1);
equal(count, 1, 'instances | same reference is memoized');
var count = 0;
var fn = run(function() { count++; }, 'memoize', []);
fn(o1);
fn(o2);
equal(count, 2, 'instances | different references are not memoized');
});
method('partial', function() {
var format = function(place, last){
return (last || '') + this.toFixed(place);
}
Number.prototype.two = run(format, 'partial', [2]);
equal((18).two(), '18.00');
equal((9999).two(), '9999.00');
equal((9999).two('$'), '$9999.00');
Number.prototype.euro = run(format, 'partial', [undefined, '€']);
equal((9999.77).euro(), '€10000', 'euro | no params | 9.999,77');
equal((9999.77).euro(0), '€10000', 'euro | 0 | 9.999');
equal((9999.77).euro(1), '€9999.8', 'euro | 1 | 9.999,8');
equal((9999.77).euro(2), '€9999.77', 'euro | 2 | 9.999,77');
equal((9999.77).euro(3), '€9999.770', 'euro | 3 | 9.999,777');
Number.prototype.noop = run(format, 'partial');
equal((1000).noop(3, '$'), '$1000.000', 'noop | 1 000,000');
equal((1000).noop(4, '$'), '$1000.0000', 'noop | 1 000,0000');
equal((1000).noop(5, '$'), '$1000.00000', 'noop | 1 000,00000');
var partial = run(function(first) {
return first;
}, 'partial', [['a', 'b']]);
equal(partial(), ['a','b'], 'can be passed arrays');
var partial = run(function(first) {
return Array.prototype.slice.call(arguments);
}, 'partial', [0]);
equal(partial('a'), [0, 'a'], 'falsy values can be passed');
function stringifyArray(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (testIsArray(arr[i])) {
result.push('[' + stringifyArray(arr[i]) + ']');
} else {
result.push(String(arr[i]));
}
}
return result.join(' ');
}
var fn = function() {
return stringifyArray(arguments);
}
equal(run(fn, 'partial', [null, 'a'])('b'), 'null a b', 'null first will not act as a placeholder');
equal(run(fn, 'partial', ['a', null])('b'), 'a null b', 'null second will not act as a placeholder');
equal(run(fn, 'partial', [null, null, 'a'])(), 'null null a', 'null repeated first');
equal(run(fn, 'partial', ['a', null, null])(), 'a null null', 'null repeated last');
equal(run(fn, 'partial', [null, null, null])(), 'null null null', 'all null');
equal(run(fn, 'partial', [null, null, null])('a','b','c'), 'null null null a b c', 'all null overflowing');
equal(run(fn, 'partial', [undefined, 'a'])('b'), 'b a', 'undefined first will act as a placeholder');
equal(run(fn, 'partial', ['a', undefined])('b'), 'a b', 'undefined second will act as a placeholder');
equal(run(fn, 'partial', [undefined, undefined, 'a'])('a', 'b'), 'a b a', 'two placeholders first');
equal(run(fn, 'partial', ['a', undefined, undefined])('a', 'b'), 'a a b', 'two placeholders last');
equal(run(fn, 'partial', [undefined, undefined, undefined])('a', 'b', 'c'), 'a b c', 'all placeholders');
equal(run(fn, 'partial', [undefined, undefined, undefined])('a', 'b'), 'a b undefined', 'all placeholders with last undefined');
equal(run(fn, 'partial', [undefined, undefined, undefined])('a','b','c','d','e'), 'a b c d e', 'all undefined overflowing');
equal(run(fn, 'partial', [null, undefined, null])(), 'null undefined null', 'null and undefined mixed');
equal(run(fn, 'partial', [null, undefined, null])('a'), 'null a null', 'null and undefined mixed with 1 arg');
equal(run(fn, 'partial', [null, undefined, null])('a', 'b'), 'null a null b', 'null and undefined mixed with 2 args');
equal(run(fn, 'partial', [null, undefined, null])('a', 'b', 'c'), 'null a null b c', 'null and undefined mixed with 3 args');
equal(run(fn, 'partial', [undefined, null, undefined])(), 'undefined null undefined', 'undefined and null mixed');
equal(run(fn, 'partial', [undefined, null, undefined])('a'), 'a null undefined', 'undefined and null mixed with 1 arg');
equal(run(fn, 'partial', [undefined, null, undefined])('a', 'b'), 'a null b', 'undefined and null mixed with 2 args');
equal(run(fn, 'partial', [undefined, null, undefined])('a', 'b', 'c'), 'a null b c', 'undefined and null mixed with 3 args');
equal(run(fn, 'partial', ['a'])(undefined), 'a undefined', 'passing undefined');
equal(run(fn, 'partial', ['a'])(undefined, 'b'), 'a undefined b', 'passing undefined first');
equal(run(fn, 'partial', ['a'])('b', undefined), 'a b undefined', 'passing undefined second');
equal(run(fn, 'partial', [undefined])(undefined), 'undefined', 'passing undefined to a placeholder');
equal(run(fn, 'partial', [undefined])(undefined, 'b'), 'undefined b', 'passing undefined to a placeholder first');
equal(run(fn, 'partial', [undefined])('b', undefined), 'b undefined', 'passing undefined to a placeholder second');
equal(run(fn, 'partial', ['a'])(null), 'a null', 'passing null');
equal(run(fn, 'partial', ['a'])(null, 'b'), 'a null b', 'passing null first');
equal(run(fn, 'partial', ['a'])('b', null), 'a b null', 'passing null second');
equal(run(fn, 'partial', [undefined])(null), 'null', 'passing null to a placeholder');
equal(run(fn, 'partial', [undefined])(null, 'b'), 'null b', 'passing null to a placeholder first');
equal(run(fn, 'partial', [undefined])('b', null), 'b null', 'passing null to a placeholder second');
// More complex
equal(run(fn, 'partial', [[undefined]])('a'), '[undefined] a', 'array of undefined is not a placeholder');
equal(run(fn, 'partial', [[undefined], undefined])('a'), '[undefined] a', 'placeholder after array');
equal(run(fn, 'partial', [undefined, [undefined]])('a'), 'a [undefined]', 'placeholder before array');
equal(run(fn, 'partial', [[null]])('a'), '[null] a', 'array of null is not a placeholder');
equal(run(fn, 'partial', [[null], undefined])('a'), '[null] a', 'placeholder after array');
equal(run(fn, 'partial', [undefined, [null]])('a'), 'a [null]', 'placeholder before array');
// Tests lovingly borrowed from Underscore
var obj = {name: 'moe'};
var func = function() { return this.name + ' ' + Array.prototype.slice.call(arguments).join(' '); };
obj.func = run(func, 'partial', ['a', 'b']);
equal(obj.func('c', 'd'), 'moe a b c d', 'can partially apply');
obj.func = run(func, 'partial', [undefined, 'b', undefined, 'd']);
equal(obj.func('a', 'c'), 'moe a b c d', 'can partially apply with placeholders');
func = run(function() { return arguments.length; }, 'partial', [undefined, 'b', undefined, 'd']);
equal(func('a', 'c', 'e'), 5, 'accepts more arguments than the number of placeholders');
equal(func('a'), 4, 'accepts fewer arguments than the number of placeholders');
func = run(function() { return typeof arguments[2]; }, 'partial', [undefined, 'b', undefined, 'd']);
equal(func('a'), 'undefined', 'unfilled placeholders are undefined');
// passes context
function MyWidget(name, options) {
this.name = name;
this.options = options;
}
MyWidget.prototype.get = function() {
return this.name;
};
var MyWidgetWithCoolOpts = run(MyWidget, 'partial', [undefined, {a: 1}]);
var widget = new MyWidgetWithCoolOpts('foo');
equal(widget instanceof MyWidget, true, 'Can partially bind a constructor');
equal(widget.get(), 'foo', 'keeps prototype');
equal(widget.options, {a: 1}, 'options equal');
// explicit return value in constructor
function MyWidget2() {
return {foo:'bar'};
}
var MyFilledWidget = run(MyWidget2, 'partial', [undefined, {a: 1}]);
var widget = new MyFilledWidget();
equal(widget instanceof MyWidget, false, 'explicit return value is no longer an instance of the constructor');
equal(widget.foo, 'bar', 'respects return value');
// Tests lovingly borrowed from Lodash
function identity(n) {
return n;
}
var partial = run(identity, 'partial', ['a']);
equal(partial(), 'a', 'partially applies arguments');
var fn = function(a, b) { return [a, b]; };
var partial = run(fn, 'partial', ['a']);
equal(partial('b'), ['a', 'b'], 'creates a function that can be invoked with additional arguments');
var fn = function() { return arguments.length; };
var partial = run(fn, 'partial', []);
equal(partial(), 0, 'works when there are no partially applied arguments and the created function is invoked without additional arguments');
var partial = run(identity, 'partial', []);
equal(partial('a'), 'a', 'works when there are no partially applied arguments and the created function is invoked with additional arguments');
// Placeholders are "undefined" in Sugar.
var fn = function() { return Array.prototype.slice.call(arguments); };
var partial = run(fn, 'partial', [undefined,'b',undefined]);
equal(partial('a', 'c'), ['a','b','c'], 'placeholders | filling 2');
equal(partial('a'), ['a','b',undefined], 'placeholders | filling 1');
equal(partial(), [undefined,'b',undefined], 'placeholders | filling none');
equal(partial('a','c','d'), ['a','b','c','d'], 'placeholders | filling 2 adding 1');
var fn = function(a, b, c) {};
var partial = run(fn, 'partial', ['a']);
equal(partial.length, 0, 'creates a function with a length of 0');
var object = {};
function Foo(value) {
return value && object;
}
var partial = run(Foo, 'partial', []);
equal(new partial instanceof Foo, true, 'ensure new partialed is an instance of func');
equal(new partial(true), object, 'ensure new partialed return value');
function greet(greeting, name) {
return greeting + ' ' + name;
}
var partial1 = run(greet, 'partial', ['hi']);
var partial2 = run(partial1, 'partial', ['barney']);
var partial3 = run(partial1, 'partial', ['pebbles']);
equal(partial1('fred'), 'hi fred');
equal(partial2(), 'hi barney');
equal(partial3(), 'hi pebbles');
var fn = function() {
var result = [this.a];
Array.prototype.push.apply(result, arguments);
return result;
};
var object = { 'a': 1, 'fn': fn };
var a = fn.bind(object);
var b = run(a, 'partial', [2]);
equal(b(3), [1,2,3], 'should work with combinations of bound and partial functions');
var a = run(fn, 'partial', [2]);
var b = a.bind(object);
equal(b(3), [1,2,3], 'should work with combinations of partial and bound functions');
// Function#bind is spec so our hands are tied here
var fn = function() { return Array.prototype.slice.call(arguments); };
var object = { 'fn': fn };
var a = fn.bind(object, undefined, 2);
var b = run(a, 'partial', [1, undefined, 4]);
equal(b(3, 5), [undefined,2,1,3,4,5], 'should not work with combinations of functions with placeholders');
var a = run(fn, 'partial', [undefined, 2]);
var b = a.bind(object, 1, undefined, 4);
equal(b(3, 5), [1, 2, undefined, 4, 3, 5], 'should not work with combinations of functions with placeholders');
});
method('lock', function() {
// Force 3 arguments as .length could be lying
var fn = run(takesNone, 'lock', []);
equal(fn(), safeArray(u, u, u), 'takes 0 | default | 0 args');
equal(fn(1), safeArray(u, u, u), 'takes 0 | default | 1 arg');
equal(fn(1,2,3), safeArray(u, u, u), 'takes 0 | default | 3 args');
var fn = run(takesOne, 'lock', []);
equal(fn(), safeArray(u, u, u), 'takes 1 | default | 0 args');
equal(fn(1), safeArray(1, u, u), 'takes 1 | default | 1 arg');
equal(fn(1,2,3), safeArray(1, u, u), 'takes 1 | default | 3 args');
var fn = run(takesTwo, 'lock', []);
equal(fn(), safeArray(u, u, u), 'takes 2 | default | 0 args');
equal(fn(1), safeArray(1, u, u), 'takes 2 | default | 1 arg');
equal(fn(1,2,3), safeArray(1, 2, u), 'takes 2 | default | 3 args');
var fn = run(takesNone, 'lock', [1]);
equal(fn(), safeArray(u, u, u), 'takes 0 | manual 1 | 0 args');
equal(fn(1), safeArray(1, u, u), 'takes 0 | manual 1 | 1 arg');
equal(fn(1,2,3), safeArray(1, u, u), 'takes 0 | manual 1 | 3 args');
var fn = run(takesOne, 'lock', [1]);