Add _.divide and _.multiply.

This commit is contained in:
John-David Dalton
2016-03-19 21:00:29 -07:00
parent b2bff1ad45
commit 6eb0cb1565
4 changed files with 111 additions and 51 deletions

View File

@@ -1074,6 +1074,29 @@
return result;
}
/**
* Creates a function that performs a mathematical operation on two values.
*
* @private
* @param {Function} operator The function to perform the operation.
* @returns {Function} Returns the new mathematical operation function.
*/
function createMathOperation(operator) {
return function(value, other) {
var result;
if (value === undefined && other === undefined) {
return 0;
}
if (value !== undefined) {
result = value;
}
if (other !== undefined) {
result = result === undefined ? other : operator(result, other);
}
return result;
};
}
/**
* Used by `_.deburr` to convert latin-1 supplementary letters to basic latin letters.
*
@@ -14615,19 +14638,9 @@
* _.add(6, 4);
* // => 10
*/
function add(augend, addend) {
var result;
if (augend === undefined && addend === undefined) {
return 0;
}
if (augend !== undefined) {
result = augend;
}
if (addend !== undefined) {
result = result === undefined ? addend : (result + addend);
}
return result;
}
var add = createMathOperation(function(augend, addend) {
return augend + addend;
});
/**
* Computes `number` rounded up to `precision`.
@@ -14652,6 +14665,25 @@
*/
var ceil = createRound('ceil');
/**
* Divide two numbers.
*
* @static
* @memberOf _
* @since 4.7.0
* @category Math
* @param {number} dividend The first number in a division.
* @param {number} divisor The second number in a division.
* @returns {number} Returns the quotient.
* @example
*
* _.divide(6, 4);
* // => 1.5
*/
var divide = createMathOperation(function(dividend, divisor) {
return dividend / divisor;
});
/**
* Computes `number` rounded down to `precision`.
*
@@ -14799,6 +14831,25 @@
: undefined;
}
/**
* Multiply two numbers.
*
* @static
* @memberOf _
* @since 4.7.0
* @category Math
* @param {number} multiplier The first number in a multiplication.
* @param {number} multiplicand The second number in a multiplication.
* @returns {number} Returns the product.
* @example
*
* _.multiply(6, 4);
* // => 24
*/
var multiply = createMathOperation(function(multiplier, multiplicand) {
return multiplier * multiplicand;
});
/**
* Computes `number` rounded to `precision`.
*
@@ -14837,19 +14888,9 @@
* _.subtract(6, 4);
* // => 2
*/
function subtract(minuend, subtrahend) {
var result;
if (minuend === undefined && subtrahend === undefined) {
return 0;
}
if (minuend !== undefined) {
result = minuend;
}
if (subtrahend !== undefined) {
result = result === undefined ? subtrahend : (result - subtrahend);
}
return result;
}
var subtract = createMathOperation(function(minuend, subtrahend) {
return minuend - subtrahend;
});
/**
* Computes the sum of the values in `array`.
@@ -15076,6 +15117,7 @@
lodash.cloneDeepWith = cloneDeepWith;
lodash.cloneWith = cloneWith;
lodash.deburr = deburr;
lodash.divide = divide;
lodash.endsWith = endsWith;
lodash.eq = eq;
lodash.escape = escape;
@@ -15155,6 +15197,7 @@
lodash.mean = mean;
lodash.min = min;
lodash.minBy = minBy;
lodash.multiply = multiply;
lodash.noConflict = noConflict;
lodash.noop = noop;
lodash.now = now;