25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 NAN = 0 / 0;
  11. /** `Object#toString` result references. */
  12. var symbolTag = '[object Symbol]';
  13. /** Used to match leading and trailing whitespace. */
  14. var reTrim = /^\s+|\s+$/g;
  15. /** Used to detect bad signed hexadecimal string values. */
  16. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  17. /** Used to detect binary string values. */
  18. var reIsBinary = /^0b[01]+$/i;
  19. /** Used to detect octal string values. */
  20. var reIsOctal = /^0o[0-7]+$/i;
  21. /** Built-in method references without a dependency on `root`. */
  22. var freeParseInt = parseInt;
  23. /** Used for built-in method references. */
  24. var objectProto = Object.prototype;
  25. /**
  26. * Used to resolve the
  27. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  28. * of values.
  29. */
  30. var objectToString = objectProto.toString;
  31. /**
  32. * Checks if `value` is the
  33. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  34. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  35. *
  36. * @static
  37. * @memberOf _
  38. * @since 0.1.0
  39. * @category Lang
  40. * @param {*} value The value to check.
  41. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  42. * @example
  43. *
  44. * _.isObject({});
  45. * // => true
  46. *
  47. * _.isObject([1, 2, 3]);
  48. * // => true
  49. *
  50. * _.isObject(_.noop);
  51. * // => true
  52. *
  53. * _.isObject(null);
  54. * // => false
  55. */
  56. function isObject(value) {
  57. var type = typeof value;
  58. return !!value && (type == 'object' || type == 'function');
  59. }
  60. /**
  61. * Checks if `value` is object-like. A value is object-like if it's not `null`
  62. * and has a `typeof` result of "object".
  63. *
  64. * @static
  65. * @memberOf _
  66. * @since 4.0.0
  67. * @category Lang
  68. * @param {*} value The value to check.
  69. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  70. * @example
  71. *
  72. * _.isObjectLike({});
  73. * // => true
  74. *
  75. * _.isObjectLike([1, 2, 3]);
  76. * // => true
  77. *
  78. * _.isObjectLike(_.noop);
  79. * // => false
  80. *
  81. * _.isObjectLike(null);
  82. * // => false
  83. */
  84. function isObjectLike(value) {
  85. return !!value && typeof value == 'object';
  86. }
  87. /**
  88. * Checks if `value` is classified as a `Symbol` primitive or object.
  89. *
  90. * @static
  91. * @memberOf _
  92. * @since 4.0.0
  93. * @category Lang
  94. * @param {*} value The value to check.
  95. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  96. * @example
  97. *
  98. * _.isSymbol(Symbol.iterator);
  99. * // => true
  100. *
  101. * _.isSymbol('abc');
  102. * // => false
  103. */
  104. function isSymbol(value) {
  105. return typeof value == 'symbol' ||
  106. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  107. }
  108. /**
  109. * Converts `value` to a number.
  110. *
  111. * @static
  112. * @memberOf _
  113. * @since 4.0.0
  114. * @category Lang
  115. * @param {*} value The value to process.
  116. * @returns {number} Returns the number.
  117. * @example
  118. *
  119. * _.toNumber(3.2);
  120. * // => 3.2
  121. *
  122. * _.toNumber(Number.MIN_VALUE);
  123. * // => 5e-324
  124. *
  125. * _.toNumber(Infinity);
  126. * // => Infinity
  127. *
  128. * _.toNumber('3.2');
  129. * // => 3.2
  130. */
  131. function toNumber(value) {
  132. if (typeof value == 'number') {
  133. return value;
  134. }
  135. if (isSymbol(value)) {
  136. return NAN;
  137. }
  138. if (isObject(value)) {
  139. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  140. value = isObject(other) ? (other + '') : other;
  141. }
  142. if (typeof value != 'string') {
  143. return value === 0 ? value : +value;
  144. }
  145. value = value.replace(reTrim, '');
  146. var isBinary = reIsBinary.test(value);
  147. return (isBinary || reIsOctal.test(value))
  148. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  149. : (reIsBadHex.test(value) ? NAN : +value);
  150. }
  151. module.exports = toNumber;