index.js 6.9 KB

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