arrays.js (lodash-3.0.0) | : | arrays.js (lodash-4.0.0) | ||
---|---|---|---|---|
(function() { | (function() { | |||
var _ = typeof require == 'function' ? require('..') : window._; | ||||
module('Arrays'); | QUnit.module('Arrays'); | |||
test('first', function() { | QUnit.test('first', function(assert) { | |||
equal(_.first([1, 2, 3]), 1, 'can pull out the first element of an array'); | assert.equal(_.first([1, 2, 3]), 1, 'can pull out the first element of an ar | |||
equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"'); | ray'); | |||
deepEqual(_.first([1, 2, 3], 0), [], 'can pass an index to first'); | assert.equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"'); | |||
deepEqual(_.first([1, 2, 3], 2), [1, 2], 'can pass an index to first'); | assert.deepEqual(_.first([1, 2, 3], 0), [], 'returns an empty array when n < | |||
deepEqual(_.first([1, 2, 3], 5), [1, 2, 3], 'can pass an index to first'); | = 0 (0 case)'); | |||
assert.deepEqual(_.first([1, 2, 3], -1), [], 'returns an empty array when n | ||||
<= 0 (negative case)'); | ||||
assert.deepEqual(_.first([1, 2, 3], 2), [1, 2], 'can fetch the first n eleme | ||||
nts'); | ||||
assert.deepEqual(_.first([1, 2, 3], 5), [1, 2, 3], 'returns the whole array | ||||
if n > length'); | ||||
var result = (function(){ return _.first(arguments); }(4, 3, 2, 1)); | var result = (function(){ return _.first(arguments); }(4, 3, 2, 1)); | |||
equal(result, 4, 'works on an arguments object.'); | assert.equal(result, 4, 'works on an arguments object'); | |||
result = _.map([[1, 2, 3], [1, 2, 3]], _.first); | result = _.map([[1, 2, 3], [1, 2, 3]], _.first); | |||
deepEqual(result, [1, 1], 'works well with _.map'); | assert.deepEqual(result, [1, 1], 'works well with _.map'); | |||
result = (function() { return _.first([1, 2, 3], 2); }()); | assert.equal(_.first(null), void 0, 'returns undefined when called on null') | |||
deepEqual(result, [1, 2]); | ; | |||
equal(_.first(null), undefined, 'handles nulls'); | ||||
strictEqual(_.first([1, 2, 3], -1).length, 0); | ||||
}); | }); | |||
test('head', function() { | QUnit.test('head', function(assert) { | |||
strictEqual(_.first, _.head, 'alias for first'); | assert.strictEqual(_.head, _.first, 'is an alias for first'); | |||
}); | }); | |||
test('take', function() { | QUnit.test('take', function(assert) { | |||
strictEqual(_.first, _.take, 'alias for first'); | assert.strictEqual(_.take, _.first, 'is an alias for first'); | |||
}); | }); | |||
test('rest', function() { | QUnit.test('rest', function(assert) { | |||
var numbers = [1, 2, 3, 4]; | var numbers = [1, 2, 3, 4]; | |||
deepEqual(_.rest(numbers), [2, 3, 4], 'working rest()'); | assert.deepEqual(_.rest(numbers), [2, 3, 4], 'fetches all but the first elem | |||
deepEqual(_.rest(numbers, 0), [1, 2, 3, 4], 'working rest(0)'); | ent'); | |||
deepEqual(_.rest(numbers, 2), [3, 4], 'rest can take an index'); | assert.deepEqual(_.rest(numbers, 0), [1, 2, 3, 4], 'returns the whole array | |||
when index is 0'); | ||||
assert.deepEqual(_.rest(numbers, 2), [3, 4], 'returns elements starting at t | ||||
he given index'); | ||||
var result = (function(){ return _(arguments).rest(); }(1, 2, 3, 4)); | var result = (function(){ return _(arguments).rest(); }(1, 2, 3, 4)); | |||
deepEqual(result, [2, 3, 4], 'works on arguments object'); | assert.deepEqual(result, [2, 3, 4], 'works on an arguments object'); | |||
result = _.map([[1, 2, 3], [1, 2, 3]], _.rest); | result = _.map([[1, 2, 3], [1, 2, 3]], _.rest); | |||
deepEqual(_.flatten(result), [2, 3, 2, 3], 'works well with _.map'); | assert.deepEqual(_.flatten(result), [2, 3, 2, 3], 'works well with _.map'); | |||
result = (function(){ return _(arguments).rest(); }(1, 2, 3, 4)); | ||||
deepEqual(result, [2, 3, 4], 'works on arguments object'); | ||||
}); | }); | |||
test('tail', function() { | QUnit.test('tail', function(assert) { | |||
strictEqual(_.rest, _.tail, 'alias for rest'); | assert.strictEqual(_.tail, _.rest, 'is an alias for rest'); | |||
}); | }); | |||
test('drop', function() { | QUnit.test('drop', function(assert) { | |||
strictEqual(_.rest, _.drop, 'alias for rest'); | assert.strictEqual(_.drop, _.rest, 'is an alias for rest'); | |||
}); | }); | |||
test('initial', function() { | QUnit.test('initial', function(assert) { | |||
deepEqual(_.initial([1, 2, 3, 4, 5]), [1, 2, 3, 4], 'working initial()'); | assert.deepEqual(_.initial([1, 2, 3, 4, 5]), [1, 2, 3, 4], 'returns all but | |||
deepEqual(_.initial([1, 2, 3, 4], 2), [1, 2], 'initial can take an index'); | the last element'); | |||
deepEqual(_.initial([1, 2, 3, 4], 6), [], 'initial can take a large index'); | assert.deepEqual(_.initial([1, 2, 3, 4], 2), [1, 2], 'returns all but the la | |||
st n elements'); | ||||
assert.deepEqual(_.initial([1, 2, 3, 4], 6), [], 'returns an empty array whe | ||||
n n > length'); | ||||
var result = (function(){ return _(arguments).initial(); }(1, 2, 3, 4)); | var result = (function(){ return _(arguments).initial(); }(1, 2, 3, 4)); | |||
deepEqual(result, [1, 2, 3], 'initial works on arguments object'); | assert.deepEqual(result, [1, 2, 3], 'works on an arguments object'); | |||
result = _.map([[1, 2, 3], [1, 2, 3]], _.initial); | result = _.map([[1, 2, 3], [1, 2, 3]], _.initial); | |||
deepEqual(_.flatten(result), [1, 2, 1, 2], 'initial works with _.map'); | assert.deepEqual(_.flatten(result), [1, 2, 1, 2], 'works well with _.map'); | |||
}); | }); | |||
test('last', function() { | QUnit.test('last', function(assert) { | |||
equal(_.last([1, 2, 3]), 3, 'can pull out the last element of an array'); | assert.equal(_.last([1, 2, 3]), 3, 'can pull out the last element of an arra | |||
deepEqual(_.last([1, 2, 3], 0), [], 'can pass an index to last'); | y'); | |||
deepEqual(_.last([1, 2, 3], 2), [2, 3], 'can pass an index to last'); | assert.equal(_([1, 2, 3]).last(), 3, 'can perform OO-style "last()"'); | |||
deepEqual(_.last([1, 2, 3], 5), [1, 2, 3], 'can pass an index to last'); | assert.deepEqual(_.last([1, 2, 3], 0), [], 'returns an empty array when n <= | |||
0 (0 case)'); | ||||
assert.deepEqual(_.last([1, 2, 3], -1), [], 'returns an empty array when n < | ||||
= 0 (negative case)'); | ||||
assert.deepEqual(_.last([1, 2, 3], 2), [2, 3], 'can fetch the last n element | ||||
s'); | ||||
assert.deepEqual(_.last([1, 2, 3], 5), [1, 2, 3], 'returns the whole array i | ||||
f n > length'); | ||||
var result = (function(){ return _(arguments).last(); }(1, 2, 3, 4)); | var result = (function(){ return _(arguments).last(); }(1, 2, 3, 4)); | |||
equal(result, 4, 'works on an arguments object'); | assert.equal(result, 4, 'works on an arguments object'); | |||
result = _.map([[1, 2, 3], [1, 2, 3]], _.last); | result = _.map([[1, 2, 3], [1, 2, 3]], _.last); | |||
deepEqual(result, [3, 3], 'works well with _.map'); | assert.deepEqual(result, [3, 3], 'works well with _.map'); | |||
assert.equal(_.last(null), void 0, 'returns undefined when called on null'); | ||||
equal(_.last(null), undefined, 'handles nulls'); | ||||
strictEqual(_.last([1, 2, 3], -1).length, 0); | ||||
}); | }); | |||
test('compact', function() { | QUnit.test('compact', function(assert) { | |||
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all fal | assert.deepEqual(_.compact([1, false, null, 0, '', void 0, NaN, 2]), [1, 2], | |||
sy values'); | 'removes all falsy values'); | |||
var result = (function(){ return _.compact(arguments).length; }(0, 1, false, | var result = (function(){ return _.compact(arguments); }(0, 1, false, 2, fal | |||
2, false, 3)); | se, 3)); | |||
equal(result, 3, 'works on an arguments object'); | assert.deepEqual(result, [1, 2, 3], 'works on an arguments object'); | |||
result = _.map([[1, false, false], [false, false, 3]], _.compact); | ||||
assert.deepEqual(result, [[1], [3]], 'works well with _.map'); | ||||
}); | }); | |||
test('flatten', function() { | QUnit.test('flatten', function(assert) { | |||
assert.deepEqual(_.flatten(null), [], 'supports null'); | ||||
assert.deepEqual(_.flatten(void 0), [], 'supports undefined'); | ||||
assert.deepEqual(_.flatten([[], [[]], []]), [], 'supports empty arrays'); | ||||
assert.deepEqual(_.flatten([[], [[]], []], true), [[]], 'can shallowly flatt | ||||
en empty arrays'); | ||||
var list = [1, [2], [3, [[[4]]]]]; | var list = [1, [2], [3, [[[4]]]]]; | |||
deepEqual(_.flatten(list), [1, 2, 3, 4], 'can flatten nested arrays'); | assert.deepEqual(_.flatten(list), [1, 2, 3, 4], 'can flatten nested arrays') | |||
deepEqual(_.flatten(list, true), [1, 2, 3, [[[4]]]], 'can shallowly flatten | ; | |||
nested arrays'); | assert.deepEqual(_.flatten(list, true), [1, 2, 3, [[[4]]]], 'can shallowly f | |||
latten nested arrays'); | ||||
var result = (function(){ return _.flatten(arguments); }(1, [2], [3, [[[4]]] ])); | var result = (function(){ return _.flatten(arguments); }(1, [2], [3, [[[4]]] ])); | |||
deepEqual(result, [1, 2, 3, 4], 'works on an arguments object'); | assert.deepEqual(result, [1, 2, 3, 4], 'works on an arguments object'); | |||
list = [[1], [2], [3], [[4]]]; | list = [[1], [2], [3], [[4]]]; | |||
deepEqual(_.flatten(list, true), [1, 2, 3, [4]], 'can shallowly flatten arra | assert.deepEqual(_.flatten(list, true), [1, 2, 3, [4]], 'can shallowly flatt | |||
ys containing only other arrays'); | en arrays containing only other arrays'); | |||
assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3], true).length, 23 | ||||
, 'can flatten medium length arrays'); | ||||
assert.equal(_.flatten([_.range(10), _.range(10), 5, 1, 3]).length, 23, 'can | ||||
shallowly flatten medium length arrays'); | ||||
assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3]).length | ||||
, 1056003, 'can handle massive arrays'); | ||||
assert.equal(_.flatten([new Array(1000000), _.range(56000), 5, 1, 3], true). | ||||
length, 1056003, 'can handle massive arrays in shallow mode'); | ||||
var x = _.range(100000); | ||||
for (var i = 0; i < 1000; i++) x = [x]; | ||||
assert.deepEqual(_.flatten(x), _.range(100000), 'can handle very deep arrays | ||||
'); | ||||
assert.deepEqual(_.flatten(x, true), x[0], 'can handle very deep arrays in s | ||||
hallow mode'); | ||||
}); | }); | |||
test('without', function() { | QUnit.test('without', function(assert) { | |||
var list = [1, 2, 1, 0, 3, 1, 4]; | var list = [1, 2, 1, 0, 3, 1, 4]; | |||
deepEqual(_.without(list, 0, 1), [2, 3, 4], 'can remove all instances of an object'); | assert.deepEqual(_.without(list, 0, 1), [2, 3, 4], 'removes all instances of the given values'); | |||
var result = (function(){ return _.without(arguments, 0, 1); }(1, 2, 1, 0, 3 , 1, 4)); | var result = (function(){ return _.without(arguments, 0, 1); }(1, 2, 1, 0, 3 , 1, 4)); | |||
deepEqual(result, [2, 3, 4], 'works on an arguments object'); | assert.deepEqual(result, [2, 3, 4], 'works on an arguments object'); | |||
list = [{one : 1}, {two : 2}]; | list = [{one: 1}, {two: 2}]; | |||
equal(_.without(list, {one : 1}).length, 2, 'uses real object identity for c | assert.deepEqual(_.without(list, {one: 1}), list, 'compares objects by refer | |||
omparisons.'); | ence (value case)'); | |||
equal(_.without(list, list[0]).length, 1, 'ditto.'); | assert.deepEqual(_.without(list, list[0]), [{two: 2}], 'compares objects by | |||
reference (reference case)'); | ||||
}); | ||||
QUnit.test('sortedIndex', function(assert) { | ||||
var numbers = [10, 20, 30, 40, 50]; | ||||
var indexFor35 = _.sortedIndex(numbers, 35); | ||||
assert.equal(indexFor35, 3, 'finds the index at which a value should be inse | ||||
rted to retain order'); | ||||
var indexFor30 = _.sortedIndex(numbers, 30); | ||||
assert.equal(indexFor30, 2, 'finds the smallest index at which a value could | ||||
be inserted to retain order'); | ||||
var objects = [{x: 10}, {x: 20}, {x: 30}, {x: 40}]; | ||||
var iterator = function(obj){ return obj.x; }; | ||||
assert.strictEqual(_.sortedIndex(objects, {x: 25}, iterator), 2, 'uses the r | ||||
esult of `iterator` for order comparisons'); | ||||
assert.strictEqual(_.sortedIndex(objects, {x: 35}, 'x'), 3, 'when `iterator` | ||||
is a string, uses that key for order comparisons'); | ||||
var context = {1: 2, 2: 3, 3: 4}; | ||||
iterator = function(obj){ return this[obj]; }; | ||||
assert.strictEqual(_.sortedIndex([1, 3], 2, iterator, context), 1, 'can exec | ||||
ute its iterator in the given context'); | ||||
var values = [0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, | ||||
16383, 32767, 65535, 131071, 262143, 524287, | ||||
1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217 | ||||
727, 268435455, 536870911, 1073741823, 2147483647]; | ||||
var largeArray = Array(Math.pow(2, 32) - 1); | ||||
var length = values.length; | ||||
// Sparsely populate `array` | ||||
while (length--) { | ||||
largeArray[values[length]] = values[length]; | ||||
} | ||||
assert.equal(_.sortedIndex(largeArray, 2147483648), 2147483648, 'works with | ||||
large indexes'); | ||||
}); | }); | |||
test('uniq', function() { | QUnit.test('uniq', function(assert) { | |||
var list = [1, 2, 1, 3, 1, 4]; | var list = [1, 2, 1, 3, 1, 4]; | |||
deepEqual(_.uniq(list), [1, 2, 3, 4], 'can find the unique values of an unso rted array'); | assert.deepEqual(_.uniq(list), [1, 2, 3, 4], 'can find the unique values of an unsorted array'); | |||
list = [1, 1, 1, 2, 2, 3]; | list = [1, 1, 1, 2, 2, 3]; | |||
deepEqual(_.uniq(list, true), [1, 2, 3], 'can find the unique values of a so rted array faster'); | assert.deepEqual(_.uniq(list, true), [1, 2, 3], 'can find the unique values of a sorted array faster'); | |||
list = [{name: 'moe'}, {name: 'curly'}, {name: 'larry'}, {name: 'curly'}]; | list = [{name: 'moe'}, {name: 'curly'}, {name: 'larry'}, {name: 'curly'}]; | |||
var iterator = function(value) { return value.name; }; | var iterator = function(value) { return value.name; }; | |||
deepEqual(_.map(_.uniq(list, false, iterator), iterator), ['moe', 'curly', ' larry'], 'can find the unique values of an array using a custom iterator'); | assert.deepEqual(_.map(_.uniq(list, false, iterator), iterator), ['moe', 'cu rly', 'larry'], 'can find the unique values of an array using a custom iterator' ); | |||
deepEqual(_.map(_.uniq(list, iterator), iterator), ['moe', 'curly', 'larry'] , 'can find the unique values of an array using a custom iterator without specif ying whether array is sorted'); | assert.deepEqual(_.map(_.uniq(list, iterator), iterator), ['moe', 'curly', ' larry'], 'can find the unique values of an array using a custom iterator without specifying whether array is sorted'); | |||
iterator = function(value) { return value + 1; }; | iterator = function(value) { return value + 1; }; | |||
list = [1, 2, 2, 3, 4, 4]; | list = [1, 2, 2, 3, 4, 4]; | |||
deepEqual(_.uniq(list, true, iterator), [1, 2, 3, 4], 'iterator works with s | assert.deepEqual(_.uniq(list, true, iterator), [1, 2, 3, 4], 'iterator works | |||
orted array'); | with sorted array'); | |||
var kittens = [ | ||||
{kitten: 'Celery', cuteness: 8}, | ||||
{kitten: 'Juniper', cuteness: 10}, | ||||
{kitten: 'Spottis', cuteness: 10} | ||||
]; | ||||
var expected = [ | ||||
{kitten: 'Celery', cuteness: 8}, | ||||
{kitten: 'Juniper', cuteness: 10} | ||||
]; | ||||
assert.deepEqual(_.uniq(kittens, true, 'cuteness'), expected, 'string iterat | ||||
or works with sorted array'); | ||||
var result = (function(){ return _.uniq(arguments); }(1, 2, 1, 3, 1, 4)); | var result = (function(){ return _.uniq(arguments); }(1, 2, 1, 3, 1, 4)); | |||
deepEqual(result, [1, 2, 3, 4], 'works on an arguments object'); | assert.deepEqual(result, [1, 2, 3, 4], 'works on an arguments object'); | |||
var a = {}, b = {}, c = {}; | var a = {}, b = {}, c = {}; | |||
deepEqual(_.uniq([a, b, a, b, c]), [a, b, c], 'works on values that can be t ested for equivalency but not ordered'); | assert.deepEqual(_.uniq([a, b, a, b, c]), [a, b, c], 'works on values that c an be tested for equivalency but not ordered'); | |||
deepEqual(_.uniq(null), []); | assert.deepEqual(_.uniq(null), []); | |||
var context = {}; | var context = {}; | |||
list = [3]; | list = [3]; | |||
_.uniq(list, function(value, index, array) { | _.uniq(list, function(value, index, array) { | |||
strictEqual(this, context); | assert.strictEqual(this, context); | |||
strictEqual(value, 3); | assert.strictEqual(value, 3); | |||
strictEqual(index, 0); | assert.strictEqual(index, 0); | |||
strictEqual(array, list); | assert.strictEqual(array, list); | |||
}, context); | }, context); | |||
deepEqual(_.uniq([{a: 1, b: 1}, {a: 1, b: 2}, {a: 1, b: 3}, {a: 2, b: 1}], ' | assert.deepEqual(_.uniq([{a: 1, b: 1}, {a: 1, b: 2}, {a: 1, b: 3}, {a: 2, b: | |||
a'), [{a: 1, b: 1}, {a: 2, b: 1}], 'can use pluck like iterator'); | 1}], 'a'), [{a: 1, b: 1}, {a: 2, b: 1}], 'can use pluck like iterator'); | |||
deepEqual(_.uniq([{0: 1, b: 1}, {0: 1, b: 2}, {0: 1, b: 3}, {0: 2, b: 1}], 0 | assert.deepEqual(_.uniq([{0: 1, b: 1}, {0: 1, b: 2}, {0: 1, b: 3}, {0: 2, b: | |||
), [{0: 1, b: 1}, {0: 2, b: 1}], 'can use falsey pluck like iterator'); | 1}], 0), [{0: 1, b: 1}, {0: 2, b: 1}], 'can use falsey pluck like iterator'); | |||
}); | }); | |||
test('unique', function() { | QUnit.test('unique', function(assert) { | |||
strictEqual(_.uniq, _.unique, 'alias for uniq'); | assert.strictEqual(_.unique, _.uniq, 'is an alias for uniq'); | |||
}); | }); | |||
test('intersection', function() { | QUnit.test('intersection', function(assert) { | |||
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho']; | var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho']; | |||
deepEqual(_.intersection(stooges, leaders), ['moe'], 'can take the set inter | assert.deepEqual(_.intersection(stooges, leaders), ['moe'], 'can take the se | |||
section of two arrays'); | t intersection of two arrays'); | |||
deepEqual(_(stooges).intersection(leaders), ['moe'], 'can perform an OO-styl | assert.deepEqual(_(stooges).intersection(leaders), ['moe'], 'can perform an | |||
e intersection'); | OO-style intersection'); | |||
var result = (function(){ return _.intersection(arguments, leaders); }('moe' , 'curly', 'larry')); | var result = (function(){ return _.intersection(arguments, leaders); }('moe' , 'curly', 'larry')); | |||
deepEqual(result, ['moe'], 'works on an arguments object'); | assert.deepEqual(result, ['moe'], 'works on an arguments object'); | |||
var theSixStooges = ['moe', 'moe', 'curly', 'curly', 'larry', 'larry']; | var theSixStooges = ['moe', 'moe', 'curly', 'curly', 'larry', 'larry']; | |||
deepEqual(_.intersection(theSixStooges, leaders), ['moe'], 'returns a duplic ate-free array'); | assert.deepEqual(_.intersection(theSixStooges, leaders), ['moe'], 'returns a duplicate-free array'); | |||
result = _.intersection([2, 4, 3, 1], [1, 2, 3]); | result = _.intersection([2, 4, 3, 1], [1, 2, 3]); | |||
deepEqual(result, [2, 3, 1], 'preserves order of first array'); | assert.deepEqual(result, [2, 3, 1], 'preserves order of first array'); | |||
result = _.intersection(null, [1, 2, 3]); | result = _.intersection(null, [1, 2, 3]); | |||
equal(Object.prototype.toString.call(result), '[object Array]', 'returns an | assert.equal(Object.prototype.toString.call(result), '[object Array]', 'retu | |||
empty array when passed null as first argument'); | rns an empty array when passed null as first argument'); | |||
equal(result.length, 0, 'returns an empty array when passed null as first ar | assert.equal(result.length, 0, 'returns an empty array when passed null as f | |||
gument'); | irst argument'); | |||
result = _.intersection([1, 2, 3], null); | result = _.intersection([1, 2, 3], null); | |||
equal(Object.prototype.toString.call(result), '[object Array]', 'returns an | assert.equal(Object.prototype.toString.call(result), '[object Array]', 'retu | |||
empty array when passed null as argument beyond the first'); | rns an empty array when passed null as argument beyond the first'); | |||
equal(result.length, 0, 'returns an empty array when passed null as argument | assert.equal(result.length, 0, 'returns an empty array when passed null as a | |||
beyond the first'); | rgument beyond the first'); | |||
}); | }); | |||
test('union', function() { | QUnit.test('union', function(assert) { | |||
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]); | var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]); | |||
deepEqual(result, [1, 2, 3, 30, 40], 'takes the union of a list of arrays'); | assert.deepEqual(result, [1, 2, 3, 30, 40], 'takes the union of a list of ar rays'); | |||
result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]); | result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]); | |||
deepEqual(result, [1, 2, 3, 30, 40, [1]], 'takes the union of a list of nest ed arrays'); | assert.deepEqual(result, [1, 2, 3, 30, 40, [1]], 'takes the union of a list of nested arrays'); | |||
var args = null; | var args = null; | |||
(function(){ args = arguments; }(1, 2, 3)); | (function(){ args = arguments; }(1, 2, 3)); | |||
result = _.union(args, [2, 30, 1], [1, 40]); | result = _.union(args, [2, 30, 1], [1, 40]); | |||
deepEqual(result, [1, 2, 3, 30, 40], 'takes the union of a list of arrays'); | assert.deepEqual(result, [1, 2, 3, 30, 40], 'takes the union of a list of ar rays'); | |||
result = _.union([1, 2, 3], 4); | result = _.union([1, 2, 3], 4); | |||
deepEqual(result, [1, 2, 3], 'restrict the union to arrays only'); | assert.deepEqual(result, [1, 2, 3], 'restrict the union to arrays only'); | |||
}); | }); | |||
test('difference', function() { | QUnit.test('difference', function(assert) { | |||
var result = _.difference([1, 2, 3], [2, 30, 40]); | var result = _.difference([1, 2, 3], [2, 30, 40]); | |||
deepEqual(result, [1, 3], 'takes the difference of two arrays'); | assert.deepEqual(result, [1, 3], 'takes the difference of two arrays'); | |||
result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]); | result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]); | |||
deepEqual(result, [3, 4], 'takes the difference of three arrays'); | assert.deepEqual(result, [3, 4], 'takes the difference of three arrays'); | |||
result = _.difference([1, 2, 3], 1); | result = _.difference([1, 2, 3], 1); | |||
deepEqual(result, [1, 2, 3], 'restrict the difference to arrays only'); | assert.deepEqual(result, [1, 2, 3], 'restrict the difference to arrays only' ); | |||
}); | }); | |||
test('zip', function() { | QUnit.test('zip', function(assert) { | |||
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true] ; | var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true] ; | |||
deepEqual(_.zip(names, ages, leaders), [ | assert.deepEqual(_.zip(names, ages, leaders), [ | |||
['moe', 30, true], | ['moe', 30, true], | |||
['larry', 40, undefined], | ['larry', 40, void 0], | |||
['curly', 50, undefined] | ['curly', 50, void 0] | |||
], 'zipped together arrays of different lengths'); | ], 'zipped together arrays of different lengths'); | |||
var stooges = _.zip(['moe', 30, 'stooge 1'], ['larry', 40, 'stooge 2'], ['cu rly', 50, 'stooge 3']); | var stooges = _.zip(['moe', 30, 'stooge 1'], ['larry', 40, 'stooge 2'], ['cu rly', 50, 'stooge 3']); | |||
deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], ['stooge 1', 's tooge 2', 'stooge 3']], 'zipped pairs'); | assert.deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], ['stooge 1', 'stooge 2', 'stooge 3']], 'zipped pairs'); | |||
// In the case of difference lengths of the tuples undefineds | // In the case of different lengths of the tuples, undefined values | |||
// should be used as placeholder | // should be used as placeholder | |||
stooges = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']); | stooges = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']); | |||
deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], [undefined, und efined, 'extra data']], 'zipped pairs with empties'); | assert.deepEqual(stooges, [['moe', 'larry', 'curly'], [30, 40, 50], [void 0, void 0, 'extra data']], 'zipped pairs with empties'); | |||
var empty = _.zip([]); | var empty = _.zip([]); | |||
deepEqual(empty, [], 'unzipped empty'); | assert.deepEqual(empty, [], 'unzipped empty'); | |||
deepEqual(_.zip(null), [], 'handles null'); | assert.deepEqual(_.zip(null), [], 'handles null'); | |||
deepEqual(_.zip(), [], '_.zip() returns []'); | assert.deepEqual(_.zip(), [], '_.zip() returns []'); | |||
}); | }); | |||
test('object', function() { | QUnit.test('unzip', function(assert) { | |||
assert.deepEqual(_.unzip(null), [], 'handles null'); | ||||
assert.deepEqual(_.unzip([['a', 'b'], [1, 2]]), [['a', 1], ['b', 2]]); | ||||
// complements zip | ||||
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]); | ||||
assert.deepEqual(_.unzip(zipped), [['fred', 'barney'], [30, 40], [true, fals | ||||
e]]); | ||||
zipped = _.zip(['moe', 30], ['larry', 40], ['curly', 50, 'extra data']); | ||||
assert.deepEqual(_.unzip(zipped), [['moe', 30, void 0], ['larry', 40, void 0 | ||||
], ['curly', 50, 'extra data']], 'Uses length of largest array'); | ||||
}); | ||||
QUnit.test('object', function(assert) { | ||||
var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]); | var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]); | |||
var shouldBe = {moe: 30, larry: 40, curly: 50}; | var shouldBe = {moe: 30, larry: 40, curly: 50}; | |||
deepEqual(result, shouldBe, 'two arrays zipped together into an object'); | assert.deepEqual(result, shouldBe, 'two arrays zipped together into an objec t'); | |||
result = _.object([['one', 1], ['two', 2], ['three', 3]]); | result = _.object([['one', 1], ['two', 2], ['three', 3]]); | |||
shouldBe = {one: 1, two: 2, three: 3}; | shouldBe = {one: 1, two: 2, three: 3}; | |||
deepEqual(result, shouldBe, 'an array of pairs zipped together into an objec t'); | assert.deepEqual(result, shouldBe, 'an array of pairs zipped together into a n object'); | |||
var stooges = {moe: 30, larry: 40, curly: 50}; | var stooges = {moe: 30, larry: 40, curly: 50}; | |||
deepEqual(_.object(_.pairs(stooges)), stooges, 'an object converted to pairs and back to an object'); | assert.deepEqual(_.object(_.pairs(stooges)), stooges, 'an object converted t o pairs and back to an object'); | |||
deepEqual(_.object(null), {}, 'handles nulls'); | assert.deepEqual(_.object(null), {}, 'handles nulls'); | |||
}); | }); | |||
test('indexOf', function() { | QUnit.test('indexOf', function(assert) { | |||
var numbers = [1, 2, 3]; | var numbers = [1, 2, 3]; | |||
equal(_.indexOf(numbers, 2), 1, 'can compute indexOf'); | assert.equal(_.indexOf(numbers, 2), 1, 'can compute indexOf'); | |||
var result = (function(){ return _.indexOf(arguments, 2); }(1, 2, 3)); | var result = (function(){ return _.indexOf(arguments, 2); }(1, 2, 3)); | |||
equal(result, 1, 'works on an arguments object'); | assert.equal(result, 1, 'works on an arguments object'); | |||
equal(_.indexOf(null, 2), -1, 'handles nulls properly'); | ||||
_.each([null, void 0, [], false], function(val) { | ||||
var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val); | ||||
assert.equal(_.indexOf(val, 2), -1, msg); | ||||
assert.equal(_.indexOf(val, 2, -1), -1, msg); | ||||
assert.equal(_.indexOf(val, 2, -20), -1, msg); | ||||
assert.equal(_.indexOf(val, 2, 15), -1, msg); | ||||
}); | ||||
var num = 35; | var num = 35; | |||
numbers = [10, 20, 30, 40, 50]; | numbers = [10, 20, 30, 40, 50]; | |||
var index = _.indexOf(numbers, num, true); | var index = _.indexOf(numbers, num, true); | |||
equal(index, -1, '35 is not in the list'); | assert.equal(index, -1, '35 is not in the list'); | |||
numbers = [10, 20, 30, 40, 50]; num = 40; | numbers = [10, 20, 30, 40, 50]; num = 40; | |||
index = _.indexOf(numbers, num, true); | index = _.indexOf(numbers, num, true); | |||
equal(index, 3, '40 is in the list'); | assert.equal(index, 3, '40 is in the list'); | |||
numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40; | numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40; | |||
equal(_.indexOf(numbers, num, true), 1, '40 is in the list'); | assert.equal(_.indexOf(numbers, num, true), 1, '40 is in the list'); | |||
equal(_.indexOf(numbers, 6, true), -1, '6 isnt in the list'); | assert.equal(_.indexOf(numbers, 6, true), -1, '6 isnt in the list'); | |||
equal(_.indexOf([1, 2, 5, 4, 6, 7], 5, true), -1, 'sorted indexOf doesn\'t u | assert.equal(_.indexOf([1, 2, 5, 4, 6, 7], 5, true), -1, 'sorted indexOf doe | |||
ses binary search'); | sn\'t uses binary search'); | |||
ok(_.every(['1', [], {}, null], function() { | assert.ok(_.every(['1', [], {}, null], function() { | |||
return _.indexOf(numbers, num, {}) === 1; | return _.indexOf(numbers, num, {}) === 1; | |||
}), 'non-nums as fromIndex make indexOf assume sorted'); | }), 'non-nums as fromIndex make indexOf assume sorted'); | |||
numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | |||
index = _.indexOf(numbers, 2, 5); | index = _.indexOf(numbers, 2, 5); | |||
equal(index, 7, 'supports the fromIndex argument'); | assert.equal(index, 7, 'supports the fromIndex argument'); | |||
index = _.indexOf([,,,], undefined); | index = _.indexOf([,,, 0], void 0); | |||
equal(index, 0, 'treats sparse arrays as if they were dense'); | assert.equal(index, 0, 'treats sparse arrays as if they were dense'); | |||
var array = [1, 2, 3, 1, 2, 3]; | var array = [1, 2, 3, 1, 2, 3]; | |||
strictEqual(_.indexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the right | assert.strictEqual(_.indexOf(array, 1, -3), 3, 'neg `fromIndex` starts at th | |||
index'); | e right index'); | |||
strictEqual(_.indexOf(array, 1, -2), -1, 'neg `fromIndex` starts at the righ | assert.strictEqual(_.indexOf(array, 1, -2), -1, 'neg `fromIndex` starts at t | |||
t index'); | he right index'); | |||
strictEqual(_.indexOf(array, 2, -3), 4); | assert.strictEqual(_.indexOf(array, 2, -3), 4); | |||
_.each([-6, -8, -Infinity], function(fromIndex) { | _.each([-6, -8, -Infinity], function(fromIndex) { | |||
strictEqual(_.indexOf(array, 1, fromIndex), 0); | assert.strictEqual(_.indexOf(array, 1, fromIndex), 0); | |||
}); | ||||
assert.strictEqual(_.indexOf([1, 2, 3], 1, true), 0); | ||||
index = _.indexOf([], void 0, true); | ||||
assert.equal(index, -1, 'empty array with truthy `isSorted` returns -1'); | ||||
}); | ||||
QUnit.test('indexOf with NaN', function(assert) { | ||||
assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN), 2, 'Expected [1, 2, NaN | ||||
] to contain NaN'); | ||||
assert.strictEqual(_.indexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2, Na | ||||
N] to contain NaN'); | ||||
assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, 1), 2, 'startIndex does | ||||
not affect result'); | ||||
assert.strictEqual(_.indexOf([1, 2, NaN, NaN], NaN, -2), 2, 'startIndex does | ||||
not affect result'); | ||||
(function() { | ||||
assert.strictEqual(_.indexOf(arguments, NaN), 2, 'Expected arguments [1, 2 | ||||
, NaN] to contain NaN'); | ||||
}(1, 2, NaN, NaN)); | ||||
}); | ||||
QUnit.test('indexOf with +- 0', function(assert) { | ||||
_.each([-0, +0], function(val) { | ||||
assert.strictEqual(_.indexOf([1, 2, val, val], val), 2); | ||||
assert.strictEqual(_.indexOf([1, 2, val, val], -val), 2); | ||||
}); | }); | |||
strictEqual(_.indexOf([1, 2, 3], 1, true), 0); | ||||
}); | }); | |||
test('lastIndexOf', function() { | QUnit.test('lastIndexOf', function(assert) { | |||
var numbers = [1, 0, 1]; | var numbers = [1, 0, 1]; | |||
var falsey = [void 0, '', 0, false, NaN, null, undefined]; | var falsey = [void 0, '', 0, false, NaN, null, void 0]; | |||
equal(_.lastIndexOf(numbers, 1), 2); | assert.equal(_.lastIndexOf(numbers, 1), 2); | |||
numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0]; | numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0]; | |||
numbers.lastIndexOf = null; | numbers.lastIndexOf = null; | |||
equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without t | assert.equal(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even wi | |||
he native function'); | thout the native function'); | |||
equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element'); | assert.equal(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element'); | |||
var result = (function(){ return _.lastIndexOf(arguments, 1); }(1, 0, 1, 0, 0, 1, 0, 0, 0)); | var result = (function(){ return _.lastIndexOf(arguments, 1); }(1, 0, 1, 0, 0, 1, 0, 0, 0)); | |||
equal(result, 5, 'works on an arguments object'); | assert.equal(result, 5, 'works on an arguments object'); | |||
equal(_.lastIndexOf(null, 2), -1, 'handles nulls properly'); | ||||
_.each([null, void 0, [], false], function(val) { | ||||
var msg = 'Handles: ' + (_.isArray(val) ? '[]' : val); | ||||
assert.equal(_.lastIndexOf(val, 2), -1, msg); | ||||
assert.equal(_.lastIndexOf(val, 2, -1), -1, msg); | ||||
assert.equal(_.lastIndexOf(val, 2, -20), -1, msg); | ||||
assert.equal(_.lastIndexOf(val, 2, 15), -1, msg); | ||||
}); | ||||
numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | |||
var index = _.lastIndexOf(numbers, 2, 2); | var index = _.lastIndexOf(numbers, 2, 2); | |||
equal(index, 1, 'supports the fromIndex argument'); | assert.equal(index, 1, 'supports the fromIndex argument'); | |||
var array = [1, 2, 3, 1, 2, 3]; | var array = [1, 2, 3, 1, 2, 3]; | |||
strictEqual(_.lastIndexOf(array, 1, 0), 0, 'starts at the correct from idx') | assert.strictEqual(_.lastIndexOf(array, 1, 0), 0, 'starts at the correct fro | |||
; | m idx'); | |||
strictEqual(_.lastIndexOf(array, 3), 5, 'should return the index of the last | assert.strictEqual(_.lastIndexOf(array, 3), 5, 'should return the index of t | |||
matched value'); | he last matched value'); | |||
strictEqual(_.lastIndexOf(array, 4), -1, 'should return `-1` for an unmatche | assert.strictEqual(_.lastIndexOf(array, 4), -1, 'should return `-1` for an u | |||
d value'); | nmatched value'); | |||
strictEqual(_.lastIndexOf(array, 1, 2), 0, 'should work with a positive `fro mIndex`'); | assert.strictEqual(_.lastIndexOf(array, 1, 2), 0, 'should work with a positi ve `fromIndex`'); | |||
_.each([6, 8, Math.pow(2, 32), Infinity], function(fromIndex) { | _.each([6, 8, Math.pow(2, 32), Infinity], function(fromIndex) { | |||
strictEqual(_.lastIndexOf(array, undefined, fromIndex), -1); | assert.strictEqual(_.lastIndexOf(array, void 0, fromIndex), -1); | |||
strictEqual(_.lastIndexOf(array, 1, fromIndex), 3); | assert.strictEqual(_.lastIndexOf(array, 1, fromIndex), 3); | |||
strictEqual(_.lastIndexOf(array, '', fromIndex), -1); | assert.strictEqual(_.lastIndexOf(array, '', fromIndex), -1); | |||
}); | }); | |||
var expected = _.map(falsey, function(value) { | var expected = _.map(falsey, function(value) { | |||
return typeof value == 'number' ? -1 : 5; | return typeof value == 'number' ? -1 : 5; | |||
}); | }); | |||
var actual = _.map(falsey, function(fromIndex) { | var actual = _.map(falsey, function(fromIndex) { | |||
return _.lastIndexOf(array, 3, fromIndex); | return _.lastIndexOf(array, 3, fromIndex); | |||
}); | }); | |||
deepEqual(actual, expected, 'should treat falsey `fromIndex` values, except | assert.deepEqual(actual, expected, 'should treat falsey `fromIndex` values, | |||
`0` and `NaN`, as `array.length`'); | except `0` and `NaN`, as `array.length`'); | |||
strictEqual(_.lastIndexOf(array, 3, '1'), 5, 'should treat non-number `fromI | assert.strictEqual(_.lastIndexOf(array, 3, '1'), 5, 'should treat non-number | |||
ndex` values as `array.length`'); | `fromIndex` values as `array.length`'); | |||
strictEqual(_.lastIndexOf(array, 3, true), 5, 'should treat non-number `from | assert.strictEqual(_.lastIndexOf(array, 3, true), 5, 'should treat non-numbe | |||
Index` values as `array.length`'); | r `fromIndex` values as `array.length`'); | |||
strictEqual(_.lastIndexOf(array, 2, -3), 1, 'should work with a negative `fr | assert.strictEqual(_.lastIndexOf(array, 2, -3), 1, 'should work with a negat | |||
omIndex`'); | ive `fromIndex`'); | |||
strictEqual(_.lastIndexOf(array, 1, -3), 3, 'neg `fromIndex` starts at the r | assert.strictEqual(_.lastIndexOf(array, 1, -3), 3, 'neg `fromIndex` starts a | |||
ight index'); | t the right index'); | |||
deepEqual(_.map([-6, -8, -Infinity], function(fromIndex) { | assert.deepEqual(_.map([-6, -8, -Infinity], function(fromIndex) { | |||
return _.lastIndexOf(array, 1, fromIndex); | return _.lastIndexOf(array, 1, fromIndex); | |||
}), [0, -1, -1]); | }), [0, -1, -1]); | |||
}); | }); | |||
test('range', function() { | QUnit.test('lastIndexOf with NaN', function(assert) { | |||
deepEqual(_.range(0), [], 'range with 0 as a first argument generates an emp | assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN), 3, 'Expected [1, 2, | |||
ty array'); | NaN] to contain NaN'); | |||
deepEqual(_.range(4), [0, 1, 2, 3], 'range with a single positive argument g | assert.strictEqual(_.lastIndexOf([1, 2, Infinity], NaN), -1, 'Expected [1, 2 | |||
enerates an array of elements 0,1,2,...,n-1'); | , NaN] to contain NaN'); | |||
deepEqual(_.range(5, 8), [5, 6, 7], 'range with two arguments a & b, a&l | ||||
t;b generates an array of elements a,a+1,a+2,...,b-2,b-1'); | assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, 2), 2, 'fromIndex do | |||
deepEqual(_.range(8, 5), [], 'range with two arguments a & b, b<a gen | es not affect result'); | |||
erates an empty array'); | assert.strictEqual(_.lastIndexOf([1, 2, NaN, NaN], NaN, -2), 2, 'fromIndex d | |||
deepEqual(_.range(3, 10, 3), [3, 6, 9], 'range with three arguments a & | oes not affect result'); | |||
b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b | ||||
- (multiplier of a) < c'); | (function() { | |||
deepEqual(_.range(3, 10, 15), [3], 'range with three arguments a & b &am | assert.strictEqual(_.lastIndexOf(arguments, NaN), 3, 'Expected arguments [ | |||
p; c, c > b-a, a < b generates an array with a single element, equal to a' | 1, 2, NaN] to contain NaN'); | |||
); | }(1, 2, NaN, NaN)); | |||
deepEqual(_.range(12, 7, -2), [12, 10, 8], 'range with three arguments a &am | }); | |||
p; b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and e | ||||
nds with the number not less than b'); | QUnit.test('lastIndexOf with +- 0', function(assert) { | |||
deepEqual(_.range(0, -10, -1), [0, -1, -2, -3, -4, -5, -6, -7, -8, -9], 'fin | _.each([-0, +0], function(val) { | |||
al example in the Python docs'); | assert.strictEqual(_.lastIndexOf([1, 2, val, val], val), 3); | |||
assert.strictEqual(_.lastIndexOf([1, 2, val, val], -val), 3); | ||||
assert.strictEqual(_.lastIndexOf([-1, 1, 2], -val), -1); | ||||
}); | ||||
}); | }); | |||
QUnit.test('findIndex', function(assert) { | ||||
var objects = [ | ||||
{a: 0, b: 0}, | ||||
{a: 1, b: 1}, | ||||
{a: 2, b: 2}, | ||||
{a: 0, b: 0} | ||||
]; | ||||
assert.equal(_.findIndex(objects, function(obj) { | ||||
return obj.a === 0; | ||||
}), 0); | ||||
assert.equal(_.findIndex(objects, function(obj) { | ||||
return obj.b * obj.a === 4; | ||||
}), 2); | ||||
assert.equal(_.findIndex(objects, 'a'), 1, 'Uses lookupIterator'); | ||||
assert.equal(_.findIndex(objects, function(obj) { | ||||
return obj.b * obj.a === 5; | ||||
}), -1); | ||||
assert.equal(_.findIndex(null, _.noop), -1); | ||||
assert.strictEqual(_.findIndex(objects, function(a) { | ||||
return a.foo === null; | ||||
}), -1); | ||||
_.findIndex([{a: 1}], function(a, key, obj) { | ||||
assert.equal(key, 0); | ||||
assert.deepEqual(obj, [{a: 1}]); | ||||
assert.strictEqual(this, objects, 'called with context'); | ||||
}, objects); | ||||
var sparse = []; | ||||
sparse[20] = {a: 2, b: 2}; | ||||
assert.equal(_.findIndex(sparse, function(obj) { | ||||
return obj && obj.b * obj.a === 4; | ||||
}), 20, 'Works with sparse arrays'); | ||||
var array = [1, 2, 3, 4]; | ||||
array.match = 55; | ||||
assert.strictEqual(_.findIndex(array, function(x) { return x === 55; }), -1, | ||||
'doesn\'t match array-likes keys'); | ||||
}); | ||||
QUnit.test('findLastIndex', function(assert) { | ||||
var objects = [ | ||||
{a: 0, b: 0}, | ||||
{a: 1, b: 1}, | ||||
{a: 2, b: 2}, | ||||
{a: 0, b: 0} | ||||
]; | ||||
assert.equal(_.findLastIndex(objects, function(obj) { | ||||
return obj.a === 0; | ||||
}), 3); | ||||
assert.equal(_.findLastIndex(objects, function(obj) { | ||||
return obj.b * obj.a === 4; | ||||
}), 2); | ||||
assert.equal(_.findLastIndex(objects, 'a'), 2, 'Uses lookupIterator'); | ||||
assert.equal(_.findLastIndex(objects, function(obj) { | ||||
return obj.b * obj.a === 5; | ||||
}), -1); | ||||
assert.equal(_.findLastIndex(null, _.noop), -1); | ||||
assert.strictEqual(_.findLastIndex(objects, function(a) { | ||||
return a.foo === null; | ||||
}), -1); | ||||
_.findLastIndex([{a: 1}], function(a, key, obj) { | ||||
assert.equal(key, 0); | ||||
assert.deepEqual(obj, [{a: 1}]); | ||||
assert.strictEqual(this, objects, 'called with context'); | ||||
}, objects); | ||||
var sparse = []; | ||||
sparse[20] = {a: 2, b: 2}; | ||||
assert.equal(_.findLastIndex(sparse, function(obj) { | ||||
return obj && obj.b * obj.a === 4; | ||||
}), 20, 'Works with sparse arrays'); | ||||
var array = [1, 2, 3, 4]; | ||||
array.match = 55; | ||||
assert.strictEqual(_.findLastIndex(array, function(x) { return x === 55; }), | ||||
-1, 'doesn\'t match array-likes keys'); | ||||
}); | ||||
QUnit.test('range', function(assert) { | ||||
assert.deepEqual(_.range(0), [], 'range with 0 as a first argument generates | ||||
an empty array'); | ||||
assert.deepEqual(_.range(4), [0, 1, 2, 3], 'range with a single positive arg | ||||
ument generates an array of elements 0,1,2,...,n-1'); | ||||
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'); | ||||
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'); | ||||
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, equa | ||||
l to a'); | ||||
assert.deepEqual(_.range(12, 7, -2), [12, 10, 8], 'range with three argument | ||||
s a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2 | ||||
c and ends with the number not less than b'); | ||||
assert.deepEqual(_.range(0, -10, -1), [0, -1, -2, -3, -4, -5, -6, -7, -8, -9 | ||||
], 'final example in the Python docs'); | ||||
assert.strictEqual(1 / _.range(-0, 1)[0], -Infinity, 'should preserve -0'); | ||||
assert.deepEqual(_.range(8, 5), [8, 7, 6], 'negative range generates descend | ||||
ing array'); | ||||
assert.deepEqual(_.range(-3), [0, -1, -2], 'negative range generates descend | ||||
ing array'); | ||||
}); | ||||
QUnit.test('chunk', function(assert) { | ||||
assert.deepEqual(_.chunk([], 2), [], 'chunk for empty array returns an empty | ||||
array'); | ||||
assert.deepEqual(_.chunk([1, 2, 3], 0), [], 'chunk into parts of 0 elements | ||||
returns empty array'); | ||||
assert.deepEqual(_.chunk([1, 2, 3], -1), [], 'chunk into parts of negative a | ||||
mount of elements returns an empty array'); | ||||
assert.deepEqual(_.chunk([1, 2, 3]), [], 'defaults to empty array (chunk siz | ||||
e 0)'); | ||||
assert.deepEqual(_.chunk([1, 2, 3], 1), [[1], [2], [3]], 'chunk into parts o | ||||
f 1 elements returns original array'); | ||||
assert.deepEqual(_.chunk([1, 2, 3], 3), [[1, 2, 3]], 'chunk into parts of cu | ||||
rrent array length elements returns the original array'); | ||||
assert.deepEqual(_.chunk([1, 2, 3], 5), [[1, 2, 3]], 'chunk into parts of mo | ||||
re then current array length elements returns the original array'); | ||||
assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 2), [[10, 20], [30, 4 | ||||
0], [50, 60], [70]], 'chunk into parts of less then current array length element | ||||
s'); | ||||
assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 3), [[10, 20, 30], [4 | ||||
0, 50, 60], [70]], 'chunk into parts of less then current array length elements' | ||||
); | ||||
}); | ||||
}()); | }()); | |||
End of changes. 93 change blocks. | ||||
197 lines changed or deleted | 484 lines changed or added |