1 $(document).ready(function() {
5 test("functions: bind", function() {
6 var context = {name : 'moe'};
7 var func = function(arg) { return "name: " + (this.name || arg); };
8 var bound = _.bind(func, context);
9 equals(bound(), 'name: moe', 'can bind a function to a context');
11 bound = _(func).bind(context);
12 equals(bound(), 'name: moe', 'can do OO-style binding');
14 bound = _.bind(func, null, 'curly');
15 equals(bound(), 'name: curly', 'can bind without specifying a context');
17 func = function(salutation, name) { return salutation + ': ' + name; };
18 func = _.bind(func, this, 'hello');
19 equals(func('moe'), 'hello: moe', 'the function was partially applied in advance');
21 var func = _.bind(func, this, 'curly');
22 equals(func(), 'hello: curly', 'the function was completely applied in advance');
24 var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };
25 func = _.bind(func, this, 'hello', 'moe', 'curly');
26 equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
28 func = function(context, message) { equals(this, context, message); };
29 _.bind(func, 0, 0, 'can bind a function to `0`')();
30 _.bind(func, '', '', 'can bind a function to an empty string')();
31 _.bind(func, false, false, 'can bind a function to `false`')();
33 // These tests are only meaningful when using a browser without a native bind function
34 // To test this with a modern browser, set underscore's nativeBind to undefined
35 var F = function () { return this; };
36 var Boundf = _.bind(F, {hello: "moe curly"});
37 equal(new Boundf().hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");
38 equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");
41 test("functions: bindAll", function() {
42 var curly = {name : 'curly'}, moe = {
44 getName : function() { return 'name: ' + this.name; },
45 sayHi : function() { return 'hi: ' + this.name; }
47 curly.getName = moe.getName;
48 _.bindAll(moe, 'getName', 'sayHi');
49 curly.sayHi = moe.sayHi;
50 equals(curly.getName(), 'name: curly', 'unbound function is bound to current object');
51 equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
53 curly = {name : 'curly'};
56 getName : function() { return 'name: ' + this.name; },
57 sayHi : function() { return 'hi: ' + this.name; }
60 curly.sayHi = moe.sayHi;
61 equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
64 test("functions: memoize", function() {
65 var fib = function(n) {
66 return n < 2 ? n : fib(n - 1) + fib(n - 2);
68 var fastFib = _.memoize(fib);
69 equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');
70 equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
72 var o = function(str) {
75 var fastO = _.memoize(o);
76 equals(o('toString'), 'toString', 'checks hasOwnProperty');
77 equals(fastO('toString'), 'toString', 'checks hasOwnProperty');
80 asyncTest("functions: delay", 2, function() {
82 _.delay(function(){ delayed = true; }, 100);
83 setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);
84 setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);
87 asyncTest("functions: defer", 1, function() {
89 _.defer(function(bool){ deferred = bool; }, true);
90 _.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
93 asyncTest("functions: throttle", 2, function() {
95 var incr = function(){ counter++; };
96 var throttledIncr = _.throttle(incr, 100);
97 throttledIncr(); throttledIncr(); throttledIncr();
98 setTimeout(throttledIncr, 70);
99 setTimeout(throttledIncr, 120);
100 setTimeout(throttledIncr, 140);
101 setTimeout(throttledIncr, 190);
102 setTimeout(throttledIncr, 220);
103 setTimeout(throttledIncr, 240);
104 _.delay(function(){ ok(counter == 1, "incr was called immediately"); }, 30);
105 _.delay(function(){ ok(counter == 4, "incr was throttled"); start(); }, 400);
108 asyncTest("functions: throttle arguments", 2, function() {
110 var update = function(val){ value = val; };
111 var throttledUpdate = _.throttle(update, 100);
112 throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);
113 setTimeout(function(){ throttledUpdate(4); }, 120);
114 setTimeout(function(){ throttledUpdate(5); }, 140);
115 setTimeout(function(){ throttledUpdate(6); }, 250);
116 _.delay(function(){ equals(value, 1, "updated to latest value"); }, 40);
117 _.delay(function(){ equals(value, 6, "updated to latest value"); start(); }, 400);
120 asyncTest("functions: throttle once", 1, function() {
122 var incr = function(){ counter++; };
123 var throttledIncr = _.throttle(incr, 100);
125 _.delay(function(){ ok(counter == 1, "incr was called once"); start(); }, 220);
128 asyncTest("functions: throttle twice", 1, function() {
130 var incr = function(){ counter++; };
131 var throttledIncr = _.throttle(incr, 100);
132 throttledIncr(); throttledIncr();
133 _.delay(function(){ ok(counter == 2, "incr was called twice"); start(); }, 220);
136 asyncTest("functions: debounce", 1, function() {
138 var incr = function(){ counter++; };
139 var debouncedIncr = _.debounce(incr, 50);
140 debouncedIncr(); debouncedIncr(); debouncedIncr();
141 setTimeout(debouncedIncr, 30);
142 setTimeout(debouncedIncr, 60);
143 setTimeout(debouncedIncr, 90);
144 setTimeout(debouncedIncr, 120);
145 setTimeout(debouncedIncr, 150);
146 _.delay(function(){ ok(counter == 1, "incr was debounced"); start(); }, 220);
149 test("functions: once", function() {
151 var increment = _.once(function(){ num++; });
157 test("functions: wrap", function() {
158 var greet = function(name){ return "hi: " + name; };
159 var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
160 equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
162 var inner = function(){ return "Hello "; };
163 var obj = {name : "Moe"};
164 obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });
165 equals(obj.hi(), "Hello Moe");
167 var noop = function(){};
168 var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); });
169 var ret = wrapped(['whats', 'your'], 'vector', 'victor');
170 same(ret, [noop, ['whats', 'your'], 'vector', 'victor']);
173 test("functions: compose", function() {
174 var greet = function(name){ return "hi: " + name; };
175 var exclaim = function(sentence){ return sentence + '!'; };
176 var composed = _.compose(exclaim, greet);
177 equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
179 composed = _.compose(greet, exclaim);
180 equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
183 test("functions: after", function() {
184 var testAfter = function(afterAmount, timesCalled) {
186 var after = _.after(afterAmount, function() {
189 while (timesCalled--) after();
193 equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");
194 equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
195 equals(testAfter(0, 0), 1, "after(0) should fire immediately");