mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Add _.subtract.
This commit is contained in:
committed by
John-David Dalton
parent
98e607e610
commit
f6d81e1bd8
32
lodash.js
32
lodash.js
@@ -13100,9 +13100,9 @@
|
|||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Math
|
* @category Math
|
||||||
* @param {number} augend The first number to add.
|
* @param {number} augend The first number in an addition.
|
||||||
* @param {number} addend The second number to add.
|
* @param {number} addend The second number in an addition.
|
||||||
* @returns {number} Returns the sum.
|
* @returns {number} Returns the total.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* _.add(6, 4);
|
* _.add(6, 4);
|
||||||
@@ -13293,6 +13293,31 @@
|
|||||||
*/
|
*/
|
||||||
var round = createRound('round');
|
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`.
|
* Gets the sum of the values in `array`.
|
||||||
*
|
*
|
||||||
@@ -13631,6 +13656,7 @@
|
|||||||
lodash.sortedLastIndexOf = sortedLastIndexOf;
|
lodash.sortedLastIndexOf = sortedLastIndexOf;
|
||||||
lodash.startCase = startCase;
|
lodash.startCase = startCase;
|
||||||
lodash.startsWith = startsWith;
|
lodash.startsWith = startsWith;
|
||||||
|
lodash.subtract = subtract;
|
||||||
lodash.sum = sum;
|
lodash.sum = sum;
|
||||||
lodash.sumBy = sumBy;
|
lodash.sumBy = sumBy;
|
||||||
lodash.template = template;
|
lodash.template = template;
|
||||||
|
|||||||
51
test/test.js
51
test/test.js
@@ -901,10 +901,12 @@
|
|||||||
QUnit.module('lodash.add');
|
QUnit.module('lodash.add');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
QUnit.test('should add two numbers together', function(assert) {
|
QUnit.test('should add two numbers', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(3);
|
||||||
|
|
||||||
assert.strictEqual(_.add(6, 4), 10);
|
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) {
|
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');
|
QUnit.module('lodash.sum');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -22375,7 +22420,7 @@
|
|||||||
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
QUnit.test('should accept falsey arguments', function(assert) {
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
assert.expect(275);
|
assert.expect(276);
|
||||||
|
|
||||||
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user