index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. * Creates an array of `array` values not included in the other given arrays
  804. * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  805. * for equality comparisons. The order of result values is determined by the
  806. * order they occur in the first array.
  807. *
  808. * **Note:** Unlike `_.pullAll`, this method returns a new array.
  809. *
  810. * @static
  811. * @memberOf _
  812. * @since 0.1.0
  813. * @category Array
  814. * @param {Array} array The array to inspect.
  815. * @param {...Array} [values] The values to exclude.
  816. * @returns {Array} Returns the new array of filtered values.
  817. * @see _.without, _.xor
  818. * @example
  819. *
  820. * _.difference([2, 1], [2, 3]);
  821. * // => [1]
  822. */
  823. var difference = baseRest(function(array, values) {
  824. return isArrayLikeObject(array)
  825. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true))
  826. : [];
  827. });
  828. /**
  829. * Performs a
  830. * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  831. * comparison between two values to determine if they are equivalent.
  832. *
  833. * @static
  834. * @memberOf _
  835. * @since 4.0.0
  836. * @category Lang
  837. * @param {*} value The value to compare.
  838. * @param {*} other The other value to compare.
  839. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  840. * @example
  841. *
  842. * var object = { 'a': 1 };
  843. * var other = { 'a': 1 };
  844. *
  845. * _.eq(object, object);
  846. * // => true
  847. *
  848. * _.eq(object, other);
  849. * // => false
  850. *
  851. * _.eq('a', 'a');
  852. * // => true
  853. *
  854. * _.eq('a', Object('a'));
  855. * // => false
  856. *
  857. * _.eq(NaN, NaN);
  858. * // => true
  859. */
  860. function eq(value, other) {
  861. return value === other || (value !== value && other !== other);
  862. }
  863. /**
  864. * Checks if `value` is likely an `arguments` object.
  865. *
  866. * @static
  867. * @memberOf _
  868. * @since 0.1.0
  869. * @category Lang
  870. * @param {*} value The value to check.
  871. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  872. * else `false`.
  873. * @example
  874. *
  875. * _.isArguments(function() { return arguments; }());
  876. * // => true
  877. *
  878. * _.isArguments([1, 2, 3]);
  879. * // => false
  880. */
  881. function isArguments(value) {
  882. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  883. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  884. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  885. }
  886. /**
  887. * Checks if `value` is classified as an `Array` object.
  888. *
  889. * @static
  890. * @memberOf _
  891. * @since 0.1.0
  892. * @category Lang
  893. * @param {*} value The value to check.
  894. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  895. * @example
  896. *
  897. * _.isArray([1, 2, 3]);
  898. * // => true
  899. *
  900. * _.isArray(document.body.children);
  901. * // => false
  902. *
  903. * _.isArray('abc');
  904. * // => false
  905. *
  906. * _.isArray(_.noop);
  907. * // => false
  908. */
  909. var isArray = Array.isArray;
  910. /**
  911. * Checks if `value` is array-like. A value is considered array-like if it's
  912. * not a function and has a `value.length` that's an integer greater than or
  913. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  914. *
  915. * @static
  916. * @memberOf _
  917. * @since 4.0.0
  918. * @category Lang
  919. * @param {*} value The value to check.
  920. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  921. * @example
  922. *
  923. * _.isArrayLike([1, 2, 3]);
  924. * // => true
  925. *
  926. * _.isArrayLike(document.body.children);
  927. * // => true
  928. *
  929. * _.isArrayLike('abc');
  930. * // => true
  931. *
  932. * _.isArrayLike(_.noop);
  933. * // => false
  934. */
  935. function isArrayLike(value) {
  936. return value != null && isLength(value.length) && !isFunction(value);
  937. }
  938. /**
  939. * This method is like `_.isArrayLike` except that it also checks if `value`
  940. * is an object.
  941. *
  942. * @static
  943. * @memberOf _
  944. * @since 4.0.0
  945. * @category Lang
  946. * @param {*} value The value to check.
  947. * @returns {boolean} Returns `true` if `value` is an array-like object,
  948. * else `false`.
  949. * @example
  950. *
  951. * _.isArrayLikeObject([1, 2, 3]);
  952. * // => true
  953. *
  954. * _.isArrayLikeObject(document.body.children);
  955. * // => true
  956. *
  957. * _.isArrayLikeObject('abc');
  958. * // => false
  959. *
  960. * _.isArrayLikeObject(_.noop);
  961. * // => false
  962. */
  963. function isArrayLikeObject(value) {
  964. return isObjectLike(value) && isArrayLike(value);
  965. }
  966. /**
  967. * Checks if `value` is classified as a `Function` object.
  968. *
  969. * @static
  970. * @memberOf _
  971. * @since 0.1.0
  972. * @category Lang
  973. * @param {*} value The value to check.
  974. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  975. * @example
  976. *
  977. * _.isFunction(_);
  978. * // => true
  979. *
  980. * _.isFunction(/abc/);
  981. * // => false
  982. */
  983. function isFunction(value) {
  984. // The use of `Object#toString` avoids issues with the `typeof` operator
  985. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  986. var tag = isObject(value) ? objectToString.call(value) : '';
  987. return tag == funcTag || tag == genTag;
  988. }
  989. /**
  990. * Checks if `value` is a valid array-like length.
  991. *
  992. * **Note:** This method is loosely based on
  993. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  994. *
  995. * @static
  996. * @memberOf _
  997. * @since 4.0.0
  998. * @category Lang
  999. * @param {*} value The value to check.
  1000. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  1001. * @example
  1002. *
  1003. * _.isLength(3);
  1004. * // => true
  1005. *
  1006. * _.isLength(Number.MIN_VALUE);
  1007. * // => false
  1008. *
  1009. * _.isLength(Infinity);
  1010. * // => false
  1011. *
  1012. * _.isLength('3');
  1013. * // => false
  1014. */
  1015. function isLength(value) {
  1016. return typeof value == 'number' &&
  1017. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  1018. }
  1019. /**
  1020. * Checks if `value` is the
  1021. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  1022. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1023. *
  1024. * @static
  1025. * @memberOf _
  1026. * @since 0.1.0
  1027. * @category Lang
  1028. * @param {*} value The value to check.
  1029. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  1030. * @example
  1031. *
  1032. * _.isObject({});
  1033. * // => true
  1034. *
  1035. * _.isObject([1, 2, 3]);
  1036. * // => true
  1037. *
  1038. * _.isObject(_.noop);
  1039. * // => true
  1040. *
  1041. * _.isObject(null);
  1042. * // => false
  1043. */
  1044. function isObject(value) {
  1045. var type = typeof value;
  1046. return !!value && (type == 'object' || type == 'function');
  1047. }
  1048. /**
  1049. * Checks if `value` is object-like. A value is object-like if it's not `null`
  1050. * and has a `typeof` result of "object".
  1051. *
  1052. * @static
  1053. * @memberOf _
  1054. * @since 4.0.0
  1055. * @category Lang
  1056. * @param {*} value The value to check.
  1057. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  1058. * @example
  1059. *
  1060. * _.isObjectLike({});
  1061. * // => true
  1062. *
  1063. * _.isObjectLike([1, 2, 3]);
  1064. * // => true
  1065. *
  1066. * _.isObjectLike(_.noop);
  1067. * // => false
  1068. *
  1069. * _.isObjectLike(null);
  1070. * // => false
  1071. */
  1072. function isObjectLike(value) {
  1073. return !!value && typeof value == 'object';
  1074. }
  1075. module.exports = difference;