Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

565 linhas
14 KiB

há 3 anos
  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 MAX_SAFE_INTEGER = 9007199254740991;
  11. /** `Object#toString` result references. */
  12. var argsTag = '[object Arguments]',
  13. funcTag = '[object Function]',
  14. genTag = '[object GeneratorFunction]';
  15. /** Used to detect unsigned integer values. */
  16. var reIsUint = /^(?:0|[1-9]\d*)$/;
  17. /**
  18. * A specialized version of `_.forEach` for arrays without support for
  19. * iteratee shorthands.
  20. *
  21. * @private
  22. * @param {Array} [array] The array to iterate over.
  23. * @param {Function} iteratee The function invoked per iteration.
  24. * @returns {Array} Returns `array`.
  25. */
  26. function arrayEach(array, iteratee) {
  27. var index = -1,
  28. length = array ? array.length : 0;
  29. while (++index < length) {
  30. if (iteratee(array[index], index, array) === false) {
  31. break;
  32. }
  33. }
  34. return array;
  35. }
  36. /**
  37. * The base implementation of `_.times` without support for iteratee shorthands
  38. * or max array length checks.
  39. *
  40. * @private
  41. * @param {number} n The number of times to invoke `iteratee`.
  42. * @param {Function} iteratee The function invoked per iteration.
  43. * @returns {Array} Returns the array of results.
  44. */
  45. function baseTimes(n, iteratee) {
  46. var index = -1,
  47. result = Array(n);
  48. while (++index < n) {
  49. result[index] = iteratee(index);
  50. }
  51. return result;
  52. }
  53. /**
  54. * Creates a unary function that invokes `func` with its argument transformed.
  55. *
  56. * @private
  57. * @param {Function} func The function to wrap.
  58. * @param {Function} transform The argument transform.
  59. * @returns {Function} Returns the new function.
  60. */
  61. function overArg(func, transform) {
  62. return function(arg) {
  63. return func(transform(arg));
  64. };
  65. }
  66. /** Used for built-in method references. */
  67. var objectProto = Object.prototype;
  68. /** Used to check objects for own properties. */
  69. var hasOwnProperty = objectProto.hasOwnProperty;
  70. /**
  71. * Used to resolve the
  72. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  73. * of values.
  74. */
  75. var objectToString = objectProto.toString;
  76. /** Built-in value references. */
  77. var propertyIsEnumerable = objectProto.propertyIsEnumerable;
  78. /* Built-in method references for those with the same name as other `lodash` methods. */
  79. var nativeKeys = overArg(Object.keys, Object);
  80. /**
  81. * Creates an array of the enumerable property names of the array-like `value`.
  82. *
  83. * @private
  84. * @param {*} value The value to query.
  85. * @param {boolean} inherited Specify returning inherited property names.
  86. * @returns {Array} Returns the array of property names.
  87. */
  88. function arrayLikeKeys(value, inherited) {
  89. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  90. // Safari 9 makes `arguments.length` enumerable in strict mode.
  91. var result = (isArray(value) || isArguments(value))
  92. ? baseTimes(value.length, String)
  93. : [];
  94. var length = result.length,
  95. skipIndexes = !!length;
  96. for (var key in value) {
  97. if ((inherited || hasOwnProperty.call(value, key)) &&
  98. !(skipIndexes && (key == 'length' || isIndex(key, length)))) {
  99. result.push(key);
  100. }
  101. }
  102. return result;
  103. }
  104. /**
  105. * The base implementation of `_.forEach` without support for iteratee shorthands.
  106. *
  107. * @private
  108. * @param {Array|Object} collection The collection to iterate over.
  109. * @param {Function} iteratee The function invoked per iteration.
  110. * @returns {Array|Object} Returns `collection`.
  111. */
  112. var baseEach = createBaseEach(baseForOwn);
  113. /**
  114. * The base implementation of `baseForOwn` which iterates over `object`
  115. * properties returned by `keysFunc` and invokes `iteratee` for each property.
  116. * Iteratee functions may exit iteration early by explicitly returning `false`.
  117. *
  118. * @private
  119. * @param {Object} object The object to iterate over.
  120. * @param {Function} iteratee The function invoked per iteration.
  121. * @param {Function} keysFunc The function to get the keys of `object`.
  122. * @returns {Object} Returns `object`.
  123. */
  124. var baseFor = createBaseFor();
  125. /**
  126. * The base implementation of `_.forOwn` without support for iteratee shorthands.
  127. *
  128. * @private
  129. * @param {Object} object The object to iterate over.
  130. * @param {Function} iteratee The function invoked per iteration.
  131. * @returns {Object} Returns `object`.
  132. */
  133. function baseForOwn(object, iteratee) {
  134. return object && baseFor(object, iteratee, keys);
  135. }
  136. /**
  137. * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
  138. *
  139. * @private
  140. * @param {Object} object The object to query.
  141. * @returns {Array} Returns the array of property names.
  142. */
  143. function baseKeys(object) {
  144. if (!isPrototype(object)) {
  145. return nativeKeys(object);
  146. }
  147. var result = [];
  148. for (var key in Object(object)) {
  149. if (hasOwnProperty.call(object, key) && key != 'constructor') {
  150. result.push(key);
  151. }
  152. }
  153. return result;
  154. }
  155. /**
  156. * Creates a `baseEach` or `baseEachRight` function.
  157. *
  158. * @private
  159. * @param {Function} eachFunc The function to iterate over a collection.
  160. * @param {boolean} [fromRight] Specify iterating from right to left.
  161. * @returns {Function} Returns the new base function.
  162. */
  163. function createBaseEach(eachFunc, fromRight) {
  164. return function(collection, iteratee) {
  165. if (collection == null) {
  166. return collection;
  167. }
  168. if (!isArrayLike(collection)) {
  169. return eachFunc(collection, iteratee);
  170. }
  171. var length = collection.length,
  172. index = fromRight ? length : -1,
  173. iterable = Object(collection);
  174. while ((fromRight ? index-- : ++index < length)) {
  175. if (iteratee(iterable[index], index, iterable) === false) {
  176. break;
  177. }
  178. }
  179. return collection;
  180. };
  181. }
  182. /**
  183. * Creates a base function for methods like `_.forIn` and `_.forOwn`.
  184. *
  185. * @private
  186. * @param {boolean} [fromRight] Specify iterating from right to left.
  187. * @returns {Function} Returns the new base function.
  188. */
  189. function createBaseFor(fromRight) {
  190. return function(object, iteratee, keysFunc) {
  191. var index = -1,
  192. iterable = Object(object),
  193. props = keysFunc(object),
  194. length = props.length;
  195. while (length--) {
  196. var key = props[fromRight ? length : ++index];
  197. if (iteratee(iterable[key], key, iterable) === false) {
  198. break;
  199. }
  200. }
  201. return object;
  202. };
  203. }
  204. /**
  205. * Checks if `value` is a valid array-like index.
  206. *
  207. * @private
  208. * @param {*} value The value to check.
  209. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  210. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  211. */
  212. function isIndex(value, length) {
  213. length = length == null ? MAX_SAFE_INTEGER : length;
  214. return !!length &&
  215. (typeof value == 'number' || reIsUint.test(value)) &&
  216. (value > -1 && value % 1 == 0 && value < length);
  217. }
  218. /**
  219. * Checks if `value` is likely a prototype object.
  220. *
  221. * @private
  222. * @param {*} value The value to check.
  223. * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
  224. */
  225. function isPrototype(value) {
  226. var Ctor = value && value.constructor,
  227. proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
  228. return value === proto;
  229. }
  230. /**
  231. * Iterates over elements of `collection` and invokes `iteratee` for each element.
  232. * The iteratee is invoked with three arguments: (value, index|key, collection).
  233. * Iteratee functions may exit iteration early by explicitly returning `false`.
  234. *
  235. * **Note:** As with other "Collections" methods, objects with a "length"
  236. * property are iterated like arrays. To avoid this behavior use `_.forIn`
  237. * or `_.forOwn` for object iteration.
  238. *
  239. * @static
  240. * @memberOf _
  241. * @since 0.1.0
  242. * @alias each
  243. * @category Collection
  244. * @param {Array|Object} collection The collection to iterate over.
  245. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  246. * @returns {Array|Object} Returns `collection`.
  247. * @see _.forEachRight
  248. * @example
  249. *
  250. * _([1, 2]).forEach(function(value) {
  251. * console.log(value);
  252. * });
  253. * // => Logs `1` then `2`.
  254. *
  255. * _.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  256. * console.log(key);
  257. * });
  258. * // => Logs 'a' then 'b' (iteration order is not guaranteed).
  259. */
  260. function forEach(collection, iteratee) {
  261. var func = isArray(collection) ? arrayEach : baseEach;
  262. return func(collection, typeof iteratee == 'function' ? iteratee : identity);
  263. }
  264. /**
  265. * Checks if `value` is likely an `arguments` object.
  266. *
  267. * @static
  268. * @memberOf _
  269. * @since 0.1.0
  270. * @category Lang
  271. * @param {*} value The value to check.
  272. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  273. * else `false`.
  274. * @example
  275. *
  276. * _.isArguments(function() { return arguments; }());
  277. * // => true
  278. *
  279. * _.isArguments([1, 2, 3]);
  280. * // => false
  281. */
  282. function isArguments(value) {
  283. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  284. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  285. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  286. }
  287. /**
  288. * Checks if `value` is classified as an `Array` object.
  289. *
  290. * @static
  291. * @memberOf _
  292. * @since 0.1.0
  293. * @category Lang
  294. * @param {*} value The value to check.
  295. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  296. * @example
  297. *
  298. * _.isArray([1, 2, 3]);
  299. * // => true
  300. *
  301. * _.isArray(document.body.children);
  302. * // => false
  303. *
  304. * _.isArray('abc');
  305. * // => false
  306. *
  307. * _.isArray(_.noop);
  308. * // => false
  309. */
  310. var isArray = Array.isArray;
  311. /**
  312. * Checks if `value` is array-like. A value is considered array-like if it's
  313. * not a function and has a `value.length` that's an integer greater than or
  314. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  315. *
  316. * @static
  317. * @memberOf _
  318. * @since 4.0.0
  319. * @category Lang
  320. * @param {*} value The value to check.
  321. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  322. * @example
  323. *
  324. * _.isArrayLike([1, 2, 3]);
  325. * // => true
  326. *
  327. * _.isArrayLike(document.body.children);
  328. * // => true
  329. *
  330. * _.isArrayLike('abc');
  331. * // => true
  332. *
  333. * _.isArrayLike(_.noop);
  334. * // => false
  335. */
  336. function isArrayLike(value) {
  337. return value != null && isLength(value.length) && !isFunction(value);
  338. }
  339. /**
  340. * This method is like `_.isArrayLike` except that it also checks if `value`
  341. * is an object.
  342. *
  343. * @static
  344. * @memberOf _
  345. * @since 4.0.0
  346. * @category Lang
  347. * @param {*} value The value to check.
  348. * @returns {boolean} Returns `true` if `value` is an array-like object,
  349. * else `false`.
  350. * @example
  351. *
  352. * _.isArrayLikeObject([1, 2, 3]);
  353. * // => true
  354. *
  355. * _.isArrayLikeObject(document.body.children);
  356. * // => true
  357. *
  358. * _.isArrayLikeObject('abc');
  359. * // => false
  360. *
  361. * _.isArrayLikeObject(_.noop);
  362. * // => false
  363. */
  364. function isArrayLikeObject(value) {
  365. return isObjectLike(value) && isArrayLike(value);
  366. }
  367. /**
  368. * Checks if `value` is classified as a `Function` object.
  369. *
  370. * @static
  371. * @memberOf _
  372. * @since 0.1.0
  373. * @category Lang
  374. * @param {*} value The value to check.
  375. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  376. * @example
  377. *
  378. * _.isFunction(_);
  379. * // => true
  380. *
  381. * _.isFunction(/abc/);
  382. * // => false
  383. */
  384. function isFunction(value) {
  385. // The use of `Object#toString` avoids issues with the `typeof` operator
  386. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  387. var tag = isObject(value) ? objectToString.call(value) : '';
  388. return tag == funcTag || tag == genTag;
  389. }
  390. /**
  391. * Checks if `value` is a valid array-like length.
  392. *
  393. * **Note:** This method is loosely based on
  394. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  395. *
  396. * @static
  397. * @memberOf _
  398. * @since 4.0.0
  399. * @category Lang
  400. * @param {*} value The value to check.
  401. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  402. * @example
  403. *
  404. * _.isLength(3);
  405. * // => true
  406. *
  407. * _.isLength(Number.MIN_VALUE);
  408. * // => false
  409. *
  410. * _.isLength(Infinity);
  411. * // => false
  412. *
  413. * _.isLength('3');
  414. * // => false
  415. */
  416. function isLength(value) {
  417. return typeof value == 'number' &&
  418. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  419. }
  420. /**
  421. * Checks if `value` is the
  422. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  423. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  424. *
  425. * @static
  426. * @memberOf _
  427. * @since 0.1.0
  428. * @category Lang
  429. * @param {*} value The value to check.
  430. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  431. * @example
  432. *
  433. * _.isObject({});
  434. * // => true
  435. *
  436. * _.isObject([1, 2, 3]);
  437. * // => true
  438. *
  439. * _.isObject(_.noop);
  440. * // => true
  441. *
  442. * _.isObject(null);
  443. * // => false
  444. */
  445. function isObject(value) {
  446. var type = typeof value;
  447. return !!value && (type == 'object' || type == 'function');
  448. }
  449. /**
  450. * Checks if `value` is object-like. A value is object-like if it's not `null`
  451. * and has a `typeof` result of "object".
  452. *
  453. * @static
  454. * @memberOf _
  455. * @since 4.0.0
  456. * @category Lang
  457. * @param {*} value The value to check.
  458. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  459. * @example
  460. *
  461. * _.isObjectLike({});
  462. * // => true
  463. *
  464. * _.isObjectLike([1, 2, 3]);
  465. * // => true
  466. *
  467. * _.isObjectLike(_.noop);
  468. * // => false
  469. *
  470. * _.isObjectLike(null);
  471. * // => false
  472. */
  473. function isObjectLike(value) {
  474. return !!value && typeof value == 'object';
  475. }
  476. /**
  477. * Creates an array of the own enumerable property names of `object`.
  478. *
  479. * **Note:** Non-object values are coerced to objects. See the
  480. * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
  481. * for more details.
  482. *
  483. * @static
  484. * @since 0.1.0
  485. * @memberOf _
  486. * @category Object
  487. * @param {Object} object The object to query.
  488. * @returns {Array} Returns the array of property names.
  489. * @example
  490. *
  491. * function Foo() {
  492. * this.a = 1;
  493. * this.b = 2;
  494. * }
  495. *
  496. * Foo.prototype.c = 3;
  497. *
  498. * _.keys(new Foo);
  499. * // => ['a', 'b'] (iteration order is not guaranteed)
  500. *
  501. * _.keys('hi');
  502. * // => ['0', '1']
  503. */
  504. function keys(object) {
  505. return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
  506. }
  507. /**
  508. * This method returns the first argument it receives.
  509. *
  510. * @static
  511. * @since 0.1.0
  512. * @memberOf _
  513. * @category Util
  514. * @param {*} value Any value.
  515. * @returns {*} Returns `value`.
  516. * @example
  517. *
  518. * var object = { 'a': 1 };
  519. *
  520. * console.log(_.identity(object) === object);
  521. * // => true
  522. */
  523. function identity(value) {
  524. return value;
  525. }
  526. module.exports = forEach;