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

@@ -52,23 +52,23 @@ exports.aryMethod = {
'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindKey', 'add', 'after', 'ary', 'assign', 'assignIn', 'at', 'before', 'bind', 'bindKey',
'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN', 'chunk', 'cloneDeepWith', 'cloneWith', 'concat', 'countBy', 'curryN',
'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference', 'curryRightN', 'debounce', 'defaults', 'defaultsDeep', 'delay', 'difference',
'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith', 'eq', 'every', 'divide', 'drop', 'dropRight', 'dropRightWhile', 'dropWhile', 'endsWith',
'filter', 'find', 'find', 'findIndex', 'findKey', 'findLast', 'findLastIndex', 'eq', 'every', 'filter', 'find', 'find', 'findIndex', 'findKey', 'findLast',
'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth', 'forEach', 'forEachRight', 'findLastIndex', 'findLastKey', 'flatMap', 'flatMapDeep', 'flattenDepth',
'forIn', 'forInRight', 'forOwn', 'forOwnRight', 'get', 'groupBy', 'gt', 'gte', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight',
'has', 'hasIn', 'includes', 'indexOf', 'intersection', 'invertBy', 'invoke', 'get', 'groupBy', 'gt', 'gte', 'has', 'hasIn', 'includes', 'indexOf',
'invokeMap', 'isEqual', 'isMatch', 'join', 'keyBy', 'lastIndexOf', 'lt', 'lte', 'intersection', 'invertBy', 'invoke', 'invokeMap', 'isEqual', 'isMatch',
'map', 'mapKeys', 'mapValues', 'matchesProperty', 'maxBy', 'merge', 'minBy', 'join', 'keyBy', 'lastIndexOf', 'lt', 'lte', 'map', 'mapKeys', 'mapValues',
'omit', 'omitBy', 'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt', 'partial', 'matchesProperty', 'maxBy', 'merge', 'minBy', 'multiply', 'omit', 'omitBy',
'partialRight', 'partition', 'pick', 'pickBy', 'pull', 'pullAll', 'pullAt', 'overArgs', 'pad', 'padEnd', 'padStart', 'parseInt', 'partial', 'partialRight',
'random', 'range', 'rangeRight', 'rearg', 'reject', 'remove', 'repeat', 'partition', 'pick', 'pickBy', 'pull', 'pullAll', 'pullAt', 'random', 'range',
'restFrom', 'result', 'sampleSize', 'some', 'sortBy', 'sortedIndex', 'rangeRight', 'rearg', 'reject', 'remove', 'repeat', 'restFrom', 'result',
'sortedIndexOf', 'sortedLastIndex', 'sortedLastIndexOf', 'sortedUniqBy', 'sampleSize', 'some', 'sortBy', 'sortedIndex', 'sortedIndexOf', 'sortedLastIndex',
'split', 'spreadFrom', 'startsWith', 'subtract', 'sumBy', 'take', 'takeRight', 'sortedLastIndexOf', 'sortedUniqBy', 'split', 'spreadFrom', 'startsWith',
'takeRightWhile', 'takeWhile', 'tap', 'throttle', 'thru', 'times', 'trimChars', 'subtract', 'sumBy', 'take', 'takeRight', 'takeRightWhile', 'takeWhile', 'tap',
'trimCharsEnd', 'trimCharsStart', 'truncate', 'union', 'uniqBy', 'uniqWith', 'throttle', 'thru', 'times', 'trimChars', 'trimCharsEnd', 'trimCharsStart',
'unset', 'unzipWith', 'without', 'wrap', 'xor', 'zip', 'zipObject', 'truncate', 'union', 'uniqBy', 'uniqWith', 'unset', 'unzipWith', 'without',
'zipObjectDeep' 'wrap', 'xor', 'zip', 'zipObject', 'zipObjectDeep'
], ],
'3': [ '3': [
'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith', 'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith',
@@ -248,12 +248,14 @@ exports.skipRearg = {
'assignIn': true, 'assignIn': true,
'concat': true, 'concat': true,
'difference': true, 'difference': true,
'divide': true,
'gt': true, 'gt': true,
'gte': true, 'gte': true,
'lt': true, 'lt': true,
'lte': true, 'lte': true,
'matchesProperty': true, 'matchesProperty': true,
'merge': true, 'merge': true,
'multiply': true,
'partial': true, 'partial': true,
'partialRight': true, 'partialRight': true,
'random': true, 'random': true,

View File

@@ -1074,6 +1074,29 @@
return result; 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. * Used by `_.deburr` to convert latin-1 supplementary letters to basic latin letters.
* *
@@ -14615,19 +14638,9 @@
* _.add(6, 4); * _.add(6, 4);
* // => 10 * // => 10
*/ */
function add(augend, addend) { var add = createMathOperation(function(augend, addend) {
var result; return augend + addend;
if (augend === undefined && addend === undefined) { });
return 0;
}
if (augend !== undefined) {
result = augend;
}
if (addend !== undefined) {
result = result === undefined ? addend : (result + addend);
}
return result;
}
/** /**
* Computes `number` rounded up to `precision`. * Computes `number` rounded up to `precision`.
@@ -14652,6 +14665,25 @@
*/ */
var ceil = createRound('ceil'); 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`. * Computes `number` rounded down to `precision`.
* *
@@ -14799,6 +14831,25 @@
: undefined; : 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`. * Computes `number` rounded to `precision`.
* *
@@ -14837,19 +14888,9 @@
* _.subtract(6, 4); * _.subtract(6, 4);
* // => 2 * // => 2
*/ */
function subtract(minuend, subtrahend) { var subtract = createMathOperation(function(minuend, subtrahend) {
var result; return minuend - subtrahend;
if (minuend === undefined && subtrahend === undefined) { });
return 0;
}
if (minuend !== undefined) {
result = minuend;
}
if (subtrahend !== undefined) {
result = result === undefined ? subtrahend : (result - subtrahend);
}
return result;
}
/** /**
* Computes the sum of the values in `array`. * Computes the sum of the values in `array`.
@@ -15076,6 +15117,7 @@
lodash.cloneDeepWith = cloneDeepWith; lodash.cloneDeepWith = cloneDeepWith;
lodash.cloneWith = cloneWith; lodash.cloneWith = cloneWith;
lodash.deburr = deburr; lodash.deburr = deburr;
lodash.divide = divide;
lodash.endsWith = endsWith; lodash.endsWith = endsWith;
lodash.eq = eq; lodash.eq = eq;
lodash.escape = escape; lodash.escape = escape;
@@ -15155,6 +15197,7 @@
lodash.mean = mean; lodash.mean = mean;
lodash.min = min; lodash.min = min;
lodash.minBy = minBy; lodash.minBy = minBy;
lodash.multiply = multiply;
lodash.noConflict = noConflict; lodash.noConflict = noConflict;
lodash.noop = noop; lodash.noop = noop;
lodash.now = now; lodash.now = now;

View File

@@ -929,7 +929,7 @@
var func = fp[methodName], var func = fp[methodName],
isAdd = methodName == 'add'; isAdd = methodName == 'add';
QUnit.test('`fp.' + methodName + '` should have `rearg` applied', function(assert) { QUnit.test('`fp.' + methodName + '` should not have `rearg` applied', function(assert) {
assert.expect(1); assert.expect(1);
assert.strictEqual(func('1')('2'), isAdd ? '12' : -1); assert.strictEqual(func('1')('2'), isAdd ? '12' : -1);
@@ -1021,6 +1021,21 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('fp.divide and fp.multiply');
_.each(['divide', 'multiply'], function(methodName) {
var func = fp[methodName],
isDivide = methodName == 'divide';
QUnit.test('`fp.' + methodName + '` should not have `rearg` applied', function(assert) {
assert.expect(1);
assert.strictEqual(func('2')('4'), isDivide ? 0.5 : 8);
});
});
/*--------------------------------------------------------------------------*/
QUnit.module('fp.extend'); QUnit.module('fp.extend');
(function() { (function() {

View File

@@ -24677,11 +24677,13 @@
(function() { (function() {
var funcs = [ var funcs = [
'add',
'camelCase', 'camelCase',
'capitalize', 'capitalize',
'ceil', 'ceil',
'clone', 'clone',
'deburr', 'deburr',
'divide',
'endsWith', 'endsWith',
'escape', 'escape',
'escapeRegExp', 'escapeRegExp',
@@ -24731,6 +24733,7 @@
'maxBy', 'maxBy',
'min', 'min',
'minBy', 'minBy',
'multiply',
'pad', 'pad',
'padEnd', 'padEnd',
'padStart', 'padStart',
@@ -24749,6 +24752,7 @@
'some', 'some',
'startCase', 'startCase',
'startsWith', 'startsWith',
'subtract',
'sum', 'sum',
'toInteger', 'toInteger',
'toLower', 'toLower',
@@ -24770,9 +24774,7 @@
assert.expect(1); assert.expect(1);
if (!isNpm) { if (!isNpm) {
var array = [1, 2, 3], var actual = _()[methodName]();
actual = _(array)[methodName]();
assert.notOk(actual instanceof _); assert.notOk(actual instanceof _);
} }
else { else {
@@ -24784,9 +24786,7 @@
assert.expect(1); assert.expect(1);
if (!isNpm) { if (!isNpm) {
var array = [1, 2, 3], var actual = _().chain()[methodName]();
actual = _(array).chain()[methodName]();
assert.ok(actual instanceof _); assert.ok(actual instanceof _);
} }
else { else {
@@ -25012,7 +25012,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(305); assert.expect(307);
var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray); var emptyArrays = lodashStable.map(falsey, alwaysEmptyArray);