2 var _ = typeof require == 'function' ? require('..') : window._;
4 QUnit.module('Arrays');
6 QUnit.test('first', function(assert) {
7 assert.equal(_.first([1, 2, 3]), 1, 'can pull out the first element of an array');
8 assert.equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
9 assert.deepEqual(_.first([1, 2, 3], 0), [], 'returns an empty array when n <= 0 (0 case)');
10 assert.deepEqual(_.first([1, 2, 3], -1), [], 'returns an empty array when n <= 0 (negative case)');
11 assert.deepEqual(_.first([1, 2, 3], 2), [1, 2], 'can fetch the first n elements');
12 assert.deepEqual(_.first([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length');
13 var result = (function(){ return _.first(arguments); }(4, 3, 2, 1));
14 assert.equal(result, 4, 'works on an arguments object');
15 result = _.map([[1, 2, 3], [1, 2, 3]], _.first);
16 assert.deepEqual(result, [1, 1], 'works well with _.map');
17 assert.equal(_.first(null), void 0, 'returns undefined when called on null');
20 QUnit.test('head', function(assert) {
21 assert.strictEqual(_.head, _.first, 'is an alias for first');
24 QUnit.test('take', function(assert) {
25 assert.strictEqual(_.take, _.first, 'is an alias for first');
28 QUnit.test('rest', function(assert) {
29 var numbers = [1, 2, 3, 4];
30 assert.deepEqual(_.rest(numbers), [2, 3, 4], 'fetches all but the first element');
31 assert.deepEqual(_.rest(numbers, 0), [1, 2, 3, 4], 'returns the whole array when index is 0');
32 assert.deepEqual(_.rest(numbers, 2), [3, 4], 'returns elements starting at the given index');
33 var result = (function(){ return _(arguments).rest(); }(1, 2, 3, 4));
34 assert.deepEqual(result, [2, 3, 4], 'works on an arguments object');
35 result = _.map([[1, 2, 3], [1, 2, 3]], _.rest);
36 assert.deepEqual(_.flatten(result), [2, 3, 2, 3], 'works well with _.map');
39 QUnit.test('tail', function(assert) {
40 assert.strictEqual(_.tail, _.rest, 'is an alias for rest');
43 QUnit.test('drop', function(assert) {
44 assert.strictEqual(_.drop, _.rest, 'is an alias for rest');
47 QUnit.test('initial', function(assert) {
48 assert.deepEqual(_.initial([1, 2, 3, 4, 5]), [1, 2, 3, 4], 'returns all but the last element');
49 assert.deepEqual(_.initial([1, 2, 3, 4], 2), [1, 2], 'returns all but the last n elements');
50 assert.deepEqual(_.initial([1, 2, 3, 4], 6), [], 'returns an empty array when n > length');
51 var result = (function(){ return _(arguments).initial(); }(1, 2, 3, 4));
52 assert.deepEqual(result, [1, 2, 3], 'works on an arguments object');
53 result = _.map([[1, 2, 3], [1, 2, 3]], _.initial);
54 assert.deepEqual(_.flatten(result), [1, 2, 1, 2], 'works well with _.map');
57 QUnit.test('last', function(assert) {
58 assert.equal(_.last([1, 2, 3]), 3, 'can pull out the last element of an array');
59 assert.equal(_([1, 2, 3]).last(), 3, 'can perform OO-style "last()"');
60 assert.deepEqual(_.last([1, 2, 3], 0), [], 'returns an empty array when n <= 0 (0 case)');
61 assert.deepEqual(_.last([1, 2, 3], -1), [], 'returns an empty array when n <= 0 (negative case)');
62 assert.deepEqual(_.last([1, 2, 3], 2), [2, 3], 'can fetch the last n elements');
63 assert.deepEqual(_.last([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length');
64 var result = (function(){ return _(arguments).last(); }(1, 2, 3, 4));
65 assert.equal(result, 4, 'works on an arguments object');
66 result = _.map([[1, 2, 3], [1, 2, 3]], _.last);
67 assert.deepEqual(result, [3, 3], 'works well with _.map');
68 assert.equal(_.last(null), void 0, 'returns undefined when called on null');
71 QUnit.test('compact', function(assert) {
72 assert.deepEqual(_.compact([1, false, null, 0, '', void 0, NaN, 2]), [1, 2], 'removes all falsy values');
73 var result = (function(){ return _.compact(arguments); }(0, 1, false, 2, false, 3));
74 assert.deepEqual(result, [1, 2, 3], 'works on an arguments object');
75 result = _.map([[1, false, false], [false, false, 3]], _.compact);
76 assert.deepEqual(result, [[1], [3]], 'works well with _.map');
79 QUnit.test('flatten', function(assert) {
80 assert.deepEqual(_.flatten(null), [], 'supports null');
81 assert.deepEqual(_.flatten(void 0), [], 'supports undefined');
83 assert.deepEqual(_.flatten([[], [[]], []]), [], 'supports empty arrays');
84 assert.deepEqual(_.flatten([[], [[]], []], true), [[]], 'can shallowly flatten empty arrays');
86 var list = [1, [2], [3, [[[4]]]]];
87 assert.deepEqual(_.flatten(list), [1, 2, 3, 4], 'can flatten nested arrays');
88 assert.deepEqual(_.flatten(list, true), [1, 2, 3, [[[4]]]], 'can shallowly flatten nested arrays');
89 var result = (function(){ return _.flatten(arguments); }(1, [2], [3, [[[4]]]]));
90 assert.deepEqual(result, [1, 2, 3, 4], 'works on an arguments object');
91 list = [[1], [2], [3], [[4]]];
92 assert.deepEqual(_.flatten(list, true), [1, 2, 3, [4]], 'can shallowly flatten arrays containing only other arrays');
94 assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3], true).length, 23, 'can flatten medium length arrays');
95 assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3]).length, 23, 'can shallowly flatten medium length arrays');
96 assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3]).length, 1056003, 'can handle massive arrays');
97 assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3], true).length, 1056003, 'can handle massive arrays in shallow mode');
99 var x = _.range(100000);
100 for (var i = 0; i < 1000; i++) x = [x];
101 assert.deepEqual(_.flatten(x), _.range(100000), 'can handle very deep arrays');
102 assert.deepEqual(_.flatten(x, true), x[0], 'can handle very deep arrays in shallow mode');
105 QUnit.test('without', function(assert) {
106 var list = [1, 2, 1, 0, 3, 1, 4];
107 assert.deepEqual(_.without(list, 0, 1), [2, 3, 4], 'removes all instances of the given values');
108 var result = (function(){ return _.without(arguments, 0, 1); }(1, 2, 1, 0, 3, 1, 4));
109 assert.deepEqual(result, [2, 3, 4], 'works on an arguments object');
111 list = [{one: 1}, {two: 2}];
112 assert.deepEqual(_.without(list, {one: 1}), list, 'compares objects by reference (value case)');
113 assert.deepEqual(_.without(list, list[0]), [{two: 2}], 'compares objects by reference (reference case)');
116 QUnit.test('sortedIndex', function(assert) {
117 var numbers = [10, 20, 30, 40, 50];
118 var indexFor35 = _.sortedIndex(numbers, 35);
119 assert.equal(indexFor35, 3, 'finds the index at which a value should be inserted to retain order');
120 var indexFor30 = _.sortedIndex(numbers, 30);
121 assert.equal(indexFor30, 2, 'finds the smallest index at which a value could be inserted to retain order');
123 var objects = [{x: 10}, {x: 20}, {x: 30}, {x: 40}];
124 var iterator = function(obj){ return obj.x; };
125 assert.strictEqual(_.sortedIndex(objects, {x: 25}, iterator), 2, 'uses the result of `iterator` for order comparisons');
126 assert.strictEqual(_.sortedIndex(objects, {x: 35}, 'x'), 3, 'when `iterator` is a string, uses that key for order comparisons');
128 var context = {1: 2, 2: 3, 3: 4};
129 iterator = function(obj){ return this[obj]; };
130 assert.strictEqual(_.sortedIndex([1, 3], 2, iterator, context), 1, 'can execute its iterator in the given context');
132 var values = [0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287,
133 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647];
134 var largeArray = Array(Math.pow(2, 32) - 1);
135 var length = values.length;
136 // Sparsely populate `array`
138 largeArray[values[length]] = values[length];
140 assert.equal(_.sortedIndex(largeArray, 2147483648), 2147483648, 'works with large indexes');
143 QUnit.test('uniq', function(assert) {
144 var list = [1, 2, 1, 3, 1, 4];
145 assert.deepEqual(_.uniq(list), [1, 2, 3, 4], 'can find the unique values of an unsorted array');
146 list = [1, 1, 1, 2, 2, 3];
147 assert.deepEqual(_.uniq(list, true), [1, 2, 3], 'can find the unique values of a sorted array faster');
149 list = [{name: 'Moe'}, {name: 'Curly'}, {name: 'Larry'}, {name: 'Curly'}];
150 var expected = [{name: 'Moe'}, {name: 'Curly'}, {name: 'Larry'}];
151 var iterator = function(stooge) { return stooge.name; };
152 assert.deepEqual(_.uniq(list, false, iterator), expected, 'uses the result of `iterator` for uniqueness comparisons (unsorted case)');
153 assert.deepEqual(_.uniq(list, iterator), expected, '`sorted` argument defaults to false when omitted');
154 assert.deepEqual(_.uniq(list, 'name'), expected, 'when `iterator` is a string, uses that key for comparisons (unsorted case)');
156 list = [{score: 8}, {score: 10}, {score: 10}];
157 expected = [{score: 8}, {score: 10}];
158 iterator = function(item) { return item.score; };
159 assert.deepEqual(_.uniq(list, true, iterator), expected, 'uses the result of `iterator` for uniqueness comparisons (sorted case)');
160 assert.deepEqual(_.uniq(list, true, 'score'), expected, 'when `iterator` is a string, uses that key for comparisons (sorted case)');
162 assert.deepEqual(_.uniq([{0: 1}, {0: 1}, {0: 1}, {0: 2}], 0), [{0: 1}, {0: 2}], 'can use falsey pluck like iterator');
164 var result = (function(){ return _.uniq(arguments); }(1, 2, 1, 3, 1, 4));
165 assert.deepEqual(result, [1, 2, 3, 4], 'works on an arguments object');
167 var a = {}, b = {}, c = {};
168 assert.deepEqual(_.uniq([a, b, a, b, c]), [a, b, c], 'works on values that can be tested for equivalency but not ordered');
170 assert.deepEqual(_.uniq(null), [], 'returns an empty array when `array` is not iterable');
174 _.uniq(list, function(value, index, array) {
175 assert.strictEqual(this, context, 'executes its iterator in the given context');
176 assert.strictEqual(value, 3, 'passes its iterator the value');
177 assert.strictEqual(index, 0, 'passes its iterator the index');
178 assert.strictEqual(array, list, 'passes its iterator the entire array');
183 QUnit.test('unique', function(assert) {
184 assert.strictEqual(_.unique, _.uniq, 'is an alias for uniq');
187 QUnit.test('intersection', function(assert) {
188 var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
189 assert.deepEqual(_.intersection(stooges, leaders), ['moe'], 'can find the set intersection of two arrays');
190 assert.deepEqual(_(stooges).intersection(leaders), ['moe'], 'can perform an OO-style intersection');
191 var result = (function(){ return _.intersection(arguments, leaders); }('moe', 'curly', 'larry'));
192 assert.deepEqual(result, ['moe'], 'works on an arguments object');
193 var theSixStooges = ['moe', 'moe', 'curly', 'curly', 'larry', 'larry'];
194 assert.deepEqual(_.intersection(theSixStooges, leaders), ['moe'], 'returns a duplicate-free array');
195 result = _.intersection([2, 4, 3, 1], [1, 2, 3]);
196 assert.deepEqual(result, [2, 3, 1], 'preserves the order of the first array');
197 result = _.intersection(null, [1, 2, 3]);
198 assert.deepEqual(result, [], 'returns an empty array when passed null as the first argument');
199 result = _.intersection([1, 2, 3], null);
200 assert.deepEqual(result, [], 'returns an empty array when passed null as an argument beyond the first');
203 QUnit.test('union', function(assert) {
204 var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
205 assert.deepEqual(result, [1, 2, 3, 30, 40], 'can find the union of a list of arrays');
207 result = _([1, 2, 3]).union([2, 30, 1], [1, 40]);
208 assert.deepEqual(result, [1, 2, 3, 30, 40], 'can perform an OO-style union');
210 result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);
211 assert.deepEqual(result, [1, 2, 3, 30, 40, [1]], 'can find the union of a list of nested arrays');
213 result = _.union([10, 20], [1, 30, 10], [0, 40]);
214 assert.deepEqual(result, [10, 20, 1, 30, 0, 40], 'orders values by their first encounter');
216 result = (function(){ return _.union(arguments, [2, 30, 1], [1, 40]); }(1, 2, 3));
217 assert.deepEqual(result, [1, 2, 3, 30, 40], 'works on an arguments object');
219 assert.deepEqual(_.union([1, 2, 3], 4), [1, 2, 3], 'restricts the union to arrays only');
222 QUnit.test('difference', function(assert) {
223 var result = _.difference([1, 2, 3], [2, 30, 40]);
224 assert.deepEqual(result, [1, 3], 'can find the difference of two arrays');
226 result = _([1, 2, 3]).difference([2, 30, 40]);
227 assert.deepEqual(result, [1, 3], 'can perform an OO-style difference');
229 result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]);
230 assert.deepEqual(result, [3, 4], 'can find the difference of three arrays');
232 result = _.difference([8, 9, 3, 1], [3, 8]);
233 assert.deepEqual(result, [9, 1], 'preserves the order of the first array');
235 result = (function(){ return _.difference(arguments, [2, 30, 40]); }(1, 2, 3));
236 assert.deepEqual(result, [1, 3], 'works on an arguments object');
238 result = _.difference([1, 2, 3], 1);
239 assert.deepEqual(result, [1, 2, 3], 'restrict the difference to arrays only');
242 QUnit.test('zip', function(assert) {
243 var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
244 assert.deepEqual(_.zip(names, ages, leaders), [
246 ['larry', 40, void 0],
247 ['curly', 50, void 0]
248 ], 'zipped together arrays of different lengths');
250 var stooges = _.zip(['moe', 30, 'stooge 1'], ['larry', 40, 'stooge 2'], ['curly', 50, 'stooge 3']);
251 assert.deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], ['stooge 1', 'stooge 2', 'stooge 3']], 'zipped pairs');
253 // In the case of different lengths of the tuples, undefined values
254 // should be used as placeholder
255 stooges = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']);
256 assert.deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], [void 0, void 0, 'extra data']], 'zipped pairs with empties');
258 var empty = _.zip([]);
259 assert.deepEqual(empty, [], 'unzipped empty');
261 assert.deepEqual(_.zip(null), [], 'handles null');
262 assert.deepEqual(_.zip(), [], '_.zip() returns []');
265 QUnit.test('unzip', function(assert) {
266 assert.deepEqual(_.unzip(null), [], 'handles null');
268 assert.deepEqual(_.unzip([['a', 'b'], [1, 2]]), [['a', 1], ['b', 2]]);
271 var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
272 assert.deepEqual(_.unzip(zipped), [['fred', 'barney'], [30, 40], [true, false]]);
274 zipped = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']);
275 assert.deepEqual(_.unzip(zipped), [['moe', 30, void 0], ['larry', 40, void 0], ['curly', 50, 'extra data']], 'Uses length of largest array');
278 QUnit.test('object', function(assert) {
279 var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
280 var shouldBe = {moe: 30, larry: 40, curly: 50};
281 assert.deepEqual(result, shouldBe, 'two arrays zipped together into an object');
283 result = _.object([['one', 1], ['two', 2], ['three', 3]]);
284 shouldBe = {one: 1, two: 2, three: 3};
285 assert.deepEqual(result, shouldBe, 'an array of pairs zipped together into an object');
287 var stooges = {moe: 30, larry: 40, curly: 50};
288 assert.deepEqual(_.object(_.pairs(stooges)), stooges, 'an object converted to pairs and back to an object');
290 assert.deepEqual(_.object(null), {}, 'handles nulls');
293 QUnit.test('indexOf', function(assert) {
294 var numbers = [1, 2, 3];
295 assert.equal(_.indexOf(numbers, 2), 1, 'can compute indexOf');
296 var result = (function(){ return _.indexOf(arguments, 2); }(1, 2, 3));
297 assert.equal(result, 1, 'works on an arguments object');
299 _.each([null, void 0, [], false], function(val) {
300 var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val);
301 assert.equal(_.indexOf(val, 2), -1, msg);
302 assert.equal(_.indexOf(val, 2, -1), -1, msg);
303 assert.equal(_.indexOf(val, 2, -20), -1, msg);
304 assert.equal(_.indexOf(val, 2, 15), -1, msg);
308 numbers = [10, 20, 30, 40, 50];
309 var index = _.indexOf(numbers, num, true);
310 assert.equal(index, -1, '35 is not in the list');
312 numbers = [10, 20, 30, 40, 50]; num = 40;
313 index = _.indexOf(numbers, num, true);
314 assert.equal(index, 3, '40 is in the list');
316 numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;
317 assert.equal(_.indexOf(numbers, num, true), 1, '40 is in the list');
318 assert.equal(_.indexOf(numbers, 6, true), -1, '6 isnt in the list');
319 assert.equal(_.indexOf([1, 2, 5, 4, 6, 7], 5, true), -1, 'sorted indexOf doesn\'t uses binary search');
320 assert.ok(_.every(['1', [], {}, null], function() {
321 return _.indexOf(numbers, num, {}) === 1;
322 }), 'non-nums as fromIndex make indexOf assume sorted');
324 numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
325 index = _.indexOf(numbers, 2, 5);
326 assert.equal(index, 7, 'supports the fromIndex argument');
328 index = _.indexOf([,,, 0], void 0);
329 assert.equal(index, 0, 'treats sparse arrays as if they were dense');
331 var array = [1, 2, 3, 1, 2, 3];
332 assert.strictEqual(_.indexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the right index');
333 assert.strictEqual(_.indexOf(array, 1, -2), -1, 'neg `fromIndex` starts at the right index');
334 assert.strictEqual(_.indexOf(array, 2, -3), 4);
335 _.each([-6, -8, -Infinity], function(fromIndex) {
336 assert.strictEqual(_.indexOf(array, 1, fromIndex), 0);
338 assert.strictEqual(_.indexOf([1, 2, 3], 1, true), 0);
340 index = _.indexOf([], void 0, true);
341 assert.equal(index, -1, 'empty array with truthy `isSorted` returns -1');
344 QUnit.test('indexOf with NaN', function(assert) {
345 assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN), 2, 'Expected [1, 2, NaN] to contain NaN');
346 assert.strictEqual(_.indexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN');
348 assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, 1), 2, 'startIndex does not affect result');
349 assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, -2), 2, 'startIndex does not affect result');
352 assert.strictEqual(_.indexOf(arguments, NaN), 2, 'Expected arguments [1, 2, NaN] to contain NaN');
356 QUnit.test('indexOf with +- 0', function(assert) {
357 _.each([-0, +0], function(val) {
358 assert.strictEqual(_.indexOf([1, 2, val, val], val), 2);
359 assert.strictEqual(_.indexOf([1, 2, val, val], -val), 2);
363 QUnit.test('lastIndexOf', function(assert) {
364 var numbers = [1, 0, 1];
365 var falsey = [void 0, '', 0, false, NaN, null, void 0];
366 assert.equal(_.lastIndexOf(numbers, 1), 2);
368 numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
369 numbers.lastIndexOf = null;
370 assert.equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
371 assert.equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
372 var result = (function(){ return _.lastIndexOf(arguments, 1); }(1, 0, 1, 0, 0, 1, 0, 0, 0));
373 assert.equal(result, 5, 'works on an arguments object');
375 _.each([null, void 0, [], false], function(val) {
376 var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val);
377 assert.equal(_.lastIndexOf(val, 2), -1, msg);
378 assert.equal(_.lastIndexOf(val, 2, -1), -1, msg);
379 assert.equal(_.lastIndexOf(val, 2, -20), -1, msg);
380 assert.equal(_.lastIndexOf(val, 2, 15), -1, msg);
383 numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
384 var index = _.lastIndexOf(numbers, 2, 2);
385 assert.equal(index, 1, 'supports the fromIndex argument');
387 var array = [1, 2, 3, 1, 2, 3];
389 assert.strictEqual(_.lastIndexOf(array, 1, 0), 0, 'starts at the correct from idx');
390 assert.strictEqual(_.lastIndexOf(array, 3), 5, 'should return the index of the last matched value');
391 assert.strictEqual(_.lastIndexOf(array, 4), -1, 'should return `-1` for an unmatched value');
393 assert.strictEqual(_.lastIndexOf(array, 1, 2), 0, 'should work with a positive `fromIndex`');
395 _.each([6, 8, Math.pow(2, 32), Infinity], function(fromIndex) {
396 assert.strictEqual(_.lastIndexOf(array, void 0, fromIndex), -1);
397 assert.strictEqual(_.lastIndexOf(array, 1, fromIndex), 3);
398 assert.strictEqual(_.lastIndexOf(array, '', fromIndex), -1);
401 var expected = _.map(falsey, function(value) {
402 return typeof value == 'number' ? -1 : 5;
405 var actual = _.map(falsey, function(fromIndex) {
406 return _.lastIndexOf(array, 3, fromIndex);
409 assert.deepEqual(actual, expected, 'should treat falsey `fromIndex` values, except `0` and `NaN`, as `array.length`');
410 assert.strictEqual(_.lastIndexOf(array, 3, '1'), 5, 'should treat non-number `fromIndex` values as `array.length`');
411 assert.strictEqual(_.lastIndexOf(array, 3, true), 5, 'should treat non-number `fromIndex` values as `array.length`');
413 assert.strictEqual(_.lastIndexOf(array, 2, -3), 1, 'should work with a negative `fromIndex`');
414 assert.strictEqual(_.lastIndexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the right index');
416 assert.deepEqual(_.map([-6, -8, -Infinity], function(fromIndex) {
417 return _.lastIndexOf(array, 1, fromIndex);
421 QUnit.test('lastIndexOf with NaN', function(assert) {
422 assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN), 3, 'Expected [1, 2, NaN] to contain NaN');
423 assert.strictEqual(_.lastIndexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN');
425 assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, 2), 2, 'fromIndex does not affect result');
426 assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, -2), 2, 'fromIndex does not affect result');
429 assert.strictEqual(_.lastIndexOf(arguments, NaN), 3, 'Expected arguments [1, 2, NaN] to contain NaN');
433 QUnit.test('lastIndexOf with +- 0', function(assert) {
434 _.each([-0, +0], function(val) {
435 assert.strictEqual(_.lastIndexOf([1, 2, val, val], val), 3);
436 assert.strictEqual(_.lastIndexOf([1, 2, val, val], -val), 3);
437 assert.strictEqual(_.lastIndexOf([-1, 1, 2], -val), -1);
441 QUnit.test('findIndex', function(assert) {
449 assert.equal(_.findIndex(objects, function(obj) {
453 assert.equal(_.findIndex(objects, function(obj) {
454 return obj.b * obj.a === 4;
457 assert.equal(_.findIndex(objects, 'a'), 1, 'Uses lookupIterator');
459 assert.equal(_.findIndex(objects, function(obj) {
460 return obj.b * obj.a === 5;
463 assert.equal(_.findIndex(null, _.noop), -1);
464 assert.strictEqual(_.findIndex(objects, function(a) {
465 return a.foo === null;
467 _.findIndex([{a: 1}], function(a, key, obj) {
468 assert.equal(key, 0);
469 assert.deepEqual(obj, [{a: 1}]);
470 assert.strictEqual(this, objects, 'called with context');
474 sparse[20] = {a: 2, b: 2};
475 assert.equal(_.findIndex(sparse, function(obj) {
476 return obj && obj.b * obj.a === 4;
477 }), 20, 'Works with sparse arrays');
479 var array = [1, 2, 3, 4];
481 assert.strictEqual(_.findIndex(array, function(x) { return x === 55; }), -1, 'doesn\'t match array-likes keys');
484 QUnit.test('findLastIndex', function(assert) {
492 assert.equal(_.findLastIndex(objects, function(obj) {
496 assert.equal(_.findLastIndex(objects, function(obj) {
497 return obj.b * obj.a === 4;
500 assert.equal(_.findLastIndex(objects, 'a'), 2, 'Uses lookupIterator');
502 assert.equal(_.findLastIndex(objects, function(obj) {
503 return obj.b * obj.a === 5;
506 assert.equal(_.findLastIndex(null, _.noop), -1);
507 assert.strictEqual(_.findLastIndex(objects, function(a) {
508 return a.foo === null;
510 _.findLastIndex([{a: 1}], function(a, key, obj) {
511 assert.equal(key, 0);
512 assert.deepEqual(obj, [{a: 1}]);
513 assert.strictEqual(this, objects, 'called with context');
517 sparse[20] = {a: 2, b: 2};
518 assert.equal(_.findLastIndex(sparse, function(obj) {
519 return obj && obj.b * obj.a === 4;
520 }), 20, 'Works with sparse arrays');
522 var array = [1, 2, 3, 4];
524 assert.strictEqual(_.findLastIndex(array, function(x) { return x === 55; }), -1, 'doesn\'t match array-likes keys');
527 QUnit.test('range', function(assert) {
528 assert.deepEqual(_.range(0), [], 'range with 0 as a first argument generates an empty array');
529 assert.deepEqual(_.range(4), [0, 1, 2, 3], 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
530 assert.deepEqual(_.range(5, 8), [5, 6, 7], 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
531 assert.deepEqual(_.range(3, 10, 3), [3, 6, 9], 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
532 assert.deepEqual(_.range(3, 10, 15), [3], 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
533 assert.deepEqual(_.range(12, 7, -2), [12, 10, 8], 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
534 assert.deepEqual(_.range(0, -10, -1), [0, -1, -2, -3, -4, -5, -6, -7, -8, -9], 'final example in the Python docs');
535 assert.strictEqual(1 / _.range(-0, 1)[0], -Infinity, 'should preserve -0');
536 assert.deepEqual(_.range(8, 5), [8, 7, 6], 'negative range generates descending array');
537 assert.deepEqual(_.range(-3), [0, -1, -2], 'negative range generates descending array');
540 QUnit.test('chunk', function(assert) {
541 assert.deepEqual(_.chunk([], 2), [], 'chunk for empty array returns an empty array');
543 assert.deepEqual(_.chunk([1, 2, 3], 0), [], 'chunk into parts of 0 elements returns empty array');
544 assert.deepEqual(_.chunk([1, 2, 3], -1), [], 'chunk into parts of negative amount of elements returns an empty array');
545 assert.deepEqual(_.chunk([1, 2, 3]), [], 'defaults to empty array (chunk size 0)');
547 assert.deepEqual(_.chunk([1, 2, 3], 1), [[1], [2], [3]], 'chunk into parts of 1 elements returns original array');
549 assert.deepEqual(_.chunk([1, 2, 3], 3), [[1, 2, 3]], 'chunk into parts of current array length elements returns the original array');
550 assert.deepEqual(_.chunk([1, 2, 3], 5), [[1, 2, 3]], 'chunk into parts of more then current array length elements returns the original array');
552 assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 2), [[10, 20], [30, 40], [50, 60], [70]], 'chunk into parts of less then current array length elements');
553 assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 3), [[10, 20, 30], [40, 50, 60], [70]], 'chunk into parts of less then current array length elements');