mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 19:37:49 +00:00
Add _.isInteger and _.isSafeInteger.
This commit is contained in:
102
lodash.js
102
lodash.js
@@ -48,21 +48,16 @@
|
|||||||
/** Used as the `TypeError` message for "Functions" methods. */
|
/** Used as the `TypeError` message for "Functions" methods. */
|
||||||
var FUNC_ERROR_TEXT = 'Expected a function';
|
var FUNC_ERROR_TEXT = 'Expected a function';
|
||||||
|
|
||||||
|
/** Used as references for various `Number` constants. */
|
||||||
|
var INFINITY = 1 / 0,
|
||||||
|
MAX_SAFE_INTEGER = 9007199254740991,
|
||||||
|
MAX_VALUE = 1.7976931348623157e308;
|
||||||
|
|
||||||
/** Used as references for the maximum length and index of an array. */
|
/** Used as references for the maximum length and index of an array. */
|
||||||
var MAX_ARRAY_LENGTH = 4294967295,
|
var MAX_ARRAY_LENGTH = 4294967295,
|
||||||
MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1,
|
MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1,
|
||||||
HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
|
HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
|
||||||
|
|
||||||
/**
|
|
||||||
* Used as the [maximum length](http://ecma-international.org/ecma-262/6.0/#sec-number.max_safe_integer)
|
|
||||||
* of an array-like value.
|
|
||||||
*/
|
|
||||||
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
||||||
|
|
||||||
/** Used as references for `-Infinity` and `Infinity`. */
|
|
||||||
var NEGATIVE_INFINITY = 1 / -0,
|
|
||||||
POSITIVE_INFINITY = 1 / 0;
|
|
||||||
|
|
||||||
/** Used as the internal argument placeholder. */
|
/** Used as the internal argument placeholder. */
|
||||||
var PLACEHOLDER = '__lodash_placeholder__';
|
var PLACEHOLDER = '__lodash_placeholder__';
|
||||||
|
|
||||||
@@ -1424,9 +1419,9 @@
|
|||||||
* `remove`, `rest`, `restParam`, `reverse`, `set`, `setWith`, `shuffle`,
|
* `remove`, `rest`, `restParam`, `reverse`, `set`, `setWith`, `shuffle`,
|
||||||
* `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, `take`,
|
* `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, `take`,
|
||||||
* `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`,
|
* `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`,
|
||||||
* `times`, `toArray`, `toPath`, `toPlainObject`, `transform`, `union`, `uniq`,
|
* `times`, `toArray`, `toPath`, `toPlainObject`, `transform`, `union`,
|
||||||
* `uniqBy`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`,
|
* `uniq`, `uniqBy`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`,
|
||||||
* `without`, `wrap`, `xor`, `zip`, `zipObject`, and `zipWith`
|
* `valuesIn`, `without`, `wrap`, `xor`, `zip`, `zipObject`, and `zipWith`
|
||||||
*
|
*
|
||||||
* The wrapper methods that are **not** chainable by default are:
|
* The wrapper methods that are **not** chainable by default are:
|
||||||
* `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clone`, `cloneDeep`,
|
* `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clone`, `cloneDeep`,
|
||||||
@@ -1435,16 +1430,17 @@
|
|||||||
* `findLastIndex`, `findLastKey`, `first`, `floor`, `get`, `gt`, `gte`,
|
* `findLastIndex`, `findLastKey`, `first`, `floor`, `get`, `gt`, `gte`,
|
||||||
* `has`, `hasIn`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
|
* `has`, `hasIn`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
|
||||||
* `isArray`, `isArrayLike`, `isBoolean`, `isDate`, `isElement`, `isEmpty`,
|
* `isArray`, `isArrayLike`, `isBoolean`, `isDate`, `isElement`, `isEmpty`,
|
||||||
* `isEqual`, `isEqualWith`, `isError`, `isFinite` `isFunction`, `isMatch`,
|
* `isEqual`, `isEqualWith`, `isError`, `isFinite` `isFunction`, `isInteger`,
|
||||||
* `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`,
|
* `isMatch`, `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`,
|
||||||
* `isObjectLike`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`,
|
* `isObject`, `isObjectLike`, `isPlainObject`, `isRegExp`, `isSafeInteger`,
|
||||||
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lt`, `lte`,
|
* `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`, `last`,
|
||||||
* `max`, `min`, `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`,
|
* `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`, `now`,
|
||||||
* `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`,
|
* `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
|
||||||
* `round`, `runInContext`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
|
* `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,
|
||||||
* `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`,
|
* `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`,
|
||||||
* `startsWith`, `sum`, `sumBy`, `template`, `toInteger', 'trim`, `trimLeft`,
|
* `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`, `sumBy`, `template`,
|
||||||
* `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words`
|
* `toInteger`, `trim`, `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`,
|
||||||
|
* `value`, and `words`
|
||||||
*
|
*
|
||||||
* The wrapper method `sample` will return a wrapped value when `n` is provided,
|
* The wrapper method `sample` will return a wrapped value when `n` is provided,
|
||||||
* otherwise an unwrapped value is returned.
|
* otherwise an unwrapped value is returned.
|
||||||
@@ -8550,6 +8546,34 @@
|
|||||||
return isObject(value) && objToString.call(value) == funcTag;
|
return isObject(value) && objToString.call(value) == funcTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is an integer.
|
||||||
|
*
|
||||||
|
* **Note:** This method is based on [`Number.isInteger`](http://ecma-international.org/ecma-262/6.0/#sec-number.isinteger).
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Lang
|
||||||
|
* @param {*} value The value to check.
|
||||||
|
* @returns {boolean} Returns `true` if `value` is an integer, else `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.isInteger(3);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isInteger(Number.MAX_VALUE);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isInteger(3.14);
|
||||||
|
* // => false
|
||||||
|
*
|
||||||
|
* _.isInteger(Infinity);
|
||||||
|
* // => false
|
||||||
|
*/
|
||||||
|
function isInteger(value) {
|
||||||
|
return value === toInteger(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
|
* Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
|
||||||
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
* (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
||||||
@@ -8856,6 +8880,36 @@
|
|||||||
return isObject(value) && objToString.call(value) == regexpTag;
|
return isObject(value) && objToString.call(value) == regexpTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if `value` is a safe integer. An integer is safe if it's an IEEE-754
|
||||||
|
* double precision number which isn't the result of rounding result of unsafe
|
||||||
|
* integers.
|
||||||
|
*
|
||||||
|
* **Note:** This method is based on [`Number.isSafeInteger`](http://ecma-international.org/ecma-262/6.0/#sec-number.issafeinteger).
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Lang
|
||||||
|
* @param {*} value The value to check.
|
||||||
|
* @returns {boolean} Returns `true` if `value` is a safe integer, else `false`.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* _.isSafeInteger(3);
|
||||||
|
* // => true
|
||||||
|
*
|
||||||
|
* _.isSafeInteger(Number.MAX_VALUE);
|
||||||
|
* // => false
|
||||||
|
*
|
||||||
|
* _.isSafeInteger(3.14);
|
||||||
|
* // => false
|
||||||
|
*
|
||||||
|
* _.isSafeInteger(Infinity);
|
||||||
|
* // => false
|
||||||
|
*/
|
||||||
|
function isSafeInteger(value) {
|
||||||
|
return isInteger(value) && value >= -MAX_SAFE_INTEGER && value <= MAX_SAFE_INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `value` is classified as a `String` primitive or object.
|
* Checks if `value` is classified as a `String` primitive or object.
|
||||||
*
|
*
|
||||||
@@ -12096,6 +12150,7 @@
|
|||||||
lodash.isError = isError;
|
lodash.isError = isError;
|
||||||
lodash.isFinite = isFinite;
|
lodash.isFinite = isFinite;
|
||||||
lodash.isFunction = isFunction;
|
lodash.isFunction = isFunction;
|
||||||
|
lodash.isInteger = isInteger;
|
||||||
lodash.isMatch = isMatch;
|
lodash.isMatch = isMatch;
|
||||||
lodash.isMatchWith = isMatchWith;
|
lodash.isMatchWith = isMatchWith;
|
||||||
lodash.isNaN = isNaN;
|
lodash.isNaN = isNaN;
|
||||||
@@ -12107,6 +12162,7 @@
|
|||||||
lodash.isObjectLike = isObjectLike;
|
lodash.isObjectLike = isObjectLike;
|
||||||
lodash.isPlainObject = isPlainObject;
|
lodash.isPlainObject = isPlainObject;
|
||||||
lodash.isRegExp = isRegExp;
|
lodash.isRegExp = isRegExp;
|
||||||
|
lodash.isSafeInteger = isSafeInteger;
|
||||||
lodash.isString = isString;
|
lodash.isString = isString;
|
||||||
lodash.isTypedArray = isTypedArray;
|
lodash.isTypedArray = isTypedArray;
|
||||||
lodash.isUndefined = isUndefined;
|
lodash.isUndefined = isUndefined;
|
||||||
|
|||||||
15
test/test.js
15
test/test.js
@@ -9657,12 +9657,13 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should not error on host objects (test in IE)', function(assert) {
|
QUnit.test('should not error on host objects (test in IE)', function(assert) {
|
||||||
assert.expect(18);
|
assert.expect(20);
|
||||||
|
|
||||||
var funcs = [
|
var funcs = [
|
||||||
'isArguments', 'isArray', 'isArrayLike', 'isBoolean', 'isDate', 'isElement',
|
'isArguments', 'isArray', 'isArrayLike', 'isBoolean', 'isDate',
|
||||||
'isError', 'isFinite', 'isFunction', 'isNaN', 'isNil', 'isNull', 'isNumber',
|
'isElement', 'isError', 'isFinite', 'isFunction', 'isInteger', 'isNaN',
|
||||||
'isObject', 'isObjectLike', 'isRegExp', 'isString', 'isUndefined'
|
'isNil', 'isNull', 'isNumber', 'isObject', 'isObjectLike', 'isRegExp',
|
||||||
|
'isSafeInteger', 'isString', 'isUndefined'
|
||||||
];
|
];
|
||||||
|
|
||||||
_.each(funcs, function(methodName) {
|
_.each(funcs, function(methodName) {
|
||||||
@@ -18435,7 +18436,7 @@
|
|||||||
assert.strictEqual(_.toInteger('3.14'), 3);
|
assert.strictEqual(_.toInteger('3.14'), 3);
|
||||||
assert.strictEqual(_.toInteger(), 0);
|
assert.strictEqual(_.toInteger(), 0);
|
||||||
assert.strictEqual(_.toInteger(NaN), 0);
|
assert.strictEqual(_.toInteger(NaN), 0);
|
||||||
assert.strictEqual(_.toInteger(-Infinity), -Infinity);
|
assert.strictEqual(_.toInteger(-Infinity), -1.7976931348623157e+308);
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -20561,6 +20562,7 @@
|
|||||||
'isError',
|
'isError',
|
||||||
'isFinite',
|
'isFinite',
|
||||||
'isFunction',
|
'isFunction',
|
||||||
|
'isInteger',
|
||||||
'isNaN',
|
'isNaN',
|
||||||
'isNative',
|
'isNative',
|
||||||
'isNil',
|
'isNil',
|
||||||
@@ -20570,6 +20572,7 @@
|
|||||||
'isObjectLike',
|
'isObjectLike',
|
||||||
'isPlainObject',
|
'isPlainObject',
|
||||||
'isRegExp',
|
'isRegExp',
|
||||||
|
'isSafeInteger',
|
||||||
'isString',
|
'isString',
|
||||||
'isUndefined',
|
'isUndefined',
|
||||||
'join',
|
'join',
|
||||||
@@ -20824,7 +20827,7 @@
|
|||||||
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
var acceptFalsey = _.difference(allMethods, rejectFalsey);
|
||||||
|
|
||||||
QUnit.test('should accept falsey arguments', function(assert) {
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
assert.expect(233);
|
assert.expect(235);
|
||||||
|
|
||||||
var emptyArrays = _.map(falsey, _.constant([]));
|
var emptyArrays = _.map(falsey, _.constant([]));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user