index.js 5.9 KB

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