mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 09:47:48 +00:00
Ensure _.sum and _.sumBy return 0 for empty arrays. [closes #1883]
This commit is contained in:
@@ -841,7 +841,7 @@
|
|||||||
result = result === undefined ? current : (result + current);
|
result = result === undefined ? current : (result + current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return length ? result : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13917,7 +13917,7 @@
|
|||||||
function sum(array) {
|
function sum(array) {
|
||||||
return (array && array.length)
|
return (array && array.length)
|
||||||
? baseSum(array, identity)
|
? baseSum(array, identity)
|
||||||
: undefined;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13945,7 +13945,7 @@
|
|||||||
function sumBy(array, iteratee) {
|
function sumBy(array, iteratee) {
|
||||||
return (array && array.length)
|
return (array && array.length)
|
||||||
? baseSum(array, getIteratee(iteratee))
|
? baseSum(array, getIteratee(iteratee))
|
||||||
: undefined;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|||||||
13
test/test.js
13
test/test.js
@@ -12878,6 +12878,15 @@
|
|||||||
var array = [4, 2, 8, 6];
|
var array = [4, 2, 8, 6];
|
||||||
assert.strictEqual(_.mean(array), 5);
|
assert.strictEqual(_.mean(array), 5);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should return `NaN` when passing empty `array` values', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(empties, alwaysNaN),
|
||||||
|
actual = lodashStable.map(empties, _.mean);
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -18713,10 +18722,10 @@
|
|||||||
assert.strictEqual(_.sum(array), 12);
|
assert.strictEqual(_.sum(array), 12);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should return `undefined` when passing empty `array` values', function(assert) {
|
QUnit.test('should return `0` when passing empty `array` values', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var expected = lodashStable.map(empties, alwaysUndefined),
|
var expected = lodashStable.map(empties, alwaysZero),
|
||||||
actual = lodashStable.map(empties, _.sum);
|
actual = lodashStable.map(empties, _.sum);
|
||||||
|
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
|
|||||||
Reference in New Issue
Block a user