mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +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`.
|
// Ensure wrappers are instances of `baseLodash`.
|
||||||
lodash.prototype = baseLodash.prototype;
|
lodash.prototype = baseLodash.prototype;
|
||||||
|
|
||||||
@@ -11301,6 +11339,7 @@
|
|||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
// Add functions that return unwrapped values when chaining.
|
// Add functions that return unwrapped values when chaining.
|
||||||
|
lodash.add = add;
|
||||||
lodash.attempt = attempt;
|
lodash.attempt = attempt;
|
||||||
lodash.camelCase = camelCase;
|
lodash.camelCase = camelCase;
|
||||||
lodash.capitalize = capitalize;
|
lodash.capitalize = capitalize;
|
||||||
@@ -11370,6 +11409,7 @@
|
|||||||
lodash.sortedLastIndex = sortedLastIndex;
|
lodash.sortedLastIndex = sortedLastIndex;
|
||||||
lodash.startCase = startCase;
|
lodash.startCase = startCase;
|
||||||
lodash.startsWith = startsWith;
|
lodash.startsWith = startsWith;
|
||||||
|
lodash.sum = sum;
|
||||||
lodash.template = template;
|
lodash.template = template;
|
||||||
lodash.trim = trim;
|
lodash.trim = trim;
|
||||||
lodash.trimLeft = trimLeft;
|
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');
|
QUnit.module('lodash(...).commit');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -15579,6 +15601,7 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
var funcs = [
|
var funcs = [
|
||||||
|
'add',
|
||||||
'clone',
|
'clone',
|
||||||
'contains',
|
'contains',
|
||||||
'every',
|
'every',
|
||||||
@@ -15610,6 +15633,7 @@
|
|||||||
'parseInt',
|
'parseInt',
|
||||||
'pop',
|
'pop',
|
||||||
'shift',
|
'shift',
|
||||||
|
'sum',
|
||||||
'random',
|
'random',
|
||||||
'reduce',
|
'reduce',
|
||||||
'reduceRight',
|
'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');
|
QUnit.module('lodash methods');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -15832,7 +15883,7 @@
|
|||||||
|
|
||||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
test('should accept falsey arguments', 209, function() {
|
test('should accept falsey arguments', 211, function() {
|
||||||
var emptyArrays = _.map(falsey, _.constant([])),
|
var emptyArrays = _.map(falsey, _.constant([])),
|
||||||
isExposed = '_' in root,
|
isExposed = '_' in root,
|
||||||
oldDash = root._;
|
oldDash = root._;
|
||||||
|
|||||||
Reference in New Issue
Block a user