index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /**
  28. * A faster alternative to `Function#apply`, this function invokes `func`
  29. * with the `this` binding of `thisArg` and the arguments of `args`.
  30. *
  31. * @private
  32. * @param {Function} func The function to invoke.
  33. * @param {*} thisArg The `this` binding of `func`.
  34. * @param {Array} args The arguments to invoke `func` with.
  35. * @returns {*} Returns the result of `func`.
  36. */
  37. function apply(func, thisArg, args) {
  38. switch (args.length) {
  39. case 0: return func.call(thisArg);
  40. case 1: return func.call(thisArg, args[0]);
  41. case 2: return func.call(thisArg, args[0], args[1]);
  42. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  43. }
  44. return func.apply(thisArg, args);
  45. }
  46. /** Used for built-in method references. */
  47. var objectProto = Object.prototype;
  48. /**
  49. * Used to resolve the
  50. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  51. * of values.
  52. */
  53. var objectToString = objectProto.toString;
  54. /* Built-in method references for those with the same name as other `lodash` methods. */
  55. var nativeMax = Math.max;
  56. /**
  57. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  58. *
  59. * @private
  60. * @param {Function} func The function to apply a rest parameter to.
  61. * @param {number} [start=func.length-1] The start position of the rest parameter.
  62. * @returns {Function} Returns the new function.
  63. */
  64. function baseRest(func, start) {
  65. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  66. return function() {
  67. var args = arguments,
  68. index = -1,
  69. length = nativeMax(args.length - start, 0),
  70. array = Array(length);
  71. while (++index < length) {
  72. array[index] = args[start + index];
  73. }
  74. index = -1;
  75. var otherArgs = Array(start + 1);
  76. while (++index < start) {
  77. otherArgs[index] = args[index];
  78. }
  79. otherArgs[start] = array;
  80. return apply(func, this, otherArgs);
  81. };
  82. }
  83. /**
  84. * Creates a function that invokes `func` with the `this` binding of the
  85. * created function and arguments from `start` and beyond provided as
  86. * an array.
  87. *
  88. * **Note:** This method is based on the
  89. * [rest parameter](https://mdn.io/rest_parameters).
  90. *
  91. * @static
  92. * @memberOf _
  93. * @since 4.0.0
  94. * @category Function
  95. * @param {Function} func The function to apply a rest parameter to.
  96. * @param {number} [start=func.length-1] The start position of the rest parameter.
  97. * @returns {Function} Returns the new function.
  98. * @example
  99. *
  100. * var say = _.rest(function(what, names) {
  101. * return what + ' ' + _.initial(names).join(', ') +
  102. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  103. * });
  104. *
  105. * say('hello', 'fred', 'barney', 'pebbles');
  106. * // => 'hello fred, barney, & pebbles'
  107. */
  108. function rest(func, start) {
  109. if (typeof func != 'function') {
  110. throw new TypeError(FUNC_ERROR_TEXT);
  111. }
  112. start = start === undefined ? start : toInteger(start);
  113. return baseRest(func, start);
  114. }
  115. /**
  116. * Checks if `value` is the
  117. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  118. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  119. *
  120. * @static
  121. * @memberOf _
  122. * @since 0.1.0
  123. * @category Lang
  124. * @param {*} value The value to check.
  125. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  126. * @example
  127. *
  128. * _.isObject({});
  129. * // => true
  130. *
  131. * _.isObject([1, 2, 3]);
  132. * // => true
  133. *
  134. * _.isObject(_.noop);
  135. * // => true
  136. *
  137. * _.isObject(null);
  138. * // => false
  139. */
  140. function isObject(value) {
  141. var type = typeof value;
  142. return !!value && (type == 'object' || type == 'function');
  143. }
  144. /**
  145. * Checks if `value` is object-like. A value is object-like if it's not `null`
  146. * and has a `typeof` result of "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 object-like, else `false`.
  154. * @example
  155. *
  156. * _.isObjectLike({});
  157. * // => true
  158. *
  159. * _.isObjectLike([1, 2, 3]);
  160. * // => true
  161. *
  162. * _.isObjectLike(_.noop);
  163. * // => false
  164. *
  165. * _.isObjectLike(null);
  166. * // => false
  167. */
  168. function isObjectLike(value) {
  169. return !!value && typeof value == 'object';
  170. }
  171. /**
  172. * Checks if `value` is classified as a `Symbol` primitive or object.
  173. *
  174. * @static
  175. * @memberOf _
  176. * @since 4.0.0
  177. * @category Lang
  178. * @param {*} value The value to check.
  179. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  180. * @example
  181. *
  182. * _.isSymbol(Symbol.iterator);
  183. * // => true
  184. *
  185. * _.isSymbol('abc');
  186. * // => false
  187. */
  188. function isSymbol(value) {
  189. return typeof value == 'symbol' ||
  190. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  191. }
  192. /**
  193. * Converts `value` to a finite number.
  194. *
  195. * @static
  196. * @memberOf _
  197. * @since 4.12.0
  198. * @category Lang
  199. * @param {*} value The value to convert.
  200. * @returns {number} Returns the converted number.
  201. * @example
  202. *
  203. * _.toFinite(3.2);
  204. * // => 3.2
  205. *
  206. * _.toFinite(Number.MIN_VALUE);
  207. * // => 5e-324
  208. *
  209. * _.toFinite(Infinity);
  210. * // => 1.7976931348623157e+308
  211. *
  212. * _.toFinite('3.2');
  213. * // => 3.2
  214. */
  215. function toFinite(value) {
  216. if (!value) {
  217. return value === 0 ? value : 0;
  218. }
  219. value = toNumber(value);
  220. if (value === INFINITY || value === -INFINITY) {
  221. var sign = (value < 0 ? -1 : 1);
  222. return sign * MAX_INTEGER;
  223. }
  224. return value === value ? value : 0;
  225. }
  226. /**
  227. * Converts `value` to an integer.
  228. *
  229. * **Note:** This method is loosely based on
  230. * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
  231. *
  232. * @static
  233. * @memberOf _
  234. * @since 4.0.0
  235. * @category Lang
  236. * @param {*} value The value to convert.
  237. * @returns {number} Returns the converted integer.
  238. * @example
  239. *
  240. * _.toInteger(3.2);
  241. * // => 3
  242. *
  243. * _.toInteger(Number.MIN_VALUE);
  244. * // => 0
  245. *
  246. * _.toInteger(Infinity);
  247. * // => 1.7976931348623157e+308
  248. *
  249. * _.toInteger('3.2');
  250. * // => 3
  251. */
  252. function toInteger(value) {
  253. var result = toFinite(value),
  254. remainder = result % 1;
  255. return result === result ? (remainder ? result - remainder : result) : 0;
  256. }
  257. /**
  258. * Converts `value` to a number.
  259. *
  260. * @static
  261. * @memberOf _
  262. * @since 4.0.0
  263. * @category Lang
  264. * @param {*} value The value to process.
  265. * @returns {number} Returns the number.
  266. * @example
  267. *
  268. * _.toNumber(3.2);
  269. * // => 3.2
  270. *
  271. * _.toNumber(Number.MIN_VALUE);
  272. * // => 5e-324
  273. *
  274. * _.toNumber(Infinity);
  275. * // => Infinity
  276. *
  277. * _.toNumber('3.2');
  278. * // => 3.2
  279. */
  280. function toNumber(value) {
  281. if (typeof value == 'number') {
  282. return value;
  283. }
  284. if (isSymbol(value)) {
  285. return NAN;
  286. }
  287. if (isObject(value)) {
  288. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  289. value = isObject(other) ? (other + '') : other;
  290. }
  291. if (typeof value != 'string') {
  292. return value === 0 ? value : +value;
  293. }
  294. value = value.replace(reTrim, '');
  295. var isBinary = reIsBinary.test(value);
  296. return (isBinary || reIsOctal.test(value))
  297. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  298. : (reIsBadHex.test(value) ? NAN : +value);
  299. }
  300. module.exports = rest;