collections.js (lodash-4.0.0) | : | collections.js (lodash-4.17.21) | ||
---|---|---|---|---|
skipping to change at line 190 | skipping to change at line 190 | |||
sum = _([1, 2, 3]).reduce(function(memo, num){ return memo + num; }, 0); | sum = _([1, 2, 3]).reduce(function(memo, num){ return memo + num; }, 0); | |||
assert.equal(sum, 6, 'OO-style reduce'); | assert.equal(sum, 6, 'OO-style reduce'); | |||
sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }); | sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }); | |||
assert.equal(sum, 6, 'default initial value'); | assert.equal(sum, 6, 'default initial value'); | |||
var prod = _.reduce([1, 2, 3, 4], function(memo, num){ return memo * num; }) ; | var prod = _.reduce([1, 2, 3, 4], function(memo, num){ return memo * num; }) ; | |||
assert.equal(prod, 24, 'can reduce via multiplication'); | assert.equal(prod, 24, 'can reduce via multiplication'); | |||
assert.ok(_.reduce(null, _.noop, 138) === 138, 'handles a null (with initial value) properly'); | assert.strictEqual(_.reduce(null, _.noop, 138), 138, 'handles a null (with i nitial value) properly'); | |||
assert.equal(_.reduce([], _.noop, void 0), void 0, 'undefined can be passed as a special case'); | assert.equal(_.reduce([], _.noop, void 0), void 0, 'undefined can be passed as a special case'); | |||
assert.equal(_.reduce([_], _.noop), _, 'collection of length one with no ini tial value returns the first item'); | assert.equal(_.reduce([_], _.noop), _, 'collection of length one with no ini tial value returns the first item'); | |||
assert.equal(_.reduce([], _.noop), void 0, 'returns undefined when collectio n is empty and no initial value'); | assert.equal(_.reduce([], _.noop), void 0, 'returns undefined when collectio n is empty and no initial value'); | |||
}); | }); | |||
QUnit.test('foldl', function(assert) { | QUnit.test('foldl', function(assert) { | |||
assert.strictEqual(_.foldl, _.reduce, 'is an alias for reduce'); | assert.strictEqual(_.foldl, _.reduce, 'is an alias for reduce'); | |||
}); | }); | |||
QUnit.test('inject', function(assert) { | QUnit.test('inject', function(assert) { | |||
skipping to change at line 214 | skipping to change at line 214 | |||
QUnit.test('reduceRight', function(assert) { | QUnit.test('reduceRight', function(assert) { | |||
var list = _.reduceRight(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }, ''); | var list = _.reduceRight(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }, ''); | |||
assert.equal(list, 'bazbarfoo', 'can perform right folds'); | assert.equal(list, 'bazbarfoo', 'can perform right folds'); | |||
list = _.reduceRight(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }); | list = _.reduceRight(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }); | |||
assert.equal(list, 'bazbarfoo', 'default initial value'); | assert.equal(list, 'bazbarfoo', 'default initial value'); | |||
var sum = _.reduceRight({a: 1, b: 2, c: 3}, function(memo, num){ return memo + num; }); | var sum = _.reduceRight({a: 1, b: 2, c: 3}, function(memo, num){ return memo + num; }); | |||
assert.equal(sum, 6, 'default initial value on object'); | assert.equal(sum, 6, 'default initial value on object'); | |||
assert.ok(_.reduceRight(null, _.noop, 138) === 138, 'handles a null (with in itial value) properly'); | assert.strictEqual(_.reduceRight(null, _.noop, 138), 138, 'handles a null (w ith initial value) properly'); | |||
assert.equal(_.reduceRight([_], _.noop), _, 'collection of length one with n o initial value returns the first item'); | assert.equal(_.reduceRight([_], _.noop), _, 'collection of length one with n o initial value returns the first item'); | |||
assert.equal(_.reduceRight([], _.noop, void 0), void 0, 'undefined can be pa ssed as a special case'); | assert.equal(_.reduceRight([], _.noop, void 0), void 0, 'undefined can be pa ssed as a special case'); | |||
assert.equal(_.reduceRight([], _.noop), void 0, 'returns undefined when coll ection is empty and no initial value'); | assert.equal(_.reduceRight([], _.noop), void 0, 'returns undefined when coll ection is empty and no initial value'); | |||
// Assert that the correct arguments are being passed. | // Assert that the correct arguments are being passed. | |||
var args, | var args, | |||
init = {}, | init = {}, | |||
object = {a: 1, b: 2}, | object = {a: 1, b: 2}, | |||
skipping to change at line 270 | skipping to change at line 270 | |||
assert.strictEqual(_.find(array, function(n) { return n > 2; }), 3, 'should return first found `value`'); | assert.strictEqual(_.find(array, function(n) { return n > 2; }), 3, 'should return first found `value`'); | |||
assert.strictEqual(_.find(array, function() { return false; }), void 0, 'sho uld return `undefined` if `value` is not found'); | assert.strictEqual(_.find(array, function() { return false; }), void 0, 'sho uld return `undefined` if `value` is not found'); | |||
array.dontmatch = 55; | array.dontmatch = 55; | |||
assert.strictEqual(_.find(array, function(x) { return x === 55; }), void 0, 'iterates array-likes correctly'); | assert.strictEqual(_.find(array, function(x) { return x === 55; }), void 0, 'iterates array-likes correctly'); | |||
// Matching an object like _.findWhere. | // Matching an object like _.findWhere. | |||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b : 4}]; | var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b : 4}]; | |||
assert.deepEqual(_.find(list, {a: 1}), {a: 1, b: 2}, 'can be used as findWhe re'); | assert.deepEqual(_.find(list, {a: 1}), {a: 1, b: 2}, 'can be used as findWhe re'); | |||
assert.deepEqual(_.find(list, {b: 4}), {a: 1, b: 4}); | assert.deepEqual(_.find(list, {b: 4}), {a: 1, b: 4}); | |||
assert.ok(!_.find(list, {c: 1}), 'undefined when not found'); | assert.notOk(_.find(list, {c: 1}), 'undefined when not found'); | |||
assert.ok(!_.find([], {c: 1}), 'undefined when searching empty list'); | assert.notOk(_.find([], {c: 1}), 'undefined when searching empty list'); | |||
var result = _.find([1, 2, 3], function(num){ return num * 2 === 4; }); | var result = _.find([1, 2, 3], function(num){ return num * 2 === 4; }); | |||
assert.equal(result, 2, 'found the first "2" and broke the loop'); | assert.equal(result, 2, 'found the first "2" and broke the loop'); | |||
var obj = { | var obj = { | |||
a: {x: 1, z: 3}, | a: {x: 1, z: 3}, | |||
b: {x: 2, z: 2}, | b: {x: 2, z: 2}, | |||
c: {x: 3, z: 4}, | c: {x: 3, z: 4}, | |||
d: {x: 4, z: 1} | d: {x: 4, z: 1} | |||
}; | }; | |||
skipping to change at line 350 | skipping to change at line 350 | |||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | |||
assert.deepEqual(_.reject(list, {a: 1}), [{a: 2, b: 2}]); | assert.deepEqual(_.reject(list, {a: 1}), [{a: 2, b: 2}]); | |||
assert.deepEqual(_.reject(list, {b: 2}), [{a: 1, b: 3}, {a: 1, b: 4}]); | assert.deepEqual(_.reject(list, {b: 2}), [{a: 1, b: 3}, {a: 1, b: 4}]); | |||
assert.deepEqual(_.reject(list, {}), [], 'Returns empty list given empty obj ect'); | assert.deepEqual(_.reject(list, {}), [], 'Returns empty list given empty obj ect'); | |||
assert.deepEqual(_.reject(list, []), [], 'Returns empty list given empty arr ay'); | assert.deepEqual(_.reject(list, []), [], 'Returns empty list given empty arr ay'); | |||
}); | }); | |||
QUnit.test('every', function(assert) { | QUnit.test('every', function(assert) { | |||
assert.ok(_.every([], _.identity), 'the empty set'); | assert.ok(_.every([], _.identity), 'the empty set'); | |||
assert.ok(_.every([true, true, true], _.identity), 'every true values'); | assert.ok(_.every([true, true, true], _.identity), 'every true values'); | |||
assert.ok(!_.every([true, false, true], _.identity), 'one false value'); | assert.notOk(_.every([true, false, true], _.identity), 'one false value'); | |||
assert.ok(_.every([0, 10, 28], function(num){ return num % 2 === 0; }), 'eve n numbers'); | assert.ok(_.every([0, 10, 28], function(num){ return num % 2 === 0; }), 'eve n numbers'); | |||
assert.ok(!_.every([0, 11, 28], function(num){ return num % 2 === 0; }), 'an | assert.notOk(_.every([0, 11, 28], function(num){ return num % 2 === 0; }), ' | |||
odd number'); | an odd number'); | |||
assert.ok(_.every([1], _.identity) === true, 'cast to boolean - true'); | assert.strictEqual(_.every([1], _.identity), true, 'cast to boolean - true') | |||
assert.ok(_.every([0], _.identity) === false, 'cast to boolean - false'); | ; | |||
assert.ok(!_.every([void 0, void 0, void 0], _.identity), 'works with arrays | assert.strictEqual(_.every([0], _.identity), false, 'cast to boolean - false | |||
of undefined'); | '); | |||
assert.notOk(_.every([void 0, void 0, void 0], _.identity), 'works with arra | ||||
ys of undefined'); | ||||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | |||
assert.ok(!_.every(list, {a: 1, b: 2}), 'Can be called with object'); | assert.notOk(_.every(list, {a: 1, b: 2}), 'Can be called with object'); | |||
assert.ok(_.every(list, 'a'), 'String mapped to object property'); | assert.ok(_.every(list, 'a'), 'String mapped to object property'); | |||
list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | |||
assert.ok(_.every(list, {b: 2}), 'Can be called with object'); | assert.ok(_.every(list, {b: 2}), 'Can be called with object'); | |||
assert.ok(!_.every(list, 'c'), 'String mapped to object property'); | assert.notOk(_.every(list, 'c'), 'String mapped to object property'); | |||
assert.ok(_.every({a: 1, b: 2, c: 3, d: 4}, _.isNumber), 'takes objects'); | assert.ok(_.every({a: 1, b: 2, c: 3, d: 4}, _.isNumber), 'takes objects'); | |||
assert.ok(!_.every({a: 1, b: 2, c: 3, d: 4}, _.isObject), 'takes objects'); | assert.notOk(_.every({a: 1, b: 2, c: 3, d: 4}, _.isObject), 'takes objects') ; | |||
assert.ok(_.every(['a', 'b', 'c', 'd'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | assert.ok(_.every(['a', 'b', 'c', 'd'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | |||
assert.ok(!_.every(['a', 'b', 'c', 'd', 'f'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | assert.notOk(_.every(['a', 'b', 'c', 'd', 'f'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | |||
}); | }); | |||
QUnit.test('all', function(assert) { | QUnit.test('all', function(assert) { | |||
assert.strictEqual(_.all, _.every, 'is an alias for every'); | assert.strictEqual(_.all, _.every, 'is an alias for every'); | |||
}); | }); | |||
QUnit.test('some', function(assert) { | QUnit.test('some', function(assert) { | |||
assert.ok(!_.some([]), 'the empty set'); | assert.notOk(_.some([]), 'the empty set'); | |||
assert.ok(!_.some([false, false, false]), 'all false values'); | assert.notOk(_.some([false, false, false]), 'all false values'); | |||
assert.ok(_.some([false, false, true]), 'one true value'); | assert.ok(_.some([false, false, true]), 'one true value'); | |||
assert.ok(_.some([null, 0, 'yes', false]), 'a string'); | assert.ok(_.some([null, 0, 'yes', false]), 'a string'); | |||
assert.ok(!_.some([null, 0, '', false]), 'falsy values'); | assert.notOk(_.some([null, 0, '', false]), 'falsy values'); | |||
assert.ok(!_.some([1, 11, 29], function(num){ return num % 2 === 0; }), 'all | assert.notOk(_.some([1, 11, 29], function(num){ return num % 2 === 0; }), 'a | |||
odd numbers'); | ll odd numbers'); | |||
assert.ok(_.some([1, 10, 29], function(num){ return num % 2 === 0; }), 'an e ven number'); | assert.ok(_.some([1, 10, 29], function(num){ return num % 2 === 0; }), 'an e ven number'); | |||
assert.ok(_.some([1], _.identity) === true, 'cast to boolean - true'); | assert.strictEqual(_.some([1], _.identity), true, 'cast to boolean - true'); | |||
assert.ok(_.some([0], _.identity) === false, 'cast to boolean - false'); | assert.strictEqual(_.some([0], _.identity), false, 'cast to boolean - false' | |||
); | ||||
assert.ok(_.some([false, false, true])); | assert.ok(_.some([false, false, true])); | |||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | |||
assert.ok(!_.some(list, {a: 5, b: 2}), 'Can be called with object'); | assert.notOk(_.some(list, {a: 5, b: 2}), 'Can be called with object'); | |||
assert.ok(_.some(list, 'a'), 'String mapped to object property'); | assert.ok(_.some(list, 'a'), 'String mapped to object property'); | |||
list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | |||
assert.ok(_.some(list, {b: 2}), 'Can be called with object'); | assert.ok(_.some(list, {b: 2}), 'Can be called with object'); | |||
assert.ok(!_.some(list, 'd'), 'String mapped to object property'); | assert.notOk(_.some(list, 'd'), 'String mapped to object property'); | |||
assert.ok(_.some({a: '1', b: '2', c: '3', d: '4', e: 6}, _.isNumber), 'takes objects'); | assert.ok(_.some({a: '1', b: '2', c: '3', d: '4', e: 6}, _.isNumber), 'takes objects'); | |||
assert.ok(!_.some({a: 1, b: 2, c: 3, d: 4}, _.isObject), 'takes objects'); | assert.notOk(_.some({a: 1, b: 2, c: 3, d: 4}, _.isObject), 'takes objects'); | |||
assert.ok(_.some(['a', 'b', 'c', 'd'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | assert.ok(_.some(['a', 'b', 'c', 'd'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | |||
assert.ok(!_.some(['x', 'y', 'z'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4 }), 'context works'); | assert.notOk(_.some(['x', 'y', 'z'], _.hasOwnProperty, {a: 1, b: 2, c: 3, d: 4}), 'context works'); | |||
}); | }); | |||
QUnit.test('any', function(assert) { | QUnit.test('any', function(assert) { | |||
assert.strictEqual(_.any, _.some, 'is an alias for some'); | assert.strictEqual(_.any, _.some, 'is an alias for some'); | |||
}); | }); | |||
QUnit.test('includes', function(assert) { | QUnit.test('includes', function(assert) { | |||
_.each([null, void 0, 0, 1, NaN, {}, []], function(val) { | _.each([null, void 0, 0, 1, NaN, {}, []], function(val) { | |||
assert.strictEqual(_.includes(val, 'hasOwnProperty'), false); | assert.strictEqual(_.includes(val, 'hasOwnProperty'), false); | |||
}); | }); | |||
assert.strictEqual(_.includes([1, 2, 3], 2), true, 'two is in the array'); | assert.strictEqual(_.includes([1, 2, 3], 2), true, 'two is in the array'); | |||
assert.ok(!_.includes([1, 3, 9], 2), 'two is not in the array'); | assert.notOk(_.includes([1, 3, 9], 2), 'two is not in the array'); | |||
assert.strictEqual(_.includes([5, 4, 3, 2, 1], 5, true), true, 'doesn\'t del egate to binary search'); | assert.strictEqual(_.includes([5, 4, 3, 2, 1], 5, true), true, 'doesn\'t del egate to binary search'); | |||
assert.ok(_.includes({moe: 1, larry: 3, curly: 9}, 3) === true, '_.includes on objects checks their values'); | assert.strictEqual(_.includes({moe: 1, larry: 3, curly: 9}, 3), true, '_.inc ludes on objects checks their values'); | |||
assert.ok(_([1, 2, 3]).includes(2), 'OO-style includes'); | assert.ok(_([1, 2, 3]).includes(2), 'OO-style includes'); | |||
var numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | var numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3]; | |||
assert.strictEqual(_.includes(numbers, 1, 1), true, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, 1), true, 'takes a fromIndex'); | |||
assert.strictEqual(_.includes(numbers, 1, -1), false, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, -1), false, 'takes a fromIndex'); | |||
assert.strictEqual(_.includes(numbers, 1, -2), false, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, -2), false, 'takes a fromIndex'); | |||
assert.strictEqual(_.includes(numbers, 1, -3), true, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, -3), true, 'takes a fromIndex'); | |||
assert.strictEqual(_.includes(numbers, 1, 6), true, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, 6), true, 'takes a fromIndex'); | |||
assert.strictEqual(_.includes(numbers, 1, 7), false, 'takes a fromIndex'); | assert.strictEqual(_.includes(numbers, 1, 7), false, 'takes a fromIndex'); | |||
skipping to change at line 550 | skipping to change at line 550 | |||
} | } | |||
var expect = {c: 1, x: 'foo', y: 5}; | var expect = {c: 1, x: 'foo', y: 5}; | |||
assert.deepEqual(_.findWhere([{y: 5, b: 6}, expect], new TestClass()), expec t, 'uses class instance properties'); | assert.deepEqual(_.findWhere([{y: 5, b: 6}, expect], new TestClass()), expec t, 'uses class instance properties'); | |||
}); | }); | |||
QUnit.test('max', function(assert) { | QUnit.test('max', function(assert) { | |||
assert.equal(-Infinity, _.max(null), 'can handle null/undefined'); | assert.equal(-Infinity, _.max(null), 'can handle null/undefined'); | |||
assert.equal(-Infinity, _.max(void 0), 'can handle null/undefined'); | assert.equal(-Infinity, _.max(void 0), 'can handle null/undefined'); | |||
assert.equal(-Infinity, _.max(null, _.identity), 'can handle null/undefined' ); | assert.equal(-Infinity, _.max(null, _.identity), 'can handle null/undefined' ); | |||
assert.equal(3, _.max([1, 2, 3]), 'can perform a regular Math.max'); | assert.equal(_.max([1, 2, 3]), 3, 'can perform a regular Math.max'); | |||
var neg = _.max([1, 2, 3], function(num){ return -num; }); | var neg = _.max([1, 2, 3], function(num){ return -num; }); | |||
assert.equal(neg, 1, 'can perform a computation-based max'); | assert.equal(neg, 1, 'can perform a computation-based max'); | |||
assert.equal(-Infinity, _.max({}), 'Maximum value of an empty object'); | assert.equal(-Infinity, _.max({}), 'Maximum value of an empty object'); | |||
assert.equal(-Infinity, _.max([]), 'Maximum value of an empty array'); | assert.equal(-Infinity, _.max([]), 'Maximum value of an empty array'); | |||
assert.equal(_.max({a: 'a'}), -Infinity, 'Maximum value of a non-numeric col lection'); | assert.equal(_.max({a: 'a'}), -Infinity, 'Maximum value of a non-numeric col lection'); | |||
assert.equal(299999, _.max(_.range(1, 300000)), 'Maximum value of a too-big array'); | assert.equal(_.max(_.range(1, 300000)), 299999, 'Maximum value of a too-big array'); | |||
assert.equal(3, _.max([1, 2, 3, 'test']), 'Finds correct max in array starti | assert.equal(_.max([1, 2, 3, 'test']), 3, 'Finds correct max in array starti | |||
ng with num and containing a NaN'); | ng with num and containing a NaN'); | |||
assert.equal(3, _.max(['test', 1, 2, 3]), 'Finds correct max in array starti | assert.equal(_.max(['test', 1, 2, 3]), 3, 'Finds correct max in array starti | |||
ng with NaN'); | ng with NaN'); | |||
assert.equal(3, _.max([1, 2, 3, null]), 'Finds correct max in array starting | assert.equal(_.max([1, 2, 3, null]), 3, 'Finds correct max in array starting | |||
with num and containing a `null`'); | with num and containing a `null`'); | |||
assert.equal(3, _.max([null, 1, 2, 3]), 'Finds correct max in array starting | assert.equal(_.max([null, 1, 2, 3]), 3, 'Finds correct max in array starting | |||
with a `null`'); | with a `null`'); | |||
assert.equal(3, _.max([1, 2, 3, '']), 'Finds correct max in array starting w | assert.equal(_.max([1, 2, 3, '']), 3, 'Finds correct max in array starting w | |||
ith num and containing an empty string'); | ith num and containing an empty string'); | |||
assert.equal(3, _.max(['', 1, 2, 3]), 'Finds correct max in array starting w | assert.equal(_.max(['', 1, 2, 3]), 3, 'Finds correct max in array starting w | |||
ith an empty string'); | ith an empty string'); | |||
assert.equal(3, _.max([1, 2, 3, false]), 'Finds correct max in array startin | assert.equal(_.max([1, 2, 3, false]), 3, 'Finds correct max in array startin | |||
g with num and containing a false'); | g with num and containing a false'); | |||
assert.equal(3, _.max([false, 1, 2, 3]), 'Finds correct max in array startin | assert.equal(_.max([false, 1, 2, 3]), 3, 'Finds correct max in array startin | |||
g with a false'); | g with a false'); | |||
assert.equal(4, _.max([0, 1, 2, 3, 4]), 'Finds correct max in array containi | assert.equal(_.max([0, 1, 2, 3, 4]), 4, 'Finds correct max in array containi | |||
ng a zero'); | ng a zero'); | |||
assert.equal(0, _.max([-3, -2, -1, 0]), 'Finds correct max in array containi | assert.equal(_.max([-3, -2, -1, 0]), 0, 'Finds correct max in array containi | |||
ng negative numbers'); | ng negative numbers'); | |||
assert.deepEqual([3, 6], _.map([[1, 2, 3], [4, 5, 6]], _.max), 'Finds correc t max in array when mapping through multiple arrays'); | assert.deepEqual(_.map([[1, 2, 3], [4, 5, 6]], _.max), [3, 6], 'Finds correc t max in array when mapping through multiple arrays'); | |||
var a = {x: -Infinity}; | var a = {x: -Infinity}; | |||
var b = {x: -Infinity}; | var b = {x: -Infinity}; | |||
var iterator = function(o){ return o.x; }; | var iterator = function(o){ return o.x; }; | |||
assert.equal(_.max([a, b], iterator), a, 'Respects iterator return value of -Infinity'); | assert.equal(_.max([a, b], iterator), a, 'Respects iterator return value of -Infinity'); | |||
assert.deepEqual(_.max([{a: 1}, {a: 0, b: 3}, {a: 4}, {a: 2}], 'a'), {a: 4}, 'String keys use property iterator'); | assert.deepEqual(_.max([{a: 1}, {a: 0, b: 3}, {a: 4}, {a: 2}], 'a'), {a: 4}, 'String keys use property iterator'); | |||
assert.deepEqual(_.max([0, 2], function(c){ return c * this.x; }, {x: 1}), 2 , 'Iterator context'); | assert.deepEqual(_.max([0, 2], function(c){ return c * this.x; }, {x: 1}), 2 , 'Iterator context'); | |||
assert.deepEqual(_.max([[1], [2, 3], [-1, 4], [5]], 0), [5], 'Lookup falsy i terator'); | assert.deepEqual(_.max([[1], [2, 3], [-1, 4], [5]], 0), [5], 'Lookup falsy i terator'); | |||
assert.deepEqual(_.max([{0: 1}, {0: 2}, {0: -1}, {a: 1}], 0), {0: 2}, 'Looku p falsy iterator'); | assert.deepEqual(_.max([{0: 1}, {0: 2}, {0: -1}, {a: 1}], 0), {0: 2}, 'Looku p falsy iterator'); | |||
}); | }); | |||
QUnit.test('min', function(assert) { | QUnit.test('min', function(assert) { | |||
assert.equal(Infinity, _.min(null), 'can handle null/undefined'); | assert.equal(_.min(null), Infinity, 'can handle null/undefined'); | |||
assert.equal(Infinity, _.min(void 0), 'can handle null/undefined'); | assert.equal(_.min(void 0), Infinity, 'can handle null/undefined'); | |||
assert.equal(Infinity, _.min(null, _.identity), 'can handle null/undefined') | assert.equal(_.min(null, _.identity), Infinity, 'can handle null/undefined') | |||
; | ; | |||
assert.equal(1, _.min([1, 2, 3]), 'can perform a regular Math.min'); | assert.equal(_.min([1, 2, 3]), 1, 'can perform a regular Math.min'); | |||
var neg = _.min([1, 2, 3], function(num){ return -num; }); | var neg = _.min([1, 2, 3], function(num){ return -num; }); | |||
assert.equal(neg, 3, 'can perform a computation-based min'); | assert.equal(neg, 3, 'can perform a computation-based min'); | |||
assert.equal(Infinity, _.min({}), 'Minimum value of an empty object'); | assert.equal(_.min({}), Infinity, 'Minimum value of an empty object'); | |||
assert.equal(Infinity, _.min([]), 'Minimum value of an empty array'); | assert.equal(_.min([]), Infinity, 'Minimum value of an empty array'); | |||
assert.equal(_.min({a: 'a'}), Infinity, 'Minimum value of a non-numeric coll ection'); | assert.equal(_.min({a: 'a'}), Infinity, 'Minimum value of a non-numeric coll ection'); | |||
assert.deepEqual([1, 4], _.map([[1, 2, 3], [4, 5, 6]], _.min), 'Finds correc t min in array when mapping through multiple arrays'); | assert.deepEqual(_.map([[1, 2, 3], [4, 5, 6]], _.min), [1, 4], 'Finds correc t min in array when mapping through multiple arrays'); | |||
var now = new Date(9999999999); | var now = new Date(9999999999); | |||
var then = new Date(0); | var then = new Date(0); | |||
assert.equal(_.min([now, then]), then); | assert.equal(_.min([now, then]), then); | |||
assert.equal(1, _.min(_.range(1, 300000)), 'Minimum value of a too-big array '); | assert.equal(_.min(_.range(1, 300000)), 1, 'Minimum value of a too-big array '); | |||
assert.equal(1, _.min([1, 2, 3, 'test']), 'Finds correct min in array starti | assert.equal(_.min([1, 2, 3, 'test']), 1, 'Finds correct min in array starti | |||
ng with num and containing a NaN'); | ng with num and containing a NaN'); | |||
assert.equal(1, _.min(['test', 1, 2, 3]), 'Finds correct min in array starti | assert.equal(_.min(['test', 1, 2, 3]), 1, 'Finds correct min in array starti | |||
ng with NaN'); | ng with NaN'); | |||
assert.equal(1, _.min([1, 2, 3, null]), 'Finds correct min in array starting | assert.equal(_.min([1, 2, 3, null]), 1, 'Finds correct min in array starting | |||
with num and containing a `null`'); | with num and containing a `null`'); | |||
assert.equal(1, _.min([null, 1, 2, 3]), 'Finds correct min in array starting | assert.equal(_.min([null, 1, 2, 3]), 1, 'Finds correct min in array starting | |||
with a `null`'); | with a `null`'); | |||
assert.equal(0, _.min([0, 1, 2, 3, 4]), 'Finds correct min in array containi | assert.equal(_.min([0, 1, 2, 3, 4]), 0, 'Finds correct min in array containi | |||
ng a zero'); | ng a zero'); | |||
assert.equal(-3, _.min([-3, -2, -1, 0]), 'Finds correct min in array contain | assert.equal(_.min([-3, -2, -1, 0]), -3, 'Finds correct min in array contain | |||
ing negative numbers'); | ing negative numbers'); | |||
var a = {x: Infinity}; | var a = {x: Infinity}; | |||
var b = {x: Infinity}; | var b = {x: Infinity}; | |||
var iterator = function(o){ return o.x; }; | var iterator = function(o){ return o.x; }; | |||
assert.equal(_.min([a, b], iterator), a, 'Respects iterator return value of Infinity'); | assert.equal(_.min([a, b], iterator), a, 'Respects iterator return value of Infinity'); | |||
assert.deepEqual(_.min([{a: 1}, {a: 0, b: 3}, {a: 4}, {a: 2}], 'a'), {a: 0, b: 3}, 'String keys use property iterator'); | assert.deepEqual(_.min([{a: 1}, {a: 0, b: 3}, {a: 4}, {a: 2}], 'a'), {a: 0, b: 3}, 'String keys use property iterator'); | |||
assert.deepEqual(_.min([0, 2], function(c){ return c * this.x; }, {x: -1}), 2, 'Iterator context'); | assert.deepEqual(_.min([0, 2], function(c){ return c * this.x; }, {x: -1}), 2, 'Iterator context'); | |||
assert.deepEqual(_.min([[1], [2, 3], [-1, 4], [5]], 0), [-1, 4], 'Lookup fal sy iterator'); | assert.deepEqual(_.min([[1], [2, 3], [-1, 4], [5]], 0), [-1, 4], 'Lookup fal sy iterator'); | |||
skipping to change at line 693 | skipping to change at line 693 | |||
assert.ok('0' in parity && '1' in parity, 'created a group for each value'); | assert.ok('0' in parity && '1' in parity, 'created a group for each value'); | |||
assert.deepEqual(parity[0], [2, 4, 6], 'put each even number in the right gr oup'); | assert.deepEqual(parity[0], [2, 4, 6], 'put each even number in the right gr oup'); | |||
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; | var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; | |||
var grouped = _.groupBy(list, 'length'); | var grouped = _.groupBy(list, 'length'); | |||
assert.deepEqual(grouped['3'], ['one', 'two', 'six', 'ten']); | assert.deepEqual(grouped['3'], ['one', 'two', 'six', 'ten']); | |||
assert.deepEqual(grouped['4'], ['four', 'five', 'nine']); | assert.deepEqual(grouped['4'], ['four', 'five', 'nine']); | |||
assert.deepEqual(grouped['5'], ['three', 'seven', 'eight']); | assert.deepEqual(grouped['5'], ['three', 'seven', 'eight']); | |||
var context = {}; | var context = {}; | |||
_.groupBy([{}], function(){ assert.ok(this === context); }, context); | _.groupBy([{}], function(){ assert.strictEqual(this, context); }, context); | |||
grouped = _.groupBy([4.2, 6.1, 6.4], function(num) { | grouped = _.groupBy([4.2, 6.1, 6.4], function(num) { | |||
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor'; | return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor'; | |||
}); | }); | |||
assert.equal(grouped.constructor.length, 1); | assert.equal(grouped.constructor.length, 1); | |||
assert.equal(grouped.hasOwnProperty.length, 2); | assert.equal(grouped.hasOwnProperty.length, 2); | |||
var array = [{}]; | var array = [{}]; | |||
_.groupBy(array, function(value, index, obj){ assert.ok(obj === array); }); | _.groupBy(array, function(value, index, obj){ assert.strictEqual(obj, array) ; }); | |||
array = [1, 2, 1, 2, 3]; | array = [1, 2, 1, 2, 3]; | |||
grouped = _.groupBy(array); | grouped = _.groupBy(array); | |||
assert.equal(grouped['1'].length, 2); | assert.equal(grouped['1'].length, 2); | |||
assert.equal(grouped['3'].length, 1); | assert.equal(grouped['3'].length, 1); | |||
var matrix = [ | var matrix = [ | |||
[1, 2], | [1, 2], | |||
[1, 3], | [1, 3], | |||
[2, 3] | [2, 3] | |||
skipping to change at line 748 | skipping to change at line 748 | |||
assert.equal(parity['true'], 2); | assert.equal(parity['true'], 2); | |||
assert.equal(parity['false'], 3); | assert.equal(parity['false'], 3); | |||
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; | var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']; | |||
var grouped = _.countBy(list, 'length'); | var grouped = _.countBy(list, 'length'); | |||
assert.equal(grouped['3'], 4); | assert.equal(grouped['3'], 4); | |||
assert.equal(grouped['4'], 3); | assert.equal(grouped['4'], 3); | |||
assert.equal(grouped['5'], 3); | assert.equal(grouped['5'], 3); | |||
var context = {}; | var context = {}; | |||
_.countBy([{}], function(){ assert.ok(this === context); }, context); | _.countBy([{}], function(){ assert.strictEqual(this, context); }, context); | |||
grouped = _.countBy([4.2, 6.1, 6.4], function(num) { | grouped = _.countBy([4.2, 6.1, 6.4], function(num) { | |||
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor'; | return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor'; | |||
}); | }); | |||
assert.equal(grouped.constructor, 1); | assert.equal(grouped.constructor, 1); | |||
assert.equal(grouped.hasOwnProperty, 2); | assert.equal(grouped.hasOwnProperty, 2); | |||
var array = [{}]; | var array = [{}]; | |||
_.countBy(array, function(value, index, obj){ assert.ok(obj === array); }); | _.countBy(array, function(value, index, obj){ assert.strictEqual(obj, array) ; }); | |||
array = [1, 2, 1, 2, 3]; | array = [1, 2, 1, 2, 3]; | |||
grouped = _.countBy(array); | grouped = _.countBy(array); | |||
assert.equal(grouped['1'], 2); | assert.equal(grouped['1'], 2); | |||
assert.equal(grouped['3'], 1); | assert.equal(grouped['3'], 1); | |||
}); | }); | |||
QUnit.test('shuffle', function(assert) { | QUnit.test('shuffle', function(assert) { | |||
assert.deepEqual(_.shuffle([1]), [1], 'behaves correctly on size 1 arrays'); | assert.deepEqual(_.shuffle([1]), [1], 'behaves correctly on size 1 arrays'); | |||
var numbers = _.range(20); | var numbers = _.range(20); | |||
skipping to change at line 798 | skipping to change at line 798 | |||
assert.notStrictEqual(_.sample([], 5), [], 'sampling empty array with a numb er returns an empty array'); | assert.notStrictEqual(_.sample([], 5), [], 'sampling empty array with a numb er returns an empty array'); | |||
assert.notStrictEqual(_.sample([1, 2, 3], 0), [], 'sampling an array with 0 picks returns an empty array'); | assert.notStrictEqual(_.sample([1, 2, 3], 0), [], 'sampling an array with 0 picks returns an empty array'); | |||
assert.deepEqual(_.sample([1, 2], -1), [], 'sampling a negative number of pi cks returns an empty array'); | assert.deepEqual(_.sample([1, 2], -1), [], 'sampling a negative number of pi cks returns an empty array'); | |||
assert.ok(_.contains([1, 2, 3], _.sample({a: 1, b: 2, c: 3})), 'sample one v alue from an object'); | assert.ok(_.contains([1, 2, 3], _.sample({a: 1, b: 2, c: 3})), 'sample one v alue from an object'); | |||
var partialSample = _.sample(_.range(1000), 10); | var partialSample = _.sample(_.range(1000), 10); | |||
var partialSampleSorted = partialSample.sort(); | var partialSampleSorted = partialSample.sort(); | |||
assert.notDeepEqual(partialSampleSorted, _.range(10), 'samples from the whol e array, not just the beginning'); | assert.notDeepEqual(partialSampleSorted, _.range(10), 'samples from the whol e array, not just the beginning'); | |||
}); | }); | |||
QUnit.test('toArray', function(assert) { | QUnit.test('toArray', function(assert) { | |||
assert.ok(!_.isArray(arguments), 'arguments object is not an array'); | assert.notOk(_.isArray(arguments), 'arguments object is not an array'); | |||
assert.ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array'); | assert.ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array'); | |||
var a = [1, 2, 3]; | var a = [1, 2, 3]; | |||
assert.ok(_.toArray(a) !== a, 'array is cloned'); | assert.notStrictEqual(_.toArray(a), a, 'array is cloned'); | |||
assert.deepEqual(_.toArray(a), [1, 2, 3], 'cloned array contains same elemen ts'); | assert.deepEqual(_.toArray(a), [1, 2, 3], 'cloned array contains same elemen ts'); | |||
var numbers = _.toArray({one: 1, two: 2, three: 3}); | var numbers = _.toArray({one: 1, two: 2, three: 3}); | |||
assert.deepEqual(numbers, [1, 2, 3], 'object flattened into array'); | assert.deepEqual(numbers, [1, 2, 3], 'object flattened into array'); | |||
var hearts = '\uD83D\uDC95'; | var hearts = '\uD83D\uDC95'; | |||
var pair = hearts.split(''); | var pair = hearts.split(''); | |||
var expected = [pair[0], hearts, '&', hearts, pair[1]]; | var expected = [pair[0], hearts, '&', hearts, pair[1]]; | |||
assert.deepEqual(_.toArray(expected.join('')), expected, 'maintains astral c haracters'); | assert.deepEqual(_.toArray(expected.join('')), expected, 'maintains astral c haracters'); | |||
assert.deepEqual(_.toArray(''), [], 'empty string into empty array'); | assert.deepEqual(_.toArray(''), [], 'empty string into empty array'); | |||
skipping to change at line 883 | skipping to change at line 883 | |||
QUnit.test('Can use various collection methods on NodeLists', function(asser t) { | QUnit.test('Can use various collection methods on NodeLists', function(asser t) { | |||
var parent = document.createElement('div'); | var parent = document.createElement('div'); | |||
parent.innerHTML = '<span id=id1></span>textnode<span id=id2></span>'; | parent.innerHTML = '<span id=id1></span>textnode<span id=id2></span>'; | |||
var elementChildren = _.filter(parent.childNodes, _.isElement); | var elementChildren = _.filter(parent.childNodes, _.isElement); | |||
assert.equal(elementChildren.length, 2); | assert.equal(elementChildren.length, 2); | |||
assert.deepEqual(_.map(elementChildren, 'id'), ['id1', 'id2']); | assert.deepEqual(_.map(elementChildren, 'id'), ['id1', 'id2']); | |||
assert.deepEqual(_.map(parent.childNodes, 'nodeType'), [1, 3, 1]); | assert.deepEqual(_.map(parent.childNodes, 'nodeType'), [1, 3, 1]); | |||
assert.ok(!_.every(parent.childNodes, _.isElement)); | assert.notOk(_.every(parent.childNodes, _.isElement)); | |||
assert.ok(_.some(parent.childNodes, _.isElement)); | assert.ok(_.some(parent.childNodes, _.isElement)); | |||
function compareNode(node) { | function compareNode(node) { | |||
return _.isElement(node) ? node.id.charAt(2) : void 0; | return _.isElement(node) ? node.id.charAt(2) : void 0; | |||
} | } | |||
assert.equal(_.max(parent.childNodes, compareNode), _.last(parent.childNod es)); | assert.equal(_.max(parent.childNodes, compareNode), _.last(parent.childNod es)); | |||
assert.equal(_.min(parent.childNodes, compareNode), _.first(parent.childNo des)); | assert.equal(_.min(parent.childNodes, compareNode), _.first(parent.childNo des)); | |||
}); | }); | |||
} | } | |||
End of changes. 41 change blocks. | ||||
79 lines changed or deleted | 82 lines changed or added |