Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.js 11 KiB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 funcTag = '[object Function]',
  16. genTag = '[object GeneratorFunction]',
  17. symbolTag = '[object Symbol]';
  18. /** Used to match leading and trailing whitespace. */
  19. var reTrim = /^\s+|\s+$/g;
  20. /** Used to detect bad signed hexadecimal string values. */
  21. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  22. /** Used to detect binary string values. */
  23. var reIsBinary = /^0b[01]+$/i;
  24. /** Used to detect octal string values. */
  25. var reIsOctal = /^0o[0-7]+$/i;
  26. /** Used to detect unsigned integer values. */
  27. var reIsUint = /^(?:0|[1-9]\d*)$/;
  28. /** Built-in method references without a dependency on `root`. */
  29. var freeParseInt = parseInt;
  30. /** Used for built-in method references. */
  31. var objectProto = Object.prototype;
  32. /**
  33. * Used to resolve the
  34. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  35. * of values.
  36. */
  37. var objectToString = objectProto.toString;
  38. /* Built-in method references for those with the same name as other `lodash` methods. */
  39. var nativeCeil = Math.ceil,
  40. nativeMax = Math.max;
  41. /**
  42. * The base implementation of `_.range` and `_.rangeRight` which doesn't
  43. * coerce arguments.
  44. *
  45. * @private
  46. * @param {number} start The start of the range.
  47. * @param {number} end The end of the range.
  48. * @param {number} step The value to increment or decrement by.
  49. * @param {boolean} [fromRight] Specify iterating from right to left.
  50. * @returns {Array} Returns the range of numbers.
  51. */
  52. function baseRange(start, end, step, fromRight) {
  53. var index = -1,
  54. length = nativeMax(nativeCeil((end - start) / (step || 1)), 0),
  55. result = Array(length);
  56. while (length--) {
  57. result[fromRight ? length : ++index] = start;
  58. start += step;
  59. }
  60. return result;
  61. }
  62. /**
  63. * Creates a `_.range` or `_.rangeRight` function.
  64. *
  65. * @private
  66. * @param {boolean} [fromRight] Specify iterating from right to left.
  67. * @returns {Function} Returns the new range function.
  68. */
  69. function createRange(fromRight) {
  70. return function(start, end, step) {
  71. if (step && typeof step != 'number' && isIterateeCall(start, end, step)) {
  72. end = step = undefined;
  73. }
  74. // Ensure the sign of `-0` is preserved.
  75. start = toFinite(start);
  76. if (end === undefined) {
  77. end = start;
  78. start = 0;
  79. } else {
  80. end = toFinite(end);
  81. }
  82. step = step === undefined ? (start < end ? 1 : -1) : toFinite(step);
  83. return baseRange(start, end, step, fromRight);
  84. };
  85. }
  86. /**
  87. * Checks if `value` is a valid array-like index.
  88. *
  89. * @private
  90. * @param {*} value The value to check.
  91. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  92. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  93. */
  94. function isIndex(value, length) {
  95. length = length == null ? MAX_SAFE_INTEGER : length;
  96. return !!length &&
  97. (typeof value == 'number' || reIsUint.test(value)) &&
  98. (value > -1 && value % 1 == 0 && value < length);
  99. }
  100. /**
  101. * Checks if the given arguments are from an iteratee call.
  102. *
  103. * @private
  104. * @param {*} value The potential iteratee value argument.
  105. * @param {*} index The potential iteratee index or key argument.
  106. * @param {*} object The potential iteratee object argument.
  107. * @returns {boolean} Returns `true` if the arguments are from an iteratee call,
  108. * else `false`.
  109. */
  110. function isIterateeCall(value, index, object) {
  111. if (!isObject(object)) {
  112. return false;
  113. }
  114. var type = typeof index;
  115. if (type == 'number'
  116. ? (isArrayLike(object) && isIndex(index, object.length))
  117. : (type == 'string' && index in object)
  118. ) {
  119. return eq(object[index], value);
  120. }
  121. return false;
  122. }
  123. /**
  124. * Performs a
  125. * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  126. * comparison between two values to determine if they are equivalent.
  127. *
  128. * @static
  129. * @memberOf _
  130. * @since 4.0.0
  131. * @category Lang
  132. * @param {*} value The value to compare.
  133. * @param {*} other The other value to compare.
  134. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  135. * @example
  136. *
  137. * var object = { 'a': 1 };
  138. * var other = { 'a': 1 };
  139. *
  140. * _.eq(object, object);
  141. * // => true
  142. *
  143. * _.eq(object, other);
  144. * // => false
  145. *
  146. * _.eq('a', 'a');
  147. * // => true
  148. *
  149. * _.eq('a', Object('a'));
  150. * // => false
  151. *
  152. * _.eq(NaN, NaN);
  153. * // => true
  154. */
  155. function eq(value, other) {
  156. return value === other || (value !== value && other !== other);
  157. }
  158. /**
  159. * Checks if `value` is array-like. A value is considered array-like if it's
  160. * not a function and has a `value.length` that's an integer greater than or
  161. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  162. *
  163. * @static
  164. * @memberOf _
  165. * @since 4.0.0
  166. * @category Lang
  167. * @param {*} value The value to check.
  168. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  169. * @example
  170. *
  171. * _.isArrayLike([1, 2, 3]);
  172. * // => true
  173. *
  174. * _.isArrayLike(document.body.children);
  175. * // => true
  176. *
  177. * _.isArrayLike('abc');
  178. * // => true
  179. *
  180. * _.isArrayLike(_.noop);
  181. * // => false
  182. */
  183. function isArrayLike(value) {
  184. return value != null && isLength(value.length) && !isFunction(value);
  185. }
  186. /**
  187. * Checks if `value` is classified as a `Function` object.
  188. *
  189. * @static
  190. * @memberOf _
  191. * @since 0.1.0
  192. * @category Lang
  193. * @param {*} value The value to check.
  194. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  195. * @example
  196. *
  197. * _.isFunction(_);
  198. * // => true
  199. *
  200. * _.isFunction(/abc/);
  201. * // => false
  202. */
  203. function isFunction(value) {
  204. // The use of `Object#toString` avoids issues with the `typeof` operator
  205. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  206. var tag = isObject(value) ? objectToString.call(value) : '';
  207. return tag == funcTag || tag == genTag;
  208. }
  209. /**
  210. * Checks if `value` is a valid array-like length.
  211. *
  212. * **Note:** This method is loosely based on
  213. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  214. *
  215. * @static
  216. * @memberOf _
  217. * @since 4.0.0
  218. * @category Lang
  219. * @param {*} value The value to check.
  220. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  221. * @example
  222. *
  223. * _.isLength(3);
  224. * // => true
  225. *
  226. * _.isLength(Number.MIN_VALUE);
  227. * // => false
  228. *
  229. * _.isLength(Infinity);
  230. * // => false
  231. *
  232. * _.isLength('3');
  233. * // => false
  234. */
  235. function isLength(value) {
  236. return typeof value == 'number' &&
  237. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  238. }
  239. /**
  240. * Checks if `value` is the
  241. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  242. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  243. *
  244. * @static
  245. * @memberOf _
  246. * @since 0.1.0
  247. * @category Lang
  248. * @param {*} value The value to check.
  249. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  250. * @example
  251. *
  252. * _.isObject({});
  253. * // => true
  254. *
  255. * _.isObject([1, 2, 3]);
  256. * // => true
  257. *
  258. * _.isObject(_.noop);
  259. * // => true
  260. *
  261. * _.isObject(null);
  262. * // => false
  263. */
  264. function isObject(value) {
  265. var type = typeof value;
  266. return !!value && (type == 'object' || type == 'function');
  267. }
  268. /**
  269. * Checks if `value` is object-like. A value is object-like if it's not `null`
  270. * and has a `typeof` result of "object".
  271. *
  272. * @static
  273. * @memberOf _
  274. * @since 4.0.0
  275. * @category Lang
  276. * @param {*} value The value to check.
  277. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  278. * @example
  279. *
  280. * _.isObjectLike({});
  281. * // => true
  282. *
  283. * _.isObjectLike([1, 2, 3]);
  284. * // => true
  285. *
  286. * _.isObjectLike(_.noop);
  287. * // => false
  288. *
  289. * _.isObjectLike(null);
  290. * // => false
  291. */
  292. function isObjectLike(value) {
  293. return !!value && typeof value == 'object';
  294. }
  295. /**
  296. * Checks if `value` is classified as a `Symbol` primitive or object.
  297. *
  298. * @static
  299. * @memberOf _
  300. * @since 4.0.0
  301. * @category Lang
  302. * @param {*} value The value to check.
  303. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  304. * @example
  305. *
  306. * _.isSymbol(Symbol.iterator);
  307. * // => true
  308. *
  309. * _.isSymbol('abc');
  310. * // => false
  311. */
  312. function isSymbol(value) {
  313. return typeof value == 'symbol' ||
  314. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  315. }
  316. /**
  317. * Converts `value` to a finite number.
  318. *
  319. * @static
  320. * @memberOf _
  321. * @since 4.12.0
  322. * @category Lang
  323. * @param {*} value The value to convert.
  324. * @returns {number} Returns the converted number.
  325. * @example
  326. *
  327. * _.toFinite(3.2);
  328. * // => 3.2
  329. *
  330. * _.toFinite(Number.MIN_VALUE);
  331. * // => 5e-324
  332. *
  333. * _.toFinite(Infinity);
  334. * // => 1.7976931348623157e+308
  335. *
  336. * _.toFinite('3.2');
  337. * // => 3.2
  338. */
  339. function toFinite(value) {
  340. if (!value) {
  341. return value === 0 ? value : 0;
  342. }
  343. value = toNumber(value);
  344. if (value === INFINITY || value === -INFINITY) {
  345. var sign = (value < 0 ? -1 : 1);
  346. return sign * MAX_INTEGER;
  347. }
  348. return value === value ? value : 0;
  349. }
  350. /**
  351. * Converts `value` to a number.
  352. *
  353. * @static
  354. * @memberOf _
  355. * @since 4.0.0
  356. * @category Lang
  357. * @param {*} value The value to process.
  358. * @returns {number} Returns the number.
  359. * @example
  360. *
  361. * _.toNumber(3.2);
  362. * // => 3.2
  363. *
  364. * _.toNumber(Number.MIN_VALUE);
  365. * // => 5e-324
  366. *
  367. * _.toNumber(Infinity);
  368. * // => Infinity
  369. *
  370. * _.toNumber('3.2');
  371. * // => 3.2
  372. */
  373. function toNumber(value) {
  374. if (typeof value == 'number') {
  375. return value;
  376. }
  377. if (isSymbol(value)) {
  378. return NAN;
  379. }
  380. if (isObject(value)) {
  381. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  382. value = isObject(other) ? (other + '') : other;
  383. }
  384. if (typeof value != 'string') {
  385. return value === 0 ? value : +value;
  386. }
  387. value = value.replace(reTrim, '');
  388. var isBinary = reIsBinary.test(value);
  389. return (isBinary || reIsOctal.test(value))
  390. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  391. : (reIsBadHex.test(value) ? NAN : +value);
  392. }
  393. /**
  394. * Creates an array of numbers (positive and/or negative) progressing from
  395. * `start` up to, but not including, `end`. A step of `-1` is used if a negative
  396. * `start` is specified without an `end` or `step`. If `end` is not specified,
  397. * it's set to `start` with `start` then set to `0`.
  398. *
  399. * **Note:** JavaScript follows the IEEE-754 standard for resolving
  400. * floating-point values which can produce unexpected results.
  401. *
  402. * @static
  403. * @since 0.1.0
  404. * @memberOf _
  405. * @category Util
  406. * @param {number} [start=0] The start of the range.
  407. * @param {number} end The end of the range.
  408. * @param {number} [step=1] The value to increment or decrement by.
  409. * @returns {Array} Returns the range of numbers.
  410. * @see _.inRange, _.rangeRight
  411. * @example
  412. *
  413. * _.range(4);
  414. * // => [0, 1, 2, 3]
  415. *
  416. * _.range(-4);
  417. * // => [0, -1, -2, -3]
  418. *
  419. * _.range(1, 5);
  420. * // => [1, 2, 3, 4]
  421. *
  422. * _.range(0, 20, 5);
  423. * // => [0, 5, 10, 15]
  424. *
  425. * _.range(0, -4, -1);
  426. * // => [0, -1, -2, -3]
  427. *
  428. * _.range(1, 4, 0);
  429. * // => [1, 1, 1]
  430. *
  431. * _.range(0);
  432. * // => []
  433. */
  434. var range = createRange();
  435. module.exports = range;