index.js
1 /** 2 * lodash (Custom Build) <https://lodash.com/> 3 * Build: `lodash modularize exports="npm" -o ./` 4 * Copyright jQuery Foundation and other contributors <https://jquery.org/> 5 * Released under MIT license <https://lodash.com/license> 6 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> 7 * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 8 */ 9 10 /** Used as references for various `Number` constants. */ 11 var INFINITY = 1 / 0, 12 MAX_INTEGER = 1.7976931348623157e+308, 13 NAN = 0 / 0; 14 15 /** `Object#toString` result references. */ 16 var symbolTag = '[object Symbol]'; 17 18 /** Used to match leading and trailing whitespace. */ 19 var reTrim = /^\s+|\s+$/g; 20 21 /** Used to detect bad signed hexadecimal string values. */ 22 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; 23 24 /** Used to detect binary string values. */ 25 var reIsBinary = /^0b[01]+$/i; 26 27 /** Used to detect octal string values. */ 28 var reIsOctal = /^0o[0-7]+$/i; 29 30 /** Built-in method references without a dependency on `root`. */ 31 var freeParseInt = parseInt; 32 33 /** Used for built-in method references. */ 34 var objectProto = Object.prototype; 35 36 /** 37 * Used to resolve the 38 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) 39 * of values. 40 */ 41 var objectToString = objectProto.toString; 42 43 /** 44 * Checks if `value` is an integer. 45 * 46 * **Note:** This method is based on 47 * [`Number.isInteger`](https://mdn.io/Number/isInteger). 48 * 49 * @static 50 * @memberOf _ 51 * @since 4.0.0 52 * @category Lang 53 * @param {*} value The value to check. 54 * @returns {boolean} Returns `true` if `value` is an integer, else `false`. 55 * @example 56 * 57 * _.isInteger(3); 58 * // => true 59 * 60 * _.isInteger(Number.MIN_VALUE); 61 * // => false 62 * 63 * _.isInteger(Infinity); 64 * // => false 65 * 66 * _.isInteger('3'); 67 * // => false 68 */ 69 function isInteger(value) { 70 return typeof value == 'number' && value == toInteger(value); 71 } 72 73 /** 74 * Checks if `value` is the 75 * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) 76 * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) 77 * 78 * @static 79 * @memberOf _ 80 * @since 0.1.0 81 * @category Lang 82 * @param {*} value The value to check. 83 * @returns {boolean} Returns `true` if `value` is an object, else `false`. 84 * @example 85 * 86 * _.isObject({}); 87 * // => true 88 * 89 * _.isObject([1, 2, 3]); 90 * // => true 91 * 92 * _.isObject(_.noop); 93 * // => true 94 * 95 * _.isObject(null); 96 * // => false 97 */ 98 function isObject(value) { 99 var type = typeof value; 100 return !!value && (type == 'object' || type == 'function'); 101 } 102 103 /** 104 * Checks if `value` is object-like. A value is object-like if it's not `null` 105 * and has a `typeof` result of "object". 106 * 107 * @static 108 * @memberOf _ 109 * @since 4.0.0 110 * @category Lang 111 * @param {*} value The value to check. 112 * @returns {boolean} Returns `true` if `value` is object-like, else `false`. 113 * @example 114 * 115 * _.isObjectLike({}); 116 * // => true 117 * 118 * _.isObjectLike([1, 2, 3]); 119 * // => true 120 * 121 * _.isObjectLike(_.noop); 122 * // => false 123 * 124 * _.isObjectLike(null); 125 * // => false 126 */ 127 function isObjectLike(value) { 128 return !!value && typeof value == 'object'; 129 } 130 131 /** 132 * Checks if `value` is classified as a `Symbol` primitive or object. 133 * 134 * @static 135 * @memberOf _ 136 * @since 4.0.0 137 * @category Lang 138 * @param {*} value The value to check. 139 * @returns {boolean} Returns `true` if `value` is a symbol, else `false`. 140 * @example 141 * 142 * _.isSymbol(Symbol.iterator); 143 * // => true 144 * 145 * _.isSymbol('abc'); 146 * // => false 147 */ 148 function isSymbol(value) { 149 return typeof value == 'symbol' || 150 (isObjectLike(value) && objectToString.call(value) == symbolTag); 151 } 152 153 /** 154 * Converts `value` to a finite number. 155 * 156 * @static 157 * @memberOf _ 158 * @since 4.12.0 159 * @category Lang 160 * @param {*} value The value to convert. 161 * @returns {number} Returns the converted number. 162 * @example 163 * 164 * _.toFinite(3.2); 165 * // => 3.2 166 * 167 * _.toFinite(Number.MIN_VALUE); 168 * // => 5e-324 169 * 170 * _.toFinite(Infinity); 171 * // => 1.7976931348623157e+308 172 * 173 * _.toFinite('3.2'); 174 * // => 3.2 175 */ 176 function toFinite(value) { 177 if (!value) { 178 return value === 0 ? value : 0; 179 } 180 value = toNumber(value); 181 if (value === INFINITY || value === -INFINITY) { 182 var sign = (value < 0 ? -1 : 1); 183 return sign * MAX_INTEGER; 184 } 185 return value === value ? value : 0; 186 } 187 188 /** 189 * Converts `value` to an integer. 190 * 191 * **Note:** This method is loosely based on 192 * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger). 193 * 194 * @static 195 * @memberOf _ 196 * @since 4.0.0 197 * @category Lang 198 * @param {*} value The value to convert. 199 * @returns {number} Returns the converted integer. 200 * @example 201 * 202 * _.toInteger(3.2); 203 * // => 3 204 * 205 * _.toInteger(Number.MIN_VALUE); 206 * // => 0 207 * 208 * _.toInteger(Infinity); 209 * // => 1.7976931348623157e+308 210 * 211 * _.toInteger('3.2'); 212 * // => 3 213 */ 214 function toInteger(value) { 215 var result = toFinite(value), 216 remainder = result % 1; 217 218 return result === result ? (remainder ? result - remainder : result) : 0; 219 } 220 221 /** 222 * Converts `value` to a number. 223 * 224 * @static 225 * @memberOf _ 226 * @since 4.0.0 227 * @category Lang 228 * @param {*} value The value to process. 229 * @returns {number} Returns the number. 230 * @example 231 * 232 * _.toNumber(3.2); 233 * // => 3.2 234 * 235 * _.toNumber(Number.MIN_VALUE); 236 * // => 5e-324 237 * 238 * _.toNumber(Infinity); 239 * // => Infinity 240 * 241 * _.toNumber('3.2'); 242 * // => 3.2 243 */ 244 function toNumber(value) { 245 if (typeof value == 'number') { 246 return value; 247 } 248 if (isSymbol(value)) { 249 return NAN; 250 } 251 if (isObject(value)) { 252 var other = typeof value.valueOf == 'function' ? value.valueOf() : value; 253 value = isObject(other) ? (other + '') : other; 254 } 255 if (typeof value != 'string') { 256 return value === 0 ? value : +value; 257 } 258 value = value.replace(reTrim, ''); 259 var isBinary = reIsBinary.test(value); 260 return (isBinary || reIsOctal.test(value)) 261 ? freeParseInt(value.slice(2), isBinary ? 2 : 8) 262 : (reIsBadHex.test(value) ? NAN : +value); 263 } 264 265 module.exports = isInteger;