_.isSafeInteger(value)
value _.
2023-09-11 14:15:01 时间
155
_.isSafeInteger(value)
_.isSafeInteger检查一个值是否是一个安全的整数。如果一个值是一个 IEEE-754 双精度数字而不是四舍五入的不安全的整数的结果就说它是安全的
参数
value (*): 需要检查的值
返回值
(boolean): 如果是安全的整数返回true,否则false
例子
_.isSafeInteger(3); // => true _.isSafeInteger(Number.MIN_VALUE); // => false _.isSafeInteger(Infinity); // => false _.isSafeInteger('3'); // => false
源代码
/** * lodash (Custom Build) <https://lodash.com/> * Build: `lodash modularize exports="npm" -o ./` * Copyright jQuery Foundation and other contributors <https://jquery.org/> * Released under MIT license <https://lodash.com/license> * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors */ /** Used as references for various `Number` constants. */ var INFINITY = 1 / 0, MAX_SAFE_INTEGER = 9007199254740991, MAX_INTEGER = 1.7976931348623157e+308, NAN = 0 / 0; /** `Object#toString` result references. */ var symbolTag = '[object Symbol]'; /** Used to match leading and trailing whitespace. */ var reTrim = /^\s+|\s+$/g; /** Used to detect bad signed hexadecimal string values. */ var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; /** Used to detect binary string values. */ var reIsBinary = /^0b[01]+$/i; /** Used to detect octal string values. */ var reIsOctal = /^0o[0-7]+$/i; /** Built-in method references without a dependency on `root`. */ var freeParseInt = parseInt; /** Used for built-in method references. */ var objectProto = Object.prototype; /** * Used to resolve the * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) * of values. */ var objectToString = objectProto.toString; /** * Checks if `value` is an integer. * * **Note:** This method is based on * [`Number.isInteger`](https://mdn.io/Number/isInteger). * * @static * @memberOf _ * @since 4.0.0 * @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.MIN_VALUE); * // => false * * _.isInteger(Infinity); * // => false * * _.isInteger('3'); * // => false */ //判断一个值是否是一个整数 function isInteger(value) { return typeof value == 'number' && value == toInteger(value); } /** * Checks if `value` is the * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) * * @static * @memberOf _ * @since 0.1.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is an object, else `false`. * @example * * _.isObject({}); * // => true * * _.isObject([1, 2, 3]); * // => true * * _.isObject(_.noop); * // => true * * _.isObject(null); * // => false */ function isObject(value) { var type = typeof value; return !!value && (type == 'object' || type == 'function'); } /** * Checks if `value` is object-like. A value is object-like if it's not `null` * and has a `typeof` result of "object". * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is object-like, else `false`. * @example * * _.isObjectLike({}); * // => true * * _.isObjectLike([1, 2, 3]); * // => true * * _.isObjectLike(_.noop); * // => false * * _.isObjectLike(null); * // => false */ function isObjectLike(value) { return !!value && typeof value == 'object'; } /** * 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 a rounded unsafe integer. * * **Note:** This method is based on * [`Number.isSafeInteger`](https://mdn.io/Number/isSafeInteger). * * @static * @memberOf _ * @since 4.0.0 * @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.MIN_VALUE); * // => false * * _.isSafeInteger(Infinity); * // => false * * _.isSafeInteger('3'); * // => false */ //检查一个值是否是一个安全的整数。如果一个值是一个 IEEE-754 双精度数字而不是四舍五入的不安全的整数的结果就说它是安全的 function isSafeInteger(value) { //判断value是不是整数 //判断value是否大于-MAX_SAFE_INTEGER //判断value是否小于MAX_SAFE_INTEGER return isInteger(value) && value >= -MAX_SAFE_INTEGER && value <= MAX_SAFE_INTEGER; } /** * Checks if `value` is classified as a `Symbol` primitive or object. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to check. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. * @example * * _.isSymbol(Symbol.iterator); * // => true * * _.isSymbol('abc'); * // => false */ //判断一个值是否被归类为symbol原生对象 function isSymbol(value) { return typeof value == 'symbol' || (isObjectLike(value) && objectToString.call(value) == symbolTag); } /** * Converts `value` to a finite number. * * @static * @memberOf _ * @since 4.12.0 * @category Lang * @param {*} value The value to convert. * @returns {number} Returns the converted number. * @example * * _.toFinite(3.2); * // => 3.2 * * _.toFinite(Number.MIN_VALUE); * // => 5e-324 * * _.toFinite(Infinity); * // => 1.7976931348623157e+308 * * _.toFinite('3.2'); * // => 3.2 */ //将一个值转换成 有限 的数字 function toFinite(value) { if (!value) {//如果value为假,返回0 return value === 0 ? value : 0; } value = toNumber(value);//将value转换成number类型 if (value === INFINITY || value === -INFINITY) {//如果是无限 var sign = (value < 0 ? -1 : 1);//正负标记 return sign * MAX_INTEGER;//转换成有限值 } return value === value ? value : 0;//返回结果,如果是NaN就返回0 } /** * Converts `value` to an integer. * * **Note:** This method is loosely based on * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger). * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to convert. * @returns {number} Returns the converted integer. * @example * * _.toInteger(3.2); * // => 3 * * _.toInteger(Number.MIN_VALUE); * // => 0 * * _.toInteger(Infinity); * // => 1.7976931348623157e+308 * * _.toInteger('3.2'); * // => 3 */ //将一个值转换成一个整数 function toInteger(value) { var result = toFinite(value),//将value转换成有限值 remainder = result % 1;//取余获取到小数部分 return result === result ? (remainder ? result - remainder : result) : 0; //如果是NaN返回0,如果是小数,减去小数部分,如果没有小数部分直接返回 } /** * Converts `value` to a number. * * @static * @memberOf _ * @since 4.0.0 * @category Lang * @param {*} value The value to process. * @returns {number} Returns the number. * @example * * _.toNumber(3.2); * // => 3.2 * * _.toNumber(Number.MIN_VALUE); * // => 5e-324 * * _.toNumber(Infinity); * // => Infinity * * _.toNumber('3.2'); * // => 3.2 */ //将一个值转换成数字 function toNumber(value) { if (typeof value == 'number') {//如果是number直接返回 return value; } if (isSymbol(value)) {//如果是Symbol类型,返回NaN return NAN; } if (isObject(value)) {//如果value是对象 var other = typeof value.valueOf == 'function' ? value.valueOf() : value; //如果value有valueOf属性,就调用valueOf()取值,否则取原值 value = isObject(other) ? (other + '') : other; //如果other是对象,就转成字符串,否则取原值 } if (typeof value != 'string') {//value被转成字符串后的处理 return value === 0 ? value : +value; } value = value.replace(reTrim, '');//正则把空白字符都转换成空字符串 var isBinary = reIsBinary.test(value);//判断是否是二进制 return (isBinary || reIsOctal.test(value)) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : (reIsBadHex.test(value) ? NAN : +value); //如果是二进制或者八进制,调用parseInt转换 //否则判断不良签名的十六进制转为NaN } module.exports = isSafeInteger;
相关文章
- js获取相同name文本框的value
- Access the value of a member expression
- 【翻译】Six Important .NET Concepts: Stack, Heap, Value Types, Reference Types, Boxing, and Unboxing
- _.toNumber(value)
- _.toArray(value)
- _.isTypedArray(value)
- _.isFinite(value)
- _.isBoolean(value)
- _.cloneDeep(value)
- _.sortedIndexOf(array, value)
- _.sortedIndex(array, value)
- _.lastIndexOf(array, value, [fromIndex=array.length-1])
- _.fill(array, value, [start=0], [end=array.length])
- java根据key或value对Map进行排序
- 翻译:last_value()函数(已提交到MariaDB官方手册)
- 浅析Java8新特性-Optional方法介绍(Optional.ofNullable-构造方法、map-逐层安全地拆解value、filter-过滤值、orElse/orElseThrow-最终返回、stream-转为流)及常用实践(仅作为方法返回值、清晰地表达返回值中没有结果的可能性、勿滥用Optional)、Optional的设计思想实现
- Java8List转Map的value冲突重复将value合并成List解决
- [LeetCode] 981. Time Based Key-Value Store 基于时间的键值存储
- Pass value from child popup window to parent page window using JavaScript--reference
- 【python】dict多种方法实现去除字典value为0 的元素