From f6d81e1bd8404771a495cee14c829975f904a443 Mon Sep 17 00:00:00 2001 From: DARCHY Jeremy Date: Fri, 6 Nov 2015 16:53:54 +0100 Subject: [PATCH] Add `_.subtract`. --- lodash.js | 32 +++++++++++++++++++++++++++++--- test/test.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/lodash.js b/lodash.js index 5493d17c7..b339dcaa9 100644 --- a/lodash.js +++ b/lodash.js @@ -13100,9 +13100,9 @@ * @static * @memberOf _ * @category Math - * @param {number} augend The first number to add. - * @param {number} addend The second number to add. - * @returns {number} Returns the sum. + * @param {number} augend The first number in an addition. + * @param {number} addend The second number in an addition. + * @returns {number} Returns the total. * @example * * _.add(6, 4); @@ -13293,6 +13293,31 @@ */ var round = createRound('round'); + /** + * Subtract two numbers. + * + * @static + * @memberOf _ + * @category Math + * @param {number} minuend The first number in a subtraction. + * @param {number} subtrahend The second number in a subtraction. + * @returns {number} Returns the difference. + * @example + * + * _.subtract(6, 4); + * // => 2 + */ + function subtract(minuend, subtrahend) { + var result; + if (minuend === minuend && minuend != null) { + result = minuend; + } + if (subtrahend === subtrahend && subtrahend != null) { + result = result === undefined ? subtrahend : (result - subtrahend); + } + return result; + } + /** * Gets the sum of the values in `array`. * @@ -13631,6 +13656,7 @@ lodash.sortedLastIndexOf = sortedLastIndexOf; lodash.startCase = startCase; lodash.startsWith = startsWith; + lodash.subtract = subtract; lodash.sum = sum; lodash.sumBy = sumBy; lodash.template = template; diff --git a/test/test.js b/test/test.js index 8fb3ca186..ad4c97178 100644 --- a/test/test.js +++ b/test/test.js @@ -901,10 +901,12 @@ QUnit.module('lodash.add'); (function() { - QUnit.test('should add two numbers together', function(assert) { - assert.expect(1); + QUnit.test('should add two numbers', function(assert) { + assert.expect(3); assert.strictEqual(_.add(6, 4), 10); + assert.strictEqual(_.add(-6, 4), -2); + assert.strictEqual(_.add(-6, -4), -10); }); QUnit.test('should not coerce arguments to numbers', function(assert) { @@ -17412,6 +17414,49 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.subtract'); + + (function() { + QUnit.test('should subtract two numbers', function(assert) { + assert.expect(3); + + assert.strictEqual(_.subtract(6, 4), 2); + assert.strictEqual(_.subtract(-6, 4), -10); + assert.strictEqual(_.subtract(-6, -4), -2); + }); + + QUnit.test('should coerce arguments only numbers', function(assert) { + assert.expect(2); + + assert.strictEqual(_.subtract('6', '4'), 2); + assert.deepEqual(_.subtract('x', 'y'), NaN); + }); + + QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) { + assert.expect(1); + + if (!isNpm) { + assert.strictEqual(_(1).subtract(2), -1); + } + else { + skipTest(assert); + } + }); + + QUnit.test('should return a wrapped value when explicitly chaining', function(assert) { + assert.expect(1); + + if (!isNpm) { + assert.ok(_(1).chain().subtract(2) instanceof _); + } + else { + skipTest(assert); + } + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.sum'); (function() { @@ -22375,7 +22420,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(275); + assert.expect(276); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));