Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
$(document).ready(function() {
2
3
module("Functions");
4
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');
10
11
bound = _(func).bind(context);
12
equals(bound(), 'name: moe', 'can do OO-style binding');
13
14
bound = _.bind(func, null, 'curly');
15
equals(bound(), 'name: curly', 'can bind without specifying a context');
16
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');
20
21
var func = _.bind(func, this, 'curly');
22
equals(func(), 'hello: curly', 'the function was completely applied in advance');
23
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');
27
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`')();
32
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");
39
});
40
41
test("functions: bindAll", function() {
42
var curly = {name : 'curly'}, moe = {
43
name : 'moe',
44
getName : function() { return 'name: ' + this.name; },
45
sayHi : function() { return 'hi: ' + this.name; }
46
};
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');
52
53
curly = {name : 'curly'};
54
moe = {
55
name : 'moe',
56
getName : function() { return 'name: ' + this.name; },
57
sayHi : function() { return 'hi: ' + this.name; }
58
};
59
_.bindAll(moe);
60
curly.sayHi = moe.sayHi;
61
equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
62
});
63
64
test("functions: memoize", function() {
65
var fib = function(n) {
66
return n < 2 ? n : fib(n - 1) + fib(n - 2);
67
};
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');
71
72
var o = function(str) {
73
return str;
74
};
75
var fastO = _.memoize(o);
76
equals(o('toString'), 'toString', 'checks hasOwnProperty');
77
equals(fastO('toString'), 'toString', 'checks hasOwnProperty');
78
});
79
80
asyncTest("functions: delay", 2, function() {
81
var delayed = false;
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);
85
});
86
87
asyncTest("functions: defer", 1, function() {
88
var deferred = false;
89
_.defer(function(bool){ deferred = bool; }, true);
90
_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
91
});
92
93
asyncTest("functions: throttle", 2, function() {
94
var counter = 0;
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);
106
});
107
108
asyncTest("functions: throttle arguments", 2, function() {
109
var value = 0;
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); }, 260);
116
setTimeout(function(){ throttledUpdate(7); }, 270);
117
_.delay(function(){ ok(value == 1, "updated to latest value"); }, 40);
118
_.delay(function(){ ok(value == 7, "updated to latest value"); start(); }, 400);
119
});
120
121
asyncTest("functions: throttle once", 1, function() {
122
var counter = 0;
123
var incr = function(){ counter++; };
124
var throttledIncr = _.throttle(incr, 100);
125
throttledIncr();
126
_.delay(function(){ ok(counter == 1, "incr was called once"); start(); }, 220);
127
});
128
129
asyncTest("functions: throttle twice", 1, function() {
130
var counter = 0;
131
var incr = function(){ counter++; };
132
var throttledIncr = _.throttle(incr, 100);
133
throttledIncr(); throttledIncr();
134
_.delay(function(){ ok(counter == 2, "incr was called twice"); start(); }, 220);
135
});
136
137
asyncTest("functions: debounce", 1, function() {
138
var counter = 0;
139
var incr = function(){ counter++; };
140
var debouncedIncr = _.debounce(incr, 50);
141
debouncedIncr(); debouncedIncr(); debouncedIncr();
142
setTimeout(debouncedIncr, 30);
143
setTimeout(debouncedIncr, 60);
144
setTimeout(debouncedIncr, 90);
145
setTimeout(debouncedIncr, 120);
146
setTimeout(debouncedIncr, 150);
147
_.delay(function(){ ok(counter == 1, "incr was debounced"); start(); }, 220);
148
});
149
150
test("functions: once", function() {
151
var num = 0;
152
var increment = _.once(function(){ num++; });
153
increment();
154
increment();
155
equals(num, 1);
156
});
157
158
test("functions: wrap", function() {
159
var greet = function(name){ return "hi: " + name; };
160
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
161
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
162
163
var inner = function(){ return "Hello "; };
164
var obj = {name : "Moe"};
165
obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });
166
equals(obj.hi(), "Hello Moe");
167
});
168
169
test("functions: compose", function() {
170
var greet = function(name){ return "hi: " + name; };
171
var exclaim = function(sentence){ return sentence + '!'; };
172
var composed = _.compose(exclaim, greet);
173
equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
174
175
composed = _.compose(greet, exclaim);
176
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
177
});
178
179
test("functions: after", function() {
180
var testAfter = function(afterAmount, timesCalled) {
181
var afterCalled = 0;
182
var after = _.after(afterAmount, function() {
183
afterCalled++;
184
});
185
while (timesCalled--) after();
186
return afterCalled;
187
};
188
189
equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");
190
equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
191
});
192
193
});
194
195