1 $(document).ready(function() {
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');
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');
15 _.forEach([1, 2, 3], function(num){ answers.push(num); });
16 equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');
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;
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');
30 _.each(null, function(){ ++answers; });
31 equals(answers, 0, 'handles a null properly');
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');
38 doubled = _.collect([1, 2, 3], function(num){ return num * 2; });
39 equals(doubled.join(', '), '2, 4, 6', 'aliased as "collect"');
41 var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});
42 equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');
44 var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
45 equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
47 var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });
48 ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
50 var ids = _.map(document.images, function(n){ return n.id; });
51 ok(ids[0] == 'chart_image', 'can use collection methods on HTMLCollections');
53 var ifnull = _.map(null, function(){});
54 ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');
56 var length = _.map(Array(2), function(v) { return v; }).length;
57 equals(length, 2, "can preserve a sparse array's length");
60 test('collections: reduce', function() {
61 var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);
62 equals(sum, 6, 'can sum up an array');
64 var context = {multiplier : 3};
65 sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);
66 equals(sum, 18, 'can reduce with a context object');
68 sum = _.inject([1, 2, 3], function(sum, num){ return sum + num; }, 0);
69 equals(sum, 6, 'aliased as "inject"');
71 sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
72 equals(sum, 6, 'OO-style reduce');
74 var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
75 equals(sum, 6, 'default initial value');
79 _.reduce(null, function(){});
83 ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
85 ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
86 equals(_.reduce([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
87 raises(function() { _.reduce([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
92 equals(_.reduce(sparseArray, function(a, b){ return a - b; }), 25, 'initially-sparse arrays with no memo');
95 test('collections: reduceRight', function() {
96 var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
97 equals(list, 'bazbarfoo', 'can perform right folds');
99 var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
100 equals(list, 'bazbarfoo', 'aliased as "foldr"');
102 var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
103 equals(list, 'bazbarfoo', 'default initial value');
107 _.reduceRight(null, function(){});
111 ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');
113 ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');
115 equals(_.reduceRight([], function(){}, undefined), undefined, 'undefined can be passed as a special case');
116 raises(function() { _.reduceRight([], function(){}); }, TypeError, 'throws an error for empty arrays with no initial value');
118 var sparseArray = [];
121 equals(_.reduceRight(sparseArray, function(a, b){ return a - b; }), -25, 'initially-sparse arrays with no memo');
124 test('collections: detect', function() {
125 var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
126 equals(result, 2, 'found the first "2" and broke the loop');
129 test('collections: select', function() {
130 var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
131 equals(evens.join(', '), '2, 4, 6', 'selected each even number');
133 evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
134 equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');
137 test('collections: reject', function() {
138 var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
139 equals(odds.join(', '), '1, 3, 5', 'rejected each even number');
142 test('collections: all', function() {
143 ok(_.all([], _.identity), 'the empty set');
144 ok(_.all([true, true, true], _.identity), 'all true values');
145 ok(!_.all([true, false, true], _.identity), 'one false value');
146 ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');
147 ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');
148 ok(_.every([true, true, true], _.identity), 'aliased as "every"');
151 test('collections: any', function() {
152 var nativeSome = Array.prototype.some;
153 Array.prototype.some = null;
154 ok(!_.any([]), 'the empty set');
155 ok(!_.any([false, false, false]), 'all false values');
156 ok(_.any([false, false, true]), 'one true value');
157 ok(_.any([null, 0, 'yes', false]), 'a string');
158 ok(!_.any([null, 0, '', false]), 'falsy values');
159 ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');
160 ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');
161 ok(_.some([false, false, true]), 'aliased as "some"');
162 Array.prototype.some = nativeSome;
165 test('collections: include', function() {
166 ok(_.include([1,2,3], 2), 'two is in the array');
167 ok(!_.include([1,3,9], 2), 'two is not in the array');
168 ok(_.contains({moe:1, larry:3, curly:9}, 3) === true, '_.include on objects checks their values');
169 ok(_([1,2,3]).include(2), 'OO-style include');
172 test('collections: invoke', function() {
173 var list = [[5, 1, 7], [3, 2, 1]];
174 var result = _.invoke(list, 'sort');
175 equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
176 equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
179 test('collections: invoke w/ function reference', function() {
180 var list = [[5, 1, 7], [3, 2, 1]];
181 var result = _.invoke(list, Array.prototype.sort);
182 equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
183 equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
186 // Relevant when using ClojureScript
187 test('collections: invoke when strings have a call method', function() {
188 String.prototype.call = function(){return 42;}
189 var list = [[5, 1, 7], [3, 2, 1]];
191 equals(s.call(), 42, "call function exists");
192 var result = _.invoke(list, 'sort');
193 equals(result[0].join(', '), '1, 5, 7', 'first array sorted');
194 equals(result[1].join(', '), '1, 2, 3', 'second array sorted');
195 delete String.prototype.call;
196 equals(s.call, undefined, "call function removed");
199 test('collections: pluck', function() {
200 var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];
201 equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');
204 test('collections: max', function() {
205 equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');
207 var neg = _.max([1, 2, 3], function(num){ return -num; });
208 equals(neg, 1, 'can perform a computation-based max');
210 equals(-Infinity, _.max({}), 'Maximum value of an empty object');
211 equals(-Infinity, _.max([]), 'Maximum value of an empty array');
214 test('collections: min', function() {
215 equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');
217 var neg = _.min([1, 2, 3], function(num){ return -num; });
218 equals(neg, 3, 'can perform a computation-based min');
220 equals(Infinity, _.min({}), 'Minimum value of an empty object');
221 equals(Infinity, _.min([]), 'Minimum value of an empty array');
224 test('collections: sortBy', function() {
225 var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
226 people = _.sortBy(people, function(person){ return person.age; });
227 equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
230 test('collections: groupBy', function() {
231 var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
232 ok('0' in parity && '1' in parity, 'created a group for each value');
233 equals(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
235 var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
236 var grouped = _.groupBy(list, 'length');
237 equals(grouped['3'].join(' '), 'one two six ten');
238 equals(grouped['4'].join(' '), 'four five nine');
239 equals(grouped['5'].join(' '), 'three seven eight');
242 test('collections: sortedIndex', function() {
243 var numbers = [10, 20, 30, 40, 50], num = 35;
244 var index = _.sortedIndex(numbers, num);
245 equals(index, 3, '35 should be inserted at index 3');
248 test('collections: shuffle', function() {
249 var numbers = _.range(10);
250 var shuffled = _.shuffle(numbers).sort();
251 notStrictEqual(numbers, shuffled, 'original object is unmodified');
252 equals(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
255 test('collections: toArray', function() {
256 ok(!_.isArray(arguments), 'arguments object is not an array');
257 ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
259 ok(_.toArray(a) !== a, 'array is cloned');
260 equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
262 var numbers = _.toArray({one : 1, two : 2, three : 3});
263 equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
266 test('collections: size', function() {
267 equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');