index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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 size to enable large array optimizations. */
  10. var LARGE_ARRAY_SIZE = 200;
  11. /** Used to stand-in for `undefined` hash values. */
  12. var HASH_UNDEFINED = '__lodash_hash_undefined__';
  13. /** Used as references for various `Number` constants. */
  14. var INFINITY = 1 / 0,
  15. MAX_SAFE_INTEGER = 9007199254740991;
  16. /** `Object#toString` result references. */
  17. var argsTag = '[object Arguments]',
  18. funcTag = '[object Function]',
  19. genTag = '[object GeneratorFunction]';
  20. /**
  21. * Used to match `RegExp`
  22. * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
  23. */
  24. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  25. /** Used to detect host constructors (Safari). */
  26. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  27. /** Detect free variable `global` from Node.js. */
  28. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  29. /** Detect free variable `self`. */
  30. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  31. /** Used as a reference to the global object. */
  32. var root = freeGlobal || freeSelf || Function('return this')();
  33. /**
  34. * A faster alternative to `Function#apply`, this function invokes `func`
  35. * with the `this` binding of `thisArg` and the arguments of `args`.
  36. *
  37. * @private
  38. * @param {Function} func The function to invoke.
  39. * @param {*} thisArg The `this` binding of `func`.
  40. * @param {Array} args The arguments to invoke `func` with.
  41. * @returns {*} Returns the result of `func`.
  42. */
  43. function apply(func, thisArg, args) {
  44. switch (args.length) {
  45. case 0: return func.call(thisArg);
  46. case 1: return func.call(thisArg, args[0]);
  47. case 2: return func.call(thisArg, args[0], args[1]);
  48. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  49. }
  50. return func.apply(thisArg, args);
  51. }
  52. /**
  53. * A specialized version of `_.includes` for arrays without support for
  54. * specifying an index to search from.
  55. *
  56. * @private
  57. * @param {Array} [array] The array to inspect.
  58. * @param {*} target The value to search for.
  59. * @returns {boolean} Returns `true` if `target` is found, else `false`.
  60. */
  61. function arrayIncludes(array, value) {
  62. var length = array ? array.length : 0;
  63. return !!length && baseIndexOf(array, value, 0) > -1;
  64. }
  65. /**
  66. * This function is like `arrayIncludes` except that it accepts a comparator.
  67. *
  68. * @private
  69. * @param {Array} [array] The array to inspect.
  70. * @param {*} target The value to search for.
  71. * @param {Function} comparator The comparator invoked per element.
  72. * @returns {boolean} Returns `true` if `target` is found, else `false`.
  73. */
  74. function arrayIncludesWith(array, value, comparator) {
  75. var index = -1,
  76. length = array ? array.length : 0;
  77. while (++index < length) {
  78. if (comparator(value, array[index])) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. /**
  85. * Appends the elements of `values` to `array`.
  86. *
  87. * @private
  88. * @param {Array} array The array to modify.
  89. * @param {Array} values The values to append.
  90. * @returns {Array} Returns `array`.
  91. */
  92. function arrayPush(array, values) {
  93. var index = -1,
  94. length = values.length,
  95. offset = array.length;
  96. while (++index < length) {
  97. array[offset + index] = values[index];
  98. }
  99. return array;
  100. }
  101. /**
  102. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  103. * support for iteratee shorthands.
  104. *
  105. * @private
  106. * @param {Array} array The array to inspect.
  107. * @param {Function} predicate The function invoked per iteration.
  108. * @param {number} fromIndex The index to search from.
  109. * @param {boolean} [fromRight] Specify iterating from right to left.
  110. * @returns {number} Returns the index of the matched value, else `-1`.
  111. */
  112. function baseFindIndex(array, predicate, fromIndex, fromRight) {
  113. var length = array.length,
  114. index = fromIndex + (fromRight ? 1 : -1);
  115. while ((fromRight ? index-- : ++index < length)) {
  116. if (predicate(array[index], index, array)) {
  117. return index;
  118. }
  119. }
  120. return -1;
  121. }
  122. /**
  123. * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
  124. *
  125. * @private
  126. * @param {Array} array The array to inspect.
  127. * @param {*} value The value to search for.
  128. * @param {number} fromIndex The index to search from.
  129. * @returns {number} Returns the index of the matched value, else `-1`.
  130. */
  131. function baseIndexOf(array, value, fromIndex) {
  132. if (value !== value) {
  133. return baseFindIndex(array, baseIsNaN, fromIndex);
  134. }
  135. var index = fromIndex - 1,
  136. length = array.length;
  137. while (++index < length) {
  138. if (array[index] === value) {
  139. return index;
  140. }
  141. }
  142. return -1;
  143. }
  144. /**
  145. * The base implementation of `_.isNaN` without support for number objects.
  146. *
  147. * @private
  148. * @param {*} value The value to check.
  149. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  150. */
  151. function baseIsNaN(value) {
  152. return value !== value;
  153. }
  154. /**
  155. * Checks if a cache value for `key` exists.
  156. *
  157. * @private
  158. * @param {Object} cache The cache to query.
  159. * @param {string} key The key of the entry to check.
  160. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  161. */
  162. function cacheHas(cache, key) {
  163. return cache.has(key);
  164. }
  165. /**
  166. * Gets the value at `key` of `object`.
  167. *
  168. * @private
  169. * @param {Object} [object] The object to query.
  170. * @param {string} key The key of the property to get.
  171. * @returns {*} Returns the property value.
  172. */
  173. function getValue(object, key) {
  174. return object == null ? undefined : object[key];
  175. }
  176. /**
  177. * Checks if `value` is a host object in IE < 9.
  178. *
  179. * @private
  180. * @param {*} value The value to check.
  181. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  182. */
  183. function isHostObject(value) {
  184. // Many host objects are `Object` objects that can coerce to strings
  185. // despite having improperly defined `toString` methods.
  186. var result = false;
  187. if (value != null && typeof value.toString != 'function') {
  188. try {
  189. result = !!(value + '');
  190. } catch (e) {}
  191. }
  192. return result;
  193. }
  194. /**
  195. * Converts `set` to an array of its values.
  196. *
  197. * @private
  198. * @param {Object} set The set to convert.
  199. * @returns {Array} Returns the values.
  200. */
  201. function setToArray(set) {
  202. var index = -1,
  203. result = Array(set.size);
  204. set.forEach(function(value) {
  205. result[++index] = value;
  206. });
  207. return result;
  208. }
  209. /** Used for built-in method references. */
  210. var arrayProto = Array.prototype,
  211. funcProto = Function.prototype,
  212. objectProto = Object.prototype;
  213. /** Used to detect overreaching core-js shims. */
  214. var coreJsData = root['__core-js_shared__'];
  215. /** Used to detect methods masquerading as native. */
  216. var maskSrcKey = (function() {
  217. var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  218. return uid ? ('Symbol(src)_1.' + uid) : '';
  219. }());
  220. /** Used to resolve the decompiled source of functions. */
  221. var funcToString = funcProto.toString;
  222. /** Used to check objects for own properties. */
  223. var hasOwnProperty = objectProto.hasOwnProperty;
  224. /**
  225. * Used to resolve the
  226. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  227. * of values.
  228. */
  229. var objectToString = objectProto.toString;
  230. /** Used to detect if a method is native. */
  231. var reIsNative = RegExp('^' +
  232. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  233. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  234. );
  235. /** Built-in value references. */
  236. var Symbol = root.Symbol,
  237. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  238. splice = arrayProto.splice,
  239. spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
  240. /* Built-in method references for those with the same name as other `lodash` methods. */
  241. var nativeMax = Math.max;
  242. /* Built-in method references that are verified to be native. */
  243. var Map = getNative(root, 'Map'),
  244. Set = getNative(root, 'Set'),
  245. nativeCreate = getNative(Object, 'create');
  246. /**
  247. * Creates a hash object.
  248. *
  249. * @private
  250. * @constructor
  251. * @param {Array} [entries] The key-value pairs to cache.
  252. */
  253. function Hash(entries) {
  254. var index = -1,
  255. length = entries ? entries.length : 0;
  256. this.clear();
  257. while (++index < length) {
  258. var entry = entries[index];
  259. this.set(entry[0], entry[1]);
  260. }
  261. }
  262. /**
  263. * Removes all key-value entries from the hash.
  264. *
  265. * @private
  266. * @name clear
  267. * @memberOf Hash
  268. */
  269. function hashClear() {
  270. this.__data__ = nativeCreate ? nativeCreate(null) : {};
  271. }
  272. /**
  273. * Removes `key` and its value from the hash.
  274. *
  275. * @private
  276. * @name delete
  277. * @memberOf Hash
  278. * @param {Object} hash The hash to modify.
  279. * @param {string} key The key of the value to remove.
  280. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  281. */
  282. function hashDelete(key) {
  283. return this.has(key) && delete this.__data__[key];
  284. }
  285. /**
  286. * Gets the hash value for `key`.
  287. *
  288. * @private
  289. * @name get
  290. * @memberOf Hash
  291. * @param {string} key The key of the value to get.
  292. * @returns {*} Returns the entry value.
  293. */
  294. function hashGet(key) {
  295. var data = this.__data__;
  296. if (nativeCreate) {
  297. var result = data[key];
  298. return result === HASH_UNDEFINED ? undefined : result;
  299. }
  300. return hasOwnProperty.call(data, key) ? data[key] : undefined;
  301. }
  302. /**
  303. * Checks if a hash value for `key` exists.
  304. *
  305. * @private
  306. * @name has
  307. * @memberOf Hash
  308. * @param {string} key The key of the entry to check.
  309. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  310. */
  311. function hashHas(key) {
  312. var data = this.__data__;
  313. return nativeCreate ? data[key] !== undefined : hasOwnProperty.call(data, key);
  314. }
  315. /**
  316. * Sets the hash `key` to `value`.
  317. *
  318. * @private
  319. * @name set
  320. * @memberOf Hash
  321. * @param {string} key The key of the value to set.
  322. * @param {*} value The value to set.
  323. * @returns {Object} Returns the hash instance.
  324. */
  325. function hashSet(key, value) {
  326. var data = this.__data__;
  327. data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
  328. return this;
  329. }
  330. // Add methods to `Hash`.
  331. Hash.prototype.clear = hashClear;
  332. Hash.prototype['delete'] = hashDelete;
  333. Hash.prototype.get = hashGet;
  334. Hash.prototype.has = hashHas;
  335. Hash.prototype.set = hashSet;
  336. /**
  337. * Creates an list cache object.
  338. *
  339. * @private
  340. * @constructor
  341. * @param {Array} [entries] The key-value pairs to cache.
  342. */
  343. function ListCache(entries) {
  344. var index = -1,
  345. length = entries ? entries.length : 0;
  346. this.clear();
  347. while (++index < length) {
  348. var entry = entries[index];
  349. this.set(entry[0], entry[1]);
  350. }
  351. }
  352. /**
  353. * Removes all key-value entries from the list cache.
  354. *
  355. * @private
  356. * @name clear
  357. * @memberOf ListCache
  358. */
  359. function listCacheClear() {
  360. this.__data__ = [];
  361. }
  362. /**
  363. * Removes `key` and its value from the list cache.
  364. *
  365. * @private
  366. * @name delete
  367. * @memberOf ListCache
  368. * @param {string} key The key of the value to remove.
  369. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  370. */
  371. function listCacheDelete(key) {
  372. var data = this.__data__,
  373. index = assocIndexOf(data, key);
  374. if (index < 0) {
  375. return false;
  376. }
  377. var lastIndex = data.length - 1;
  378. if (index == lastIndex) {
  379. data.pop();
  380. } else {
  381. splice.call(data, index, 1);
  382. }
  383. return true;
  384. }
  385. /**
  386. * Gets the list cache value for `key`.
  387. *
  388. * @private
  389. * @name get
  390. * @memberOf ListCache
  391. * @param {string} key The key of the value to get.
  392. * @returns {*} Returns the entry value.
  393. */
  394. function listCacheGet(key) {
  395. var data = this.__data__,
  396. index = assocIndexOf(data, key);
  397. return index < 0 ? undefined : data[index][1];
  398. }
  399. /**
  400. * Checks if a list cache value for `key` exists.
  401. *
  402. * @private
  403. * @name has
  404. * @memberOf ListCache
  405. * @param {string} key The key of the entry to check.
  406. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  407. */
  408. function listCacheHas(key) {
  409. return assocIndexOf(this.__data__, key) > -1;
  410. }
  411. /**
  412. * Sets the list cache `key` to `value`.
  413. *
  414. * @private
  415. * @name set
  416. * @memberOf ListCache
  417. * @param {string} key The key of the value to set.
  418. * @param {*} value The value to set.
  419. * @returns {Object} Returns the list cache instance.
  420. */
  421. function listCacheSet(key, value) {
  422. var data = this.__data__,
  423. index = assocIndexOf(data, key);
  424. if (index < 0) {
  425. data.push([key, value]);
  426. } else {
  427. data[index][1] = value;
  428. }
  429. return this;
  430. }
  431. // Add methods to `ListCache`.
  432. ListCache.prototype.clear = listCacheClear;
  433. ListCache.prototype['delete'] = listCacheDelete;
  434. ListCache.prototype.get = listCacheGet;
  435. ListCache.prototype.has = listCacheHas;
  436. ListCache.prototype.set = listCacheSet;
  437. /**
  438. * Creates a map cache object to store key-value pairs.
  439. *
  440. * @private
  441. * @constructor
  442. * @param {Array} [entries] The key-value pairs to cache.
  443. */
  444. function MapCache(entries) {
  445. var index = -1,
  446. length = entries ? entries.length : 0;
  447. this.clear();
  448. while (++index < length) {
  449. var entry = entries[index];
  450. this.set(entry[0], entry[1]);
  451. }
  452. }
  453. /**
  454. * Removes all key-value entries from the map.
  455. *
  456. * @private
  457. * @name clear
  458. * @memberOf MapCache
  459. */
  460. function mapCacheClear() {
  461. this.__data__ = {
  462. 'hash': new Hash,
  463. 'map': new (Map || ListCache),
  464. 'string': new Hash
  465. };
  466. }
  467. /**
  468. * Removes `key` and its value from the map.
  469. *
  470. * @private
  471. * @name delete
  472. * @memberOf MapCache
  473. * @param {string} key The key of the value to remove.
  474. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  475. */
  476. function mapCacheDelete(key) {
  477. return getMapData(this, key)['delete'](key);
  478. }
  479. /**
  480. * Gets the map value for `key`.
  481. *
  482. * @private
  483. * @name get
  484. * @memberOf MapCache
  485. * @param {string} key The key of the value to get.
  486. * @returns {*} Returns the entry value.
  487. */
  488. function mapCacheGet(key) {
  489. return getMapData(this, key).get(key);
  490. }
  491. /**
  492. * Checks if a map value for `key` exists.
  493. *
  494. * @private
  495. * @name has
  496. * @memberOf MapCache
  497. * @param {string} key The key of the entry to check.
  498. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  499. */
  500. function mapCacheHas(key) {
  501. return getMapData(this, key).has(key);
  502. }
  503. /**
  504. * Sets the map `key` to `value`.
  505. *
  506. * @private
  507. * @name set
  508. * @memberOf MapCache
  509. * @param {string} key The key of the value to set.
  510. * @param {*} value The value to set.
  511. * @returns {Object} Returns the map cache instance.
  512. */
  513. function mapCacheSet(key, value) {
  514. getMapData(this, key).set(key, value);
  515. return this;
  516. }
  517. // Add methods to `MapCache`.
  518. MapCache.prototype.clear = mapCacheClear;
  519. MapCache.prototype['delete'] = mapCacheDelete;
  520. MapCache.prototype.get = mapCacheGet;
  521. MapCache.prototype.has = mapCacheHas;
  522. MapCache.prototype.set = mapCacheSet;
  523. /**
  524. *
  525. * Creates an array cache object to store unique values.
  526. *
  527. * @private
  528. * @constructor
  529. * @param {Array} [values] The values to cache.
  530. */
  531. function SetCache(values) {
  532. var index = -1,
  533. length = values ? values.length : 0;
  534. this.__data__ = new MapCache;
  535. while (++index < length) {
  536. this.add(values[index]);
  537. }
  538. }
  539. /**
  540. * Adds `value` to the array cache.
  541. *
  542. * @private
  543. * @name add
  544. * @memberOf SetCache
  545. * @alias push
  546. * @param {*} value The value to cache.
  547. * @returns {Object} Returns the cache instance.
  548. */
  549. function setCacheAdd(value) {
  550. this.__data__.set(value, HASH_UNDEFINED);
  551. return this;
  552. }
  553. /**
  554. * Checks if `value` is in the array cache.
  555. *
  556. * @private
  557. * @name has
  558. * @memberOf SetCache
  559. * @param {*} value The value to search for.
  560. * @returns {number} Returns `true` if `value` is found, else `false`.
  561. */
  562. function setCacheHas(value) {
  563. return this.__data__.has(value);
  564. }
  565. // Add methods to `SetCache`.
  566. SetCache.prototype.add = SetCache.prototype.push = setCacheAdd;
  567. SetCache.prototype.has = setCacheHas;
  568. /**
  569. * Gets the index at which the `key` is found in `array` of key-value pairs.
  570. *
  571. * @private
  572. * @param {Array} array The array to inspect.
  573. * @param {*} key The key to search for.
  574. * @returns {number} Returns the index of the matched value, else `-1`.
  575. */
  576. function assocIndexOf(array, key) {
  577. var length = array.length;
  578. while (length--) {
  579. if (eq(array[length][0], key)) {
  580. return length;
  581. }
  582. }
  583. return -1;
  584. }
  585. /**
  586. * The base implementation of `_.flatten` with support for restricting flattening.
  587. *
  588. * @private
  589. * @param {Array} array The array to flatten.
  590. * @param {number} depth The maximum recursion depth.
  591. * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
  592. * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
  593. * @param {Array} [result=[]] The initial result value.
  594. * @returns {Array} Returns the new flattened array.
  595. */
  596. function baseFlatten(array, depth, predicate, isStrict, result) {
  597. var index = -1,
  598. length = array.length;
  599. predicate || (predicate = isFlattenable);
  600. result || (result = []);
  601. while (++index < length) {
  602. var value = array[index];
  603. if (depth > 0 && predicate(value)) {
  604. if (depth > 1) {
  605. // Recursively flatten arrays (susceptible to call stack limits).
  606. baseFlatten(value, depth - 1, predicate, isStrict, result);
  607. } else {
  608. arrayPush(result, value);
  609. }
  610. } else if (!isStrict) {
  611. result[result.length] = value;
  612. }
  613. }
  614. return result;
  615. }
  616. /**
  617. * The base implementation of `_.isNative` without bad shim checks.
  618. *
  619. * @private
  620. * @param {*} value The value to check.
  621. * @returns {boolean} Returns `true` if `value` is a native function,
  622. * else `false`.
  623. */
  624. function baseIsNative(value) {
  625. if (!isObject(value) || isMasked(value)) {
  626. return false;
  627. }
  628. var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
  629. return pattern.test(toSource(value));
  630. }
  631. /**
  632. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  633. *
  634. * @private
  635. * @param {Function} func The function to apply a rest parameter to.
  636. * @param {number} [start=func.length-1] The start position of the rest parameter.
  637. * @returns {Function} Returns the new function.
  638. */
  639. function baseRest(func, start) {
  640. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  641. return function() {
  642. var args = arguments,
  643. index = -1,
  644. length = nativeMax(args.length - start, 0),
  645. array = Array(length);
  646. while (++index < length) {
  647. array[index] = args[start + index];
  648. }
  649. index = -1;
  650. var otherArgs = Array(start + 1);
  651. while (++index < start) {
  652. otherArgs[index] = args[index];
  653. }
  654. otherArgs[start] = array;
  655. return apply(func, this, otherArgs);
  656. };
  657. }
  658. /**
  659. * The base implementation of `_.uniqBy` without support for iteratee shorthands.
  660. *
  661. * @private
  662. * @param {Array} array The array to inspect.
  663. * @param {Function} [iteratee] The iteratee invoked per element.
  664. * @param {Function} [comparator] The comparator invoked per element.
  665. * @returns {Array} Returns the new duplicate free array.
  666. */
  667. function baseUniq(array, iteratee, comparator) {
  668. var index = -1,
  669. includes = arrayIncludes,
  670. length = array.length,
  671. isCommon = true,
  672. result = [],
  673. seen = result;
  674. if (comparator) {
  675. isCommon = false;
  676. includes = arrayIncludesWith;
  677. }
  678. else if (length >= LARGE_ARRAY_SIZE) {
  679. var set = iteratee ? null : createSet(array);
  680. if (set) {
  681. return setToArray(set);
  682. }
  683. isCommon = false;
  684. includes = cacheHas;
  685. seen = new SetCache;
  686. }
  687. else {
  688. seen = iteratee ? [] : result;
  689. }
  690. outer:
  691. while (++index < length) {
  692. var value = array[index],
  693. computed = iteratee ? iteratee(value) : value;
  694. value = (comparator || value !== 0) ? value : 0;
  695. if (isCommon && computed === computed) {
  696. var seenIndex = seen.length;
  697. while (seenIndex--) {
  698. if (seen[seenIndex] === computed) {
  699. continue outer;
  700. }
  701. }
  702. if (iteratee) {
  703. seen.push(computed);
  704. }
  705. result.push(value);
  706. }
  707. else if (!includes(seen, computed, comparator)) {
  708. if (seen !== result) {
  709. seen.push(computed);
  710. }
  711. result.push(value);
  712. }
  713. }
  714. return result;
  715. }
  716. /**
  717. * Creates a set object of `values`.
  718. *
  719. * @private
  720. * @param {Array} values The values to add to the set.
  721. * @returns {Object} Returns the new set.
  722. */
  723. var createSet = !(Set && (1 / setToArray(new Set([,-0]))[1]) == INFINITY) ? noop : function(values) {
  724. return new Set(values);
  725. };
  726. /**
  727. * Gets the data for `map`.
  728. *
  729. * @private
  730. * @param {Object} map The map to query.
  731. * @param {string} key The reference key.
  732. * @returns {*} Returns the map data.
  733. */
  734. function getMapData(map, key) {
  735. var data = map.__data__;
  736. return isKeyable(key)
  737. ? data[typeof key == 'string' ? 'string' : 'hash']
  738. : data.map;
  739. }
  740. /**
  741. * Gets the native function at `key` of `object`.
  742. *
  743. * @private
  744. * @param {Object} object The object to query.
  745. * @param {string} key The key of the method to get.
  746. * @returns {*} Returns the function if it's native, else `undefined`.
  747. */
  748. function getNative(object, key) {
  749. var value = getValue(object, key);
  750. return baseIsNative(value) ? value : undefined;
  751. }
  752. /**
  753. * Checks if `value` is a flattenable `arguments` object or array.
  754. *
  755. * @private
  756. * @param {*} value The value to check.
  757. * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
  758. */
  759. function isFlattenable(value) {
  760. return isArray(value) || isArguments(value) ||
  761. !!(spreadableSymbol && value && value[spreadableSymbol]);
  762. }
  763. /**
  764. * Checks if `value` is suitable for use as unique object key.
  765. *
  766. * @private
  767. * @param {*} value The value to check.
  768. * @returns {boolean} Returns `true` if `value` is suitable, else `false`.
  769. */
  770. function isKeyable(value) {
  771. var type = typeof value;
  772. return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')
  773. ? (value !== '__proto__')
  774. : (value === null);
  775. }
  776. /**
  777. * Checks if `func` has its source masked.
  778. *
  779. * @private
  780. * @param {Function} func The function to check.
  781. * @returns {boolean} Returns `true` if `func` is masked, else `false`.
  782. */
  783. function isMasked(func) {
  784. return !!maskSrcKey && (maskSrcKey in func);
  785. }
  786. /**
  787. * Converts `func` to its source code.
  788. *
  789. * @private
  790. * @param {Function} func The function to process.
  791. * @returns {string} Returns the source code.
  792. */
  793. function toSource(func) {
  794. if (func != null) {
  795. try {
  796. return funcToString.call(func);
  797. } catch (e) {}
  798. try {
  799. return (func + '');
  800. } catch (e) {}
  801. }
  802. return '';
  803. }
  804. /**
  805. * Gets the last element of `array`.
  806. *
  807. * @static
  808. * @memberOf _
  809. * @since 0.1.0
  810. * @category Array
  811. * @param {Array} array The array to query.
  812. * @returns {*} Returns the last element of `array`.
  813. * @example
  814. *
  815. * _.last([1, 2, 3]);
  816. * // => 3
  817. */
  818. function last(array) {
  819. var length = array ? array.length : 0;
  820. return length ? array[length - 1] : undefined;
  821. }
  822. /**
  823. * This method is like `_.union` except that it accepts `comparator` which
  824. * is invoked to compare elements of `arrays`. Result values are chosen from
  825. * the first array in which the value occurs. The comparator is invoked
  826. * with two arguments: (arrVal, othVal).
  827. *
  828. * @static
  829. * @memberOf _
  830. * @since 4.0.0
  831. * @category Array
  832. * @param {...Array} [arrays] The arrays to inspect.
  833. * @param {Function} [comparator] The comparator invoked per element.
  834. * @returns {Array} Returns the new array of combined values.
  835. * @example
  836. *
  837. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  838. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  839. *
  840. * _.unionWith(objects, others, _.isEqual);
  841. * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  842. */
  843. var unionWith = baseRest(function(arrays) {
  844. var comparator = last(arrays);
  845. if (isArrayLikeObject(comparator)) {
  846. comparator = undefined;
  847. }
  848. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator);
  849. });
  850. /**
  851. * Performs a
  852. * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  853. * comparison between two values to determine if they are equivalent.
  854. *
  855. * @static
  856. * @memberOf _
  857. * @since 4.0.0
  858. * @category Lang
  859. * @param {*} value The value to compare.
  860. * @param {*} other The other value to compare.
  861. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  862. * @example
  863. *
  864. * var object = { 'a': 1 };
  865. * var other = { 'a': 1 };
  866. *
  867. * _.eq(object, object);
  868. * // => true
  869. *
  870. * _.eq(object, other);
  871. * // => false
  872. *
  873. * _.eq('a', 'a');
  874. * // => true
  875. *
  876. * _.eq('a', Object('a'));
  877. * // => false
  878. *
  879. * _.eq(NaN, NaN);
  880. * // => true
  881. */
  882. function eq(value, other) {
  883. return value === other || (value !== value && other !== other);
  884. }
  885. /**
  886. * Checks if `value` is likely an `arguments` object.
  887. *
  888. * @static
  889. * @memberOf _
  890. * @since 0.1.0
  891. * @category Lang
  892. * @param {*} value The value to check.
  893. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  894. * else `false`.
  895. * @example
  896. *
  897. * _.isArguments(function() { return arguments; }());
  898. * // => true
  899. *
  900. * _.isArguments([1, 2, 3]);
  901. * // => false
  902. */
  903. function isArguments(value) {
  904. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  905. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  906. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  907. }
  908. /**
  909. * Checks if `value` is classified as an `Array` object.
  910. *
  911. * @static
  912. * @memberOf _
  913. * @since 0.1.0
  914. * @category Lang
  915. * @param {*} value The value to check.
  916. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  917. * @example
  918. *
  919. * _.isArray([1, 2, 3]);
  920. * // => true
  921. *
  922. * _.isArray(document.body.children);
  923. * // => false
  924. *
  925. * _.isArray('abc');
  926. * // => false
  927. *
  928. * _.isArray(_.noop);
  929. * // => false
  930. */
  931. var isArray = Array.isArray;
  932. /**
  933. * Checks if `value` is array-like. A value is considered array-like if it's
  934. * not a function and has a `value.length` that's an integer greater than or
  935. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  936. *
  937. * @static
  938. * @memberOf _
  939. * @since 4.0.0
  940. * @category Lang
  941. * @param {*} value The value to check.
  942. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  943. * @example
  944. *
  945. * _.isArrayLike([1, 2, 3]);
  946. * // => true
  947. *
  948. * _.isArrayLike(document.body.children);
  949. * // => true
  950. *
  951. * _.isArrayLike('abc');
  952. * // => true
  953. *
  954. * _.isArrayLike(_.noop);
  955. * // => false
  956. */
  957. function isArrayLike(value) {
  958. return value != null && isLength(value.length) && !isFunction(value);
  959. }
  960. /**
  961. * This method is like `_.isArrayLike` except that it also checks if `value`
  962. * is an object.
  963. *
  964. * @static
  965. * @memberOf _
  966. * @since 4.0.0
  967. * @category Lang
  968. * @param {*} value The value to check.
  969. * @returns {boolean} Returns `true` if `value` is an array-like object,
  970. * else `false`.
  971. * @example
  972. *
  973. * _.isArrayLikeObject([1, 2, 3]);
  974. * // => true
  975. *
  976. * _.isArrayLikeObject(document.body.children);
  977. * // => true
  978. *
  979. * _.isArrayLikeObject('abc');
  980. * // => false
  981. *
  982. * _.isArrayLikeObject(_.noop);
  983. * // => false
  984. */
  985. function isArrayLikeObject(value) {
  986. return isObjectLike(value) && isArrayLike(value);
  987. }
  988. /**
  989. * Checks if `value` is classified as a `Function` object.
  990. *
  991. * @static
  992. * @memberOf _
  993. * @since 0.1.0
  994. * @category Lang
  995. * @param {*} value The value to check.
  996. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  997. * @example
  998. *
  999. * _.isFunction(_);
  1000. * // => true
  1001. *
  1002. * _.isFunction(/abc/);
  1003. * // => false
  1004. */
  1005. function isFunction(value) {
  1006. // The use of `Object#toString` avoids issues with the `typeof` operator
  1007. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  1008. var tag = isObject(value) ? objectToString.call(value) : '';
  1009. return tag == funcTag || tag == genTag;
  1010. }
  1011. /**
  1012. * Checks if `value` is a valid array-like length.
  1013. *
  1014. * **Note:** This method is loosely based on
  1015. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  1016. *
  1017. * @static
  1018. * @memberOf _
  1019. * @since 4.0.0
  1020. * @category Lang
  1021. * @param {*} value The value to check.
  1022. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  1023. * @example
  1024. *
  1025. * _.isLength(3);
  1026. * // => true
  1027. *
  1028. * _.isLength(Number.MIN_VALUE);
  1029. * // => false
  1030. *
  1031. * _.isLength(Infinity);
  1032. * // => false
  1033. *
  1034. * _.isLength('3');
  1035. * // => false
  1036. */
  1037. function isLength(value) {
  1038. return typeof value == 'number' &&
  1039. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  1040. }
  1041. /**
  1042. * Checks if `value` is the
  1043. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  1044. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1045. *
  1046. * @static
  1047. * @memberOf _
  1048. * @since 0.1.0
  1049. * @category Lang
  1050. * @param {*} value The value to check.
  1051. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  1052. * @example
  1053. *
  1054. * _.isObject({});
  1055. * // => true
  1056. *
  1057. * _.isObject([1, 2, 3]);
  1058. * // => true
  1059. *
  1060. * _.isObject(_.noop);
  1061. * // => true
  1062. *
  1063. * _.isObject(null);
  1064. * // => false
  1065. */
  1066. function isObject(value) {
  1067. var type = typeof value;
  1068. return !!value && (type == 'object' || type == 'function');
  1069. }
  1070. /**
  1071. * Checks if `value` is object-like. A value is object-like if it's not `null`
  1072. * and has a `typeof` result of "object".
  1073. *
  1074. * @static
  1075. * @memberOf _
  1076. * @since 4.0.0
  1077. * @category Lang
  1078. * @param {*} value The value to check.
  1079. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  1080. * @example
  1081. *
  1082. * _.isObjectLike({});
  1083. * // => true
  1084. *
  1085. * _.isObjectLike([1, 2, 3]);
  1086. * // => true
  1087. *
  1088. * _.isObjectLike(_.noop);
  1089. * // => false
  1090. *
  1091. * _.isObjectLike(null);
  1092. * // => false
  1093. */
  1094. function isObjectLike(value) {
  1095. return !!value && typeof value == 'object';
  1096. }
  1097. /**
  1098. * This method returns `undefined`.
  1099. *
  1100. * @static
  1101. * @memberOf _
  1102. * @since 2.3.0
  1103. * @category Util
  1104. * @example
  1105. *
  1106. * _.times(2, _.noop);
  1107. * // => [undefined, undefined]
  1108. */
  1109. function noop() {
  1110. // No operation performed.
  1111. }
  1112. module.exports = unionWith;