From e516d99b2d53a4d416e3993e31411af41ea24b92 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 4 Sep 2015 08:35:36 -0700 Subject: [PATCH] Expose `_.toInteger`. --- lodash.js | 37 ++++++++++++++++++++++++++----------- test/test.js | 5 +++-- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lodash.js b/lodash.js index a7364a758..57f34df1e 100644 --- a/lodash.js +++ b/lodash.js @@ -4384,17 +4384,6 @@ return typeof value == 'function' ? value : identity; } - /** - * Converts `value` to an integer. - * - * @private - * @param {*} value The value to convert. - * @returns {number} Returns the integer. - */ - function toInteger(value) { - return nativeFloor(value) || 0; - } - /** * Creates a clone of `wrapper`. * @@ -8863,6 +8852,31 @@ return func(value); } + /** + * Converts `value` to an integer. + * + * **Note:** This function is based on [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger). + * + * @static + * @memberOf _ + * @category Lang + * @param {*} value The value to convert. + * @returns {number} Returns the converted integer. + * @example + * + * _.toInteger('3.14'); + * // => 3 + * + * _.toInteger(NaN); + * // => 0 + * + * _.toInteger(Infinity); + * // => Infinity + */ + function toInteger(value) { + return nativeFloor(value) || 0; + } + /** * Converts `value` to a plain object flattening inherited enumerable * properties of `value` to own properties of the plain object. @@ -11949,6 +11963,7 @@ lodash.sum = sum; lodash.sumBy = sumBy; lodash.template = template; + lodash.toInteger = toInteger; lodash.trim = trim; lodash.trimLeft = trimLeft; lodash.trimRight = trimRight; diff --git a/test/test.js b/test/test.js index c7ec4ba73..1e1fb4428 100644 --- a/test/test.js +++ b/test/test.js @@ -17387,7 +17387,8 @@ 'random', 'reduce', 'reduceRight', - 'some' + 'some', + 'toInteger' ]; _.each(funcs, function(methodName) { @@ -17596,7 +17597,7 @@ var acceptFalsey = _.difference(allMethods, rejectFalsey); - test('should accept falsey arguments', 232, function() { + test('should accept falsey arguments', 233, function() { var emptyArrays = _.map(falsey, _.constant([])); _.each(acceptFalsey, function(methodName) {