index.js 6.7 KB

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