index.js 29 KB

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