diff --git a/lodash.js b/lodash.js index 55b28e708..c30b35516 100644 --- a/lodash.js +++ b/lodash.js @@ -4783,13 +4783,14 @@ * * @private * @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. */ - function createMathOperation(operator) { + function createMathOperation(operator, defaultValue) { return function(value, other) { var result; if (value === undefined && other === undefined) { - return 0; + return defaultValue; } if (value !== undefined) { result = value; @@ -15533,7 +15534,7 @@ */ var add = createMathOperation(function(augend, addend) { return augend + addend; - }); + }, 0); /** * Computes `number` rounded up to `precision`. @@ -15575,7 +15576,7 @@ */ var divide = createMathOperation(function(dividend, divisor) { return dividend / divisor; - }); + }, 1); /** * Computes `number` rounded down to `precision`. @@ -15768,7 +15769,7 @@ */ var multiply = createMathOperation(function(multiplier, multiplicand) { return multiplier * multiplicand; - }); + }, 1); /** * Computes `number` rounded to `precision`. @@ -15810,7 +15811,7 @@ */ var subtract = createMathOperation(function(minuend, subtrahend) { return minuend - subtrahend; - }); + }, 0); /** * Computes the sum of the values in `array`. diff --git a/test/test.js b/test/test.js index 8a138fb4b..cae1f4619 100644 --- a/test/test.js +++ b/test/test.js @@ -21217,12 +21217,13 @@ QUnit.module('math operator methods'); 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.strictEqual(func(), 0); + assert.strictEqual(func(), isAddSub ? 0 : 1); }); QUnit.test('`_.' + methodName + '` should work with only one defined argument', function(assert) {