index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /**
  2. * lodash 4.1.3 (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. /** `Object#toString` result references. */
  14. var funcTag = '[object Function]',
  15. genTag = '[object GeneratorFunction]';
  16. /**
  17. * Used to match `RegExp`
  18. * [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns).
  19. */
  20. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  21. /** Used to detect host constructors (Safari). */
  22. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  23. /** Used to determine if values are of the language type `Object`. */
  24. var objectTypes = {
  25. 'function': true,
  26. 'object': true
  27. };
  28. /** Detect free variable `exports`. */
  29. var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType)
  30. ? exports
  31. : undefined;
  32. /** Detect free variable `module`. */
  33. var freeModule = (objectTypes[typeof module] && module && !module.nodeType)
  34. ? module
  35. : undefined;
  36. /** Detect free variable `global` from Node.js. */
  37. var freeGlobal = checkGlobal(freeExports && freeModule && typeof global == 'object' && global);
  38. /** Detect free variable `self`. */
  39. var freeSelf = checkGlobal(objectTypes[typeof self] && self);
  40. /** Detect free variable `window`. */
  41. var freeWindow = checkGlobal(objectTypes[typeof window] && window);
  42. /** Detect `this` as the global object. */
  43. var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
  44. /**
  45. * Used as a reference to the global object.
  46. *
  47. * The `this` value is used if it's the global object to avoid Greasemonkey's
  48. * restricted `window` object, otherwise the `window` object is used.
  49. */
  50. var root = freeGlobal ||
  51. ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) ||
  52. freeSelf || thisGlobal || Function('return this')();
  53. /**
  54. * Checks if `value` is a global object.
  55. *
  56. * @private
  57. * @param {*} value The value to check.
  58. * @returns {null|Object} Returns `value` if it's a global object, else `null`.
  59. */
  60. function checkGlobal(value) {
  61. return (value && value.Object === Object) ? value : null;
  62. }
  63. /**
  64. * Checks if `value` is a host object in IE < 9.
  65. *
  66. * @private
  67. * @param {*} value The value to check.
  68. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  69. */
  70. function isHostObject(value) {
  71. // Many host objects are `Object` objects that can coerce to strings
  72. // despite having improperly defined `toString` methods.
  73. var result = false;
  74. if (value != null && typeof value.toString != 'function') {
  75. try {
  76. result = !!(value + '');
  77. } catch (e) {}
  78. }
  79. return result;
  80. }
  81. /** Used for built-in method references. */
  82. var arrayProto = Array.prototype,
  83. objectProto = Object.prototype;
  84. /** Used to resolve the decompiled source of functions. */
  85. var funcToString = Function.prototype.toString;
  86. /** Used to check objects for own properties. */
  87. var hasOwnProperty = objectProto.hasOwnProperty;
  88. /**
  89. * Used to resolve the
  90. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  91. * of values.
  92. */
  93. var objectToString = objectProto.toString;
  94. /** Used to detect if a method is native. */
  95. var reIsNative = RegExp('^' +
  96. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  97. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  98. );
  99. /** Built-in value references. */
  100. var splice = arrayProto.splice;
  101. /* Built-in method references that are verified to be native. */
  102. var Map = getNative(root, 'Map'),
  103. nativeCreate = getNative(Object, 'create');
  104. /**
  105. * Creates a hash object.
  106. *
  107. * @private
  108. * @constructor
  109. * @returns {Object} Returns the new hash object.
  110. */
  111. function Hash() {}
  112. /**
  113. * Removes `key` and its value from the hash.
  114. *
  115. * @private
  116. * @param {Object} hash The hash to modify.
  117. * @param {string} key The key of the value to remove.
  118. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  119. */
  120. function hashDelete(hash, key) {
  121. return hashHas(hash, key) && delete hash[key];
  122. }
  123. /**
  124. * Gets the hash value for `key`.
  125. *
  126. * @private
  127. * @param {Object} hash The hash to query.
  128. * @param {string} key The key of the value to get.
  129. * @returns {*} Returns the entry value.
  130. */
  131. function hashGet(hash, key) {
  132. if (nativeCreate) {
  133. var result = hash[key];
  134. return result === HASH_UNDEFINED ? undefined : result;
  135. }
  136. return hasOwnProperty.call(hash, key) ? hash[key] : undefined;
  137. }
  138. /**
  139. * Checks if a hash value for `key` exists.
  140. *
  141. * @private
  142. * @param {Object} hash The hash to query.
  143. * @param {string} key The key of the entry to check.
  144. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  145. */
  146. function hashHas(hash, key) {
  147. return nativeCreate ? hash[key] !== undefined : hasOwnProperty.call(hash, key);
  148. }
  149. /**
  150. * Sets the hash `key` to `value`.
  151. *
  152. * @private
  153. * @param {Object} hash The hash to modify.
  154. * @param {string} key The key of the value to set.
  155. * @param {*} value The value to set.
  156. */
  157. function hashSet(hash, key, value) {
  158. hash[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
  159. }
  160. // Avoid inheriting from `Object.prototype` when possible.
  161. Hash.prototype = nativeCreate ? nativeCreate(null) : objectProto;
  162. /**
  163. * Creates a map cache object to store key-value pairs.
  164. *
  165. * @private
  166. * @constructor
  167. * @param {Array} [values] The values to cache.
  168. */
  169. function MapCache(values) {
  170. var index = -1,
  171. length = values ? values.length : 0;
  172. this.clear();
  173. while (++index < length) {
  174. var entry = values[index];
  175. this.set(entry[0], entry[1]);
  176. }
  177. }
  178. /**
  179. * Removes all key-value entries from the map.
  180. *
  181. * @private
  182. * @name clear
  183. * @memberOf MapCache
  184. */
  185. function mapClear() {
  186. this.__data__ = {
  187. 'hash': new Hash,
  188. 'map': Map ? new Map : [],
  189. 'string': new Hash
  190. };
  191. }
  192. /**
  193. * Removes `key` and its value from the map.
  194. *
  195. * @private
  196. * @name delete
  197. * @memberOf MapCache
  198. * @param {string} key The key of the value to remove.
  199. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  200. */
  201. function mapDelete(key) {
  202. var data = this.__data__;
  203. if (isKeyable(key)) {
  204. return hashDelete(typeof key == 'string' ? data.string : data.hash, key);
  205. }
  206. return Map ? data.map['delete'](key) : assocDelete(data.map, key);
  207. }
  208. /**
  209. * Gets the map value for `key`.
  210. *
  211. * @private
  212. * @name get
  213. * @memberOf MapCache
  214. * @param {string} key The key of the value to get.
  215. * @returns {*} Returns the entry value.
  216. */
  217. function mapGet(key) {
  218. var data = this.__data__;
  219. if (isKeyable(key)) {
  220. return hashGet(typeof key == 'string' ? data.string : data.hash, key);
  221. }
  222. return Map ? data.map.get(key) : assocGet(data.map, key);
  223. }
  224. /**
  225. * Checks if a map value for `key` exists.
  226. *
  227. * @private
  228. * @name has
  229. * @memberOf MapCache
  230. * @param {string} key The key of the entry to check.
  231. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  232. */
  233. function mapHas(key) {
  234. var data = this.__data__;
  235. if (isKeyable(key)) {
  236. return hashHas(typeof key == 'string' ? data.string : data.hash, key);
  237. }
  238. return Map ? data.map.has(key) : assocHas(data.map, key);
  239. }
  240. /**
  241. * Sets the map `key` to `value`.
  242. *
  243. * @private
  244. * @name set
  245. * @memberOf MapCache
  246. * @param {string} key The key of the value to set.
  247. * @param {*} value The value to set.
  248. * @returns {Object} Returns the map cache instance.
  249. */
  250. function mapSet(key, value) {
  251. var data = this.__data__;
  252. if (isKeyable(key)) {
  253. hashSet(typeof key == 'string' ? data.string : data.hash, key, value);
  254. } else if (Map) {
  255. data.map.set(key, value);
  256. } else {
  257. assocSet(data.map, key, value);
  258. }
  259. return this;
  260. }
  261. // Add methods to `MapCache`.
  262. MapCache.prototype.clear = mapClear;
  263. MapCache.prototype['delete'] = mapDelete;
  264. MapCache.prototype.get = mapGet;
  265. MapCache.prototype.has = mapHas;
  266. MapCache.prototype.set = mapSet;
  267. /**
  268. * Creates a stack cache object to store key-value pairs.
  269. *
  270. * @private
  271. * @constructor
  272. * @param {Array} [values] The values to cache.
  273. */
  274. function Stack(values) {
  275. var index = -1,
  276. length = values ? values.length : 0;
  277. this.clear();
  278. while (++index < length) {
  279. var entry = values[index];
  280. this.set(entry[0], entry[1]);
  281. }
  282. }
  283. /**
  284. * Removes all key-value entries from the stack.
  285. *
  286. * @private
  287. * @name clear
  288. * @memberOf Stack
  289. */
  290. function stackClear() {
  291. this.__data__ = { 'array': [], 'map': null };
  292. }
  293. /**
  294. * Removes `key` and its value from the stack.
  295. *
  296. * @private
  297. * @name delete
  298. * @memberOf Stack
  299. * @param {string} key The key of the value to remove.
  300. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  301. */
  302. function stackDelete(key) {
  303. var data = this.__data__,
  304. array = data.array;
  305. return array ? assocDelete(array, key) : data.map['delete'](key);
  306. }
  307. /**
  308. * Gets the stack value for `key`.
  309. *
  310. * @private
  311. * @name get
  312. * @memberOf Stack
  313. * @param {string} key The key of the value to get.
  314. * @returns {*} Returns the entry value.
  315. */
  316. function stackGet(key) {
  317. var data = this.__data__,
  318. array = data.array;
  319. return array ? assocGet(array, key) : data.map.get(key);
  320. }
  321. /**
  322. * Checks if a stack value for `key` exists.
  323. *
  324. * @private
  325. * @name has
  326. * @memberOf Stack
  327. * @param {string} key The key of the entry to check.
  328. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  329. */
  330. function stackHas(key) {
  331. var data = this.__data__,
  332. array = data.array;
  333. return array ? assocHas(array, key) : data.map.has(key);
  334. }
  335. /**
  336. * Sets the stack `key` to `value`.
  337. *
  338. * @private
  339. * @name set
  340. * @memberOf Stack
  341. * @param {string} key The key of the value to set.
  342. * @param {*} value The value to set.
  343. * @returns {Object} Returns the stack cache instance.
  344. */
  345. function stackSet(key, value) {
  346. var data = this.__data__,
  347. array = data.array;
  348. if (array) {
  349. if (array.length < (LARGE_ARRAY_SIZE - 1)) {
  350. assocSet(array, key, value);
  351. } else {
  352. data.array = null;
  353. data.map = new MapCache(array);
  354. }
  355. }
  356. var map = data.map;
  357. if (map) {
  358. map.set(key, value);
  359. }
  360. return this;
  361. }
  362. // Add methods to `Stack`.
  363. Stack.prototype.clear = stackClear;
  364. Stack.prototype['delete'] = stackDelete;
  365. Stack.prototype.get = stackGet;
  366. Stack.prototype.has = stackHas;
  367. Stack.prototype.set = stackSet;
  368. /**
  369. * Removes `key` and its value from the associative array.
  370. *
  371. * @private
  372. * @param {Array} array The array to modify.
  373. * @param {string} key The key of the value to remove.
  374. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  375. */
  376. function assocDelete(array, key) {
  377. var index = assocIndexOf(array, key);
  378. if (index < 0) {
  379. return false;
  380. }
  381. var lastIndex = array.length - 1;
  382. if (index == lastIndex) {
  383. array.pop();
  384. } else {
  385. splice.call(array, index, 1);
  386. }
  387. return true;
  388. }
  389. /**
  390. * Gets the associative array value for `key`.
  391. *
  392. * @private
  393. * @param {Array} array The array to query.
  394. * @param {string} key The key of the value to get.
  395. * @returns {*} Returns the entry value.
  396. */
  397. function assocGet(array, key) {
  398. var index = assocIndexOf(array, key);
  399. return index < 0 ? undefined : array[index][1];
  400. }
  401. /**
  402. * Checks if an associative array value for `key` exists.
  403. *
  404. * @private
  405. * @param {Array} array The array to query.
  406. * @param {string} key The key of the entry to check.
  407. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  408. */
  409. function assocHas(array, key) {
  410. return assocIndexOf(array, key) > -1;
  411. }
  412. /**
  413. * Gets the index at which the `key` is found in `array` of key-value pairs.
  414. *
  415. * @private
  416. * @param {Array} array The array to search.
  417. * @param {*} key The key to search for.
  418. * @returns {number} Returns the index of the matched value, else `-1`.
  419. */
  420. function assocIndexOf(array, key) {
  421. var length = array.length;
  422. while (length--) {
  423. if (eq(array[length][0], key)) {
  424. return length;
  425. }
  426. }
  427. return -1;
  428. }
  429. /**
  430. * Sets the associative array `key` to `value`.
  431. *
  432. * @private
  433. * @param {Array} array The array to modify.
  434. * @param {string} key The key of the value to set.
  435. * @param {*} value The value to set.
  436. */
  437. function assocSet(array, key, value) {
  438. var index = assocIndexOf(array, key);
  439. if (index < 0) {
  440. array.push([key, value]);
  441. } else {
  442. array[index][1] = value;
  443. }
  444. }
  445. /**
  446. * Gets the native function at `key` of `object`.
  447. *
  448. * @private
  449. * @param {Object} object The object to query.
  450. * @param {string} key The key of the method to get.
  451. * @returns {*} Returns the function if it's native, else `undefined`.
  452. */
  453. function getNative(object, key) {
  454. var value = object[key];
  455. return isNative(value) ? value : undefined;
  456. }
  457. /**
  458. * Checks if `value` is suitable for use as unique object key.
  459. *
  460. * @private
  461. * @param {*} value The value to check.
  462. * @returns {boolean} Returns `true` if `value` is suitable, else `false`.
  463. */
  464. function isKeyable(value) {
  465. var type = typeof value;
  466. return type == 'number' || type == 'boolean' ||
  467. (type == 'string' && value != '__proto__') || value == null;
  468. }
  469. /**
  470. * Converts `func` to its source code.
  471. *
  472. * @private
  473. * @param {Function} func The function to process.
  474. * @returns {string} Returns the source code.
  475. */
  476. function toSource(func) {
  477. if (func != null) {
  478. try {
  479. return funcToString.call(func);
  480. } catch (e) {}
  481. try {
  482. return (func + '');
  483. } catch (e) {}
  484. }
  485. return '';
  486. }
  487. /**
  488. * Performs a
  489. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  490. * comparison between two values to determine if they are equivalent.
  491. *
  492. * @static
  493. * @memberOf _
  494. * @since 4.0.0
  495. * @category Lang
  496. * @param {*} value The value to compare.
  497. * @param {*} other The other value to compare.
  498. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  499. * @example
  500. *
  501. * var object = { 'user': 'fred' };
  502. * var other = { 'user': 'fred' };
  503. *
  504. * _.eq(object, object);
  505. * // => true
  506. *
  507. * _.eq(object, other);
  508. * // => false
  509. *
  510. * _.eq('a', 'a');
  511. * // => true
  512. *
  513. * _.eq('a', Object('a'));
  514. * // => false
  515. *
  516. * _.eq(NaN, NaN);
  517. * // => true
  518. */
  519. function eq(value, other) {
  520. return value === other || (value !== value && other !== other);
  521. }
  522. /**
  523. * Checks if `value` is classified as a `Function` object.
  524. *
  525. * @static
  526. * @memberOf _
  527. * @since 0.1.0
  528. * @category Lang
  529. * @param {*} value The value to check.
  530. * @returns {boolean} Returns `true` if `value` is correctly classified,
  531. * else `false`.
  532. * @example
  533. *
  534. * _.isFunction(_);
  535. * // => true
  536. *
  537. * _.isFunction(/abc/);
  538. * // => false
  539. */
  540. function isFunction(value) {
  541. // The use of `Object#toString` avoids issues with the `typeof` operator
  542. // in Safari 8 which returns 'object' for typed array and weak map constructors,
  543. // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  544. var tag = isObject(value) ? objectToString.call(value) : '';
  545. return tag == funcTag || tag == genTag;
  546. }
  547. /**
  548. * Checks if `value` is the
  549. * [language type](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
  550. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  551. *
  552. * @static
  553. * @memberOf _
  554. * @since 0.1.0
  555. * @category Lang
  556. * @param {*} value The value to check.
  557. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  558. * @example
  559. *
  560. * _.isObject({});
  561. * // => true
  562. *
  563. * _.isObject([1, 2, 3]);
  564. * // => true
  565. *
  566. * _.isObject(_.noop);
  567. * // => true
  568. *
  569. * _.isObject(null);
  570. * // => false
  571. */
  572. function isObject(value) {
  573. var type = typeof value;
  574. return !!value && (type == 'object' || type == 'function');
  575. }
  576. /**
  577. * Checks if `value` is a native function.
  578. *
  579. * @static
  580. * @memberOf _
  581. * @since 3.0.0
  582. * @category Lang
  583. * @param {*} value The value to check.
  584. * @returns {boolean} Returns `true` if `value` is a native function,
  585. * else `false`.
  586. * @example
  587. *
  588. * _.isNative(Array.prototype.push);
  589. * // => true
  590. *
  591. * _.isNative(_);
  592. * // => false
  593. */
  594. function isNative(value) {
  595. if (!isObject(value)) {
  596. return false;
  597. }
  598. var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
  599. return pattern.test(toSource(value));
  600. }
  601. module.exports = Stack;