Lines Matching refs:_

2   var _ = typeof require == 'function' ? require('..') : window._;  variable
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));
15 result = _.map([[1, 2, 3], [1, 2, 3]], _.first);
17 assert.equal(_.first(null), void 0, 'returns undefined when called on null');
20 assert.equal(_.first([]), void 0, 'return undefined when called on a empty array');
25 assert.strictEqual(_.head, _.first, 'is an alias for first');
29 assert.strictEqual(_.take, _.first, 'is an alias for first');
34 assert.deepEqual(_.rest(numbers), [2, 3, 4], 'fetches all but the first element');
35 assert.deepEqual(_.rest(numbers, 0), [1, 2, 3, 4], 'returns the whole array when index is 0');
36 assert.deepEqual(_.rest(numbers, 2), [3, 4], 'returns elements starting at the given index');
37 var result = (function(){ return _(arguments).rest(); }(1, 2, 3, 4));
39 result = _.map([[1, 2, 3], [1, 2, 3]], _.rest);
40 assert.deepEqual(_.flatten(result), [2, 3, 2, 3], 'works well with _.map');
44 assert.strictEqual(_.tail, _.rest, 'is an alias for rest');
48 assert.strictEqual(_.drop, _.rest, 'is an alias for rest');
52 assert.deepEqual(_.initial([1, 2, 3, 4, 5]), [1, 2, 3, 4], 'returns all but the last element');
53 assert.deepEqual(_.initial([1, 2, 3, 4], 2), [1, 2], 'returns all but the last n elements');
54 assert.deepEqual(_.initial([1, 2, 3, 4], 6), [], 'returns an empty array when n > length');
55 var result = (function(){ return _(arguments).initial(); }(1, 2, 3, 4));
57 result = _.map([[1, 2, 3], [1, 2, 3]], _.initial);
58 assert.deepEqual(_.flatten(result), [1, 2, 1, 2], 'works well with _.map');
62 assert.equal(_.last([1, 2, 3]), 3, 'can pull out the last element of an array');
63 assert.equal(_([1, 2, 3]).last(), 3, 'can perform OO-style "last()"');
64 assert.deepEqual(_.last([1, 2, 3], 0), [], 'returns an empty array when n <= 0 (0 case)');
65 … assert.deepEqual(_.last([1, 2, 3], -1), [], 'returns an empty array when n <= 0 (negative case)');
66 assert.deepEqual(_.last([1, 2, 3], 2), [2, 3], 'can fetch the last n elements');
67 assert.deepEqual(_.last([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length');
68 var result = (function(){ return _(arguments).last(); }(1, 2, 3, 4));
70 result = _.map([[1, 2, 3], [1, 2, 3]], _.last);
72 assert.equal(_.last(null), void 0, 'returns undefined when called on null');
76 assert.equal(_.last(arr), void 0, 'return undefined when called on a empty array');
80 …assert.deepEqual(_.compact([1, false, null, 0, '', void 0, NaN, 2]), [1, 2], 'removes all falsy va…
81 var result = (function(){ return _.compact(arguments); }(0, 1, false, 2, false, 3));
83 result = _.map([[1, false, false], [false, false, 3]], _.compact);
88 assert.deepEqual(_.flatten(null), [], 'supports null');
89 assert.deepEqual(_.flatten(void 0), [], 'supports undefined');
91 assert.deepEqual(_.flatten([[], [[]], []]), [], 'supports empty arrays');
92 assert.deepEqual(_.flatten([[], [[]], []], true), [[]], 'can shallowly flatten empty arrays');
95 assert.deepEqual(_.flatten(list), [1, 2, 3, 4], 'can flatten nested arrays');
96 …assert.deepEqual(_.flatten(list, true), [1, 2, 3, [[[4]]]], 'can shallowly flatten nested arrays');
97 var result = (function(){ return _.flatten(arguments); }(1, [2], [3, [[[4]]]]));
100 …assert.deepEqual(_.flatten(list, true), [1, 2, 3, [4]], 'can shallowly flatten arrays containing o…
102 …assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3], true).length, 23, 'can flatten medium …
103 …assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3]).length, 23, 'can shallowly flatten med…
104 …assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3]).length, 1056003, 'can handle…
105 …assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3], true).length, 1056003, 'can …
107 var x = _.range(100000);
109 assert.deepEqual(_.flatten(x), _.range(100000), 'can handle very deep arrays');
110 assert.deepEqual(_.flatten(x, true), x[0], 'can handle very deep arrays in shallow mode');
115 assert.deepEqual(_.without(list, 0, 1), [2, 3, 4], 'removes all instances of the given values');
116 var result = (function(){ return _.without(arguments, 0, 1); }(1, 2, 1, 0, 3, 1, 4));
120 assert.deepEqual(_.without(list, {one: 1}), list, 'compares objects by reference (value case)');
121 …assert.deepEqual(_.without(list, list[0]), [{two: 2}], 'compares objects by reference (reference c…
126 var indexFor35 = _.sortedIndex(numbers, 35);
128 var indexFor30 = _.sortedIndex(numbers, 30);
133 …assert.strictEqual(_.sortedIndex(objects, {x: 25}, iterator), 2, 'uses the result of `iterator` fo…
134 …assert.strictEqual(_.sortedIndex(objects, {x: 35}, 'x'), 3, 'when `iterator` is a string, uses tha…
138 …assert.strictEqual(_.sortedIndex([1, 3], 2, iterator, context), 1, 'can execute its iterator in th…
148 assert.equal(_.sortedIndex(largeArray, 2147483648), 2147483648, 'works with large indexes');
153 assert.deepEqual(_.uniq(list), [1, 2, 3, 4], 'can find the unique values of an unsorted array');
155 …assert.deepEqual(_.uniq(list, true), [1, 2, 3], 'can find the unique values of a sorted array fast…
160 …assert.deepEqual(_.uniq(list, false, iterator), expected, 'uses the result of `iterator` for uniqu…
161 …assert.deepEqual(_.uniq(list, iterator), expected, '`sorted` argument defaults to false when omitt…
162 …assert.deepEqual(_.uniq(list, 'name'), expected, 'when `iterator` is a string, uses that key for c…
167 …assert.deepEqual(_.uniq(list, true, iterator), expected, 'uses the result of `iterator` for unique…
168 …assert.deepEqual(_.uniq(list, true, 'score'), expected, 'when `iterator` is a string, uses that ke…
170 …assert.deepEqual(_.uniq([{0: 1}, {0: 1}, {0: 1}, {0: 2}], 0), [{0: 1}, {0: 2}], 'can use falsey pl…
172 var result = (function(){ return _.uniq(arguments); }(1, 2, 1, 3, 1, 4));
176 …assert.deepEqual(_.uniq([a, b, a, b, c]), [a, b, c], 'works on values that can be tested for equiv…
178 assert.deepEqual(_.uniq(null), [], 'returns an empty array when `array` is not iterable');
182 _.uniq(list, function(value, index, array) {
192 assert.strictEqual(_.unique, _.uniq, 'is an alias for uniq');
197 …assert.deepEqual(_.intersection(stooges, leaders), ['moe'], 'can find the set intersection of two …
198 …assert.deepEqual(_(stooges).intersection(leaders), ['moe'], 'can perform an OO-style intersection'…
199 … var result = (function(){ return _.intersection(arguments, leaders); }('moe', 'curly', 'larry'));
202 …assert.deepEqual(_.intersection(theSixStooges, leaders), ['moe'], 'returns a duplicate-free array'…
203 result = _.intersection([2, 4, 3, 1], [1, 2, 3]);
205 result = _.intersection(null, [1, 2, 3]);
207 result = _.intersection([1, 2, 3], null);
212 var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
215 result = _([1, 2, 3]).union([2, 30, 1], [1, 40]);
218 result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);
221 result = _.union([10, 20], [1, 30, 10], [0, 40]);
224 result = (function(){ return _.union(arguments, [2, 30, 1], [1, 40]); }(1, 2, 3));
227 assert.deepEqual(_.union([1, 2, 3], 4), [1, 2, 3], 'restricts the union to arrays only');
231 var result = _.difference([1, 2, 3], [2, 30, 40]);
234 result = _([1, 2, 3]).difference([2, 30, 40]);
237 result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]);
240 result = _.difference([8, 9, 3, 1], [3, 8]);
243 result = (function(){ return _.difference(arguments, [2, 30, 40]); }(1, 2, 3));
246 result = _.difference([1, 2, 3], 1);
252 assert.deepEqual(_.zip(names, ages, leaders), [
258 …var stooges = _.zip(['moe', 30, 'stooge 1'], ['larry', 40, 'stooge 2'], ['curly', 50, 'stooge 3']);
263 stooges = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']);
266 var empty = _.zip([]);
269 assert.deepEqual(_.zip(null), [], 'handles null');
270 assert.deepEqual(_.zip(), [], '_.zip() returns []');
274 assert.deepEqual(_.unzip(null), [], 'handles null');
276 assert.deepEqual(_.unzip([['a', 'b'], [1, 2]]), [['a', 1], ['b', 2]]);
279 var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
280 assert.deepEqual(_.unzip(zipped), [['fred', 'barney'], [30, 40], [true, false]]);
282 zipped = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']);
283 …assert.deepEqual(_.unzip(zipped), [['moe', 30, void 0], ['larry', 40, void 0], ['curly', 50, 'extr…
287 var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
291 result = _.object([['one', 1], ['two', 2], ['three', 3]]);
296 …assert.deepEqual(_.object(_.pairs(stooges)), stooges, 'an object converted to pairs and back to an…
298 assert.deepEqual(_.object(null), {}, 'handles nulls');
303 assert.equal(_.indexOf(numbers, 2), 1, 'can compute indexOf');
304 var result = (function(){ return _.indexOf(arguments, 2); }(1, 2, 3));
307 _.each([null, void 0, [], false], function(val) {
308 var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val);
309 assert.equal(_.indexOf(val, 2), -1, msg);
310 assert.equal(_.indexOf(val, 2, -1), -1, msg);
311 assert.equal(_.indexOf(val, 2, -20), -1, msg);
312 assert.equal(_.indexOf(val, 2, 15), -1, msg);
317 var index = _.indexOf(numbers, num, true);
321 index = _.indexOf(numbers, num, true);
325 assert.equal(_.indexOf(numbers, num, true), 1, '40 is in the list');
326 assert.equal(_.indexOf(numbers, 6, true), -1, '6 isnt in the list');
327 …assert.equal(_.indexOf([1, 2, 5, 4, 6, 7], 5, true), -1, 'sorted indexOf doesn\'t uses binary sear…
328 assert.ok(_.every(['1', [], {}, null], function() {
329 return _.indexOf(numbers, num, {}) === 1;
333 index = _.indexOf(numbers, 2, 5);
336 index = _.indexOf([,,, 0], void 0);
340 assert.strictEqual(_.indexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the right index');
341 assert.strictEqual(_.indexOf(array, 1, -2), -1, 'neg `fromIndex` starts at the right index');
342 assert.strictEqual(_.indexOf(array, 2, -3), 4);
343 _.each([-6, -8, -Infinity], function(fromIndex) {
344 assert.strictEqual(_.indexOf(array, 1, fromIndex), 0);
346 assert.strictEqual(_.indexOf([1, 2, 3], 1, true), 0);
348 index = _.indexOf([], void 0, true);
353 assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN), 2, 'Expected [1, 2, NaN] to contain NaN');
354 assert.strictEqual(_.indexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN');
356 assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, 1), 2, 'startIndex does not affect result');
357 … assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, -2), 2, 'startIndex does not affect result');
360 … assert.strictEqual(_.indexOf(arguments, NaN), 2, 'Expected arguments [1, 2, NaN] to contain NaN');
365 _.each([-0, +0], function(val) {
366 assert.strictEqual(_.indexOf([1, 2, val, val], val), 2);
367 assert.strictEqual(_.indexOf([1, 2, val, val], -val), 2);
374 assert.equal(_.lastIndexOf(numbers, 1), 2);
378 …assert.equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native funct…
379 assert.equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
380 var result = (function(){ return _.lastIndexOf(arguments, 1); }(1, 0, 1, 0, 0, 1, 0, 0, 0));
383 _.each([null, void 0, [], false], function(val) {
384 var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val);
385 assert.equal(_.lastIndexOf(val, 2), -1, msg);
386 assert.equal(_.lastIndexOf(val, 2, -1), -1, msg);
387 assert.equal(_.lastIndexOf(val, 2, -20), -1, msg);
388 assert.equal(_.lastIndexOf(val, 2, 15), -1, msg);
392 var index = _.lastIndexOf(numbers, 2, 2);
397 assert.strictEqual(_.lastIndexOf(array, 1, 0), 0, 'starts at the correct from idx');
398 …assert.strictEqual(_.lastIndexOf(array, 3), 5, 'should return the index of the last matched value'…
399 assert.strictEqual(_.lastIndexOf(array, 4), -1, 'should return `-1` for an unmatched value');
401 assert.strictEqual(_.lastIndexOf(array, 1, 2), 0, 'should work with a positive `fromIndex`');
403 _.each([6, 8, Math.pow(2, 32), Infinity], function(fromIndex) {
404 assert.strictEqual(_.lastIndexOf(array, void 0, fromIndex), -1);
405 assert.strictEqual(_.lastIndexOf(array, 1, fromIndex), 3);
406 assert.strictEqual(_.lastIndexOf(array, '', fromIndex), -1);
409 var expected = _.map(falsey, function(value) {
413 var actual = _.map(falsey, function(fromIndex) {
414 return _.lastIndexOf(array, 3, fromIndex);
418 …assert.strictEqual(_.lastIndexOf(array, 3, '1'), 5, 'should treat non-number `fromIndex` values as…
419 …assert.strictEqual(_.lastIndexOf(array, 3, true), 5, 'should treat non-number `fromIndex` values a…
421 assert.strictEqual(_.lastIndexOf(array, 2, -3), 1, 'should work with a negative `fromIndex`');
422 assert.strictEqual(_.lastIndexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the right index');
424 assert.deepEqual(_.map([-6, -8, -Infinity], function(fromIndex) {
425 return _.lastIndexOf(array, 1, fromIndex);
430 …assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN), 3, 'Expected [1, 2, NaN] to contain NaN');
431 …assert.strictEqual(_.lastIndexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, NaN] to contain NaN'…
433 …assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, 2), 2, 'fromIndex does not affect result');
434 …assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, -2), 2, 'fromIndex does not affect result'…
437 …assert.strictEqual(_.lastIndexOf(arguments, NaN), 3, 'Expected arguments [1, 2, NaN] to contain Na…
442 _.each([-0, +0], function(val) {
443 assert.strictEqual(_.lastIndexOf([1, 2, val, val], val), 3);
444 assert.strictEqual(_.lastIndexOf([1, 2, val, val], -val), 3);
445 assert.strictEqual(_.lastIndexOf([-1, 1, 2], -val), -1);
457 assert.equal(_.findIndex(objects, function(obj) {
461 assert.equal(_.findIndex(objects, function(obj) {
465 assert.equal(_.findIndex(objects, 'a'), 1, 'Uses lookupIterator');
467 assert.equal(_.findIndex(objects, function(obj) {
471 assert.equal(_.findIndex(null, _.noop), -1);
472 assert.strictEqual(_.findIndex(objects, function(a) {
475 _.findIndex([{a: 1}], function(a, key, obj) {
483 assert.equal(_.findIndex(sparse, function(obj) {
489 …assert.strictEqual(_.findIndex(array, function(x) { return x === 55; }), -1, 'doesn\'t match array…
500 assert.equal(_.findLastIndex(objects, function(obj) {
504 assert.equal(_.findLastIndex(objects, function(obj) {
508 assert.equal(_.findLastIndex(objects, 'a'), 2, 'Uses lookupIterator');
510 assert.equal(_.findLastIndex(objects, function(obj) {
514 assert.equal(_.findLastIndex(null, _.noop), -1);
515 assert.strictEqual(_.findLastIndex(objects, function(a) {
518 _.findLastIndex([{a: 1}], function(a, key, obj) {
526 assert.equal(_.findLastIndex(sparse, function(obj) {
532 …assert.strictEqual(_.findLastIndex(array, function(x) { return x === 55; }), -1, 'doesn\'t match a…
536 assert.deepEqual(_.range(0), [], 'range with 0 as a first argument generates an empty array');
537 …assert.deepEqual(_.range(4), [0, 1, 2, 3], 'range with a single positive argument generates an arr…
538 …assert.deepEqual(_.range(5, 8), [5, 6, 7], 'range with two arguments a &amp; b, a&lt;b generates a…
539 …assert.deepEqual(_.range(3, 10, 3), [3, 6, 9], 'range with three arguments a &amp; b &amp; c, c &l…
540 …assert.deepEqual(_.range(3, 10, 15), [3], 'range with three arguments a &amp; b &amp; c, c &gt; b-…
541 …assert.deepEqual(_.range(12, 7, -2), [12, 10, 8], 'range with three arguments a &amp; b &amp; c, a…
542 …assert.deepEqual(_.range(0, -10, -1), [0, -1, -2, -3, -4, -5, -6, -7, -8, -9], 'final example in t…
543 assert.strictEqual(1 / _.range(-0, 1)[0], -Infinity, 'should preserve -0');
544 assert.deepEqual(_.range(8, 5), [8, 7, 6], 'negative range generates descending array');
545 assert.deepEqual(_.range(-3), [0, -1, -2], 'negative range generates descending array');
549 assert.deepEqual(_.chunk([], 2), [], 'chunk for empty array returns an empty array');
551 … assert.deepEqual(_.chunk([1, 2, 3], 0), [], 'chunk into parts of 0 elements returns empty array');
552 …assert.deepEqual(_.chunk([1, 2, 3], -1), [], 'chunk into parts of negative amount of elements retu…
553 assert.deepEqual(_.chunk([1, 2, 3]), [], 'defaults to empty array (chunk size 0)');
555 …assert.deepEqual(_.chunk([1, 2, 3], 1), [[1], [2], [3]], 'chunk into parts of 1 elements returns o…
557 …assert.deepEqual(_.chunk([1, 2, 3], 3), [[1, 2, 3]], 'chunk into parts of current array length ele…
558 …assert.deepEqual(_.chunk([1, 2, 3], 5), [[1, 2, 3]], 'chunk into parts of more then current array …
560 …assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 2), [[10, 20], [30, 40], [50, 60], [70]], '…
561 …assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 3), [[10, 20, 30], [40, 50, 60], [70]], 'ch…