From df838c98e9e7d76d70691ebfdeeb7f6ff2268bd5 Mon Sep 17 00:00:00 2001 From: James Kyle Date: Sun, 7 Jun 2015 18:41:36 -0700 Subject: [PATCH] Add _.ceil/_.floor/_.round. --- lodash.src.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 30 +++++++++++++++++- 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/lodash.src.js b/lodash.src.js index befdfbfa2..80c556e07 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -3706,6 +3706,25 @@ }; } + /** + * Creates a `_.round`, `_.ceil`, or `_.floor` function. + * + * @private + * @param {String} type Specify the Math method to use when rounding. + */ + + function createRound(type) { + var roundFn = Math[type]; + return function(number, precision) { + if (precision) { + precision = Math.pow(10, precision); + return roundFn(number * precision) / precision; + } else { + return roundFn(number); + } + } + } + /** * Creates a function that either curries or invokes `func` with optional * `this` binding and partially applied arguments. @@ -11871,6 +11890,72 @@ : baseSum(collection, iteratee); } + /** + * Calculates the result of a number rounded to an optional precision. + * + * @static + * @memberOf _ + * @category Math + * @param {number} number The number to round. + * @param {number} [precision] The precision to round to. + * @returns {number} Returns the rounded number. + * @example + * + * _.round(4.006); + * // => 4 + * + * _.round(4.006, 2); + * // => 4.01 + * + * _.round(4060, -2); + * // => 4100 + */ + var round = createRound('round'); + + /** + * Calculates the result of a number rounded down to an optional precision. + * + * @static + * @memberOf _ + * @category Math + * @param {number} number The number to round down. + * @param {number} [precision] The precision to round down to. + * @returns {number} Returns the rounded down number. + * @example + * + * _.floor(4.006); + * // => 4 + * + * _.floor(0.046, 2); + * // => 0.04 + * + * _.floor(4060, -2); + * // => 4000 + */ + var floor = createRound('floor'); + + /** + * Calculates the result of a number rounded up to an optional precision. + * + * @static + * @memberOf _ + * @category Math + * @param {number} number The number to round up. + * @param {number} [precision] The precision to round up to. + * @returns {number} Returns the rounded up number. + * @example + * + * _.ceil(4.006); + * // => 5 + * + * _.ceil(6.004, 2); + * // => 6.01 + * + * _.ceil(6040, -2); + * // => 6100 + */ + var ceil = createRound('ceil'); + /*------------------------------------------------------------------------*/ // Ensure wrappers are instances of `baseLodash`. @@ -12028,6 +12113,7 @@ lodash.attempt = attempt; lodash.camelCase = camelCase; lodash.capitalize = capitalize; + lodash.ceil = ceil; lodash.clone = clone; lodash.cloneDeep = cloneDeep; lodash.deburr = deburr; @@ -12043,6 +12129,7 @@ lodash.findLastKey = findLastKey; lodash.findWhere = findWhere; lodash.first = first; + lodash.floor = floor; lodash.get = get; lodash.gt = gt; lodash.gte = gte; @@ -12091,6 +12178,7 @@ lodash.reduceRight = reduceRight; lodash.repeat = repeat; lodash.result = result; + lodash.round = round; lodash.runInContext = runInContext; lodash.size = size; lodash.snakeCase = snakeCase; diff --git a/test/test.js b/test/test.js index e42eaa196..869a0ce9d 100644 --- a/test/test.js +++ b/test/test.js @@ -13686,6 +13686,34 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.round, lodash.floor, and lodash.ceil'); + + _.each(['round', 'floor', 'ceil'], function(methodName) { + var func = _[methodName]; + + test('`_.' + methodName + '` should return a rounded number without a precision', 1, function() { + var actual = func(4.006); + equal(actual, methodName === 'ceil' ? 5 : 4); + }); + + test('`_.' + methodName + '` should return a rounded number with a precision of 0', 1, function() { + var actual = func(4.006, 0); + equal(actual, methodName === 'ceil' ? 5 : 4); + }); + + test('`_.' + methodName + '` should return a rounded number with a positive precision', 1, function() { + var actual = func(4.016, 2); + equal(actual, methodName === 'floor' ? 4.01 : 4.02); + }); + + test('`_.' + methodName + '` should return a rounded number with a negative precision', 1, function() { + var actual = func(4160, -2); + equal(actual, methodName === 'floor' ? 4100 : 4200); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.runInContext'); (function() { @@ -17863,7 +17891,7 @@ var acceptFalsey = _.difference(allMethods, rejectFalsey); - test('should accept falsey arguments', 225, function() { + test('should accept falsey arguments', 228, function() { var emptyArrays = _.map(falsey, _.constant([])), isExposed = '_' in root, oldDash = root._;