2 var _ = typeof require == 'function' ? require('..') : window._;
4 QUnit.module('Functions');
5 QUnit.config.asyncRetries = 3;
7 QUnit.test('bind', function(assert) {
8 var context = {name: 'moe'};
9 var func = function(arg) { return 'name: ' + (this.name || arg); };
10 var bound = _.bind(func, context);
11 assert.equal(bound(), 'name: moe', 'can bind a function to a context');
13 bound = _(func).bind(context);
14 assert.equal(bound(), 'name: moe', 'can do OO-style binding');
16 bound = _.bind(func, null, 'curly');
18 // Work around a PhantomJS bug when applying a function with null|undefined.
19 assert.ok(result === 'name: curly' || result === 'name: ' + window.name, 'can bind without specifying a context');
21 func = function(salutation, name) { return salutation + ': ' + name; };
22 func = _.bind(func, this, 'hello');
23 assert.equal(func('moe'), 'hello: moe', 'the function was partially applied in advance');
25 func = _.bind(func, this, 'curly');
26 assert.equal(func(), 'hello: curly', 'the function was completely applied in advance');
28 func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };
29 func = _.bind(func, this, 'hello', 'moe', 'curly');
30 assert.equal(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
32 func = function(ctx, message) { assert.equal(this, ctx, message); };
33 _.bind(func, 0, 0, 'can bind a function to `0`')();
34 _.bind(func, '', '', 'can bind a function to an empty string')();
35 _.bind(func, false, false, 'can bind a function to `false`')();
37 // These tests are only meaningful when using a browser without a native bind function
38 // To test this with a modern browser, set underscore's nativeBind to undefined
39 var F = function() { return this; };
40 var boundf = _.bind(F, {hello: 'moe curly'});
41 var Boundf = boundf; // make eslint happy.
42 var newBoundf = new Boundf();
43 assert.equal(newBoundf.hello, void 0, 'function should not be bound to the context, to comply with ECMAScript 5');
44 assert.equal(boundf().hello, 'moe curly', "When called without the new operator, it's OK to be bound to the context");
45 assert.ok(newBoundf instanceof F, 'a bound instance is an instance of the original function');
47 assert.raises(function() { _.bind('notafunction'); }, TypeError, 'throws an error when binding to a non-function');
50 QUnit.test('partial', function(assert) {
51 var obj = {name: 'moe'};
52 var func = function() { return this.name + ' ' + _.toArray(arguments).join(' '); };
54 obj.func = _.partial(func, 'a', 'b');
55 assert.equal(obj.func('c', 'd'), 'moe a b c d', 'can partially apply');
57 obj.func = _.partial(func, _, 'b', _, 'd');
58 assert.equal(obj.func('a', 'c'), 'moe a b c d', 'can partially apply with placeholders');
60 func = _.partial(function() { return arguments.length; }, _, 'b', _, 'd');
61 assert.equal(func('a', 'c', 'e'), 5, 'accepts more arguments than the number of placeholders');
62 assert.equal(func('a'), 4, 'accepts fewer arguments than the number of placeholders');
64 func = _.partial(function() { return typeof arguments[2]; }, _, 'b', _, 'd');
65 assert.equal(func('a'), 'undefined', 'unfilled placeholders are undefined');
68 function MyWidget(name, options) {
70 this.options = options;
72 MyWidget.prototype.get = function() {
75 var MyWidgetWithCoolOpts = _.partial(MyWidget, _, {a: 1});
76 var widget = new MyWidgetWithCoolOpts('foo');
77 assert.ok(widget instanceof MyWidget, 'Can partially bind a constructor');
78 assert.equal(widget.get(), 'foo', 'keeps prototype');
79 assert.deepEqual(widget.options, {a: 1});
81 _.partial.placeholder = obj;
82 func = _.partial(function() { return arguments.length; }, obj, 'b', obj, 'd');
83 assert.equal(func('a'), 4, 'allows the placeholder to be swapped out');
85 _.partial.placeholder = {};
86 func = _.partial(function() { return arguments.length; }, obj, 'b', obj, 'd');
87 assert.equal(func('a'), 5, 'swapping the placeholder preserves previously bound arguments');
89 _.partial.placeholder = _;
92 QUnit.test('bindAll', function(assert) {
93 var curly = {name: 'curly'};
96 getName: function() { return 'name: ' + this.name; },
97 sayHi: function() { return 'hi: ' + this.name; }
99 curly.getName = moe.getName;
100 _.bindAll(moe, 'getName', 'sayHi');
101 curly.sayHi = moe.sayHi;
102 assert.equal(curly.getName(), 'name: curly', 'unbound function is bound to current object');
103 assert.equal(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
105 curly = {name: 'curly'};
108 getName: function() { return 'name: ' + this.name; },
109 sayHi: function() { return 'hi: ' + this.name; },
110 sayLast: function() { return this.sayHi(_.last(arguments)); }
113 assert.raises(function() { _.bindAll(moe); }, Error, 'throws an error for bindAll with no functions named');
114 assert.raises(function() { _.bindAll(moe, 'sayBye'); }, TypeError, 'throws an error for bindAll if the given key is undefined');
115 assert.raises(function() { _.bindAll(moe, 'name'); }, TypeError, 'throws an error for bindAll if the given key is not a function');
117 _.bindAll(moe, 'sayHi', 'sayLast');
118 curly.sayHi = moe.sayHi;
119 assert.equal(curly.sayHi(), 'hi: moe');
121 var sayLast = moe.sayLast;
122 assert.equal(sayLast(1, 2, 3, 4, 5, 6, 7, 'Tom'), 'hi: moe', 'createCallback works with any number of arguments');
124 _.bindAll(moe, ['getName']);
125 var getName = moe.getName;
126 assert.equal(getName(), 'name: moe', 'flattens arguments into a single list');
129 QUnit.test('memoize', function(assert) {
130 var fib = function(n) {
131 return n < 2 ? n : fib(n - 1) + fib(n - 2);
133 assert.equal(fib(10), 55, 'a memoized version of fibonacci produces identical results');
134 fib = _.memoize(fib); // Redefine `fib` for memoization
135 assert.equal(fib(10), 55, 'a memoized version of fibonacci produces identical results');
137 var o = function(str) {
140 var fastO = _.memoize(o);
141 assert.equal(o('toString'), 'toString', 'checks hasOwnProperty');
142 assert.equal(fastO('toString'), 'toString', 'checks hasOwnProperty');
145 var upper = _.memoize(function(s) {
146 return s.toUpperCase();
148 assert.equal(upper('foo'), 'FOO');
149 assert.equal(upper('bar'), 'BAR');
150 assert.deepEqual(upper.cache, {foo: 'FOO', bar: 'BAR'});
151 upper.cache = {foo: 'BAR', bar: 'FOO'};
152 assert.equal(upper('foo'), 'BAR');
153 assert.equal(upper('bar'), 'FOO');
155 var hashed = _.memoize(function(key) {
156 //https://github.com/jashkenas/underscore/pull/1679#discussion_r13736209
157 assert.ok(/[a-z]+/.test(key), 'hasher doesn\'t change keys');
160 return key.toUpperCase();
163 assert.deepEqual(hashed.cache, {YEP: 'yep'}, 'takes a hasher');
165 // Test that the hash function can be used to swizzle the key.
166 var objCacher = _.memoize(function(value, key) {
167 return {key: key, value: value};
168 }, function(value, key) {
171 var myObj = objCacher('a', 'alpha');
172 var myObjAlias = objCacher('b', 'alpha');
173 assert.notStrictEqual(myObj, void 0, 'object is created if second argument used as key');
174 assert.strictEqual(myObj, myObjAlias, 'object is cached if second argument used as key');
175 assert.strictEqual(myObj.value, 'a', 'object is not modified if second argument used as key');
178 QUnit.test('delay', function(assert) {
180 var done = assert.async();
182 _.delay(function(){ delayed = true; }, 100);
183 setTimeout(function(){ assert.ok(!delayed, "didn't delay the function quite yet"); }, 50);
184 setTimeout(function(){ assert.ok(delayed, 'delayed the function'); done(); }, 150);
187 QUnit.test('defer', function(assert) {
189 var done = assert.async();
190 var deferred = false;
191 _.defer(function(bool){ deferred = bool; }, true);
192 _.delay(function(){ assert.ok(deferred, 'deferred the function'); done(); }, 50);
195 QUnit.test('throttle', function(assert) {
197 var done = assert.async();
199 var incr = function(){ counter++; };
200 var throttledIncr = _.throttle(incr, 32);
201 throttledIncr(); throttledIncr();
203 assert.equal(counter, 1, 'incr was called immediately');
204 _.delay(function(){ assert.equal(counter, 2, 'incr was throttled'); done(); }, 64);
207 QUnit.test('throttle arguments', function(assert) {
209 var done = assert.async();
211 var update = function(val){ value = val; };
212 var throttledUpdate = _.throttle(update, 32);
213 throttledUpdate(1); throttledUpdate(2);
214 _.delay(function(){ throttledUpdate(3); }, 64);
215 assert.equal(value, 1, 'updated to latest value');
216 _.delay(function(){ assert.equal(value, 3, 'updated to latest value'); done(); }, 96);
219 QUnit.test('throttle once', function(assert) {
221 var done = assert.async();
223 var incr = function(){ return ++counter; };
224 var throttledIncr = _.throttle(incr, 32);
225 var result = throttledIncr();
227 assert.equal(result, 1, 'throttled functions return their value');
228 assert.equal(counter, 1, 'incr was called once'); done();
232 QUnit.test('throttle twice', function(assert) {
234 var done = assert.async();
236 var incr = function(){ counter++; };
237 var throttledIncr = _.throttle(incr, 32);
238 throttledIncr(); throttledIncr();
239 _.delay(function(){ assert.equal(counter, 2, 'incr was called twice'); done(); }, 64);
242 QUnit.test('more throttling', function(assert) {
244 var done = assert.async();
246 var incr = function(){ counter++; };
247 var throttledIncr = _.throttle(incr, 30);
248 throttledIncr(); throttledIncr();
249 assert.equal(counter, 1);
251 assert.equal(counter, 2);
253 assert.equal(counter, 3);
258 QUnit.test('throttle repeatedly with results', function(assert) {
260 var done = assert.async();
262 var incr = function(){ return ++counter; };
263 var throttledIncr = _.throttle(incr, 100);
265 var saveResult = function() { results.push(throttledIncr()); };
266 saveResult(); saveResult();
267 _.delay(saveResult, 50);
268 _.delay(saveResult, 150);
269 _.delay(saveResult, 160);
270 _.delay(saveResult, 230);
272 assert.equal(results[0], 1, 'incr was called once');
273 assert.equal(results[1], 1, 'incr was throttled');
274 assert.equal(results[2], 1, 'incr was throttled');
275 assert.equal(results[3], 2, 'incr was called twice');
276 assert.equal(results[4], 2, 'incr was throttled');
277 assert.equal(results[5], 3, 'incr was called trailing');
282 QUnit.test('throttle triggers trailing call when invoked repeatedly', function(assert) {
284 var done = assert.async();
287 var incr = function(){ counter++; };
288 var throttledIncr = _.throttle(incr, 32);
290 var stamp = new Date;
291 while (new Date - stamp < limit) {
294 var lastCount = counter;
295 assert.ok(counter > 1);
298 assert.ok(counter > lastCount);
303 QUnit.test('throttle does not trigger leading call when leading is set to false', function(assert) {
305 var done = assert.async();
307 var incr = function(){ counter++; };
308 var throttledIncr = _.throttle(incr, 60, {leading: false});
310 throttledIncr(); throttledIncr();
311 assert.equal(counter, 0);
314 assert.equal(counter, 1);
319 QUnit.test('more throttle does not trigger leading call when leading is set to false', function(assert) {
321 var done = assert.async();
323 var incr = function(){ counter++; };
324 var throttledIncr = _.throttle(incr, 100, {leading: false});
327 _.delay(throttledIncr, 50);
328 _.delay(throttledIncr, 60);
329 _.delay(throttledIncr, 200);
330 assert.equal(counter, 0);
333 assert.equal(counter, 1);
337 assert.equal(counter, 2);
342 QUnit.test('one more throttle with leading: false test', function(assert) {
344 var done = assert.async();
346 var incr = function(){ counter++; };
347 var throttledIncr = _.throttle(incr, 100, {leading: false});
350 while (new Date - time < 350) throttledIncr();
351 assert.ok(counter <= 3);
354 assert.ok(counter <= 4);
359 QUnit.test('throttle does not trigger trailing call when trailing is set to false', function(assert) {
361 var done = assert.async();
363 var incr = function(){ counter++; };
364 var throttledIncr = _.throttle(incr, 60, {trailing: false});
366 throttledIncr(); throttledIncr(); throttledIncr();
367 assert.equal(counter, 1);
370 assert.equal(counter, 1);
372 throttledIncr(); throttledIncr();
373 assert.equal(counter, 2);
376 assert.equal(counter, 2);
382 QUnit.test('throttle continues to function after system time is set backwards', function(assert) {
384 var done = assert.async();
386 var incr = function(){ counter++; };
387 var throttledIncr = _.throttle(incr, 100);
388 var origNowFunc = _.now;
391 assert.equal(counter, 1);
393 return new Date(2013, 0, 1, 1, 1, 1);
398 assert.equal(counter, 2);
404 QUnit.test('throttle re-entrant', function(assert) {
406 var done = assert.async();
413 var append = function(arg){
415 var args = sequence.pop();
417 throttledAppend.call(args[0], args[1]);
420 throttledAppend = _.throttle(append, 32);
421 throttledAppend.call('a1', 'a2');
422 assert.equal(value, 'a1a2');
424 assert.equal(value, 'a1a2c1c2b1b2', 'append was throttled successfully');
429 QUnit.test('throttle cancel', function(assert) {
430 var done = assert.async();
432 var incr = function(){ counter++; };
433 var throttledIncr = _.throttle(incr, 32);
435 throttledIncr.cancel();
439 assert.equal(counter, 2, 'incr was called immediately');
440 _.delay(function(){ assert.equal(counter, 3, 'incr was throttled'); done(); }, 64);
443 QUnit.test('throttle cancel with leading: false', function(assert) {
444 var done = assert.async();
446 var incr = function(){ counter++; };
447 var throttledIncr = _.throttle(incr, 32, {leading: false});
449 throttledIncr.cancel();
451 assert.equal(counter, 0, 'incr was throttled');
452 _.delay(function(){ assert.equal(counter, 0, 'incr was throttled'); done(); }, 64);
455 QUnit.test('debounce', function(assert) {
457 var done = assert.async();
459 var incr = function(){ counter++; };
460 var debouncedIncr = _.debounce(incr, 32);
461 debouncedIncr(); debouncedIncr();
462 _.delay(debouncedIncr, 16);
463 _.delay(function(){ assert.equal(counter, 1, 'incr was debounced'); done(); }, 96);
466 QUnit.test('debounce cancel', function(assert) {
468 var done = assert.async();
470 var incr = function(){ counter++; };
471 var debouncedIncr = _.debounce(incr, 32);
473 debouncedIncr.cancel();
474 _.delay(function(){ assert.equal(counter, 0, 'incr was not called'); done(); }, 96);
477 QUnit.test('debounce asap', function(assert) {
479 var done = assert.async();
482 var incr = function(){ return ++counter; };
483 var debouncedIncr = _.debounce(incr, 64, true);
488 assert.equal(counter, 1, 'incr was called immediately');
489 _.delay(debouncedIncr, 16);
490 _.delay(debouncedIncr, 32);
491 _.delay(debouncedIncr, 48);
493 assert.equal(counter, 1, 'incr was debounced');
496 assert.equal(counter, 2, 'incr was called again');
501 QUnit.test('debounce asap cancel', function(assert) {
503 var done = assert.async();
506 var incr = function(){ return ++counter; };
507 var debouncedIncr = _.debounce(incr, 64, true);
509 debouncedIncr.cancel();
513 assert.equal(counter, 2, 'incr was called immediately');
514 _.delay(debouncedIncr, 16);
515 _.delay(debouncedIncr, 32);
516 _.delay(debouncedIncr, 48);
517 _.delay(function(){ assert.equal(counter, 2, 'incr was debounced'); done(); }, 128);
520 QUnit.test('debounce asap recursively', function(assert) {
522 var done = assert.async();
524 var debouncedIncr = _.debounce(function(){
526 if (counter < 10) debouncedIncr();
529 assert.equal(counter, 1, 'incr was called immediately');
530 _.delay(function(){ assert.equal(counter, 1, 'incr was debounced'); done(); }, 96);
533 QUnit.test('debounce after system time is set backwards', function(assert) {
535 var done = assert.async();
537 var origNowFunc = _.now;
538 var debouncedIncr = _.debounce(function(){
543 assert.equal(counter, 1, 'incr was called immediately');
546 return new Date(2013, 0, 1, 1, 1, 1);
551 assert.equal(counter, 2, 'incr was debounced successfully');
557 QUnit.test('debounce re-entrant', function(assert) {
559 var done = assert.async();
565 var append = function(arg){
567 var args = sequence.pop();
569 debouncedAppend.call(args[0], args[1]);
572 debouncedAppend = _.debounce(append, 32);
573 debouncedAppend.call('a1', 'a2');
574 assert.equal(value, '');
576 assert.equal(value, 'a1a2b1b2', 'append was debounced successfully');
581 QUnit.test('once', function(assert) {
583 var increment = _.once(function(){ return ++num; });
586 assert.equal(num, 1);
588 assert.equal(increment(), 1, 'stores a memo to the last value');
591 QUnit.test('Recursive onced function.', function(assert) {
593 var f = _.once(function(){
600 QUnit.test('wrap', function(assert) {
601 var greet = function(name){ return 'hi: ' + name; };
602 var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
603 assert.equal(backwards('moe'), 'hi: moe eom', 'wrapped the salutation function');
605 var inner = function(){ return 'Hello '; };
606 var obj = {name: 'Moe'};
607 obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });
608 assert.equal(obj.hi(), 'Hello Moe');
610 var noop = function(){};
611 var wrapped = _.wrap(noop, function(){ return Array.prototype.slice.call(arguments, 0); });
612 var ret = wrapped(['whats', 'your'], 'vector', 'victor');
613 assert.deepEqual(ret, [noop, ['whats', 'your'], 'vector', 'victor']);
616 QUnit.test('negate', function(assert) {
617 var isOdd = function(n){ return n & 1; };
618 assert.equal(_.negate(isOdd)(2), true, 'should return the complement of the given function');
619 assert.equal(_.negate(isOdd)(3), false, 'should return the complement of the given function');
622 QUnit.test('compose', function(assert) {
623 var greet = function(name){ return 'hi: ' + name; };
624 var exclaim = function(sentence){ return sentence + '!'; };
625 var composed = _.compose(exclaim, greet);
626 assert.equal(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
628 composed = _.compose(greet, exclaim);
629 assert.equal(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
632 function h(x, y, z) {
633 assert.equal(arguments.length, 3, 'First function called with multiple args');
637 assert.equal(arguments.length, 1, 'Composed function is called with 1 argument');
641 assert.equal(arguments.length, 1, 'Composed function is called with 1 argument');
644 composed = _.compose(f, g, h);
645 assert.equal(composed(1, 2, 3), 12);
648 QUnit.test('after', function(assert) {
649 var testAfter = function(afterAmount, timesCalled) {
651 var after = _.after(afterAmount, function() {
654 while (timesCalled--) after();
658 assert.equal(testAfter(5, 5), 1, 'after(N) should fire after being called N times');
659 assert.equal(testAfter(5, 4), 0, 'after(N) should not fire unless called N times');
660 assert.equal(testAfter(0, 0), 0, 'after(0) should not fire immediately');
661 assert.equal(testAfter(0, 1), 1, 'after(0) should fire when first invoked');
664 QUnit.test('before', function(assert) {
665 var testBefore = function(beforeAmount, timesCalled) {
666 var beforeCalled = 0;
667 var before = _.before(beforeAmount, function() { beforeCalled++; });
668 while (timesCalled--) before();
672 assert.equal(testBefore(5, 5), 4, 'before(N) should not fire after being called N times');
673 assert.equal(testBefore(5, 4), 4, 'before(N) should fire before being called N times');
674 assert.equal(testBefore(0, 0), 0, 'before(0) should not fire immediately');
675 assert.equal(testBefore(0, 1), 0, 'before(0) should not fire when first invoked');
677 var context = {num: 0};
678 var increment = _.before(3, function(){ return ++this.num; });
679 _.times(10, increment, context);
680 assert.equal(increment(), 2, 'stores a memo to the last value');
681 assert.equal(context.num, 2, 'provides context');
684 QUnit.test('iteratee', function(assert) {
685 var identity = _.iteratee();
686 assert.equal(identity, _.identity, '_.iteratee is exposed as an external function.');
691 _.each([_.iteratee(fn), _.iteratee(fn, {})], function(cb) {
692 assert.equal(cb().length, 0);
693 assert.deepEqual(_.toArray(cb(1, 2, 3)), _.range(1, 4));
694 assert.deepEqual(_.toArray(cb(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), _.range(1, 11));
699 QUnit.test('restArgs', function(assert) {
701 _.restArgs(function(a, args) {
702 assert.strictEqual(a, 1);
703 assert.deepEqual(args, [2, 3], 'collects rest arguments into an array');
706 _.restArgs(function(a, args) {
707 assert.strictEqual(a, void 0);
708 assert.deepEqual(args, [], 'passes empty array if there are not enough arguments');
711 _.restArgs(function(a, b, c, args) {
712 assert.strictEqual(arguments.length, 4);
713 assert.deepEqual(args, [4, 5], 'works on functions with many named parameters');
717 _.restArgs(function() {
718 assert.strictEqual(this, obj, 'invokes function with this context');
721 _.restArgs(function(array, iteratee, context) {
722 assert.deepEqual(array, [1, 2, 3, 4], 'startIndex can be used manually specify index of rest parameter');
723 assert.strictEqual(iteratee, void 0);
724 assert.strictEqual(context, void 0);