mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Ensure _.divide and _.multiply return 1 when no arguments are specified. [closes #2405]
This commit is contained in:
13
lodash.js
13
lodash.js
@@ -4783,13 +4783,14 @@
|
|||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Function} operator The function to perform the operation.
|
* @param {Function} operator The function to perform the operation.
|
||||||
|
* @param {number} [defaultValue] The value used for `undefined` arguments.
|
||||||
* @returns {Function} Returns the new mathematical operation function.
|
* @returns {Function} Returns the new mathematical operation function.
|
||||||
*/
|
*/
|
||||||
function createMathOperation(operator) {
|
function createMathOperation(operator, defaultValue) {
|
||||||
return function(value, other) {
|
return function(value, other) {
|
||||||
var result;
|
var result;
|
||||||
if (value === undefined && other === undefined) {
|
if (value === undefined && other === undefined) {
|
||||||
return 0;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
result = value;
|
result = value;
|
||||||
@@ -15533,7 +15534,7 @@
|
|||||||
*/
|
*/
|
||||||
var add = createMathOperation(function(augend, addend) {
|
var add = createMathOperation(function(augend, addend) {
|
||||||
return augend + addend;
|
return augend + addend;
|
||||||
});
|
}, 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes `number` rounded up to `precision`.
|
* Computes `number` rounded up to `precision`.
|
||||||
@@ -15575,7 +15576,7 @@
|
|||||||
*/
|
*/
|
||||||
var divide = createMathOperation(function(dividend, divisor) {
|
var divide = createMathOperation(function(dividend, divisor) {
|
||||||
return dividend / divisor;
|
return dividend / divisor;
|
||||||
});
|
}, 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes `number` rounded down to `precision`.
|
* Computes `number` rounded down to `precision`.
|
||||||
@@ -15768,7 +15769,7 @@
|
|||||||
*/
|
*/
|
||||||
var multiply = createMathOperation(function(multiplier, multiplicand) {
|
var multiply = createMathOperation(function(multiplier, multiplicand) {
|
||||||
return multiplier * multiplicand;
|
return multiplier * multiplicand;
|
||||||
});
|
}, 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes `number` rounded to `precision`.
|
* Computes `number` rounded to `precision`.
|
||||||
@@ -15810,7 +15811,7 @@
|
|||||||
*/
|
*/
|
||||||
var subtract = createMathOperation(function(minuend, subtrahend) {
|
var subtract = createMathOperation(function(minuend, subtrahend) {
|
||||||
return minuend - subtrahend;
|
return minuend - subtrahend;
|
||||||
});
|
}, 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the sum of the values in `array`.
|
* Computes the sum of the values in `array`.
|
||||||
|
|||||||
@@ -21217,12 +21217,13 @@
|
|||||||
QUnit.module('math operator methods');
|
QUnit.module('math operator methods');
|
||||||
|
|
||||||
lodashStable.each(['add', 'divide', 'multiply', 'subtract'], function(methodName) {
|
lodashStable.each(['add', 'divide', 'multiply', 'subtract'], function(methodName) {
|
||||||
var func = _[methodName];
|
var func = _[methodName],
|
||||||
|
isAddSub = methodName == 'add' || methodName == 'subtract';
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should return `0` when no arguments are given', function(assert) {
|
QUnit.test('`_.' + methodName + '` should return `' + (isAddSub ? 0 : 1) + '` when no arguments are given', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
assert.strictEqual(func(), 0);
|
assert.strictEqual(func(), isAddSub ? 0 : 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('`_.' + methodName + '` should work with only one defined argument', function(assert) {
|
QUnit.test('`_.' + methodName + '` should work with only one defined argument', function(assert) {
|
||||||
|
|||||||
Reference in New Issue
Block a user