mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Add _.add/_.sum methods
This commit is contained in:
@@ -11159,6 +11159,44 @@
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Adds two numbers.
|
||||
*
|
||||
* @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.
|
||||
* @example
|
||||
*
|
||||
* _.add(6, 4);
|
||||
* // => 10
|
||||
*/
|
||||
function add(augend, addend) {
|
||||
return augend + addend;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the sum of an array of numbers.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Math
|
||||
* @param {number} array The numbers to add.
|
||||
* @returns {number} Returns the sum.
|
||||
* @example
|
||||
*
|
||||
* _.sum([4, 6, 2]);
|
||||
* // => 12
|
||||
*/
|
||||
function sum(array) {
|
||||
if (!isArray(array) || array.length === 0) return NaN;
|
||||
return arrayReduce(array, add, 0);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
// Ensure wrappers are instances of `baseLodash`.
|
||||
lodash.prototype = baseLodash.prototype;
|
||||
|
||||
@@ -11301,6 +11339,7 @@
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
// Add functions that return unwrapped values when chaining.
|
||||
lodash.add = add;
|
||||
lodash.attempt = attempt;
|
||||
lodash.camelCase = camelCase;
|
||||
lodash.capitalize = capitalize;
|
||||
@@ -11370,6 +11409,7 @@
|
||||
lodash.sortedLastIndex = sortedLastIndex;
|
||||
lodash.startCase = startCase;
|
||||
lodash.startsWith = startsWith;
|
||||
lodash.sum = sum;
|
||||
lodash.template = template;
|
||||
lodash.trim = trim;
|
||||
lodash.trimLeft = trimLeft;
|
||||
|
||||
53
test/test.js
53
test/test.js
@@ -15098,6 +15098,28 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.add');
|
||||
|
||||
(function() {
|
||||
test('should add two numbers together', 1, function() {
|
||||
var actual = _.add(6, 4);
|
||||
equal(actual, 10);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash.sum');
|
||||
|
||||
(function() {
|
||||
test('should return the sum of an array of numbers', 1, function() {
|
||||
var actual = _.sum([6, 4, 2]);
|
||||
equal(actual, 12);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash(...).commit');
|
||||
|
||||
(function() {
|
||||
@@ -15579,6 +15601,7 @@
|
||||
|
||||
(function() {
|
||||
var funcs = [
|
||||
'add',
|
||||
'clone',
|
||||
'contains',
|
||||
'every',
|
||||
@@ -15610,6 +15633,7 @@
|
||||
'parseInt',
|
||||
'pop',
|
||||
'shift',
|
||||
'sum',
|
||||
'random',
|
||||
'reduce',
|
||||
'reduceRight',
|
||||
@@ -15756,6 +15780,33 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('"Math" category methods');
|
||||
|
||||
(function() {
|
||||
var mathArrayMethods = [
|
||||
'sum'
|
||||
];
|
||||
|
||||
_.each(mathArrayMethods, function(methodName) {
|
||||
var func = _[methodName];
|
||||
|
||||
test('`_.' + methodName + '` should return NaN when passing a value other than an array', 5, function() {
|
||||
var values = [undefined, null, 0, 'foo', {}];
|
||||
_.each(values, function(value) {
|
||||
var actual = _[methodName](value);
|
||||
deepEqual(actual, NaN);
|
||||
});
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should return NaN when passing an empty array', 1, function() {
|
||||
var actual = _[methodName]([]);
|
||||
deepEqual(actual, NaN);
|
||||
});
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
QUnit.module('lodash methods');
|
||||
|
||||
(function() {
|
||||
@@ -15832,7 +15883,7 @@
|
||||
|
||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||
|
||||
test('should accept falsey arguments', 209, function() {
|
||||
test('should accept falsey arguments', 211, function() {
|
||||
var emptyArrays = _.map(falsey, _.constant([])),
|
||||
isExposed = '_' in root,
|
||||
oldDash = root._;
|
||||
|
||||
Reference in New Issue
Block a user