Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
$(document).ready(function() {
2
3
module("Collections");
4
5
test("collections: each", function() {
6
_.each([1, 2, 3], function(num, i) {
7
equals(num, i + 1, 'each iterators provide value and iteration count');
8
});
9
10
var answers = [];
11
_.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});
12
equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
13
14
answers = [];
15
_.forEach([1, 2, 3], function(num){ answers.push(num); });
16
equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
17
18
answers = [];
19
var obj = {one : 1, two : 2, three : 3};
20
obj.constructor.prototype.four = 4;
21
_.each(obj, function(value, key){ answers.push(key); });
22
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
23
delete obj.constructor.prototype.four;
24
25
answer = null;
26
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
27
ok(answer, 'can reference the original collection from inside the iterator');
28
29
answers = 0;
30
_.each(null, function(){ ++answers; });
31
equals(answers, 0, 'handles a null properly');
32
});
33
34
test('collections: map', function() {
35
var doubled = _.map([1, 2, 3], function(num){ return num * 2; });
36
equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');
37
38
var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
39
equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
40
41
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
42
equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
43
44
var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });
45
ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
46
47
var ids = _.map(document.images, function(n){ return n.id; });
48
ok(ids[0] == 'chart_image', 'can use collection methods on HTMLCollections');
49
50
var ifnull = _.map(null, function(){});
51
ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');
52
});
53
54
test('collections: reduce', function() {
55
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);
56
equals(sum, 6, 'can sum up an array');
57
58
var context = {multiplier : 3};
59
sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);
60
equals(sum, 18, 'can reduce with a context object');
61
62
sum = _.inject([1, 2, 3], function(sum, num){ return sum + num; }, 0);
63
equals(sum, 6, 'aliased as "inject"');
64
65
sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
66
equals(sum, 6, 'OO-style reduce');
67
68
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
69
equals(sum, 6, 'default initial value');
70
71
var ifnull;
72
try {
73
_.reduce(null, function(){});
74
} catch (ex) {
75
ifnull = ex;
76
}
77
ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
78
79
ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
80
81
// Sparse arrays:
82
var sparseArray = [];
83
sparseArray[100] = 10;
84
sparseArray[200] = 20;
85
86
equals(_.reduce(sparseArray, function(a, b){ return a + b }), 30, 'initially-sparse arrays with no memo');
87
});
88
89
test('collections: reduceRight', function() {
90
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
91
equals(list, 'bazbarfoo', 'can perform right folds');
92
93
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
94
equals(list, 'bazbarfoo', 'aliased as "foldr"');
95
96
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
97
equals(list, 'bazbarfoo', 'default initial value');
98
99
var ifnull;
100
try {
101
_.reduceRight(null, function(){});
102
} catch (ex) {
103
ifnull = ex;
104
}
105
ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
106
107
ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
108
});
109
110
test('collections: detect', function() {
111
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
112
equals(result, 2, 'found the first "2" and broke the loop');
113
});
114
115
test('collections: select', function() {
116
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
117
equals(evens.join(', '), '2, 4, 6', 'selected each even number');
118
119
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
120
equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
121
});
122
123
test('collections: reject', function() {
124
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
125
equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
126
});
127
128
test('collections: all', function() {
129
ok(_.all([], _.identity), 'the empty set');
130
ok(_.all([true, true, true], _.identity), 'all true values');
131
ok(!_.all([true, false, true], _.identity), 'one false value');
132
ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');
133
ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');
134
ok(_.every([true, true, true], _.identity), 'aliased as "every"');
135
});
136
137
test('collections: any', function() {
138
var nativeSome = Array.prototype.some;
139
Array.prototype.some = null;
140
ok(!_.any([]), 'the empty set');
141
ok(!_.any([false, false, false]), 'all false values');
142
ok(_.any([false, false, true]), 'one true value');
143
ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');
144
ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');
145
ok(_.some([false, false, true]), 'aliased as "some"');
146
Array.prototype.some = nativeSome;
147
});
148
149
test('collections: include', function() {
150
ok(_.include([1,2,3], 2), 'two is in the array');
151
ok(!_.include([1,3,9], 2), 'two is not in the array');
152
ok(_.contains({moe:1, larry:3, curly:9}, 3) === true, '_.include on objects checks their values');
153
ok(_([1,2,3]).include(2), 'OO-style include');
154
});
155
156
test('collections: invoke', function() {
157
var list = [[5, 1, 7], [3, 2, 1]];
158
var result = _.invoke(list, 'sort');
159
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
160
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
161
});
162
163
test('collections: invoke w/ function reference', function() {
164
var list = [[5, 1, 7], [3, 2, 1]];
165
var result = _.invoke(list, Array.prototype.sort);
166
equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
167
equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
168
});
169
170
test('collections: pluck', function() {
171
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
172
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
173
});
174
175
test('collections: max', function() {
176
equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
177
178
var neg = _.max([1, 2, 3], function(num){ return -num; });
179
equals(neg, 1, 'can perform a computation-based max');
180
181
equals(-Infinity, _.max({}), 'Maximum value of an empty object');
182
equals(-Infinity, _.max([]), 'Maximum value of an empty array');
183
});
184
185
test('collections: min', function() {
186
equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
187
188
var neg = _.min([1, 2, 3], function(num){ return -num; });
189
equals(neg, 3, 'can perform a computation-based min');
190
191
equals(Infinity, _.min({}), 'Minimum value of an empty object');
192
equals(Infinity, _.min([]), 'Minimum value of an empty array');
193
});
194
195
test('collections: sortBy', function() {
196
var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
197
people = _.sortBy(people, function(person){ return person.age; });
198
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
199
});
200
201
test('collections: groupBy', function() {
202
var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
203
ok('0' in parity && '1' in parity, 'created a group for each value');
204
equals(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
205
206
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
207
var grouped = _.groupBy(list, 'length');
208
equals(grouped['3'].join(' '), 'one two six ten');
209
equals(grouped['4'].join(' '), 'four five nine');
210
equals(grouped['5'].join(' '), 'three seven eight');
211
});
212
213
test('collections: sortedIndex', function() {
214
var numbers = [10, 20, 30, 40, 50], num = 35;
215
var index = _.sortedIndex(numbers, num);
216
equals(index, 3, '35 should be inserted at index 3');
217
});
218
219
test('collections: shuffle', function() {
220
var numbers = _.range(10);
221
var shuffled = _.shuffle(numbers).sort();
222
notStrictEqual(numbers, shuffled, 'original object is unmodified');
223
equals(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
224
});
225
226
test('collections: toArray', function() {
227
ok(!_.isArray(arguments), 'arguments object is not an array');
228
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
229
var a = [1,2,3];
230
ok(_.toArray(a) !== a, 'array is cloned');
231
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
232
233
var numbers = _.toArray({one : 1, two : 2, three : 3});
234
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
235
});
236
237
test('collections: size', function() {
238
equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
239
});
240
241
});
242
243