You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16403 lines
520 KiB

8 years ago
  1. /**
  2. * @license
  3. * lodash <https://lodash.com/>
  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. ;(function() {
  10. /** Used as a safe reference for `undefined` in pre-ES5 environments. */
  11. var undefined;
  12. /** Used as the semantic version number. */
  13. var VERSION = '4.13.1';
  14. /** Used as the size to enable large array optimizations. */
  15. var LARGE_ARRAY_SIZE = 200;
  16. /** Used as the `TypeError` message for "Functions" methods. */
  17. var FUNC_ERROR_TEXT = 'Expected a function';
  18. /** Used to stand-in for `undefined` hash values. */
  19. var HASH_UNDEFINED = '__lodash_hash_undefined__';
  20. /** Used as the internal argument placeholder. */
  21. var PLACEHOLDER = '__lodash_placeholder__';
  22. /** Used to compose bitmasks for wrapper metadata. */
  23. var BIND_FLAG = 1,
  24. BIND_KEY_FLAG = 2,
  25. CURRY_BOUND_FLAG = 4,
  26. CURRY_FLAG = 8,
  27. CURRY_RIGHT_FLAG = 16,
  28. PARTIAL_FLAG = 32,
  29. PARTIAL_RIGHT_FLAG = 64,
  30. ARY_FLAG = 128,
  31. REARG_FLAG = 256,
  32. FLIP_FLAG = 512;
  33. /** Used to compose bitmasks for comparison styles. */
  34. var UNORDERED_COMPARE_FLAG = 1,
  35. PARTIAL_COMPARE_FLAG = 2;
  36. /** Used as default options for `_.truncate`. */
  37. var DEFAULT_TRUNC_LENGTH = 30,
  38. DEFAULT_TRUNC_OMISSION = '...';
  39. /** Used to detect hot functions by number of calls within a span of milliseconds. */
  40. var HOT_COUNT = 150,
  41. HOT_SPAN = 16;
  42. /** Used to indicate the type of lazy iteratees. */
  43. var LAZY_FILTER_FLAG = 1,
  44. LAZY_MAP_FLAG = 2,
  45. LAZY_WHILE_FLAG = 3;
  46. /** Used as references for various `Number` constants. */
  47. var INFINITY = 1 / 0,
  48. MAX_SAFE_INTEGER = 9007199254740991,
  49. MAX_INTEGER = 1.7976931348623157e+308,
  50. NAN = 0 / 0;
  51. /** Used as references for the maximum length and index of an array. */
  52. var MAX_ARRAY_LENGTH = 4294967295,
  53. MAX_ARRAY_INDEX = MAX_ARRAY_LENGTH - 1,
  54. HALF_MAX_ARRAY_LENGTH = MAX_ARRAY_LENGTH >>> 1;
  55. /** `Object#toString` result references. */
  56. var argsTag = '[object Arguments]',
  57. arrayTag = '[object Array]',
  58. boolTag = '[object Boolean]',
  59. dateTag = '[object Date]',
  60. errorTag = '[object Error]',
  61. funcTag = '[object Function]',
  62. genTag = '[object GeneratorFunction]',
  63. mapTag = '[object Map]',
  64. numberTag = '[object Number]',
  65. objectTag = '[object Object]',
  66. promiseTag = '[object Promise]',
  67. regexpTag = '[object RegExp]',
  68. setTag = '[object Set]',
  69. stringTag = '[object String]',
  70. symbolTag = '[object Symbol]',
  71. weakMapTag = '[object WeakMap]',
  72. weakSetTag = '[object WeakSet]';
  73. var arrayBufferTag = '[object ArrayBuffer]',
  74. dataViewTag = '[object DataView]',
  75. float32Tag = '[object Float32Array]',
  76. float64Tag = '[object Float64Array]',
  77. int8Tag = '[object Int8Array]',
  78. int16Tag = '[object Int16Array]',
  79. int32Tag = '[object Int32Array]',
  80. uint8Tag = '[object Uint8Array]',
  81. uint8ClampedTag = '[object Uint8ClampedArray]',
  82. uint16Tag = '[object Uint16Array]',
  83. uint32Tag = '[object Uint32Array]';
  84. /** Used to match empty string literals in compiled template source. */
  85. var reEmptyStringLeading = /\b__p \+= '';/g,
  86. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  87. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  88. /** Used to match HTML entities and HTML characters. */
  89. var reEscapedHtml = /&(?:amp|lt|gt|quot|#39|#96);/g,
  90. reUnescapedHtml = /[&<>"'`]/g,
  91. reHasEscapedHtml = RegExp(reEscapedHtml.source),
  92. reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
  93. /** Used to match template delimiters. */
  94. var reEscape = /<%-([\s\S]+?)%>/g,
  95. reEvaluate = /<%([\s\S]+?)%>/g,
  96. reInterpolate = /<%=([\s\S]+?)%>/g;
  97. /** Used to match property names within property paths. */
  98. var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
  99. reIsPlainProp = /^\w*$/,
  100. rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(\.|\[\])(?:\4|$))/g;
  101. /**
  102. * Used to match `RegExp`
  103. * [syntax characters](http://ecma-international.org/ecma-262/6.0/#sec-patterns).
  104. */
  105. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
  106. reHasRegExpChar = RegExp(reRegExpChar.source);
  107. /** Used to match leading and trailing whitespace. */
  108. var reTrim = /^\s+|\s+$/g,
  109. reTrimStart = /^\s+/,
  110. reTrimEnd = /\s+$/;
  111. /** Used to match non-compound words composed of alphanumeric characters. */
  112. var reBasicWord = /[a-zA-Z0-9]+/g;
  113. /** Used to match backslashes in property paths. */
  114. var reEscapeChar = /\\(\\)?/g;
  115. /**
  116. * Used to match
  117. * [ES template delimiters](http://ecma-international.org/ecma-262/6.0/#sec-template-literal-lexical-components).
  118. */
  119. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  120. /** Used to match `RegExp` flags from their coerced string values. */
  121. var reFlags = /\w*$/;
  122. /** Used to detect hexadecimal string values. */
  123. var reHasHexPrefix = /^0x/i;
  124. /** Used to detect bad signed hexadecimal string values. */
  125. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  126. /** Used to detect binary string values. */
  127. var reIsBinary = /^0b[01]+$/i;
  128. /** Used to detect host constructors (Safari). */
  129. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  130. /** Used to detect octal string values. */
  131. var reIsOctal = /^0o[0-7]+$/i;
  132. /** Used to detect unsigned integer values. */
  133. var reIsUint = /^(?:0|[1-9]\d*)$/;
  134. /** Used to match latin-1 supplementary letters (excluding mathematical operators). */
  135. var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g;
  136. /** Used to ensure capturing order of template delimiters. */
  137. var reNoMatch = /($^)/;
  138. /** Used to match unescaped characters in compiled string literals. */
  139. var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
  140. /** Used to compose unicode character classes. */
  141. var rsAstralRange = '\\ud800-\\udfff',
  142. rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
  143. rsComboSymbolsRange = '\\u20d0-\\u20f0',
  144. rsDingbatRange = '\\u2700-\\u27bf',
  145. rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff',
  146. rsMathOpRange = '\\xac\\xb1\\xd7\\xf7',
  147. rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf',
  148. rsPunctuationRange = '\\u2000-\\u206f',
  149. rsSpaceRange = ' \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000',
  150. rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde',
  151. rsVarRange = '\\ufe0e\\ufe0f',
  152. rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange;
  153. /** Used to compose unicode capture groups. */
  154. var rsApos = "['\u2019]",
  155. rsAstral = '[' + rsAstralRange + ']',
  156. rsBreak = '[' + rsBreakRange + ']',
  157. rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
  158. rsDigits = '\\d+',
  159. rsDingbat = '[' + rsDingbatRange + ']',
  160. rsLower = '[' + rsLowerRange + ']',
  161. rsMisc = '[^' + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + ']',
  162. rsFitz = '\\ud83c[\\udffb-\\udfff]',
  163. rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
  164. rsNonAstral = '[^' + rsAstralRange + ']',
  165. rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
  166. rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
  167. rsUpper = '[' + rsUpperRange + ']',
  168. rsZWJ = '\\u200d';
  169. /** Used to compose unicode regexes. */
  170. var rsLowerMisc = '(?:' + rsLower + '|' + rsMisc + ')',
  171. rsUpperMisc = '(?:' + rsUpper + '|' + rsMisc + ')',
  172. rsOptLowerContr = '(?:' + rsApos + '(?:d|ll|m|re|s|t|ve))?',
  173. rsOptUpperContr = '(?:' + rsApos + '(?:D|LL|M|RE|S|T|VE))?',
  174. reOptMod = rsModifier + '?',
  175. rsOptVar = '[' + rsVarRange + ']?',
  176. rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
  177. rsSeq = rsOptVar + reOptMod + rsOptJoin,
  178. rsEmoji = '(?:' + [rsDingbat, rsRegional, rsSurrPair].join('|') + ')' + rsSeq,
  179. rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
  180. /** Used to match apostrophes. */
  181. var reApos = RegExp(rsApos, 'g');
  182. /**
  183. * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) and
  184. * [combining diacritical marks for symbols](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols).
  185. */
  186. var reComboMark = RegExp(rsCombo, 'g');
  187. /** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
  188. var reComplexSymbol = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
  189. /** Used to match complex or compound words. */
  190. var reComplexWord = RegExp([
  191. rsUpper + '?' + rsLower + '+' + rsOptLowerContr + '(?=' + [rsBreak, rsUpper, '$'].join('|') + ')',
  192. rsUpperMisc + '+' + rsOptUpperContr + '(?=' + [rsBreak, rsUpper + rsLowerMisc, '$'].join('|') + ')',
  193. rsUpper + '?' + rsLowerMisc + '+' + rsOptLowerContr,
  194. rsUpper + '+' + rsOptUpperContr,
  195. rsDigits,
  196. rsEmoji
  197. ].join('|'), 'g');
  198. /** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
  199. var reHasComplexSymbol = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
  200. /** Used to detect strings that need a more robust regexp to match words. */
  201. var reHasComplexWord = /[a-z][A-Z]|[A-Z]{2,}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
  202. /** Used to assign default `context` object properties. */
  203. var contextProps = [
  204. 'Array', 'Buffer', 'DataView', 'Date', 'Error', 'Float32Array', 'Float64Array',
  205. 'Function', 'Int8Array', 'Int16Array', 'Int32Array', 'Map', 'Math', 'Object',
  206. 'Promise', 'Reflect', 'RegExp', 'Set', 'String', 'Symbol', 'TypeError',
  207. 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'WeakMap',
  208. '_', 'isFinite', 'parseInt', 'setTimeout'
  209. ];
  210. /** Used to make template sourceURLs easier to identify. */
  211. var templateCounter = -1;
  212. /** Used to identify `toStringTag` values of typed arrays. */
  213. var typedArrayTags = {};
  214. typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
  215. typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
  216. typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
  217. typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
  218. typedArrayTags[uint32Tag] = true;
  219. typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
  220. typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
  221. typedArrayTags[dataViewTag] = typedArrayTags[dateTag] =
  222. typedArrayTags[errorTag] = typedArrayTags[funcTag] =
  223. typedArrayTags[mapTag] = typedArrayTags[numberTag] =
  224. typedArrayTags[objectTag] = typedArrayTags[regexpTag] =
  225. typedArrayTags[setTag] = typedArrayTags[stringTag] =
  226. typedArrayTags[weakMapTag] = false;
  227. /** Used to identify `toStringTag` values supported by `_.clone`. */
  228. var cloneableTags = {};
  229. cloneableTags[argsTag] = cloneableTags[arrayTag] =
  230. cloneableTags[arrayBufferTag] = cloneableTags[dataViewTag] =
  231. cloneableTags[boolTag] = cloneableTags[dateTag] =
  232. cloneableTags[float32Tag] = cloneableTags[float64Tag] =
  233. cloneableTags[int8Tag] = cloneableTags[int16Tag] =
  234. cloneableTags[int32Tag] = cloneableTags[mapTag] =
  235. cloneableTags[numberTag] = cloneableTags[objectTag] =
  236. cloneableTags[regexpTag] = cloneableTags[setTag] =
  237. cloneableTags[stringTag] = cloneableTags[symbolTag] =
  238. cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
  239. cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
  240. cloneableTags[errorTag] = cloneableTags[funcTag] =
  241. cloneableTags[weakMapTag] = false;
  242. /** Used to map latin-1 supplementary letters to basic latin letters. */
  243. var deburredLetters = {
  244. '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A',
  245. '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a',
  246. '\xc7': 'C', '\xe7': 'c',
  247. '\xd0': 'D', '\xf0': 'd',
  248. '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E',
  249. '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e',
  250. '\xcC': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I',
  251. '\xeC': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i',
  252. '\xd1': 'N', '\xf1': 'n',
  253. '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O',
  254. '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o',
  255. '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U',
  256. '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u',
  257. '\xdd': 'Y', '\xfd': 'y', '\xff': 'y',
  258. '\xc6': 'Ae', '\xe6': 'ae',
  259. '\xde': 'Th', '\xfe': 'th',
  260. '\xdf': 'ss'
  261. };
  262. /** Used to map characters to HTML entities. */
  263. var htmlEscapes = {
  264. '&': '&amp;',
  265. '<': '&lt;',
  266. '>': '&gt;',
  267. '"': '&quot;',
  268. "'": '&#39;',
  269. '`': '&#96;'
  270. };
  271. /** Used to map HTML entities to characters. */
  272. var htmlUnescapes = {
  273. '&amp;': '&',
  274. '&lt;': '<',
  275. '&gt;': '>',
  276. '&quot;': '"',
  277. '&#39;': "'",
  278. '&#96;': '`'
  279. };
  280. /** Used to escape characters for inclusion in compiled string literals. */
  281. var stringEscapes = {
  282. '\\': '\\',
  283. "'": "'",
  284. '\n': 'n',
  285. '\r': 'r',
  286. '\u2028': 'u2028',
  287. '\u2029': 'u2029'
  288. };
  289. /** Built-in method references without a dependency on `root`. */
  290. var freeParseFloat = parseFloat,
  291. freeParseInt = parseInt;
  292. /** Detect free variable `exports`. */
  293. var freeExports = typeof exports == 'object' && exports;
  294. /** Detect free variable `module`. */
  295. var freeModule = freeExports && typeof module == 'object' && module;
  296. /** Detect the popular CommonJS extension `module.exports`. */
  297. var moduleExports = freeModule && freeModule.exports === freeExports;
  298. /** Detect free variable `global` from Node.js. */
  299. var freeGlobal = checkGlobal(typeof global == 'object' && global);
  300. /** Detect free variable `self`. */
  301. var freeSelf = checkGlobal(typeof self == 'object' && self);
  302. /** Detect `this` as the global object. */
  303. var thisGlobal = checkGlobal(typeof this == 'object' && this);
  304. /** Used as a reference to the global object. */
  305. var root = freeGlobal || freeSelf || thisGlobal || Function('return this')();
  306. /*--------------------------------------------------------------------------*/
  307. /**
  308. * Adds the key-value `pair` to `map`.
  309. *
  310. * @private
  311. * @param {Object} map The map to modify.
  312. * @param {Array} pair The key-value pair to add.
  313. * @returns {Object} Returns `map`.
  314. */
  315. function addMapEntry(map, pair) {
  316. // Don't return `Map#set` because it doesn't return the map instance in IE 11.
  317. map.set(pair[0], pair[1]);
  318. return map;
  319. }
  320. /**
  321. * Adds `value` to `set`.
  322. *
  323. * @private
  324. * @param {Object} set The set to modify.
  325. * @param {*} value The value to add.
  326. * @returns {Object} Returns `set`.
  327. */
  328. function addSetEntry(set, value) {
  329. set.add(value);
  330. return set;
  331. }
  332. /**
  333. * A faster alternative to `Function#apply`, this function invokes `func`
  334. * with the `this` binding of `thisArg` and the arguments of `args`.
  335. *
  336. * @private
  337. * @param {Function} func The function to invoke.
  338. * @param {*} thisArg The `this` binding of `func`.
  339. * @param {Array} args The arguments to invoke `func` with.
  340. * @returns {*} Returns the result of `func`.
  341. */
  342. function apply(func, thisArg, args) {
  343. var length = args.length;
  344. switch (length) {
  345. case 0: return func.call(thisArg);
  346. case 1: return func.call(thisArg, args[0]);
  347. case 2: return func.call(thisArg, args[0], args[1]);
  348. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  349. }
  350. return func.apply(thisArg, args);
  351. }
  352. /**
  353. * A specialized version of `baseAggregator` for arrays.
  354. *
  355. * @private
  356. * @param {Array} [array] The array to iterate over.
  357. * @param {Function} setter The function to set `accumulator` values.
  358. * @param {Function} iteratee The iteratee to transform keys.
  359. * @param {Object} accumulator The initial aggregated object.
  360. * @returns {Function} Returns `accumulator`.
  361. */
  362. function arrayAggregator(array, setter, iteratee, accumulator) {
  363. var index = -1,
  364. length = array ? array.length : 0;
  365. while (++index < length) {
  366. var value = array[index];
  367. setter(accumulator, value, iteratee(value), array);
  368. }
  369. return accumulator;
  370. }
  371. /**
  372. * A specialized version of `_.forEach` for arrays without support for
  373. * iteratee shorthands.
  374. *
  375. * @private
  376. * @param {Array} [array] The array to iterate over.
  377. * @param {Function} iteratee The function invoked per iteration.
  378. * @returns {Array} Returns `array`.
  379. */
  380. function arrayEach(array, iteratee) {
  381. var index = -1,
  382. length = array ? array.length : 0;
  383. while (++index < length) {
  384. if (iteratee(array[index], index, array) === false) {
  385. break;
  386. }
  387. }
  388. return array;
  389. }
  390. /**
  391. * A specialized version of `_.forEachRight` for arrays without support for
  392. * iteratee shorthands.
  393. *
  394. * @private
  395. * @param {Array} [array] The array to iterate over.
  396. * @param {Function} iteratee The function invoked per iteration.
  397. * @returns {Array} Returns `array`.
  398. */
  399. function arrayEachRight(array, iteratee) {
  400. var length = array ? array.length : 0;
  401. while (length--) {
  402. if (iteratee(array[length], length, array) === false) {
  403. break;
  404. }
  405. }
  406. return array;
  407. }
  408. /**
  409. * A specialized version of `_.every` for arrays without support for
  410. * iteratee shorthands.
  411. *
  412. * @private
  413. * @param {Array} [array] The array to iterate over.
  414. * @param {Function} predicate The function invoked per iteration.
  415. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  416. * else `false`.
  417. */
  418. function arrayEvery(array, predicate) {
  419. var index = -1,
  420. length = array ? array.length : 0;
  421. while (++index < length) {
  422. if (!predicate(array[index], index, array)) {
  423. return false;
  424. }
  425. }
  426. return true;
  427. }
  428. /**
  429. * A specialized version of `_.filter` for arrays without support for
  430. * iteratee shorthands.
  431. *
  432. * @private
  433. * @param {Array} [array] The array to iterate over.
  434. * @param {Function} predicate The function invoked per iteration.
  435. * @returns {Array} Returns the new filtered array.
  436. */
  437. function arrayFilter(array, predicate) {
  438. var index = -1,
  439. length = array ? array.length : 0,
  440. resIndex = 0,
  441. result = [];
  442. while (++index < length) {
  443. var value = array[index];
  444. if (predicate(value, index, array)) {
  445. result[resIndex++] = value;
  446. }
  447. }
  448. return result;
  449. }
  450. /**
  451. * A specialized version of `_.includes` for arrays without support for
  452. * specifying an index to search from.
  453. *
  454. * @private
  455. * @param {Array} [array] The array to search.
  456. * @param {*} target The value to search for.
  457. * @returns {boolean} Returns `true` if `target` is found, else `false`.
  458. */
  459. function arrayIncludes(array, value) {
  460. var length = array ? array.length : 0;
  461. return !!length && baseIndexOf(array, value, 0) > -1;
  462. }
  463. /**
  464. * This function is like `arrayIncludes` except that it accepts a comparator.
  465. *
  466. * @private
  467. * @param {Array} [array] The array to search.
  468. * @param {*} target The value to search for.
  469. * @param {Function} comparator The comparator invoked per element.
  470. * @returns {boolean} Returns `true` if `target` is found, else `false`.
  471. */
  472. function arrayIncludesWith(array, value, comparator) {
  473. var index = -1,
  474. length = array ? array.length : 0;
  475. while (++index < length) {
  476. if (comparator(value, array[index])) {
  477. return true;
  478. }
  479. }
  480. return false;
  481. }
  482. /**
  483. * A specialized version of `_.map` for arrays without support for iteratee
  484. * shorthands.
  485. *
  486. * @private
  487. * @param {Array} [array] The array to iterate over.
  488. * @param {Function} iteratee The function invoked per iteration.
  489. * @returns {Array} Returns the new mapped array.
  490. */
  491. function arrayMap(array, iteratee) {
  492. var index = -1,
  493. length = array ? array.length : 0,
  494. result = Array(length);
  495. while (++index < length) {
  496. result[index] = iteratee(array[index], index, array);
  497. }
  498. return result;
  499. }
  500. /**
  501. * Appends the elements of `values` to `array`.
  502. *
  503. * @private
  504. * @param {Array} array The array to modify.
  505. * @param {Array} values The values to append.
  506. * @returns {Array} Returns `array`.
  507. */
  508. function arrayPush(array, values) {
  509. var index = -1,
  510. length = values.length,
  511. offset = array.length;
  512. while (++index < length) {
  513. array[offset + index] = values[index];
  514. }
  515. return array;
  516. }
  517. /**
  518. * A specialized version of `_.reduce` for arrays without support for
  519. * iteratee shorthands.
  520. *
  521. * @private
  522. * @param {Array} [array] The array to iterate over.
  523. * @param {Function} iteratee The function invoked per iteration.
  524. * @param {*} [accumulator] The initial value.
  525. * @param {boolean} [initAccum] Specify using the first element of `array` as
  526. * the initial value.
  527. * @returns {*} Returns the accumulated value.
  528. */
  529. function arrayReduce(array, iteratee, accumulator, initAccum) {
  530. var index = -1,
  531. length = array ? array.length : 0;
  532. if (initAccum && length) {
  533. accumulator = array[++index];
  534. }
  535. while (++index < length) {
  536. accumulator = iteratee(accumulator, array[index], index, array);
  537. }
  538. return accumulator;
  539. }
  540. /**
  541. * A specialized version of `_.reduceRight` for arrays without support for
  542. * iteratee shorthands.
  543. *
  544. * @private
  545. * @param {Array} [array] The array to iterate over.
  546. * @param {Function} iteratee The function invoked per iteration.
  547. * @param {*} [accumulator] The initial value.
  548. * @param {boolean} [initAccum] Specify using the last element of `array` as
  549. * the initial value.
  550. * @returns {*} Returns the accumulated value.
  551. */
  552. function arrayReduceRight(array, iteratee, accumulator, initAccum) {
  553. var length = array ? array.length : 0;
  554. if (initAccum && length) {
  555. accumulator = array[--length];
  556. }
  557. while (length--) {
  558. accumulator = iteratee(accumulator, array[length], length, array);
  559. }
  560. return accumulator;
  561. }
  562. /**
  563. * A specialized version of `_.some` for arrays without support for iteratee
  564. * shorthands.
  565. *
  566. * @private
  567. * @param {Array} [array] The array to iterate over.
  568. * @param {Function} predicate The function invoked per iteration.
  569. * @returns {boolean} Returns `true` if any element passes the predicate check,
  570. * else `false`.
  571. */
  572. function arraySome(array, predicate) {
  573. var index = -1,
  574. length = array ? array.length : 0;
  575. while (++index < length) {
  576. if (predicate(array[index], index, array)) {
  577. return true;
  578. }
  579. }
  580. return false;
  581. }
  582. /**
  583. * The base implementation of methods like `_.findKey` and `_.findLastKey`,
  584. * without support for iteratee shorthands, which iterates over `collection`
  585. * using `eachFunc`.
  586. *
  587. * @private
  588. * @param {Array|Object} collection The collection to search.
  589. * @param {Function} predicate The function invoked per iteration.
  590. * @param {Function} eachFunc The function to iterate over `collection`.
  591. * @returns {*} Returns the found element or its key, else `undefined`.
  592. */
  593. function baseFindKey(collection, predicate, eachFunc) {
  594. var result;
  595. eachFunc(collection, function(value, key, collection) {
  596. if (predicate(value, key, collection)) {
  597. result = key;
  598. return false;
  599. }
  600. });
  601. return result;
  602. }
  603. /**
  604. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  605. * support for iteratee shorthands.
  606. *
  607. * @private
  608. * @param {Array} array The array to search.
  609. * @param {Function} predicate The function invoked per iteration.
  610. * @param {number} fromIndex The index to search from.
  611. * @param {boolean} [fromRight] Specify iterating from right to left.
  612. * @returns {number} Returns the index of the matched value, else `-1`.
  613. */
  614. function baseFindIndex(array, predicate, fromIndex, fromRight) {
  615. var length = array.length,
  616. index = fromIndex + (fromRight ? 1 : -1);
  617. while ((fromRight ? index-- : ++index < length)) {
  618. if (predicate(array[index], index, array)) {
  619. return index;
  620. }
  621. }
  622. return -1;
  623. }
  624. /**
  625. * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
  626. *
  627. * @private
  628. * @param {Array} array The array to search.
  629. * @param {*} value The value to search for.
  630. * @param {number} fromIndex The index to search from.
  631. * @returns {number} Returns the index of the matched value, else `-1`.
  632. */
  633. function baseIndexOf(array, value, fromIndex) {
  634. if (value !== value) {
  635. return indexOfNaN(array, fromIndex);
  636. }
  637. var index = fromIndex - 1,
  638. length = array.length;
  639. while (++index < length) {
  640. if (array[index] === value) {
  641. return index;
  642. }
  643. }
  644. return -1;
  645. }
  646. /**
  647. * This function is like `baseIndexOf` except that it accepts a comparator.
  648. *
  649. * @private
  650. * @param {Array} array The array to search.
  651. * @param {*} value The value to search for.
  652. * @param {number} fromIndex The index to search from.
  653. * @param {Function} comparator The comparator invoked per element.
  654. * @returns {number} Returns the index of the matched value, else `-1`.
  655. */
  656. function baseIndexOfWith(array, value, fromIndex, comparator) {
  657. var index = fromIndex - 1,
  658. length = array.length;
  659. while (++index < length) {
  660. if (comparator(array[index], value)) {
  661. return index;
  662. }
  663. }
  664. return -1;
  665. }
  666. /**
  667. * The base implementation of `_.mean` and `_.meanBy` without support for
  668. * iteratee shorthands.
  669. *
  670. * @private
  671. * @param {Array} array The array to iterate over.
  672. * @param {Function} iteratee The function invoked per iteration.
  673. * @returns {number} Returns the mean.
  674. */
  675. function baseMean(array, iteratee) {
  676. var length = array ? array.length : 0;
  677. return length ? (baseSum(array, iteratee) / length) : NAN;
  678. }
  679. /**
  680. * The base implementation of `_.reduce` and `_.reduceRight`, without support
  681. * for iteratee shorthands, which iterates over `collection` using `eachFunc`.
  682. *
  683. * @private
  684. * @param {Array|Object} collection The collection to iterate over.
  685. * @param {Function} iteratee The function invoked per iteration.
  686. * @param {*} accumulator The initial value.
  687. * @param {boolean} initAccum Specify using the first or last element of
  688. * `collection` as the initial value.
  689. * @param {Function} eachFunc The function to iterate over `collection`.
  690. * @returns {*} Returns the accumulated value.
  691. */
  692. function baseReduce(collection, iteratee, accumulator, initAccum, eachFunc) {
  693. eachFunc(collection, function(value, index, collection) {
  694. accumulator = initAccum
  695. ? (initAccum = false, value)
  696. : iteratee(accumulator, value, index, collection);
  697. });
  698. return accumulator;
  699. }
  700. /**
  701. * The base implementation of `_.sortBy` which uses `comparer` to define the
  702. * sort order of `array` and replaces criteria objects with their corresponding
  703. * values.
  704. *
  705. * @private
  706. * @param {Array} array The array to sort.
  707. * @param {Function} comparer The function to define sort order.
  708. * @returns {Array} Returns `array`.
  709. */
  710. function baseSortBy(array, comparer) {
  711. var length = array.length;
  712. array.sort(comparer);
  713. while (length--) {
  714. array[length] = array[length].value;
  715. }
  716. return array;
  717. }
  718. /**
  719. * The base implementation of `_.sum` and `_.sumBy` without support for
  720. * iteratee shorthands.
  721. *
  722. * @private
  723. * @param {Array} array The array to iterate over.
  724. * @param {Function} iteratee The function invoked per iteration.
  725. * @returns {number} Returns the sum.
  726. */
  727. function baseSum(array, iteratee) {
  728. var result,
  729. index = -1,
  730. length = array.length;
  731. while (++index < length) {
  732. var current = iteratee(array[index]);
  733. if (current !== undefined) {
  734. result = result === undefined ? current : (result + current);
  735. }
  736. }
  737. return result;
  738. }
  739. /**
  740. * The base implementation of `_.times` without support for iteratee shorthands
  741. * or max array length checks.
  742. *
  743. * @private
  744. * @param {number} n The number of times to invoke `iteratee`.
  745. * @param {Function} iteratee The function invoked per iteration.
  746. * @returns {Array} Returns the array of results.
  747. */
  748. function baseTimes(n, iteratee) {
  749. var index = -1,
  750. result = Array(n);
  751. while (++index < n) {
  752. result[index] = iteratee(index);
  753. }
  754. return result;
  755. }
  756. /**
  757. * The base implementation of `_.toPairs` and `_.toPairsIn` which creates an array
  758. * of key-value pairs for `object` corresponding to the property names of `props`.
  759. *
  760. * @private
  761. * @param {Object} object The object to query.
  762. * @param {Array} props The property names to get values for.
  763. * @returns {Object} Returns the key-value pairs.
  764. */
  765. function baseToPairs(object, props) {
  766. return arrayMap(props, function(key) {
  767. return [key, object[key]];
  768. });
  769. }
  770. /**
  771. * The base implementation of `_.unary` without support for storing wrapper metadata.
  772. *
  773. * @private
  774. * @param {Function} func The function to cap arguments for.
  775. * @returns {Function} Returns the new capped function.
  776. */
  777. function baseUnary(func) {
  778. return function(value) {
  779. return func(value);
  780. };
  781. }
  782. /**
  783. * The base implementation of `_.values` and `_.valuesIn` which creates an
  784. * array of `object` property values corresponding to the property names
  785. * of `props`.
  786. *
  787. * @private
  788. * @param {Object} object The object to query.
  789. * @param {Array} props The property names to get values for.
  790. * @returns {Object} Returns the array of property values.
  791. */
  792. function baseValues(object, props) {
  793. return arrayMap(props, function(key) {
  794. return object[key];
  795. });
  796. }
  797. /**
  798. * Checks if a cache value for `key` exists.
  799. *
  800. * @private
  801. * @param {Object} cache The cache to query.
  802. * @param {string} key The key of the entry to check.
  803. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  804. */
  805. function cacheHas(cache, key) {
  806. return cache.has(key);
  807. }
  808. /**
  809. * Used by `_.trim` and `_.trimStart` to get the index of the first string symbol
  810. * that is not found in the character symbols.
  811. *
  812. * @private
  813. * @param {Array} strSymbols The string symbols to inspect.
  814. * @param {Array} chrSymbols The character symbols to find.
  815. * @returns {number} Returns the index of the first unmatched string symbol.
  816. */
  817. function charsStartIndex(strSymbols, chrSymbols) {
  818. var index = -1,
  819. length = strSymbols.length;
  820. while (++index < length && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
  821. return index;
  822. }
  823. /**
  824. * Used by `_.trim` and `_.trimEnd` to get the index of the last string symbol
  825. * that is not found in the character symbols.
  826. *
  827. * @private
  828. * @param {Array} strSymbols The string symbols to inspect.
  829. * @param {Array} chrSymbols The character symbols to find.
  830. * @returns {number} Returns the index of the last unmatched string symbol.
  831. */
  832. function charsEndIndex(strSymbols, chrSymbols) {
  833. var index = strSymbols.length;
  834. while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
  835. return index;
  836. }
  837. /**
  838. * Checks if `value` is a global object.
  839. *
  840. * @private
  841. * @param {*} value The value to check.
  842. * @returns {null|Object} Returns `value` if it's a global object, else `null`.
  843. */
  844. function checkGlobal(value) {
  845. return (value && value.Object === Object) ? value : null;
  846. }
  847. /**
  848. * Gets the number of `placeholder` occurrences in `array`.
  849. *
  850. * @private
  851. * @param {Array} array The array to inspect.
  852. * @param {*} placeholder The placeholder to search for.
  853. * @returns {number} Returns the placeholder count.
  854. */
  855. function countHolders(array, placeholder) {
  856. var length = array.length,
  857. result = 0;
  858. while (length--) {
  859. if (array[length] === placeholder) {
  860. result++;
  861. }
  862. }
  863. return result;
  864. }
  865. /**
  866. * Used by `_.deburr` to convert latin-1 supplementary letters to basic latin letters.
  867. *
  868. * @private
  869. * @param {string} letter The matched letter to deburr.
  870. * @returns {string} Returns the deburred letter.
  871. */
  872. function deburrLetter(letter) {
  873. return deburredLetters[letter];
  874. }
  875. /**
  876. * Used by `_.escape` to convert characters to HTML entities.
  877. *
  878. * @private
  879. * @param {string} chr The matched character to escape.
  880. * @returns {string} Returns the escaped character.
  881. */
  882. function escapeHtmlChar(chr) {
  883. return htmlEscapes[chr];
  884. }
  885. /**
  886. * Used by `_.template` to escape characters for inclusion in compiled string literals.
  887. *
  888. * @private
  889. * @param {string} chr The matched character to escape.
  890. * @returns {string} Returns the escaped character.
  891. */
  892. function escapeStringChar(chr) {
  893. return '\\' + stringEscapes[chr];
  894. }
  895. /**
  896. * Gets the value at `key` of `object`.
  897. *
  898. * @private
  899. * @param {Object} [object] The object to query.
  900. * @param {string} key The key of the property to get.
  901. * @returns {*} Returns the property value.
  902. */
  903. function getValue(object, key) {
  904. return object == null ? undefined : object[key];
  905. }
  906. /**
  907. * Gets the index at which the first occurrence of `NaN` is found in `array`.
  908. *
  909. * @private
  910. * @param {Array} array The array to search.
  911. * @param {number} fromIndex The index to search from.
  912. * @param {boolean} [fromRight] Specify iterating from right to left.
  913. * @returns {number} Returns the index of the matched `NaN`, else `-1`.
  914. */
  915. function indexOfNaN(array, fromIndex, fromRight) {
  916. var length = array.length,
  917. index = fromIndex + (fromRight ? 1 : -1);
  918. while ((fromRight ? index-- : ++index < length)) {
  919. var other = array[index];
  920. if (other !== other) {
  921. return index;
  922. }
  923. }
  924. return -1;
  925. }
  926. /**
  927. * Checks if `value` is a host object in IE < 9.
  928. *
  929. * @private
  930. * @param {*} value The value to check.
  931. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  932. */
  933. function isHostObject(value) {
  934. // Many host objects are `Object` objects that can coerce to strings
  935. // despite having improperly defined `toString` methods.
  936. var result = false;
  937. if (value != null && typeof value.toString != 'function') {
  938. try {
  939. result = !!(value + '');
  940. } catch (e) {}
  941. }
  942. return result;
  943. }
  944. /**
  945. * Converts `iterator` to an array.
  946. *
  947. * @private
  948. * @param {Object} iterator The iterator to convert.
  949. * @returns {Array} Returns the converted array.
  950. */
  951. function iteratorToArray(iterator) {
  952. var data,
  953. result = [];
  954. while (!(data = iterator.next()).done) {
  955. result.push(data.value);
  956. }
  957. return result;
  958. }
  959. /**
  960. * Converts `map` to its key-value pairs.
  961. *
  962. * @private
  963. * @param {Object} map The map to convert.
  964. * @returns {Array} Returns the key-value pairs.
  965. */
  966. function mapToArray(map) {
  967. var index = -1,
  968. result = Array(map.size);
  969. map.forEach(function(value, key) {
  970. result[++index] = [key, value];
  971. });
  972. return result;
  973. }
  974. /**
  975. * Replaces all `placeholder` elements in `array` with an internal placeholder
  976. * and returns an array of their indexes.
  977. *
  978. * @private
  979. * @param {Array} array The array to modify.
  980. * @param {*} placeholder The placeholder to replace.
  981. * @returns {Array} Returns the new array of placeholder indexes.
  982. */
  983. function replaceHolders(array, placeholder) {
  984. var index = -1,
  985. length = array.length,
  986. resIndex = 0,
  987. result = [];
  988. while (++index < length) {
  989. var value = array[index];
  990. if (value === placeholder || value === PLACEHOLDER) {
  991. array[index] = PLACEHOLDER;
  992. result[resIndex++] = index;
  993. }
  994. }
  995. return result;
  996. }
  997. /**
  998. * Converts `set` to an array of its values.
  999. *
  1000. * @private
  1001. * @param {Object} set The set to convert.
  1002. * @returns {Array} Returns the values.
  1003. */
  1004. function setToArray(set) {
  1005. var index = -1,
  1006. result = Array(set.size);
  1007. set.forEach(function(value) {
  1008. result[++index] = value;
  1009. });
  1010. return result;
  1011. }
  1012. /**
  1013. * Converts `set` to its value-value pairs.
  1014. *
  1015. * @private
  1016. * @param {Object} set The set to convert.
  1017. * @returns {Array} Returns the value-value pairs.
  1018. */
  1019. function setToPairs(set) {
  1020. var index = -1,
  1021. result = Array(set.size);
  1022. set.forEach(function(value) {
  1023. result[++index] = [value, value];
  1024. });
  1025. return result;
  1026. }
  1027. /**
  1028. * Gets the number of symbols in `string`.
  1029. *
  1030. * @private
  1031. * @param {string} string The string to inspect.
  1032. * @returns {number} Returns the string size.
  1033. */
  1034. function stringSize(string) {
  1035. if (!(string && reHasComplexSymbol.test(string))) {
  1036. return string.length;
  1037. }
  1038. var result = reComplexSymbol.lastIndex = 0;
  1039. while (reComplexSymbol.test(string)) {
  1040. result++;
  1041. }
  1042. return result;
  1043. }
  1044. /**
  1045. * Converts `string` to an array.
  1046. *
  1047. * @private
  1048. * @param {string} string The string to convert.
  1049. * @returns {Array} Returns the converted array.
  1050. */
  1051. function stringToArray(string) {
  1052. return string.match(reComplexSymbol);
  1053. }
  1054. /**
  1055. * Used by `_.unescape` to convert HTML entities to characters.
  1056. *
  1057. * @private
  1058. * @param {string} chr The matched character to unescape.
  1059. * @returns {string} Returns the unescaped character.
  1060. */
  1061. function unescapeHtmlChar(chr) {
  1062. return htmlUnescapes[chr];
  1063. }
  1064. /*--------------------------------------------------------------------------*/
  1065. /**
  1066. * Create a new pristine `lodash` function using the `context` object.
  1067. *
  1068. * @static
  1069. * @memberOf _
  1070. * @since 1.1.0
  1071. * @category Util
  1072. * @param {Object} [context=root] The context object.
  1073. * @returns {Function} Returns a new `lodash` function.
  1074. * @example
  1075. *
  1076. * _.mixin({ 'foo': _.constant('foo') });
  1077. *
  1078. * var lodash = _.runInContext();
  1079. * lodash.mixin({ 'bar': lodash.constant('bar') });
  1080. *
  1081. * _.isFunction(_.foo);
  1082. * // => true
  1083. * _.isFunction(_.bar);
  1084. * // => false
  1085. *
  1086. * lodash.isFunction(lodash.foo);
  1087. * // => false
  1088. * lodash.isFunction(lodash.bar);
  1089. * // => true
  1090. *
  1091. * // Use `context` to stub `Date#getTime` use in `_.now`.
  1092. * var stubbed = _.runInContext({
  1093. * 'Date': function() {
  1094. * return { 'getTime': stubGetTime };
  1095. * }
  1096. * });
  1097. *
  1098. * // Create a suped-up `defer` in Node.js.
  1099. * var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
  1100. */
  1101. function runInContext(context) {
  1102. context = context ? _.defaults({}, context, _.pick(root, contextProps)) : root;
  1103. /** Built-in constructor references. */
  1104. var Date = context.Date,
  1105. Error = context.Error,
  1106. Math = context.Math,
  1107. RegExp = context.RegExp,
  1108. TypeError = context.TypeError;
  1109. /** Used for built-in method references. */
  1110. var arrayProto = context.Array.prototype,
  1111. objectProto = context.Object.prototype,
  1112. stringProto = context.String.prototype;
  1113. /** Used to detect overreaching core-js shims. */
  1114. var coreJsData = context['__core-js_shared__'];
  1115. /** Used to detect methods masquerading as native. */
  1116. var maskSrcKey = (function() {
  1117. var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  1118. return uid ? ('Symbol(src)_1.' + uid) : '';
  1119. }());
  1120. /** Used to resolve the decompiled source of functions. */
  1121. var funcToString = context.Function.prototype.toString;
  1122. /** Used to check objects for own properties. */
  1123. var hasOwnProperty = objectProto.hasOwnProperty;
  1124. /** Used to generate unique IDs. */
  1125. var idCounter = 0;
  1126. /** Used to infer the `Object` constructor. */
  1127. var objectCtorString = funcToString.call(Object);
  1128. /**
  1129. * Used to resolve the
  1130. * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  1131. * of values.
  1132. */
  1133. var objectToString = objectProto.toString;
  1134. /** Used to restore the original `_` reference in `_.noConflict`. */
  1135. var oldDash = root._;
  1136. /** Used to detect if a method is native. */
  1137. var reIsNative = RegExp('^' +
  1138. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  1139. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  1140. );
  1141. /** Built-in value references. */
  1142. var Buffer = moduleExports ? context.Buffer : undefined,
  1143. Reflect = context.Reflect,
  1144. Symbol = context.Symbol,
  1145. Uint8Array = context.Uint8Array,
  1146. enumerate = Reflect ? Reflect.enumerate : undefined,
  1147. getOwnPropertySymbols = Object.getOwnPropertySymbols,
  1148. iteratorSymbol = typeof (iteratorSymbol = Symbol && Symbol.iterator) == 'symbol' ? iteratorSymbol : undefined,
  1149. objectCreate = Object.create,
  1150. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  1151. splice = arrayProto.splice;
  1152. /** Built-in method references that are mockable. */
  1153. var setTimeout = function(func, wait) { return context.setTimeout.call(root, func, wait); };
  1154. /* Built-in method references for those with the same name as other `lodash` methods. */
  1155. var nativeCeil = Math.ceil,
  1156. nativeFloor = Math.floor,
  1157. nativeGetPrototype = Object.getPrototypeOf,
  1158. nativeIsFinite = context.isFinite,
  1159. nativeJoin = arrayProto.join,
  1160. nativeKeys = Object.keys,
  1161. nativeMax = Math.max,
  1162. nativeMin = Math.min,
  1163. nativeParseInt = context.parseInt,
  1164. nativeRandom = Math.random,
  1165. nativeReplace = stringProto.replace,
  1166. nativeReverse = arrayProto.reverse,
  1167. nativeSplit = stringProto.split;
  1168. /* Built-in method references that are verified to be native. */
  1169. var DataView = getNative(context, 'DataView'),
  1170. Map = getNative(context, 'Map'),
  1171. Promise = getNative(context, 'Promise'),
  1172. Set = getNative(context, 'Set'),
  1173. WeakMap = getNative(context, 'WeakMap'),
  1174. nativeCreate = getNative(Object, 'create');
  1175. /** Used to store function metadata. */
  1176. var metaMap = WeakMap && new WeakMap;
  1177. /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
  1178. var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
  1179. /** Used to lookup unminified function names. */
  1180. var realNames = {};
  1181. /** Used to detect maps, sets, and weakmaps. */
  1182. var dataViewCtorString = toSource(DataView),
  1183. mapCtorString = toSource(Map),
  1184. promiseCtorString = toSource(Promise),
  1185. setCtorString = toSource(Set),
  1186. weakMapCtorString = toSource(WeakMap);
  1187. /** Used to convert symbols to primitives and strings. */
  1188. var symbolProto = Symbol ? Symbol.prototype : undefined,
  1189. symbolValueOf = symbolProto ? symbolProto.valueOf : undefined,
  1190. symbolToString = symbolProto ? symbolProto.toString : undefined;
  1191. /*------------------------------------------------------------------------*/
  1192. /**
  1193. * Creates a `lodash` object which wraps `value` to enable implicit method
  1194. * chain sequences. Methods that operate on and return arrays, collections,
  1195. * and functions can be chained together. Methods that retrieve a single value
  1196. * or may return a primitive value will automatically end the chain sequence
  1197. * and return the unwrapped value. Otherwise, the value must be unwrapped
  1198. * with `_#value`.
  1199. *
  1200. * Explicit chain sequences, which must be unwrapped with `_#value`, may be
  1201. * enabled using `_.chain`.
  1202. *
  1203. * The execution of chained methods is lazy, that is, it's deferred until
  1204. * `_#value` is implicitly or explicitly called.
  1205. *
  1206. * Lazy evaluation allows several methods to support shortcut fusion.
  1207. * Shortcut fusion is an optimization to merge iteratee calls; this avoids
  1208. * the creation of intermediate arrays and can greatly reduce the number of
  1209. * iteratee executions. Sections of a chain sequence qualify for shortcut
  1210. * fusion if the section is applied to an array of at least `200` elements
  1211. * and any iteratees accept only one argument. The heuristic for whether a
  1212. * section qualifies for shortcut fusion is subject to change.
  1213. *
  1214. * Chaining is supported in custom builds as long as the `_#value` method is
  1215. * directly or indirectly included in the build.
  1216. *
  1217. * In addition to lodash methods, wrappers have `Array` and `String` methods.
  1218. *
  1219. * The wrapper `Array` methods are:
  1220. * `concat`, `join`, `pop`, `push`, `shift`, `sort`, `splice`, and `unshift`
  1221. *
  1222. * The wrapper `String` methods are:
  1223. * `replace` and `split`
  1224. *
  1225. * The wrapper methods that support shortcut fusion are:
  1226. * `at`, `compact`, `drop`, `dropRight`, `dropWhile`, `filter`, `find`,
  1227. * `findLast`, `head`, `initial`, `last`, `map`, `reject`, `reverse`, `slice`,
  1228. * `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray`
  1229. *
  1230. * The chainable wrapper methods are:
  1231. * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`, `at`,
  1232. * `before`, `bind`, `bindAll`, `bindKey`, `castArray`, `chain`, `chunk`,
  1233. * `commit`, `compact`, `concat`, `conforms`, `constant`, `countBy`, `create`,
  1234. * `curry`, `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`,
  1235. * `difference`, `differenceBy`, `differenceWith`, `drop`, `dropRight`,
  1236. * `dropRightWhile`, `dropWhile`, `extend`, `extendWith`, `fill`, `filter`,
  1237. * `flatMap`, `flatMapDeep`, `flatMapDepth`, `flatten`, `flattenDeep`,
  1238. * `flattenDepth`, `flip`, `flow`, `flowRight`, `fromPairs`, `functions`,
  1239. * `functionsIn`, `groupBy`, `initial`, `intersection`, `intersectionBy`,
  1240. * `intersectionWith`, `invert`, `invertBy`, `invokeMap`, `iteratee`, `keyBy`,
  1241. * `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`,
  1242. * `memoize`, `merge`, `mergeWith`, `method`, `methodOf`, `mixin`, `negate`,
  1243. * `nthArg`, `omit`, `omitBy`, `once`, `orderBy`, `over`, `overArgs`,
  1244. * `overEvery`, `overSome`, `partial`, `partialRight`, `partition`, `pick`,
  1245. * `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, `pullAllBy`,
  1246. * `pullAllWith`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`, `reject`,
  1247. * `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`,
  1248. * `slice`, `sort`, `sortBy`, `splice`, `spread`, `tail`, `take`, `takeRight`,
  1249. * `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`, `toArray`,
  1250. * `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`, `transform`, `unary`,
  1251. * `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, `uniqWith`, `unset`,
  1252. * `unshift`, `unzip`, `unzipWith`, `update`, `updateWith`, `values`,
  1253. * `valuesIn`, `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`,
  1254. * `zipObject`, `zipObjectDeep`, and `zipWith`
  1255. *
  1256. * The wrapper methods that are **not** chainable by default are:
  1257. * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`,
  1258. * `cloneDeep`, `cloneDeepWith`, `cloneWith`, `deburr`, `divide`, `each`,
  1259. * `eachRight`, `endsWith`, `eq`, `escape`, `escapeRegExp`, `every`, `find`,
  1260. * `findIndex`, `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `first`,
  1261. * `floor`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`,
  1262. * `forOwnRight`, `get`, `gt`, `gte`, `has`, `hasIn`, `head`, `identity`,
  1263. * `includes`, `indexOf`, `inRange`, `invoke`, `isArguments`, `isArray`,
  1264. * `isArrayBuffer`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`,
  1265. * `isBuffer`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`,
  1266. * `isError`, `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMap`,
  1267. * `isMatch`, `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`,
  1268. * `isNumber`, `isObject`, `isObjectLike`, `isPlainObject`, `isRegExp`,
  1269. * `isSafeInteger`, `isSet`, `isString`, `isUndefined`, `isTypedArray`,
  1270. * `isWeakMap`, `isWeakSet`, `join`, `kebabCase`, `last`, `lastIndexOf`,
  1271. * `lowerCase`, `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `meanBy`,
  1272. * `min`, `minBy`, `multiply`, `noConflict`, `noop`, `now`, `nth`, `pad`,
  1273. * `padEnd`, `padStart`, `parseInt`, `pop`, `random`, `reduce`, `reduceRight`,
  1274. * `repeat`, `result`, `round`, `runInContext`, `sample`, `shift`, `size`,
  1275. * `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`,
  1276. * `sortedLastIndexBy`, `startCase`, `startsWith`, `stubArray`, `stubFalse`,
  1277. * `stubObject`, `stubString`, `stubTrue`, `subtract`, `sum`, `sumBy`,
  1278. * `template`, `times`, `toFinite`, `toInteger`, `toJSON`, `toLength`,
  1279. * `toLower`, `toNumber`, `toSafeInteger`, `toString`, `toUpper`, `trim`,
  1280. * `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`, `upperCase`,
  1281. * `upperFirst`, `value`, and `words`
  1282. *
  1283. * @name _
  1284. * @constructor
  1285. * @category Seq
  1286. * @param {*} value The value to wrap in a `lodash` instance.
  1287. * @returns {Object} Returns the new `lodash` wrapper instance.
  1288. * @example
  1289. *
  1290. * function square(n) {
  1291. * return n * n;
  1292. * }
  1293. *
  1294. * var wrapped = _([1, 2, 3]);
  1295. *
  1296. * // Returns an unwrapped value.
  1297. * wrapped.reduce(_.add);
  1298. * // => 6
  1299. *
  1300. * // Returns a wrapped value.
  1301. * var squares = wrapped.map(square);
  1302. *
  1303. * _.isArray(squares);
  1304. * // => false
  1305. *
  1306. * _.isArray(squares.value());
  1307. * // => true
  1308. */
  1309. function lodash(value) {
  1310. if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
  1311. if (value instanceof LodashWrapper) {
  1312. return value;
  1313. }
  1314. if (hasOwnProperty.call(value, '__wrapped__')) {
  1315. return wrapperClone(value);
  1316. }
  1317. }
  1318. return new LodashWrapper(value);
  1319. }
  1320. /**
  1321. * The function whose prototype chain sequence wrappers inherit from.
  1322. *
  1323. * @private
  1324. */
  1325. function baseLodash() {
  1326. // No operation performed.
  1327. }
  1328. /**
  1329. * The base constructor for creating `lodash` wrapper objects.
  1330. *
  1331. * @private
  1332. * @param {*} value The value to wrap.
  1333. * @param {boolean} [chainAll] Enable explicit method chain sequences.
  1334. */
  1335. function LodashWrapper(value, chainAll) {
  1336. this.__wrapped__ = value;
  1337. this.__actions__ = [];
  1338. this.__chain__ = !!chainAll;
  1339. this.__index__ = 0;
  1340. this.__values__ = undefined;
  1341. }
  1342. /**
  1343. * By default, the template delimiters used by lodash are like those in
  1344. * embedded Ruby (ERB). Change the following template settings to use
  1345. * alternative delimiters.
  1346. *
  1347. * @static
  1348. * @memberOf _
  1349. * @type {Object}
  1350. */
  1351. lodash.templateSettings = {
  1352. /**
  1353. * Used to detect `data` property values to be HTML-escaped.
  1354. *
  1355. * @memberOf _.templateSettings
  1356. * @type {RegExp}
  1357. */
  1358. 'escape': reEscape,
  1359. /**
  1360. * Used to detect code to be evaluated.
  1361. *
  1362. * @memberOf _.templateSettings
  1363. * @type {RegExp}
  1364. */
  1365. 'evaluate': reEvaluate,
  1366. /**
  1367. * Used to detect `data` property values to inject.
  1368. *
  1369. * @memberOf _.templateSettings
  1370. * @type {RegExp}
  1371. */
  1372. 'interpolate': reInterpolate,
  1373. /**
  1374. * Used to reference the data object in the template text.
  1375. *
  1376. * @memberOf _.templateSettings
  1377. * @type {string}
  1378. */
  1379. 'variable': '',
  1380. /**
  1381. * Used to import variables into the compiled template.
  1382. *
  1383. * @memberOf _.templateSettings
  1384. * @type {Object}
  1385. */
  1386. 'imports': {
  1387. /**
  1388. * A reference to the `lodash` function.
  1389. *
  1390. * @memberOf _.templateSettings.imports
  1391. * @type {Function}
  1392. */
  1393. '_': lodash
  1394. }
  1395. };
  1396. // Ensure wrappers are instances of `baseLodash`.
  1397. lodash.prototype = baseLodash.prototype;
  1398. lodash.prototype.constructor = lodash;
  1399. LodashWrapper.prototype = baseCreate(baseLodash.prototype);
  1400. LodashWrapper.prototype.constructor = LodashWrapper;
  1401. /*------------------------------------------------------------------------*/
  1402. /**
  1403. * Creates a lazy wrapper object which wraps `value` to enable lazy evaluation.
  1404. *
  1405. * @private
  1406. * @constructor
  1407. * @param {*} value The value to wrap.
  1408. */
  1409. function LazyWrapper(value) {
  1410. this.__wrapped__ = value;
  1411. this.__actions__ = [];
  1412. this.__dir__ = 1;
  1413. this.__filtered__ = false;
  1414. this.__iteratees__ = [];
  1415. this.__takeCount__ = MAX_ARRAY_LENGTH;
  1416. this.__views__ = [];
  1417. }
  1418. /**
  1419. * Creates a clone of the lazy wrapper object.
  1420. *
  1421. * @private
  1422. * @name clone
  1423. * @memberOf LazyWrapper
  1424. * @returns {Object} Returns the cloned `LazyWrapper` object.
  1425. */
  1426. function lazyClone() {
  1427. var result = new LazyWrapper(this.__wrapped__);
  1428. result.__actions__ = copyArray(this.__actions__);
  1429. result.__dir__ = this.__dir__;
  1430. result.__filtered__ = this.__filtered__;
  1431. result.__iteratees__ = copyArray(this.__iteratees__);
  1432. result.__takeCount__ = this.__takeCount__;
  1433. result.__views__ = copyArray(this.__views__);
  1434. return result;
  1435. }
  1436. /**
  1437. * Reverses the direction of lazy iteration.
  1438. *
  1439. * @private
  1440. * @name reverse
  1441. * @memberOf LazyWrapper
  1442. * @returns {Object} Returns the new reversed `LazyWrapper` object.
  1443. */
  1444. function lazyReverse() {
  1445. if (this.__filtered__) {
  1446. var result = new LazyWrapper(this);
  1447. result.__dir__ = -1;
  1448. result.__filtered__ = true;
  1449. } else {
  1450. result = this.clone();
  1451. result.__dir__ *= -1;
  1452. }
  1453. return result;
  1454. }
  1455. /**
  1456. * Extracts the unwrapped value from its lazy wrapper.
  1457. *
  1458. * @private
  1459. * @name value
  1460. * @memberOf LazyWrapper
  1461. * @returns {*} Returns the unwrapped value.
  1462. */
  1463. function lazyValue() {
  1464. var array = this.__wrapped__.value(),
  1465. dir = this.__dir__,
  1466. isArr = isArray(array),
  1467. isRight = dir < 0,
  1468. arrLength = isArr ? array.length : 0,
  1469. view = getView(0, arrLength, this.__views__),
  1470. start = view.start,
  1471. end = view.end,
  1472. length = end - start,
  1473. index = isRight ? end : (start - 1),
  1474. iteratees = this.__iteratees__,
  1475. iterLength = iteratees.length,
  1476. resIndex = 0,
  1477. takeCount = nativeMin(length, this.__takeCount__);
  1478. if (!isArr || arrLength < LARGE_ARRAY_SIZE ||
  1479. (arrLength == length && takeCount == length)) {
  1480. return baseWrapperValue(array, this.__actions__);
  1481. }
  1482. var result = [];
  1483. outer:
  1484. while (length-- && resIndex < takeCount) {
  1485. index += dir;
  1486. var iterIndex = -1,
  1487. value = array[index];
  1488. while (++iterIndex < iterLength) {
  1489. var data = iteratees[iterIndex],
  1490. iteratee = data.iteratee,
  1491. type = data.type,
  1492. computed = iteratee(value);
  1493. if (type == LAZY_MAP_FLAG) {
  1494. value = computed;
  1495. } else if (!computed) {
  1496. if (type == LAZY_FILTER_FLAG) {
  1497. continue outer;
  1498. } else {
  1499. break outer;
  1500. }
  1501. }
  1502. }
  1503. result[resIndex++] = value;
  1504. }
  1505. return result;
  1506. }
  1507. // Ensure `LazyWrapper` is an instance of `baseLodash`.
  1508. LazyWrapper.prototype = baseCreate(baseLodash.prototype);
  1509. LazyWrapper.prototype.constructor = LazyWrapper;
  1510. /*------------------------------------------------------------------------*/
  1511. /**
  1512. * Creates a hash object.
  1513. *
  1514. * @private
  1515. * @constructor
  1516. * @param {Array} [entries] The key-value pairs to cache.
  1517. */
  1518. function Hash(entries) {
  1519. var index = -1,
  1520. length = entries ? entries.length : 0;
  1521. this.clear();
  1522. while (++index < length) {
  1523. var entry = entries[index];
  1524. this.set(entry[0], entry[1]);
  1525. }
  1526. }
  1527. /**
  1528. * Removes all key-value entries from the hash.
  1529. *
  1530. * @private
  1531. * @name clear
  1532. * @memberOf Hash
  1533. */
  1534. function hashClear() {
  1535. this.__data__ = nativeCreate ? nativeCreate(null) : {};
  1536. }
  1537. /**
  1538. * Removes `key` and its value from the hash.
  1539. *
  1540. * @private
  1541. * @name delete
  1542. * @memberOf Hash
  1543. * @param {Object} hash The hash to modify.
  1544. * @param {string} key The key of the value to remove.
  1545. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  1546. */
  1547. function hashDelete(key) {
  1548. return this.has(key) && delete this.__data__[key];
  1549. }
  1550. /**
  1551. * Gets the hash value for `key`.
  1552. *
  1553. * @private
  1554. * @name get
  1555. * @memberOf Hash
  1556. * @param {string} key The key of the value to get.
  1557. * @returns {*} Returns the entry value.
  1558. */
  1559. function hashGet(key) {
  1560. var data = this.__data__;
  1561. if (nativeCreate) {
  1562. var result = data[key];
  1563. return result === HASH_UNDEFINED ? undefined : result;
  1564. }
  1565. return hasOwnProperty.call(data, key) ? data[key] : undefined;
  1566. }
  1567. /**
  1568. * Checks if a hash value for `key` exists.
  1569. *
  1570. * @private
  1571. * @name has
  1572. * @memberOf Hash
  1573. * @param {string} key The key of the entry to check.
  1574. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1575. */
  1576. function hashHas(key) {
  1577. var data = this.__data__;
  1578. return nativeCreate ? data[key] !== undefined : hasOwnProperty.call(data, key);
  1579. }
  1580. /**
  1581. * Sets the hash `key` to `value`.
  1582. *
  1583. * @private
  1584. * @name set
  1585. * @memberOf Hash
  1586. * @param {string} key The key of the value to set.
  1587. * @param {*} value The value to set.
  1588. * @returns {Object} Returns the hash instance.
  1589. */
  1590. function hashSet(key, value) {
  1591. var data = this.__data__;
  1592. data[key] = (nativeCreate && value === undefined) ? HASH_UNDEFINED : value;
  1593. return this;
  1594. }
  1595. // Add methods to `Hash`.
  1596. Hash.prototype.clear = hashClear;
  1597. Hash.prototype['delete'] = hashDelete;
  1598. Hash.prototype.get = hashGet;
  1599. Hash.prototype.has = hashHas;
  1600. Hash.prototype.set = hashSet;
  1601. /*------------------------------------------------------------------------*/
  1602. /**
  1603. * Creates an list cache object.
  1604. *
  1605. * @private
  1606. * @constructor
  1607. * @param {Array} [entries] The key-value pairs to cache.
  1608. */
  1609. function ListCache(entries) {
  1610. var index = -1,
  1611. length = entries ? entries.length : 0;
  1612. this.clear();
  1613. while (++index < length) {
  1614. var entry = entries[index];
  1615. this.set(entry[0], entry[1]);
  1616. }
  1617. }
  1618. /**
  1619. * Removes all key-value entries from the list cache.
  1620. *
  1621. * @private
  1622. * @name clear
  1623. * @memberOf ListCache
  1624. */
  1625. function listCacheClear() {
  1626. this.__data__ = [];
  1627. }
  1628. /**
  1629. * Removes `key` and its value from the list cache.
  1630. *
  1631. * @private
  1632. * @name delete
  1633. * @memberOf ListCache
  1634. * @param {string} key The key of the value to remove.
  1635. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  1636. */
  1637. function listCacheDelete(key) {
  1638. var data = this.__data__,
  1639. index = assocIndexOf(data, key);
  1640. if (index < 0) {
  1641. return false;
  1642. }
  1643. var lastIndex = data.length - 1;
  1644. if (index == lastIndex) {
  1645. data.pop();
  1646. } else {
  1647. splice.call(data, index, 1);
  1648. }
  1649. return true;
  1650. }
  1651. /**
  1652. * Gets the list cache value for `key`.
  1653. *
  1654. * @private
  1655. * @name get
  1656. * @memberOf ListCache
  1657. * @param {string} key The key of the value to get.
  1658. * @returns {*} Returns the entry value.
  1659. */
  1660. function listCacheGet(key) {
  1661. var data = this.__data__,
  1662. index = assocIndexOf(data, key);
  1663. return index < 0 ? undefined : data[index][1];
  1664. }
  1665. /**
  1666. * Checks if a list cache value for `key` exists.
  1667. *
  1668. * @private
  1669. * @name has
  1670. * @memberOf ListCache
  1671. * @param {string} key The key of the entry to check.
  1672. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1673. */
  1674. function listCacheHas(key) {
  1675. return assocIndexOf(this.__data__, key) > -1;
  1676. }
  1677. /**
  1678. * Sets the list cache `key` to `value`.
  1679. *
  1680. * @private
  1681. * @name set
  1682. * @memberOf ListCache
  1683. * @param {string} key The key of the value to set.
  1684. * @param {*} value The value to set.
  1685. * @returns {Object} Returns the list cache instance.
  1686. */
  1687. function listCacheSet(key, value) {
  1688. var data = this.__data__,
  1689. index = assocIndexOf(data, key);
  1690. if (index < 0) {
  1691. data.push([key, value]);
  1692. } else {
  1693. data[index][1] = value;
  1694. }
  1695. return this;
  1696. }
  1697. // Add methods to `ListCache`.
  1698. ListCache.prototype.clear = listCacheClear;
  1699. ListCache.prototype['delete'] = listCacheDelete;
  1700. ListCache.prototype.get = listCacheGet;
  1701. ListCache.prototype.has = listCacheHas;
  1702. ListCache.prototype.set = listCacheSet;
  1703. /*------------------------------------------------------------------------*/
  1704. /**
  1705. * Creates a map cache object to store key-value pairs.
  1706. *
  1707. * @private
  1708. * @constructor
  1709. * @param {Array} [entries] The key-value pairs to cache.
  1710. */
  1711. function MapCache(entries) {
  1712. var index = -1,
  1713. length = entries ? entries.length : 0;
  1714. this.clear();
  1715. while (++index < length) {
  1716. var entry = entries[index];
  1717. this.set(entry[0], entry[1]);
  1718. }
  1719. }
  1720. /**
  1721. * Removes all key-value entries from the map.
  1722. *
  1723. * @private
  1724. * @name clear
  1725. * @memberOf MapCache
  1726. */
  1727. function mapCacheClear() {
  1728. this.__data__ = {
  1729. 'hash': new Hash,
  1730. 'map': new (Map || ListCache),
  1731. 'string': new Hash
  1732. };
  1733. }
  1734. /**
  1735. * Removes `key` and its value from the map.
  1736. *
  1737. * @private
  1738. * @name delete
  1739. * @memberOf MapCache
  1740. * @param {string} key The key of the value to remove.
  1741. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  1742. */
  1743. function mapCacheDelete(key) {
  1744. return getMapData(this, key)['delete'](key);
  1745. }
  1746. /**
  1747. * Gets the map value for `key`.
  1748. *
  1749. * @private
  1750. * @name get
  1751. * @memberOf MapCache
  1752. * @param {string} key The key of the value to get.
  1753. * @returns {*} Returns the entry value.
  1754. */
  1755. function mapCacheGet(key) {
  1756. return getMapData(this, key).get(key);
  1757. }
  1758. /**
  1759. * Checks if a map value for `key` exists.
  1760. *
  1761. * @private
  1762. * @name has
  1763. * @memberOf MapCache
  1764. * @param {string} key The key of the entry to check.
  1765. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1766. */
  1767. function mapCacheHas(key) {
  1768. return getMapData(this, key).has(key);
  1769. }
  1770. /**
  1771. * Sets the map `key` to `value`.
  1772. *
  1773. * @private
  1774. * @name set
  1775. * @memberOf MapCache
  1776. * @param {string} key The key of the value to set.
  1777. * @param {*} value The value to set.
  1778. * @returns {Object} Returns the map cache instance.
  1779. */
  1780. function mapCacheSet(key, value) {
  1781. getMapData(this, key).set(key, value);
  1782. return this;
  1783. }
  1784. // Add methods to `MapCache`.
  1785. MapCache.prototype.clear = mapCacheClear;
  1786. MapCache.prototype['delete'] = mapCacheDelete;
  1787. MapCache.prototype.get = mapCacheGet;
  1788. MapCache.prototype.has = mapCacheHas;
  1789. MapCache.prototype.set = mapCacheSet;
  1790. /*------------------------------------------------------------------------*/
  1791. /**
  1792. *
  1793. * Creates an array cache object to store unique values.
  1794. *
  1795. * @private
  1796. * @constructor
  1797. * @param {Array} [values] The values to cache.
  1798. */
  1799. function SetCache(values) {
  1800. var index = -1,
  1801. length = values ? values.length : 0;
  1802. this.__data__ = new MapCache;
  1803. while (++index < length) {
  1804. this.add(values[index]);
  1805. }
  1806. }
  1807. /**
  1808. * Adds `value` to the array cache.
  1809. *
  1810. * @private
  1811. * @name add
  1812. * @memberOf SetCache
  1813. * @alias push
  1814. * @param {*} value The value to cache.
  1815. * @returns {Object} Returns the cache instance.
  1816. */
  1817. function setCacheAdd(value) {
  1818. this.__data__.set(value, HASH_UNDEFINED);
  1819. return this;
  1820. }
  1821. /**
  1822. * Checks if `value` is in the array cache.
  1823. *
  1824. * @private
  1825. * @name has
  1826. * @memberOf SetCache
  1827. * @param {*} value The value to search for.
  1828. * @returns {number} Returns `true` if `value` is found, else `false`.
  1829. */
  1830. function setCacheHas(value) {
  1831. return this.__data__.has(value);
  1832. }
  1833. // Add methods to `SetCache`.
  1834. SetCache.prototype.add = SetCache.prototype.push = setCacheAdd;
  1835. SetCache.prototype.has = setCacheHas;
  1836. /*------------------------------------------------------------------------*/
  1837. /**
  1838. * Creates a stack cache object to store key-value pairs.
  1839. *
  1840. * @private
  1841. * @constructor
  1842. * @param {Array} [entries] The key-value pairs to cache.
  1843. */
  1844. function Stack(entries) {
  1845. this.__data__ = new ListCache(entries);
  1846. }
  1847. /**
  1848. * Removes all key-value entries from the stack.
  1849. *
  1850. * @private
  1851. * @name clear
  1852. * @memberOf Stack
  1853. */
  1854. function stackClear() {
  1855. this.__data__ = new ListCache;
  1856. }
  1857. /**
  1858. * Removes `key` and its value from the stack.
  1859. *
  1860. * @private
  1861. * @name delete
  1862. * @memberOf Stack
  1863. * @param {string} key The key of the value to remove.
  1864. * @returns {boolean} Returns `true` if the entry was removed, else `false`.
  1865. */
  1866. function stackDelete(key) {
  1867. return this.__data__['delete'](key);
  1868. }
  1869. /**
  1870. * Gets the stack value for `key`.
  1871. *
  1872. * @private
  1873. * @name get
  1874. * @memberOf Stack
  1875. * @param {string} key The key of the value to get.
  1876. * @returns {*} Returns the entry value.
  1877. */
  1878. function stackGet(key) {
  1879. return this.__data__.get(key);
  1880. }
  1881. /**
  1882. * Checks if a stack value for `key` exists.
  1883. *
  1884. * @private
  1885. * @name has
  1886. * @memberOf Stack
  1887. * @param {string} key The key of the entry to check.
  1888. * @returns {boolean} Returns `true` if an entry for `key` exists, else `false`.
  1889. */
  1890. function stackHas(key) {
  1891. return this.__data__.has(key);
  1892. }
  1893. /**
  1894. * Sets the stack `key` to `value`.
  1895. *
  1896. * @private
  1897. * @name set
  1898. * @memberOf Stack
  1899. * @param {string} key The key of the value to set.
  1900. * @param {*} value The value to set.
  1901. * @returns {Object} Returns the stack cache instance.
  1902. */
  1903. function stackSet(key, value) {
  1904. var cache = this.__data__;
  1905. if (cache instanceof ListCache && cache.__data__.length == LARGE_ARRAY_SIZE) {
  1906. cache = this.__data__ = new MapCache(cache.__data__);
  1907. }
  1908. cache.set(key, value);
  1909. return this;
  1910. }
  1911. // Add methods to `Stack`.
  1912. Stack.prototype.clear = stackClear;
  1913. Stack.prototype['delete'] = stackDelete;
  1914. Stack.prototype.get = stackGet;
  1915. Stack.prototype.has = stackHas;
  1916. Stack.prototype.set = stackSet;
  1917. /*------------------------------------------------------------------------*/
  1918. /**
  1919. * Used by `_.defaults` to customize its `_.assignIn` use.
  1920. *
  1921. * @private
  1922. * @param {*} objValue The destination value.
  1923. * @param {*} srcValue The source value.
  1924. * @param {string} key The key of the property to assign.
  1925. * @param {Object} object The parent object of `objValue`.
  1926. * @returns {*} Returns the value to assign.
  1927. */
  1928. function assignInDefaults(objValue, srcValue, key, object) {
  1929. if (objValue === undefined ||
  1930. (eq(objValue, objectProto[key]) && !hasOwnProperty.call(object, key))) {
  1931. return srcValue;
  1932. }
  1933. return objValue;
  1934. }
  1935. /**
  1936. * This function is like `assignValue` except that it doesn't assign
  1937. * `undefined` values.
  1938. *
  1939. * @private
  1940. * @param {Object} object The object to modify.
  1941. * @param {string} key The key of the property to assign.
  1942. * @param {*} value The value to assign.
  1943. */
  1944. function assignMergeValue(object, key, value) {
  1945. if ((value !== undefined && !eq(object[key], value)) ||
  1946. (typeof key == 'number' && value === undefined && !(key in object))) {
  1947. object[key] = value;
  1948. }
  1949. }
  1950. /**
  1951. * Assigns `value` to `key` of `object` if the existing value is not equivalent
  1952. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  1953. * for equality comparisons.
  1954. *
  1955. * @private
  1956. * @param {Object} object The object to modify.
  1957. * @param {string} key The key of the property to assign.
  1958. * @param {*} value The value to assign.
  1959. */
  1960. function assignValue(object, key, value) {
  1961. var objValue = object[key];
  1962. if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
  1963. (value === undefined && !(key in object))) {
  1964. object[key] = value;
  1965. }
  1966. }
  1967. /**
  1968. * Gets the index at which the `key` is found in `array` of key-value pairs.
  1969. *
  1970. * @private
  1971. * @param {Array} array The array to search.
  1972. * @param {*} key The key to search for.
  1973. * @returns {number} Returns the index of the matched value, else `-1`.
  1974. */
  1975. function assocIndexOf(array, key) {
  1976. var length = array.length;
  1977. while (length--) {
  1978. if (eq(array[length][0], key)) {
  1979. return length;
  1980. }
  1981. }
  1982. return -1;
  1983. }
  1984. /**
  1985. * Aggregates elements of `collection` on `accumulator` with keys transformed
  1986. * by `iteratee` and values set by `setter`.
  1987. *
  1988. * @private
  1989. * @param {Array|Object} collection The collection to iterate over.
  1990. * @param {Function} setter The function to set `accumulator` values.
  1991. * @param {Function} iteratee The iteratee to transform keys.
  1992. * @param {Object} accumulator The initial aggregated object.
  1993. * @returns {Function} Returns `accumulator`.
  1994. */
  1995. function baseAggregator(collection, setter, iteratee, accumulator) {
  1996. baseEach(collection, function(value, key, collection) {
  1997. setter(accumulator, value, iteratee(value), collection);
  1998. });
  1999. return accumulator;
  2000. }
  2001. /**
  2002. * The base implementation of `_.assign` without support for multiple sources
  2003. * or `customizer` functions.
  2004. *
  2005. * @private
  2006. * @param {Object} object The destination object.
  2007. * @param {Object} source The source object.
  2008. * @returns {Object} Returns `object`.
  2009. */
  2010. function baseAssign(object, source) {
  2011. return object && copyObject(source, keys(source), object);
  2012. }
  2013. /**
  2014. * The base implementation of `_.at` without support for individual paths.
  2015. *
  2016. * @private
  2017. * @param {Object} object The object to iterate over.
  2018. * @param {string[]} paths The property paths of elements to pick.
  2019. * @returns {Array} Returns the picked elements.
  2020. */
  2021. function baseAt(object, paths) {
  2022. var index = -1,
  2023. isNil = object == null,
  2024. length = paths.length,
  2025. result = Array(length);
  2026. while (++index < length) {
  2027. result[index] = isNil ? undefined : get(object, paths[index]);
  2028. }
  2029. return result;
  2030. }
  2031. /**
  2032. * The base implementation of `_.clamp` which doesn't coerce arguments to numbers.
  2033. *
  2034. * @private
  2035. * @param {number} number The number to clamp.
  2036. * @param {number} [lower] The lower bound.
  2037. * @param {number} upper The upper bound.
  2038. * @returns {number} Returns the clamped number.
  2039. */
  2040. function baseClamp(number, lower, upper) {
  2041. if (number === number) {
  2042. if (upper !== undefined) {
  2043. number = number <= upper ? number : upper;
  2044. }
  2045. if (lower !== undefined) {
  2046. number = number >= lower ? number : lower;
  2047. }
  2048. }
  2049. return number;
  2050. }
  2051. /**
  2052. * The base implementation of `_.clone` and `_.cloneDeep` which tracks
  2053. * traversed objects.
  2054. *
  2055. * @private
  2056. * @param {*} value The value to clone.
  2057. * @param {boolean} [isDeep] Specify a deep clone.
  2058. * @param {boolean} [isFull] Specify a clone including symbols.
  2059. * @param {Function} [customizer] The function to customize cloning.
  2060. * @param {string} [key] The key of `value`.
  2061. * @param {Object} [object] The parent object of `value`.
  2062. * @param {Object} [stack] Tracks traversed objects and their clone counterparts.
  2063. * @returns {*} Returns the cloned value.
  2064. */
  2065. function baseClone(value, isDeep, isFull, customizer, key, object, stack) {
  2066. var result;
  2067. if (customizer) {
  2068. result = object ? customizer(value, key, object, stack) : customizer(value);
  2069. }
  2070. if (result !== undefined) {
  2071. return result;
  2072. }
  2073. if (!isObject(value)) {
  2074. return value;
  2075. }
  2076. var isArr = isArray(value);
  2077. if (isArr) {
  2078. result = initCloneArray(value);
  2079. if (!isDeep) {
  2080. return copyArray(value, result);
  2081. }
  2082. } else {
  2083. var tag = getTag(value),
  2084. isFunc = tag == funcTag || tag == genTag;
  2085. if (isBuffer(value)) {
  2086. return cloneBuffer(value, isDeep);
  2087. }
  2088. if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
  2089. if (isHostObject(value)) {
  2090. return object ? value : {};
  2091. }
  2092. result = initCloneObject(isFunc ? {} : value);
  2093. if (!isDeep) {
  2094. return copySymbols(value, baseAssign(result, value));
  2095. }
  2096. } else {
  2097. if (!cloneableTags[tag]) {
  2098. return object ? value : {};
  2099. }
  2100. result = initCloneByTag(value, tag, baseClone, isDeep);
  2101. }
  2102. }
  2103. // Check for circular references and return its corresponding clone.
  2104. stack || (stack = new Stack);
  2105. var stacked = stack.get(value);
  2106. if (stacked) {
  2107. return stacked;
  2108. }
  2109. stack.set(value, result);
  2110. if (!isArr) {
  2111. var props = isFull ? getAllKeys(value) : keys(value);
  2112. }
  2113. // Recursively populate clone (susceptible to call stack limits).
  2114. arrayEach(props || value, function(subValue, key) {
  2115. if (props) {
  2116. key = subValue;
  2117. subValue = value[key];
  2118. }
  2119. assignValue(result, key, baseClone(subValue, isDeep, isFull, customizer, key, value, stack));
  2120. });
  2121. return result;
  2122. }
  2123. /**
  2124. * The base implementation of `_.conforms` which doesn't clone `source`.
  2125. *
  2126. * @private
  2127. * @param {Object} source The object of property predicates to conform to.
  2128. * @returns {Function} Returns the new spec function.
  2129. */
  2130. function baseConforms(source) {
  2131. var props = keys(source),
  2132. length = props.length;
  2133. return function(object) {
  2134. if (object == null) {
  2135. return !length;
  2136. }
  2137. var index = length;
  2138. while (index--) {
  2139. var key = props[index],
  2140. predicate = source[key],
  2141. value = object[key];
  2142. if ((value === undefined &&
  2143. !(key in Object(object))) || !predicate(value)) {
  2144. return false;
  2145. }
  2146. }
  2147. return true;
  2148. };
  2149. }
  2150. /**
  2151. * The base implementation of `_.create` without support for assigning
  2152. * properties to the created object.
  2153. *
  2154. * @private
  2155. * @param {Object} prototype The object to inherit from.
  2156. * @returns {Object} Returns the new object.
  2157. */
  2158. function baseCreate(proto) {
  2159. return isObject(proto) ? objectCreate(proto) : {};
  2160. }
  2161. /**
  2162. * The base implementation of `_.delay` and `_.defer` which accepts an array
  2163. * of `func` arguments.
  2164. *
  2165. * @private
  2166. * @param {Function} func The function to delay.
  2167. * @param {number} wait The number of milliseconds to delay invocation.
  2168. * @param {Object} args The arguments to provide to `func`.
  2169. * @returns {number} Returns the timer id.
  2170. */
  2171. function baseDelay(func, wait, args) {
  2172. if (typeof func != 'function') {
  2173. throw new TypeError(FUNC_ERROR_TEXT);
  2174. }
  2175. return setTimeout(function() { func.apply(undefined, args); }, wait);
  2176. }
  2177. /**
  2178. * The base implementation of methods like `_.difference` without support
  2179. * for excluding multiple arrays or iteratee shorthands.
  2180. *
  2181. * @private
  2182. * @param {Array} array The array to inspect.
  2183. * @param {Array} values The values to exclude.
  2184. * @param {Function} [iteratee] The iteratee invoked per element.
  2185. * @param {Function} [comparator] The comparator invoked per element.
  2186. * @returns {Array} Returns the new array of filtered values.
  2187. */
  2188. function baseDifference(array, values, iteratee, comparator) {
  2189. var index = -1,
  2190. includes = arrayIncludes,
  2191. isCommon = true,
  2192. length = array.length,
  2193. result = [],
  2194. valuesLength = values.length;
  2195. if (!length) {
  2196. return result;
  2197. }
  2198. if (iteratee) {
  2199. values = arrayMap(values, baseUnary(iteratee));
  2200. }
  2201. if (comparator) {
  2202. includes = arrayIncludesWith;
  2203. isCommon = false;
  2204. }
  2205. else if (values.length >= LARGE_ARRAY_SIZE) {
  2206. includes = cacheHas;
  2207. isCommon = false;
  2208. values = new SetCache(values);
  2209. }
  2210. outer:
  2211. while (++index < length) {
  2212. var value = array[index],
  2213. computed = iteratee ? iteratee(value) : value;
  2214. value = (comparator || value !== 0) ? value : 0;
  2215. if (isCommon && computed === computed) {
  2216. var valuesIndex = valuesLength;
  2217. while (valuesIndex--) {
  2218. if (values[valuesIndex] === computed) {
  2219. continue outer;
  2220. }
  2221. }
  2222. result.push(value);
  2223. }
  2224. else if (!includes(values, computed, comparator)) {
  2225. result.push(value);
  2226. }
  2227. }
  2228. return result;
  2229. }
  2230. /**
  2231. * The base implementation of `_.forEach` without support for iteratee shorthands.
  2232. *
  2233. * @private
  2234. * @param {Array|Object} collection The collection to iterate over.
  2235. * @param {Function} iteratee The function invoked per iteration.
  2236. * @returns {Array|Object} Returns `collection`.
  2237. */
  2238. var baseEach = createBaseEach(baseForOwn);
  2239. /**
  2240. * The base implementation of `_.forEachRight` without support for iteratee shorthands.
  2241. *
  2242. * @private
  2243. * @param {Array|Object} collection The collection to iterate over.
  2244. * @param {Function} iteratee The function invoked per iteration.
  2245. * @returns {Array|Object} Returns `collection`.
  2246. */
  2247. var baseEachRight = createBaseEach(baseForOwnRight, true);
  2248. /**
  2249. * The base implementation of `_.every` without support for iteratee shorthands.
  2250. *
  2251. * @private
  2252. * @param {Array|Object} collection The collection to iterate over.
  2253. * @param {Function} predicate The function invoked per iteration.
  2254. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  2255. * else `false`
  2256. */
  2257. function baseEvery(collection, predicate) {
  2258. var result = true;
  2259. baseEach(collection, function(value, index, collection) {
  2260. result = !!predicate(value, index, collection);
  2261. return result;
  2262. });
  2263. return result;
  2264. }
  2265. /**
  2266. * The base implementation of methods like `_.max` and `_.min` which accepts a
  2267. * `comparator` to determine the extremum value.
  2268. *
  2269. * @private
  2270. * @param {Array} array The array to iterate over.
  2271. * @param {Function} iteratee The iteratee invoked per iteration.
  2272. * @param {Function} comparator The comparator used to compare values.
  2273. * @returns {*} Returns the extremum value.
  2274. */
  2275. function baseExtremum(array, iteratee, comparator) {
  2276. var index = -1,
  2277. length = array.length;
  2278. while (++index < length) {
  2279. var value = array[index],
  2280. current = iteratee(value);
  2281. if (current != null && (computed === undefined
  2282. ? (current === current && !isSymbol(current))
  2283. : comparator(current, computed)
  2284. )) {
  2285. var computed = current,
  2286. result = value;
  2287. }
  2288. }
  2289. return result;
  2290. }
  2291. /**
  2292. * The base implementation of `_.fill` without an iteratee call guard.
  2293. *
  2294. * @private
  2295. * @param {Array} array The array to fill.
  2296. * @param {*} value The value to fill `array` with.
  2297. * @param {number} [start=0] The start position.
  2298. * @param {number} [end=array.length] The end position.
  2299. * @returns {Array} Returns `array`.
  2300. */
  2301. function baseFill(array, value, start, end) {
  2302. var length = array.length;
  2303. start = toInteger(start);
  2304. if (start < 0) {
  2305. start = -start > length ? 0 : (length + start);
  2306. }
  2307. end = (end === undefined || end > length) ? length : toInteger(end);
  2308. if (end < 0) {
  2309. end += length;
  2310. }
  2311. end = start > end ? 0 : toLength(end);
  2312. while (start < end) {
  2313. array[start++] = value;
  2314. }
  2315. return array;
  2316. }
  2317. /**
  2318. * The base implementation of `_.filter` without support for iteratee shorthands.
  2319. *
  2320. * @private
  2321. * @param {Array|Object} collection The collection to iterate over.
  2322. * @param {Function} predicate The function invoked per iteration.
  2323. * @returns {Array} Returns the new filtered array.
  2324. */
  2325. function baseFilter(collection, predicate) {
  2326. var result = [];
  2327. baseEach(collection, function(value, index, collection) {
  2328. if (predicate(value, index, collection)) {
  2329. result.push(value);
  2330. }
  2331. });
  2332. return result;
  2333. }
  2334. /**
  2335. * The base implementation of `_.flatten` with support for restricting flattening.
  2336. *
  2337. * @private
  2338. * @param {Array} array The array to flatten.
  2339. * @param {number} depth The maximum recursion depth.
  2340. * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
  2341. * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
  2342. * @param {Array} [result=[]] The initial result value.
  2343. * @returns {Array} Returns the new flattened array.
  2344. */
  2345. function baseFlatten(array, depth, predicate, isStrict, result) {
  2346. var index = -1,
  2347. length = array.length;
  2348. predicate || (predicate = isFlattenable);
  2349. result || (result = []);
  2350. while (++index < length) {
  2351. var value = array[index];
  2352. if (depth > 0 && predicate(value)) {
  2353. if (depth > 1) {
  2354. // Recursively flatten arrays (susceptible to call stack limits).
  2355. baseFlatten(value, depth - 1, predicate, isStrict, result);
  2356. } else {
  2357. arrayPush(result, value);
  2358. }
  2359. } else if (!isStrict) {
  2360. result[result.length] = value;
  2361. }
  2362. }
  2363. return result;
  2364. }
  2365. /**
  2366. * The base implementation of `baseForOwn` which iterates over `object`
  2367. * properties returned by `keysFunc` and invokes `iteratee` for each property.
  2368. * Iteratee functions may exit iteration early by explicitly returning `false`.
  2369. *
  2370. * @private
  2371. * @param {Object} object The object to iterate over.
  2372. * @param {Function} iteratee The function invoked per iteration.
  2373. * @param {Function} keysFunc The function to get the keys of `object`.
  2374. * @returns {Object} Returns `object`.
  2375. */
  2376. var baseFor = createBaseFor();
  2377. /**
  2378. * This function is like `baseFor` except that it iterates over properties
  2379. * in the opposite order.
  2380. *
  2381. * @private
  2382. * @param {Object} object The object to iterate over.
  2383. * @param {Function} iteratee The function invoked per iteration.
  2384. * @param {Function} keysFunc The function to get the keys of `object`.
  2385. * @returns {Object} Returns `object`.
  2386. */
  2387. var baseForRight = createBaseFor(true);
  2388. /**
  2389. * The base implementation of `_.forOwn` without support for iteratee shorthands.
  2390. *
  2391. * @private
  2392. * @param {Object} object The object to iterate over.
  2393. * @param {Function} iteratee The function invoked per iteration.
  2394. * @returns {Object} Returns `object`.
  2395. */
  2396. function baseForOwn(object, iteratee) {
  2397. return object && baseFor(object, iteratee, keys);
  2398. }
  2399. /**
  2400. * The base implementation of `_.forOwnRight` without support for iteratee shorthands.
  2401. *
  2402. * @private
  2403. * @param {Object} object The object to iterate over.
  2404. * @param {Function} iteratee The function invoked per iteration.
  2405. * @returns {Object} Returns `object`.
  2406. */
  2407. function baseForOwnRight(object, iteratee) {
  2408. return object && baseForRight(object, iteratee, keys);
  2409. }
  2410. /**
  2411. * The base implementation of `_.functions` which creates an array of
  2412. * `object` function property names filtered from `props`.
  2413. *
  2414. * @private
  2415. * @param {Object} object The object to inspect.
  2416. * @param {Array} props The property names to filter.
  2417. * @returns {Array} Returns the function names.
  2418. */
  2419. function baseFunctions(object, props) {
  2420. return arrayFilter(props, function(key) {
  2421. return isFunction(object[key]);
  2422. });
  2423. }
  2424. /**
  2425. * The base implementation of `_.get` without support for default values.
  2426. *
  2427. * @private
  2428. * @param {Object} object The object to query.
  2429. * @param {Array|string} path The path of the property to get.
  2430. * @returns {*} Returns the resolved value.
  2431. */
  2432. function baseGet(object, path) {
  2433. path = isKey(path, object) ? [path] : castPath(path);
  2434. var index = 0,
  2435. length = path.length;
  2436. while (object != null && index < length) {
  2437. object = object[toKey(path[index++])];
  2438. }
  2439. return (index && index == length) ? object : undefined;
  2440. }
  2441. /**
  2442. * The base implementation of `getAllKeys` and `getAllKeysIn` which uses
  2443. * `keysFunc` and `symbolsFunc` to get the enumerable property names and
  2444. * symbols of `object`.
  2445. *
  2446. * @private
  2447. * @param {Object} object The object to query.
  2448. * @param {Function} keysFunc The function to get the keys of `object`.
  2449. * @param {Function} symbolsFunc The function to get the symbols of `object`.
  2450. * @returns {Array} Returns the array of property names and symbols.
  2451. */
  2452. function baseGetAllKeys(object, keysFunc, symbolsFunc) {
  2453. var result = keysFunc(object);
  2454. return isArray(object) ? result : arrayPush(result, symbolsFunc(object));
  2455. }
  2456. /**
  2457. * The base implementation of `_.gt` which doesn't coerce arguments to numbers.
  2458. *
  2459. * @private
  2460. * @param {*} value The value to compare.
  2461. * @param {*} other The other value to compare.
  2462. * @returns {boolean} Returns `true` if `value` is greater than `other`,
  2463. * else `false`.
  2464. */
  2465. function baseGt(value, other) {
  2466. return value > other;
  2467. }
  2468. /**
  2469. * The base implementation of `_.has` without support for deep paths.
  2470. *
  2471. * @private
  2472. * @param {Object} [object] The object to query.
  2473. * @param {Array|string} key The key to check.
  2474. * @returns {boolean} Returns `true` if `key` exists, else `false`.
  2475. */
  2476. function baseHas(object, key) {
  2477. // Avoid a bug in IE 10-11 where objects with a [[Prototype]] of `null`,
  2478. // that are composed entirely of index properties, return `false` for
  2479. // `hasOwnProperty` checks of them.
  2480. return object != null &&
  2481. (hasOwnProperty.call(object, key) ||
  2482. (typeof object == 'object' && key in object && getPrototype(object) === null));
  2483. }
  2484. /**
  2485. * The base implementation of `_.hasIn` without support for deep paths.
  2486. *
  2487. * @private
  2488. * @param {Object} [object] The object to query.
  2489. * @param {Array|string} key The key to check.
  2490. * @returns {boolean} Returns `true` if `key` exists, else `false`.
  2491. */
  2492. function baseHasIn(object, key) {
  2493. return object != null && key in Object(object);
  2494. }
  2495. /**
  2496. * The base implementation of `_.inRange` which doesn't coerce arguments to numbers.
  2497. *
  2498. * @private
  2499. * @param {number} number The number to check.
  2500. * @param {number} start The start of the range.
  2501. * @param {number} end The end of the range.
  2502. * @returns {boolean} Returns `true` if `number` is in the range, else `false`.
  2503. */
  2504. function baseInRange(number, start, end) {
  2505. return number >= nativeMin(start, end) && number < nativeMax(start, end);
  2506. }
  2507. /**
  2508. * The base implementation of methods like `_.intersection`, without support
  2509. * for iteratee shorthands, that accepts an array of arrays to inspect.
  2510. *
  2511. * @private
  2512. * @param {Array} arrays The arrays to inspect.
  2513. * @param {Function} [iteratee] The iteratee invoked per element.
  2514. * @param {Function} [comparator] The comparator invoked per element.
  2515. * @returns {Array} Returns the new array of shared values.
  2516. */
  2517. function baseIntersection(arrays, iteratee, comparator) {
  2518. var includes = comparator ? arrayIncludesWith : arrayIncludes,
  2519. length = arrays[0].length,
  2520. othLength = arrays.length,
  2521. othIndex = othLength,
  2522. caches = Array(othLength),
  2523. maxLength = Infinity,
  2524. result = [];
  2525. while (othIndex--) {
  2526. var array = arrays[othIndex];
  2527. if (othIndex && iteratee) {
  2528. array = arrayMap(array, baseUnary(iteratee));
  2529. }
  2530. maxLength = nativeMin(array.length, maxLength);
  2531. caches[othIndex] = !comparator && (iteratee || (length >= 120 && array.length >= 120))
  2532. ? new SetCache(othIndex && array)
  2533. : undefined;
  2534. }
  2535. array = arrays[0];
  2536. var index = -1,
  2537. seen = caches[0];
  2538. outer:
  2539. while (++index < length && result.length < maxLength) {
  2540. var value = array[index],
  2541. computed = iteratee ? iteratee(value) : value;
  2542. value = (comparator || value !== 0) ? value : 0;
  2543. if (!(seen
  2544. ? cacheHas(seen, computed)
  2545. : includes(result, computed, comparator)
  2546. )) {
  2547. othIndex = othLength;
  2548. while (--othIndex) {
  2549. var cache = caches[othIndex];
  2550. if (!(cache
  2551. ? cacheHas(cache, computed)
  2552. : includes(arrays[othIndex], computed, comparator))
  2553. ) {
  2554. continue outer;
  2555. }
  2556. }
  2557. if (seen) {
  2558. seen.push(computed);
  2559. }
  2560. result.push(value);
  2561. }
  2562. }
  2563. return result;
  2564. }
  2565. /**
  2566. * The base implementation of `_.invert` and `_.invertBy` which inverts
  2567. * `object` with values transformed by `iteratee` and set by `setter`.
  2568. *
  2569. * @private
  2570. * @param {Object} object The object to iterate over.
  2571. * @param {Function} setter The function to set `accumulator` values.
  2572. * @param {Function} iteratee The iteratee to transform values.
  2573. * @param {Object} accumulator The initial inverted object.
  2574. * @returns {Function} Returns `accumulator`.
  2575. */
  2576. function baseInverter(object, setter, iteratee, accumulator) {
  2577. baseForOwn(object, function(value, key, object) {
  2578. setter(accumulator, iteratee(value), key, object);
  2579. });
  2580. return accumulator;
  2581. }
  2582. /**
  2583. * The base implementation of `_.invoke` without support for individual
  2584. * method arguments.
  2585. *
  2586. * @private
  2587. * @param {Object} object The object to query.
  2588. * @param {Array|string} path The path of the method to invoke.
  2589. * @param {Array} args The arguments to invoke the method with.
  2590. * @returns {*} Returns the result of the invoked method.
  2591. */
  2592. function baseInvoke(object, path, args) {
  2593. if (!isKey(path, object)) {
  2594. path = castPath(path);
  2595. object = parent(object, path);
  2596. path = last(path);
  2597. }
  2598. var func = object == null ? object : object[toKey(path)];
  2599. return func == null ? undefined : apply(func, object, args);
  2600. }
  2601. /**
  2602. * The base implementation of `_.isEqual` which supports partial comparisons
  2603. * and tracks traversed objects.
  2604. *
  2605. * @private
  2606. * @param {*} value The value to compare.
  2607. * @param {*} other The other value to compare.
  2608. * @param {Function} [customizer] The function to customize comparisons.
  2609. * @param {boolean} [bitmask] The bitmask of comparison flags.
  2610. * The bitmask may be composed of the following flags:
  2611. * 1 - Unordered comparison
  2612. * 2 - Partial comparison
  2613. * @param {Object} [stack] Tracks traversed `value` and `other` objects.
  2614. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  2615. */
  2616. function baseIsEqual(value, other, customizer, bitmask, stack) {
  2617. if (value === other) {
  2618. return true;
  2619. }
  2620. if (value == null || other == null || (!isObject(value) && !isObjectLike(other))) {
  2621. return value !== value && other !== other;
  2622. }
  2623. return baseIsEqualDeep(value, other, baseIsEqual, customizer, bitmask, stack);
  2624. }
  2625. /**
  2626. * A specialized version of `baseIsEqual` for arrays and objects which performs
  2627. * deep comparisons and tracks traversed objects enabling objects with circular
  2628. * references to be compared.
  2629. *
  2630. * @private
  2631. * @param {Object} object The object to compare.
  2632. * @param {Object} other The other object to compare.
  2633. * @param {Function} equalFunc The function to determine equivalents of values.
  2634. * @param {Function} [customizer] The function to customize comparisons.
  2635. * @param {number} [bitmask] The bitmask of comparison flags. See `baseIsEqual`
  2636. * for more details.
  2637. * @param {Object} [stack] Tracks traversed `object` and `other` objects.
  2638. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  2639. */
  2640. function baseIsEqualDeep(object, other, equalFunc, customizer, bitmask, stack) {
  2641. var objIsArr = isArray(object),
  2642. othIsArr = isArray(other),
  2643. objTag = arrayTag,
  2644. othTag = arrayTag;
  2645. if (!objIsArr) {
  2646. objTag = getTag(object);
  2647. objTag = objTag == argsTag ? objectTag : objTag;
  2648. }
  2649. if (!othIsArr) {
  2650. othTag = getTag(other);
  2651. othTag = othTag == argsTag ? objectTag : othTag;
  2652. }
  2653. var objIsObj = objTag == objectTag && !isHostObject(object),
  2654. othIsObj = othTag == objectTag && !isHostObject(other),
  2655. isSameTag = objTag == othTag;
  2656. if (isSameTag && !objIsObj) {
  2657. stack || (stack = new Stack);
  2658. return (objIsArr || isTypedArray(object))
  2659. ? equalArrays(object, other, equalFunc, customizer, bitmask, stack)
  2660. : equalByTag(object, other, objTag, equalFunc, customizer, bitmask, stack);
  2661. }
  2662. if (!(bitmask & PARTIAL_COMPARE_FLAG)) {
  2663. var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
  2664. othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
  2665. if (objIsWrapped || othIsWrapped) {
  2666. var objUnwrapped = objIsWrapped ? object.value() : object,
  2667. othUnwrapped = othIsWrapped ? other.value() : other;
  2668. stack || (stack = new Stack);
  2669. return equalFunc(objUnwrapped, othUnwrapped, customizer, bitmask, stack);
  2670. }
  2671. }
  2672. if (!isSameTag) {
  2673. return false;
  2674. }
  2675. stack || (stack = new Stack);
  2676. return equalObjects(object, other, equalFunc, customizer, bitmask, stack);
  2677. }
  2678. /**
  2679. * The base implementation of `_.isMatch` without support for iteratee shorthands.
  2680. *
  2681. * @private
  2682. * @param {Object} object The object to inspect.
  2683. * @param {Object} source The object of property values to match.
  2684. * @param {Array} matchData The property names, values, and compare flags to match.
  2685. * @param {Function} [customizer] The function to customize comparisons.
  2686. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  2687. */
  2688. function baseIsMatch(object, source, matchData, customizer) {
  2689. var index = matchData.length,
  2690. length = index,
  2691. noCustomizer = !customizer;
  2692. if (object == null) {
  2693. return !length;
  2694. }
  2695. object = Object(object);
  2696. while (index--) {
  2697. var data = matchData[index];
  2698. if ((noCustomizer && data[2])
  2699. ? data[1] !== object[data[0]]
  2700. : !(data[0] in object)
  2701. ) {
  2702. return false;
  2703. }
  2704. }
  2705. while (++index < length) {
  2706. data = matchData[index];
  2707. var key = data[0],
  2708. objValue = object[key],
  2709. srcValue = data[1];
  2710. if (noCustomizer && data[2]) {
  2711. if (objValue === undefined && !(key in object)) {
  2712. return false;
  2713. }
  2714. } else {
  2715. var stack = new Stack;
  2716. if (customizer) {
  2717. var result = customizer(objValue, srcValue, key, object, source, stack);
  2718. }
  2719. if (!(result === undefined
  2720. ? baseIsEqual(srcValue, objValue, customizer, UNORDERED_COMPARE_FLAG | PARTIAL_COMPARE_FLAG, stack)
  2721. : result
  2722. )) {
  2723. return false;
  2724. }
  2725. }
  2726. }
  2727. return true;
  2728. }
  2729. /**
  2730. * The base implementation of `_.isNative` without bad shim checks.
  2731. *
  2732. * @private
  2733. * @param {*} value The value to check.
  2734. * @returns {boolean} Returns `true` if `value` is a native function,
  2735. * else `false`.
  2736. */
  2737. function baseIsNative(value) {
  2738. if (!isObject(value) || isMasked(value)) {
  2739. return false;
  2740. }
  2741. var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
  2742. return pattern.test(toSource(value));
  2743. }
  2744. /**
  2745. * The base implementation of `_.iteratee`.
  2746. *
  2747. * @private
  2748. * @param {*} [value=_.identity] The value to convert to an iteratee.
  2749. * @returns {Function} Returns the iteratee.
  2750. */
  2751. function baseIteratee(value) {
  2752. // Don't store the `typeof` result in a variable to avoid a JIT bug in Safari 9.
  2753. // See https://bugs.webkit.org/show_bug.cgi?id=156034 for more details.
  2754. if (typeof value == 'function') {
  2755. return value;
  2756. }
  2757. if (value == null) {
  2758. return identity;
  2759. }
  2760. if (typeof value == 'object') {
  2761. return isArray(value)
  2762. ? baseMatchesProperty(value[0], value[1])
  2763. : baseMatches(value);
  2764. }
  2765. return property(value);
  2766. }
  2767. /**
  2768. * The base implementation of `_.keys` which doesn't skip the constructor
  2769. * property of prototypes or treat sparse arrays as dense.
  2770. *
  2771. * @private
  2772. * @param {Object} object The object to query.
  2773. * @returns {Array} Returns the array of property names.
  2774. */
  2775. function baseKeys(object) {
  2776. return nativeKeys(Object(object));
  2777. }
  2778. /**
  2779. * The base implementation of `_.keysIn` which doesn't skip the constructor
  2780. * property of prototypes or treat sparse arrays as dense.
  2781. *
  2782. * @private
  2783. * @param {Object} object The object to query.
  2784. * @returns {Array} Returns the array of property names.
  2785. */
  2786. function baseKeysIn(object) {
  2787. object = object == null ? object : Object(object);
  2788. var result = [];
  2789. for (var key in object) {
  2790. result.push(key);
  2791. }
  2792. return result;
  2793. }
  2794. // Fallback for IE < 9 with es6-shim.
  2795. if (enumerate && !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf')) {
  2796. baseKeysIn = function(object) {
  2797. return iteratorToArray(enumerate(object));
  2798. };
  2799. }
  2800. /**
  2801. * The base implementation of `_.lt` which doesn't coerce arguments to numbers.
  2802. *
  2803. * @private
  2804. * @param {*} value The value to compare.
  2805. * @param {*} other The other value to compare.
  2806. * @returns {boolean} Returns `true` if `value` is less than `other`,
  2807. * else `false`.
  2808. */
  2809. function baseLt(value, other) {
  2810. return value < other;
  2811. }
  2812. /**
  2813. * The base implementation of `_.map` without support for iteratee shorthands.
  2814. *
  2815. * @private
  2816. * @param {Array|Object} collection The collection to iterate over.
  2817. * @param {Function} iteratee The function invoked per iteration.
  2818. * @returns {Array} Returns the new mapped array.
  2819. */
  2820. function baseMap(collection, iteratee) {
  2821. var index = -1,
  2822. result = isArrayLike(collection) ? Array(collection.length) : [];
  2823. baseEach(collection, function(value, key, collection) {
  2824. result[++index] = iteratee(value, key, collection);
  2825. });
  2826. return result;
  2827. }
  2828. /**
  2829. * The base implementation of `_.matches` which doesn't clone `source`.
  2830. *
  2831. * @private
  2832. * @param {Object} source The object of property values to match.
  2833. * @returns {Function} Returns the new spec function.
  2834. */
  2835. function baseMatches(source) {
  2836. var matchData = getMatchData(source);
  2837. if (matchData.length == 1 && matchData[0][2]) {
  2838. return matchesStrictComparable(matchData[0][0], matchData[0][1]);
  2839. }
  2840. return function(object) {
  2841. return object === source || baseIsMatch(object, source, matchData);
  2842. };
  2843. }
  2844. /**
  2845. * The base implementation of `_.matchesProperty` which doesn't clone `srcValue`.
  2846. *
  2847. * @private
  2848. * @param {string} path The path of the property to get.
  2849. * @param {*} srcValue The value to match.
  2850. * @returns {Function} Returns the new spec function.
  2851. */
  2852. function baseMatchesProperty(path, srcValue) {
  2853. if (isKey(path) && isStrictComparable(srcValue)) {
  2854. return matchesStrictComparable(toKey(path), srcValue);
  2855. }
  2856. return function(object) {
  2857. var objValue = get(object, path);
  2858. return (objValue === undefined && objValue === srcValue)
  2859. ? hasIn(object, path)
  2860. : baseIsEqual(srcValue, objValue, undefined, UNORDERED_COMPARE_FLAG | PARTIAL_COMPARE_FLAG);
  2861. };
  2862. }
  2863. /**
  2864. * The base implementation of `_.merge` without support for multiple sources.
  2865. *
  2866. * @private
  2867. * @param {Object} object The destination object.
  2868. * @param {Object} source The source object.
  2869. * @param {number} srcIndex The index of `source`.
  2870. * @param {Function} [customizer] The function to customize merged values.
  2871. * @param {Object} [stack] Tracks traversed source values and their merged
  2872. * counterparts.
  2873. */
  2874. function baseMerge(object, source, srcIndex, customizer, stack) {
  2875. if (object === source) {
  2876. return;
  2877. }
  2878. if (!(isArray(source) || isTypedArray(source))) {
  2879. var props = keysIn(source);
  2880. }
  2881. arrayEach(props || source, function(srcValue, key) {
  2882. if (props) {
  2883. key = srcValue;
  2884. srcValue = source[key];
  2885. }
  2886. if (isObject(srcValue)) {
  2887. stack || (stack = new Stack);
  2888. baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack);
  2889. }
  2890. else {
  2891. var newValue = customizer
  2892. ? customizer(object[key], srcValue, (key + ''), object, source, stack)
  2893. : undefined;
  2894. if (newValue === undefined) {
  2895. newValue = srcValue;
  2896. }
  2897. assignMergeValue(object, key, newValue);
  2898. }
  2899. });
  2900. }
  2901. /**
  2902. * A specialized version of `baseMerge` for arrays and objects which performs
  2903. * deep merges and tracks traversed objects enabling objects with circular
  2904. * references to be merged.
  2905. *
  2906. * @private
  2907. * @param {Object} object The destination object.
  2908. * @param {Object} source The source object.
  2909. * @param {string} key The key of the value to merge.
  2910. * @param {number} srcIndex The index of `source`.
  2911. * @param {Function} mergeFunc The function to merge values.
  2912. * @param {Function} [customizer] The function to customize assigned values.
  2913. * @param {Object} [stack] Tracks traversed source values and their merged
  2914. * counterparts.
  2915. */
  2916. function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
  2917. var objValue = object[key],
  2918. srcValue = source[key],
  2919. stacked = stack.get(srcValue);
  2920. if (stacked) {
  2921. assignMergeValue(object, key, stacked);
  2922. return;
  2923. }
  2924. var newValue = customizer
  2925. ? customizer(objValue, srcValue, (key + ''), object, source, stack)
  2926. : undefined;
  2927. var isCommon = newValue === undefined;
  2928. if (isCommon) {
  2929. newValue = srcValue;
  2930. if (isArray(srcValue) || isTypedArray(srcValue)) {
  2931. if (isArray(objValue)) {
  2932. newValue = objValue;
  2933. }
  2934. else if (isArrayLikeObject(objValue)) {
  2935. newValue = copyArray(objValue);
  2936. }
  2937. else {
  2938. isCommon = false;
  2939. newValue = baseClone(srcValue, true);
  2940. }
  2941. }
  2942. else if (isPlainObject(srcValue) || isArguments(srcValue)) {
  2943. if (isArguments(objValue)) {
  2944. newValue = toPlainObject(objValue);
  2945. }
  2946. else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) {
  2947. isCommon = false;
  2948. newValue = baseClone(srcValue, true);
  2949. }
  2950. else {
  2951. newValue = objValue;
  2952. }
  2953. }
  2954. else {
  2955. isCommon = false;
  2956. }
  2957. }
  2958. stack.set(srcValue, newValue);
  2959. if (isCommon) {
  2960. // Recursively merge objects and arrays (susceptible to call stack limits).
  2961. mergeFunc(newValue, srcValue, srcIndex, customizer, stack);
  2962. }
  2963. stack['delete'](srcValue);
  2964. assignMergeValue(object, key, newValue);
  2965. }
  2966. /**
  2967. * The base implementation of `_.nth` which doesn't coerce `n` to an integer.
  2968. *
  2969. * @private
  2970. * @param {Array} array The array to query.
  2971. * @param {number} n The index of the element to return.
  2972. * @returns {*} Returns the nth element of `array`.
  2973. */
  2974. function baseNth(array, n) {
  2975. var length = array.length;
  2976. if (!length) {
  2977. return;
  2978. }
  2979. n += n < 0 ? length : 0;
  2980. return isIndex(n, length) ? array[n] : undefined;
  2981. }
  2982. /**
  2983. * The base implementation of `_.orderBy` without param guards.
  2984. *
  2985. * @private
  2986. * @param {Array|Object} collection The collection to iterate over.
  2987. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
  2988. * @param {string[]} orders The sort orders of `iteratees`.
  2989. * @returns {Array} Returns the new sorted array.
  2990. */
  2991. function baseOrderBy(collection, iteratees, orders) {
  2992. var index = -1;
  2993. iteratees = arrayMap(iteratees.length ? iteratees : [identity], baseUnary(getIteratee()));
  2994. var result = baseMap(collection, function(value, key, collection) {
  2995. var criteria = arrayMap(iteratees, function(iteratee) {
  2996. return iteratee(value);
  2997. });
  2998. return { 'criteria': criteria, 'index': ++index, 'value': value };
  2999. });
  3000. return baseSortBy(result, function(object, other) {
  3001. return compareMultiple(object, other, orders);
  3002. });
  3003. }
  3004. /**
  3005. * The base implementation of `_.pick` without support for individual
  3006. * property identifiers.
  3007. *
  3008. * @private
  3009. * @param {Object} object The source object.
  3010. * @param {string[]} props The property identifiers to pick.
  3011. * @returns {Object} Returns the new object.
  3012. */
  3013. function basePick(object, props) {
  3014. object = Object(object);
  3015. return arrayReduce(props, function(result, key) {
  3016. if (key in object) {
  3017. result[key] = object[key];
  3018. }
  3019. return result;
  3020. }, {});
  3021. }
  3022. /**
  3023. * The base implementation of `_.pickBy` without support for iteratee shorthands.
  3024. *
  3025. * @private
  3026. * @param {Object} object The source object.
  3027. * @param {Function} predicate The function invoked per property.
  3028. * @returns {Object} Returns the new object.
  3029. */
  3030. function basePickBy(object, predicate) {
  3031. var index = -1,
  3032. props = getAllKeysIn(object),
  3033. length = props.length,
  3034. result = {};
  3035. while (++index < length) {
  3036. var key = props[index],
  3037. value = object[key];
  3038. if (predicate(value, key)) {
  3039. result[key] = value;
  3040. }
  3041. }
  3042. return result;
  3043. }
  3044. /**
  3045. * The base implementation of `_.property` without support for deep paths.
  3046. *
  3047. * @private
  3048. * @param {string} key The key of the property to get.
  3049. * @returns {Function} Returns the new accessor function.
  3050. */
  3051. function baseProperty(key) {
  3052. return function(object) {
  3053. return object == null ? undefined : object[key];
  3054. };
  3055. }
  3056. /**
  3057. * A specialized version of `baseProperty` which supports deep paths.
  3058. *
  3059. * @private
  3060. * @param {Array|string} path The path of the property to get.
  3061. * @returns {Function} Returns the new accessor function.
  3062. */
  3063. function basePropertyDeep(path) {
  3064. return function(object) {
  3065. return baseGet(object, path);
  3066. };
  3067. }
  3068. /**
  3069. * The base implementation of `_.pullAllBy` without support for iteratee
  3070. * shorthands.
  3071. *
  3072. * @private
  3073. * @param {Array} array The array to modify.
  3074. * @param {Array} values The values to remove.
  3075. * @param {Function} [iteratee] The iteratee invoked per element.
  3076. * @param {Function} [comparator] The comparator invoked per element.
  3077. * @returns {Array} Returns `array`.
  3078. */
  3079. function basePullAll(array, values, iteratee, comparator) {
  3080. var indexOf = comparator ? baseIndexOfWith : baseIndexOf,
  3081. index = -1,
  3082. length = values.length,
  3083. seen = array;
  3084. if (array === values) {
  3085. values = copyArray(values);
  3086. }
  3087. if (iteratee) {
  3088. seen = arrayMap(array, baseUnary(iteratee));
  3089. }
  3090. while (++index < length) {
  3091. var fromIndex = 0,
  3092. value = values[index],
  3093. computed = iteratee ? iteratee(value) : value;
  3094. while ((fromIndex = indexOf(seen, computed, fromIndex, comparator)) > -1) {
  3095. if (seen !== array) {
  3096. splice.call(seen, fromIndex, 1);
  3097. }
  3098. splice.call(array, fromIndex, 1);
  3099. }
  3100. }
  3101. return array;
  3102. }
  3103. /**
  3104. * The base implementation of `_.pullAt` without support for individual
  3105. * indexes or capturing the removed elements.
  3106. *
  3107. * @private
  3108. * @param {Array} array The array to modify.
  3109. * @param {number[]} indexes The indexes of elements to remove.
  3110. * @returns {Array} Returns `array`.
  3111. */
  3112. function basePullAt(array, indexes) {
  3113. var length = array ? indexes.length : 0,
  3114. lastIndex = length - 1;
  3115. while (length--) {
  3116. var index = indexes[length];
  3117. if (length == lastIndex || index !== previous) {
  3118. var previous = index;
  3119. if (isIndex(index)) {
  3120. splice.call(array, index, 1);
  3121. }
  3122. else if (!isKey(index, array)) {
  3123. var path = castPath(index),
  3124. object = parent(array, path);
  3125. if (object != null) {
  3126. delete object[toKey(last(path))];
  3127. }
  3128. }
  3129. else {
  3130. delete array[toKey(index)];
  3131. }
  3132. }
  3133. }
  3134. return array;
  3135. }
  3136. /**
  3137. * The base implementation of `_.random` without support for returning
  3138. * floating-point numbers.
  3139. *
  3140. * @private
  3141. * @param {number} lower The lower bound.
  3142. * @param {number} upper The upper bound.
  3143. * @returns {number} Returns the random number.
  3144. */
  3145. function baseRandom(lower, upper) {
  3146. return lower + nativeFloor(nativeRandom() * (upper - lower + 1));
  3147. }
  3148. /**
  3149. * The base implementation of `_.range` and `_.rangeRight` which doesn't
  3150. * coerce arguments to numbers.
  3151. *
  3152. * @private
  3153. * @param {number} start The start of the range.
  3154. * @param {number} end The end of the range.
  3155. * @param {number} step The value to increment or decrement by.
  3156. * @param {boolean} [fromRight] Specify iterating from right to left.
  3157. * @returns {Array} Returns the range of numbers.
  3158. */
  3159. function baseRange(start, end, step, fromRight) {
  3160. var index = -1,
  3161. length = nativeMax(nativeCeil((end - start) / (step || 1)), 0),
  3162. result = Array(length);
  3163. while (length--) {
  3164. result[fromRight ? length : ++index] = start;
  3165. start += step;
  3166. }
  3167. return result;
  3168. }
  3169. /**
  3170. * The base implementation of `_.repeat` which doesn't coerce arguments.
  3171. *
  3172. * @private
  3173. * @param {string} string The string to repeat.
  3174. * @param {number} n The number of times to repeat the string.
  3175. * @returns {string} Returns the repeated string.
  3176. */
  3177. function baseRepeat(string, n) {
  3178. var result = '';
  3179. if (!string || n < 1 || n > MAX_SAFE_INTEGER) {
  3180. return result;
  3181. }
  3182. // Leverage the exponentiation by squaring algorithm for a faster repeat.
  3183. // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
  3184. do {
  3185. if (n % 2) {
  3186. result += string;
  3187. }
  3188. n = nativeFloor(n / 2);
  3189. if (n) {
  3190. string += string;
  3191. }
  3192. } while (n);
  3193. return result;
  3194. }
  3195. /**
  3196. * The base implementation of `_.set`.
  3197. *
  3198. * @private
  3199. * @param {Object} object The object to query.
  3200. * @param {Array|string} path The path of the property to set.
  3201. * @param {*} value The value to set.
  3202. * @param {Function} [customizer] The function to customize path creation.
  3203. * @returns {Object} Returns `object`.
  3204. */
  3205. function baseSet(object, path, value, customizer) {
  3206. path = isKey(path, object) ? [path] : castPath(path);
  3207. var index = -1,
  3208. length = path.length,
  3209. lastIndex = length - 1,
  3210. nested = object;
  3211. while (nested != null && ++index < length) {
  3212. var key = toKey(path[index]);
  3213. if (isObject(nested)) {
  3214. var newValue = value;
  3215. if (index != lastIndex) {
  3216. var objValue = nested[key];
  3217. newValue = customizer ? customizer(objValue, key, nested) : undefined;
  3218. if (newValue === undefined) {
  3219. newValue = objValue == null
  3220. ? (isIndex(path[index + 1]) ? [] : {})
  3221. : objValue;
  3222. }
  3223. }
  3224. assignValue(nested, key, newValue);
  3225. }
  3226. nested = nested[key];
  3227. }
  3228. return object;
  3229. }
  3230. /**
  3231. * The base implementation of `setData` without support for hot loop detection.
  3232. *
  3233. * @private
  3234. * @param {Function} func The function to associate metadata with.
  3235. * @param {*} data The metadata.
  3236. * @returns {Function} Returns `func`.
  3237. */
  3238. var baseSetData = !metaMap ? identity : function(func, data) {
  3239. metaMap.set(func, data);
  3240. return func;
  3241. };
  3242. /**
  3243. * The base implementation of `_.slice` without an iteratee call guard.
  3244. *
  3245. * @private
  3246. * @param {Array} array The array to slice.
  3247. * @param {number} [start=0] The start position.
  3248. * @param {number} [end=array.length] The end position.
  3249. * @returns {Array} Returns the slice of `array`.
  3250. */
  3251. function baseSlice(array, start, end) {
  3252. var index = -1,
  3253. length = array.length;
  3254. if (start < 0) {
  3255. start = -start > length ? 0 : (length + start);
  3256. }
  3257. end = end > length ? length : end;
  3258. if (end < 0) {
  3259. end += length;
  3260. }
  3261. length = start > end ? 0 : ((end - start) >>> 0);
  3262. start >>>= 0;
  3263. var result = Array(length);
  3264. while (++index < length) {
  3265. result[index] = array[index + start];
  3266. }
  3267. return result;
  3268. }
  3269. /**
  3270. * The base implementation of `_.some` without support for iteratee shorthands.
  3271. *
  3272. * @private
  3273. * @param {Array|Object} collection The collection to iterate over.
  3274. * @param {Function} predicate The function invoked per iteration.
  3275. * @returns {boolean} Returns `true` if any element passes the predicate check,
  3276. * else `false`.
  3277. */
  3278. function baseSome(collection, predicate) {
  3279. var result;
  3280. baseEach(collection, function(value, index, collection) {
  3281. result = predicate(value, index, collection);
  3282. return !result;
  3283. });
  3284. return !!result;
  3285. }
  3286. /**
  3287. * The base implementation of `_.sortedIndex` and `_.sortedLastIndex` which
  3288. * performs a binary search of `array` to determine the index at which `value`
  3289. * should be inserted into `array` in order to maintain its sort order.
  3290. *
  3291. * @private
  3292. * @param {Array} array The sorted array to inspect.
  3293. * @param {*} value The value to evaluate.
  3294. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  3295. * @returns {number} Returns the index at which `value` should be inserted
  3296. * into `array`.
  3297. */
  3298. function baseSortedIndex(array, value, retHighest) {
  3299. var low = 0,
  3300. high = array ? array.length : low;
  3301. if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
  3302. while (low < high) {
  3303. var mid = (low + high) >>> 1,
  3304. computed = array[mid];
  3305. if (computed !== null && !isSymbol(computed) &&
  3306. (retHighest ? (computed <= value) : (computed < value))) {
  3307. low = mid + 1;
  3308. } else {
  3309. high = mid;
  3310. }
  3311. }
  3312. return high;
  3313. }
  3314. return baseSortedIndexBy(array, value, identity, retHighest);
  3315. }
  3316. /**
  3317. * The base implementation of `_.sortedIndexBy` and `_.sortedLastIndexBy`
  3318. * which invokes `iteratee` for `value` and each element of `array` to compute
  3319. * their sort ranking. The iteratee is invoked with one argument; (value).
  3320. *
  3321. * @private
  3322. * @param {Array} array The sorted array to inspect.
  3323. * @param {*} value The value to evaluate.
  3324. * @param {Function} iteratee The iteratee invoked per element.
  3325. * @param {boolean} [retHighest] Specify returning the highest qualified index.
  3326. * @returns {number} Returns the index at which `value` should be inserted
  3327. * into `array`.
  3328. */
  3329. function baseSortedIndexBy(array, value, iteratee, retHighest) {
  3330. value = iteratee(value);
  3331. var low = 0,
  3332. high = array ? array.length : 0,
  3333. valIsNaN = value !== value,
  3334. valIsNull = value === null,
  3335. valIsSymbol = isSymbol(value),
  3336. valIsUndefined = value === undefined;
  3337. while (low < high) {
  3338. var mid = nativeFloor((low + high) / 2),
  3339. computed = iteratee(array[mid]),
  3340. othIsDefined = computed !== undefined,
  3341. othIsNull = computed === null,
  3342. othIsReflexive = computed === computed,
  3343. othIsSymbol = isSymbol(computed);
  3344. if (valIsNaN) {
  3345. var setLow = retHighest || othIsReflexive;
  3346. } else if (valIsUndefined) {
  3347. setLow = othIsReflexive && (retHighest || othIsDefined);
  3348. } else if (valIsNull) {
  3349. setLow = othIsReflexive && othIsDefined && (retHighest || !othIsNull);
  3350. } else if (valIsSymbol) {
  3351. setLow = othIsReflexive && othIsDefined && !othIsNull && (retHighest || !othIsSymbol);
  3352. } else if (othIsNull || othIsSymbol) {
  3353. setLow = false;
  3354. } else {
  3355. setLow = retHighest ? (computed <= value) : (computed < value);
  3356. }
  3357. if (setLow) {
  3358. low = mid + 1;
  3359. } else {
  3360. high = mid;
  3361. }
  3362. }
  3363. return nativeMin(high, MAX_ARRAY_INDEX);
  3364. }
  3365. /**
  3366. * The base implementation of `_.sortedUniq` and `_.sortedUniqBy` without
  3367. * support for iteratee shorthands.
  3368. *
  3369. * @private
  3370. * @param {Array} array The array to inspect.
  3371. * @param {Function} [iteratee] The iteratee invoked per element.
  3372. * @returns {Array} Returns the new duplicate free array.
  3373. */
  3374. function baseSortedUniq(array, iteratee) {
  3375. var index = -1,
  3376. length = array.length,
  3377. resIndex = 0,
  3378. result = [];
  3379. while (++index < length) {
  3380. var value = array[index],
  3381. computed = iteratee ? iteratee(value) : value;
  3382. if (!index || !eq(computed, seen)) {
  3383. var seen = computed;
  3384. result[resIndex++] = value === 0 ? 0 : value;
  3385. }
  3386. }
  3387. return result;
  3388. }
  3389. /**
  3390. * The base implementation of `_.toNumber` which doesn't ensure correct
  3391. * conversions of binary, hexadecimal, or octal string values.
  3392. *
  3393. * @private
  3394. * @param {*} value The value to process.
  3395. * @returns {number} Returns the number.
  3396. */
  3397. function baseToNumber(value) {
  3398. if (typeof value == 'number') {
  3399. return value;
  3400. }
  3401. if (isSymbol(value)) {
  3402. return NAN;
  3403. }
  3404. return +value;
  3405. }
  3406. /**
  3407. * The base implementation of `_.toString` which doesn't convert nullish
  3408. * values to empty strings.
  3409. *
  3410. * @private
  3411. * @param {*} value The value to process.
  3412. * @returns {string} Returns the string.
  3413. */
  3414. function baseToString(value) {
  3415. // Exit early for strings to avoid a performance hit in some environments.
  3416. if (typeof value == 'string') {
  3417. return value;
  3418. }
  3419. if (isSymbol(value)) {
  3420. return symbolToString ? symbolToString.call(value) : '';
  3421. }
  3422. var result = (value + '');
  3423. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  3424. }
  3425. /**
  3426. * The base implementation of `_.uniqBy` without support for iteratee shorthands.
  3427. *
  3428. * @private
  3429. * @param {Array} array The array to inspect.
  3430. * @param {Function} [iteratee] The iteratee invoked per element.
  3431. * @param {Function} [comparator] The comparator invoked per element.
  3432. * @returns {Array} Returns the new duplicate free array.
  3433. */
  3434. function baseUniq(array, iteratee, comparator) {
  3435. var index = -1,
  3436. includes = arrayIncludes,
  3437. length = array.length,
  3438. isCommon = true,
  3439. result = [],
  3440. seen = result;
  3441. if (comparator) {
  3442. isCommon = false;
  3443. includes = arrayIncludesWith;
  3444. }
  3445. else if (length >= LARGE_ARRAY_SIZE) {
  3446. var set = iteratee ? null : createSet(array);
  3447. if (set) {
  3448. return setToArray(set);
  3449. }
  3450. isCommon = false;
  3451. includes = cacheHas;
  3452. seen = new SetCache;
  3453. }
  3454. else {
  3455. seen = iteratee ? [] : result;
  3456. }
  3457. outer:
  3458. while (++index < length) {
  3459. var value = array[index],
  3460. computed = iteratee ? iteratee(value) : value;
  3461. value = (comparator || value !== 0) ? value : 0;
  3462. if (isCommon && computed === computed) {
  3463. var seenIndex = seen.length;
  3464. while (seenIndex--) {
  3465. if (seen[seenIndex] === computed) {
  3466. continue outer;
  3467. }
  3468. }
  3469. if (iteratee) {
  3470. seen.push(computed);
  3471. }
  3472. result.push(value);
  3473. }
  3474. else if (!includes(seen, computed, comparator)) {
  3475. if (seen !== result) {
  3476. seen.push(computed);
  3477. }
  3478. result.push(value);
  3479. }
  3480. }
  3481. return result;
  3482. }
  3483. /**
  3484. * The base implementation of `_.unset`.
  3485. *
  3486. * @private
  3487. * @param {Object} object The object to modify.
  3488. * @param {Array|string} path The path of the property to unset.
  3489. * @returns {boolean} Returns `true` if the property is deleted, else `false`.
  3490. */
  3491. function baseUnset(object, path) {
  3492. path = isKey(path, object) ? [path] : castPath(path);
  3493. object = parent(object, path);
  3494. var key = toKey(last(path));
  3495. return !(object != null && baseHas(object, key)) || delete object[key];
  3496. }
  3497. /**
  3498. * The base implementation of `_.update`.
  3499. *
  3500. * @private
  3501. * @param {Object} object The object to query.
  3502. * @param {Array|string} path The path of the property to update.
  3503. * @param {Function} updater The function to produce the updated value.
  3504. * @param {Function} [customizer] The function to customize path creation.
  3505. * @returns {Object} Returns `object`.
  3506. */
  3507. function baseUpdate(object, path, updater, customizer) {
  3508. return baseSet(object, path, updater(baseGet(object, path)), customizer);
  3509. }
  3510. /**
  3511. * The base implementation of methods like `_.dropWhile` and `_.takeWhile`
  3512. * without support for iteratee shorthands.
  3513. *
  3514. * @private
  3515. * @param {Array} array The array to query.
  3516. * @param {Function} predicate The function invoked per iteration.
  3517. * @param {boolean} [isDrop] Specify dropping elements instead of taking them.
  3518. * @param {boolean} [fromRight] Specify iterating from right to left.
  3519. * @returns {Array} Returns the slice of `array`.
  3520. */
  3521. function baseWhile(array, predicate, isDrop, fromRight) {
  3522. var length = array.length,
  3523. index = fromRight ? length : -1;
  3524. while ((fromRight ? index-- : ++index < length) &&
  3525. predicate(array[index], index, array)) {}
  3526. return isDrop
  3527. ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
  3528. : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index));
  3529. }
  3530. /**
  3531. * The base implementation of `wrapperValue` which returns the result of
  3532. * performing a sequence of actions on the unwrapped `value`, where each
  3533. * successive action is supplied the return value of the previous.
  3534. *
  3535. * @private
  3536. * @param {*} value The unwrapped value.
  3537. * @param {Array} actions Actions to perform to resolve the unwrapped value.
  3538. * @returns {*} Returns the resolved value.
  3539. */
  3540. function baseWrapperValue(value, actions) {
  3541. var result = value;
  3542. if (result instanceof LazyWrapper) {
  3543. result = result.value();
  3544. }
  3545. return arrayReduce(actions, function(result, action) {
  3546. return action.func.apply(action.thisArg, arrayPush([result], action.args));
  3547. }, result);
  3548. }
  3549. /**
  3550. * The base implementation of methods like `_.xor`, without support for
  3551. * iteratee shorthands, that accepts an array of arrays to inspect.
  3552. *
  3553. * @private
  3554. * @param {Array} arrays The arrays to inspect.
  3555. * @param {Function} [iteratee] The iteratee invoked per element.
  3556. * @param {Function} [comparator] The comparator invoked per element.
  3557. * @returns {Array} Returns the new array of values.
  3558. */
  3559. function baseXor(arrays, iteratee, comparator) {
  3560. var index = -1,
  3561. length = arrays.length;
  3562. while (++index < length) {
  3563. var result = result
  3564. ? arrayPush(
  3565. baseDifference(result, arrays[index], iteratee, comparator),
  3566. baseDifference(arrays[index], result, iteratee, comparator)
  3567. )
  3568. : arrays[index];
  3569. }
  3570. return (result && result.length) ? baseUniq(result, iteratee, comparator) : [];
  3571. }
  3572. /**
  3573. * This base implementation of `_.zipObject` which assigns values using `assignFunc`.
  3574. *
  3575. * @private
  3576. * @param {Array} props The property identifiers.
  3577. * @param {Array} values The property values.
  3578. * @param {Function} assignFunc The function to assign values.
  3579. * @returns {Object} Returns the new object.
  3580. */
  3581. function baseZipObject(props, values, assignFunc) {
  3582. var index = -1,
  3583. length = props.length,
  3584. valsLength = values.length,
  3585. result = {};
  3586. while (++index < length) {
  3587. var value = index < valsLength ? values[index] : undefined;
  3588. assignFunc(result, props[index], value);
  3589. }
  3590. return result;
  3591. }
  3592. /**
  3593. * Casts `value` to an empty array if it's not an array like object.
  3594. *
  3595. * @private
  3596. * @param {*} value The value to inspect.
  3597. * @returns {Array|Object} Returns the cast array-like object.
  3598. */
  3599. function castArrayLikeObject(value) {
  3600. return isArrayLikeObject(value) ? value : [];
  3601. }
  3602. /**
  3603. * Casts `value` to `identity` if it's not a function.
  3604. *
  3605. * @private
  3606. * @param {*} value The value to inspect.
  3607. * @returns {Function} Returns cast function.
  3608. */
  3609. function castFunction(value) {
  3610. return typeof value == 'function' ? value : identity;
  3611. }
  3612. /**
  3613. * Casts `value` to a path array if it's not one.
  3614. *
  3615. * @private
  3616. * @param {*} value The value to inspect.
  3617. * @returns {Array} Returns the cast property path array.
  3618. */
  3619. function castPath(value) {
  3620. return isArray(value) ? value : stringToPath(value);
  3621. }
  3622. /**
  3623. * Casts `array` to a slice if it's needed.
  3624. *
  3625. * @private
  3626. * @param {Array} array The array to inspect.
  3627. * @param {number} start The start position.
  3628. * @param {number} [end=array.length] The end position.
  3629. * @returns {Array} Returns the cast slice.
  3630. */
  3631. function castSlice(array, start, end) {
  3632. var length = array.length;
  3633. end = end === undefined ? length : end;
  3634. return (!start && end >= length) ? array : baseSlice(array, start, end);
  3635. }
  3636. /**
  3637. * Creates a clone of `buffer`.
  3638. *
  3639. * @private
  3640. * @param {Buffer} buffer The buffer to clone.
  3641. * @param {boolean} [isDeep] Specify a deep clone.
  3642. * @returns {Buffer} Returns the cloned buffer.
  3643. */
  3644. function cloneBuffer(buffer, isDeep) {
  3645. if (isDeep) {
  3646. return buffer.slice();
  3647. }
  3648. var result = new buffer.constructor(buffer.length);
  3649. buffer.copy(result);
  3650. return result;
  3651. }
  3652. /**
  3653. * Creates a clone of `arrayBuffer`.
  3654. *
  3655. * @private
  3656. * @param {ArrayBuffer} arrayBuffer The array buffer to clone.
  3657. * @returns {ArrayBuffer} Returns the cloned array buffer.
  3658. */
  3659. function cloneArrayBuffer(arrayBuffer) {
  3660. var result = new arrayBuffer.constructor(arrayBuffer.byteLength);
  3661. new Uint8Array(result).set(new Uint8Array(arrayBuffer));
  3662. return result;
  3663. }
  3664. /**
  3665. * Creates a clone of `dataView`.
  3666. *
  3667. * @private
  3668. * @param {Object} dataView The data view to clone.
  3669. * @param {boolean} [isDeep] Specify a deep clone.
  3670. * @returns {Object} Returns the cloned data view.
  3671. */
  3672. function cloneDataView(dataView, isDeep) {
  3673. var buffer = isDeep ? cloneArrayBuffer(dataView.buffer) : dataView.buffer;
  3674. return new dataView.constructor(buffer, dataView.byteOffset, dataView.byteLength);
  3675. }
  3676. /**
  3677. * Creates a clone of `map`.
  3678. *
  3679. * @private
  3680. * @param {Object} map The map to clone.
  3681. * @param {Function} cloneFunc The function to clone values.
  3682. * @param {boolean} [isDeep] Specify a deep clone.
  3683. * @returns {Object} Returns the cloned map.
  3684. */
  3685. function cloneMap(map, isDeep, cloneFunc) {
  3686. var array = isDeep ? cloneFunc(mapToArray(map), true) : mapToArray(map);
  3687. return arrayReduce(array, addMapEntry, new map.constructor);
  3688. }
  3689. /**
  3690. * Creates a clone of `regexp`.
  3691. *
  3692. * @private
  3693. * @param {Object} regexp The regexp to clone.
  3694. * @returns {Object} Returns the cloned regexp.
  3695. */
  3696. function cloneRegExp(regexp) {
  3697. var result = new regexp.constructor(regexp.source, reFlags.exec(regexp));
  3698. result.lastIndex = regexp.lastIndex;
  3699. return result;
  3700. }
  3701. /**
  3702. * Creates a clone of `set`.
  3703. *
  3704. * @private
  3705. * @param {Object} set The set to clone.
  3706. * @param {Function} cloneFunc The function to clone values.
  3707. * @param {boolean} [isDeep] Specify a deep clone.
  3708. * @returns {Object} Returns the cloned set.
  3709. */
  3710. function cloneSet(set, isDeep, cloneFunc) {
  3711. var array = isDeep ? cloneFunc(setToArray(set), true) : setToArray(set);
  3712. return arrayReduce(array, addSetEntry, new set.constructor);
  3713. }
  3714. /**
  3715. * Creates a clone of the `symbol` object.
  3716. *
  3717. * @private
  3718. * @param {Object} symbol The symbol object to clone.
  3719. * @returns {Object} Returns the cloned symbol object.
  3720. */
  3721. function cloneSymbol(symbol) {
  3722. return symbolValueOf ? Object(symbolValueOf.call(symbol)) : {};
  3723. }
  3724. /**
  3725. * Creates a clone of `typedArray`.
  3726. *
  3727. * @private
  3728. * @param {Object} typedArray The typed array to clone.
  3729. * @param {boolean} [isDeep] Specify a deep clone.
  3730. * @returns {Object} Returns the cloned typed array.
  3731. */
  3732. function cloneTypedArray(typedArray, isDeep) {
  3733. var buffer = isDeep ? cloneArrayBuffer(typedArray.buffer) : typedArray.buffer;
  3734. return new typedArray.constructor(buffer, typedArray.byteOffset, typedArray.length);
  3735. }
  3736. /**
  3737. * Compares values to sort them in ascending order.
  3738. *
  3739. * @private
  3740. * @param {*} value The value to compare.
  3741. * @param {*} other The other value to compare.
  3742. * @returns {number} Returns the sort order indicator for `value`.
  3743. */
  3744. function compareAscending(value, other) {
  3745. if (value !== other) {
  3746. var valIsDefined = value !== undefined,
  3747. valIsNull = value === null,
  3748. valIsReflexive = value === value,
  3749. valIsSymbol = isSymbol(value);
  3750. var othIsDefined = other !== undefined,
  3751. othIsNull = other === null,
  3752. othIsReflexive = other === other,
  3753. othIsSymbol = isSymbol(other);
  3754. if ((!othIsNull && !othIsSymbol && !valIsSymbol && value > other) ||
  3755. (valIsSymbol && othIsDefined && othIsReflexive && !othIsNull && !othIsSymbol) ||
  3756. (valIsNull && othIsDefined && othIsReflexive) ||
  3757. (!valIsDefined && othIsReflexive) ||
  3758. !valIsReflexive) {
  3759. return 1;
  3760. }
  3761. if ((!valIsNull && !valIsSymbol && !othIsSymbol && value < other) ||
  3762. (othIsSymbol && valIsDefined && valIsReflexive && !valIsNull && !valIsSymbol) ||
  3763. (othIsNull && valIsDefined && valIsReflexive) ||
  3764. (!othIsDefined && valIsReflexive) ||
  3765. !othIsReflexive) {
  3766. return -1;
  3767. }
  3768. }
  3769. return 0;
  3770. }
  3771. /**
  3772. * Used by `_.orderBy` to compare multiple properties of a value to another
  3773. * and stable sort them.
  3774. *
  3775. * If `orders` is unspecified, all values are sorted in ascending order. Otherwise,
  3776. * specify an order of "desc" for descending or "asc" for ascending sort order
  3777. * of corresponding values.
  3778. *
  3779. * @private
  3780. * @param {Object} object The object to compare.
  3781. * @param {Object} other The other object to compare.
  3782. * @param {boolean[]|string[]} orders The order to sort by for each property.
  3783. * @returns {number} Returns the sort order indicator for `object`.
  3784. */
  3785. function compareMultiple(object, other, orders) {
  3786. var index = -1,
  3787. objCriteria = object.criteria,
  3788. othCriteria = other.criteria,
  3789. length = objCriteria.length,
  3790. ordersLength = orders.length;
  3791. while (++index < length) {
  3792. var result = compareAscending(objCriteria[index], othCriteria[index]);
  3793. if (result) {
  3794. if (index >= ordersLength) {
  3795. return result;
  3796. }
  3797. var order = orders[index];
  3798. return result * (order == 'desc' ? -1 : 1);
  3799. }
  3800. }
  3801. // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
  3802. // that causes it, under certain circumstances, to provide the same value for
  3803. // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247
  3804. // for more details.
  3805. //
  3806. // This also ensures a stable sort in V8 and other engines.
  3807. // See https://bugs.chromium.org/p/v8/issues/detail?id=90 for more details.
  3808. return object.index - other.index;
  3809. }
  3810. /**
  3811. * Creates an array that is the composition of partially applied arguments,
  3812. * placeholders, and provided arguments into a single array of arguments.
  3813. *
  3814. * @private
  3815. * @param {Array} args The provided arguments.
  3816. * @param {Array} partials The arguments to prepend to those provided.
  3817. * @param {Array} holders The `partials` placeholder indexes.
  3818. * @params {boolean} [isCurried] Specify composing for a curried function.
  3819. * @returns {Array} Returns the new array of composed arguments.
  3820. */
  3821. function composeArgs(args, partials, holders, isCurried) {
  3822. var argsIndex = -1,
  3823. argsLength = args.length,
  3824. holdersLength = holders.length,
  3825. leftIndex = -1,
  3826. leftLength = partials.length,
  3827. rangeLength = nativeMax(argsLength - holdersLength, 0),
  3828. result = Array(leftLength + rangeLength),
  3829. isUncurried = !isCurried;
  3830. while (++leftIndex < leftLength) {
  3831. result[leftIndex] = partials[leftIndex];
  3832. }
  3833. while (++argsIndex < holdersLength) {
  3834. if (isUncurried || argsIndex < argsLength) {
  3835. result[holders[argsIndex]] = args[argsIndex];
  3836. }
  3837. }
  3838. while (rangeLength--) {
  3839. result[leftIndex++] = args[argsIndex++];
  3840. }
  3841. return result;
  3842. }
  3843. /**
  3844. * This function is like `composeArgs` except that the arguments composition
  3845. * is tailored for `_.partialRight`.
  3846. *
  3847. * @private
  3848. * @param {Array} args The provided arguments.
  3849. * @param {Array} partials The arguments to append to those provided.
  3850. * @param {Array} holders The `partials` placeholder indexes.
  3851. * @params {boolean} [isCurried] Specify composing for a curried function.
  3852. * @returns {Array} Returns the new array of composed arguments.
  3853. */
  3854. function composeArgsRight(args, partials, holders, isCurried) {
  3855. var argsIndex = -1,
  3856. argsLength = args.length,
  3857. holdersIndex = -1,
  3858. holdersLength = holders.length,
  3859. rightIndex = -1,
  3860. rightLength = partials.length,
  3861. rangeLength = nativeMax(argsLength - holdersLength, 0),
  3862. result = Array(rangeLength + rightLength),
  3863. isUncurried = !isCurried;
  3864. while (++argsIndex < rangeLength) {
  3865. result[argsIndex] = args[argsIndex];
  3866. }
  3867. var offset = argsIndex;
  3868. while (++rightIndex < rightLength) {
  3869. result[offset + rightIndex] = partials[rightIndex];
  3870. }
  3871. while (++holdersIndex < holdersLength) {
  3872. if (isUncurried || argsIndex < argsLength) {
  3873. result[offset + holders[holdersIndex]] = args[argsIndex++];
  3874. }
  3875. }
  3876. return result;
  3877. }
  3878. /**
  3879. * Copies the values of `source` to `array`.
  3880. *
  3881. * @private
  3882. * @param {Array} source The array to copy values from.
  3883. * @param {Array} [array=[]] The array to copy values to.
  3884. * @returns {Array} Returns `array`.
  3885. */
  3886. function copyArray(source, array) {
  3887. var index = -1,
  3888. length = source.length;
  3889. array || (array = Array(length));
  3890. while (++index < length) {
  3891. array[index] = source[index];
  3892. }
  3893. return array;
  3894. }
  3895. /**
  3896. * Copies properties of `source` to `object`.
  3897. *
  3898. * @private
  3899. * @param {Object} source The object to copy properties from.
  3900. * @param {Array} props The property identifiers to copy.
  3901. * @param {Object} [object={}] The object to copy properties to.
  3902. * @param {Function} [customizer] The function to customize copied values.
  3903. * @returns {Object} Returns `object`.
  3904. */
  3905. function copyObject(source, props, object, customizer) {
  3906. object || (object = {});
  3907. var index = -1,
  3908. length = props.length;
  3909. while (++index < length) {
  3910. var key = props[index];
  3911. var newValue = customizer
  3912. ? customizer(object[key], source[key], key, object, source)
  3913. : source[key];
  3914. assignValue(object, key, newValue);
  3915. }
  3916. return object;
  3917. }
  3918. /**
  3919. * Copies own symbol properties of `source` to `object`.
  3920. *
  3921. * @private
  3922. * @param {Object} source The object to copy symbols from.
  3923. * @param {Object} [object={}] The object to copy symbols to.
  3924. * @returns {Object} Returns `object`.
  3925. */
  3926. function copySymbols(source, object) {
  3927. return copyObject(source, getSymbols(source), object);
  3928. }
  3929. /**
  3930. * Creates a function like `_.groupBy`.
  3931. *
  3932. * @private
  3933. * @param {Function} setter The function to set accumulator values.
  3934. * @param {Function} [initializer] The accumulator object initializer.
  3935. * @returns {Function} Returns the new aggregator function.
  3936. */
  3937. function createAggregator(setter, initializer) {
  3938. return function(collection, iteratee) {
  3939. var func = isArray(collection) ? arrayAggregator : baseAggregator,
  3940. accumulator = initializer ? initializer() : {};
  3941. return func(collection, setter, getIteratee(iteratee), accumulator);
  3942. };
  3943. }
  3944. /**
  3945. * Creates a function like `_.assign`.
  3946. *
  3947. * @private
  3948. * @param {Function} assigner The function to assign values.
  3949. * @returns {Function} Returns the new assigner function.
  3950. */
  3951. function createAssigner(assigner) {
  3952. return rest(function(object, sources) {
  3953. var index = -1,
  3954. length = sources.length,
  3955. customizer = length > 1 ? sources[length - 1] : undefined,
  3956. guard = length > 2 ? sources[2] : undefined;
  3957. customizer = (assigner.length > 3 && typeof customizer == 'function')
  3958. ? (length--, customizer)
  3959. : undefined;
  3960. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  3961. customizer = length < 3 ? undefined : customizer;
  3962. length = 1;
  3963. }
  3964. object = Object(object);
  3965. while (++index < length) {
  3966. var source = sources[index];
  3967. if (source) {
  3968. assigner(object, source, index, customizer);
  3969. }
  3970. }
  3971. return object;
  3972. });
  3973. }
  3974. /**
  3975. * Creates a `baseEach` or `baseEachRight` function.
  3976. *
  3977. * @private
  3978. * @param {Function} eachFunc The function to iterate over a collection.
  3979. * @param {boolean} [fromRight] Specify iterating from right to left.
  3980. * @returns {Function} Returns the new base function.
  3981. */
  3982. function createBaseEach(eachFunc, fromRight) {
  3983. return function(collection, iteratee) {
  3984. if (collection == null) {
  3985. return collection;
  3986. }
  3987. if (!isArrayLike(collection)) {
  3988. return eachFunc(collection, iteratee);
  3989. }
  3990. var length = collection.length,
  3991. index = fromRight ? length : -1,
  3992. iterable = Object(collection);
  3993. while ((fromRight ? index-- : ++index < length)) {
  3994. if (iteratee(iterable[index], index, iterable) === false) {
  3995. break;
  3996. }
  3997. }
  3998. return collection;
  3999. };
  4000. }
  4001. /**
  4002. * Creates a base function for methods like `_.forIn` and `_.forOwn`.
  4003. *
  4004. * @private
  4005. * @param {boolean} [fromRight] Specify iterating from right to left.
  4006. * @returns {Function} Returns the new base function.
  4007. */
  4008. function createBaseFor(fromRight) {
  4009. return function(object, iteratee, keysFunc) {
  4010. var index = -1,
  4011. iterable = Object(object),
  4012. props = keysFunc(object),
  4013. length = props.length;
  4014. while (length--) {
  4015. var key = props[fromRight ? length : ++index];
  4016. if (iteratee(iterable[key], key, iterable) === false) {
  4017. break;
  4018. }
  4019. }
  4020. return object;
  4021. };
  4022. }
  4023. /**
  4024. * Creates a function that wraps `func` to invoke it with the optional `this`
  4025. * binding of `thisArg`.
  4026. *
  4027. * @private
  4028. * @param {Function} func The function to wrap.
  4029. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  4030. * for more details.
  4031. * @param {*} [thisArg] The `this` binding of `func`.
  4032. * @returns {Function} Returns the new wrapped function.
  4033. */
  4034. function createBaseWrapper(func, bitmask, thisArg) {
  4035. var isBind = bitmask & BIND_FLAG,
  4036. Ctor = createCtorWrapper(func);
  4037. function wrapper() {
  4038. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  4039. return fn.apply(isBind ? thisArg : this, arguments);
  4040. }
  4041. return wrapper;
  4042. }
  4043. /**
  4044. * Creates a function like `_.lowerFirst`.
  4045. *
  4046. * @private
  4047. * @param {string} methodName The name of the `String` case method to use.
  4048. * @returns {Function} Returns the new case function.
  4049. */
  4050. function createCaseFirst(methodName) {
  4051. return function(string) {
  4052. string = toString(string);
  4053. var strSymbols = reHasComplexSymbol.test(string)
  4054. ? stringToArray(string)
  4055. : undefined;
  4056. var chr = strSymbols
  4057. ? strSymbols[0]
  4058. : string.charAt(0);
  4059. var trailing = strSymbols
  4060. ? castSlice(strSymbols, 1).join('')
  4061. : string.slice(1);
  4062. return chr[methodName]() + trailing;
  4063. };
  4064. }
  4065. /**
  4066. * Creates a function like `_.camelCase`.
  4067. *
  4068. * @private
  4069. * @param {Function} callback The function to combine each word.
  4070. * @returns {Function} Returns the new compounder function.
  4071. */
  4072. function createCompounder(callback) {
  4073. return function(string) {
  4074. return arrayReduce(words(deburr(string).replace(reApos, '')), callback, '');
  4075. };
  4076. }
  4077. /**
  4078. * Creates a function that produces an instance of `Ctor` regardless of
  4079. * whether it was invoked as part of a `new` expression or by `call` or `apply`.
  4080. *
  4081. * @private
  4082. * @param {Function} Ctor The constructor to wrap.
  4083. * @returns {Function} Returns the new wrapped function.
  4084. */
  4085. function createCtorWrapper(Ctor) {
  4086. return function() {
  4087. // Use a `switch` statement to work with class constructors. See
  4088. // http://ecma-international.org/ecma-262/6.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  4089. // for more details.
  4090. var args = arguments;
  4091. switch (args.length) {
  4092. case 0: return new Ctor;
  4093. case 1: return new Ctor(args[0]);
  4094. case 2: return new Ctor(args[0], args[1]);
  4095. case 3: return new Ctor(args[0], args[1], args[2]);
  4096. case 4: return new Ctor(args[0], args[1], args[2], args[3]);
  4097. case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]);
  4098. case 6: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5]);
  4099. case 7: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
  4100. }
  4101. var thisBinding = baseCreate(Ctor.prototype),
  4102. result = Ctor.apply(thisBinding, args);
  4103. // Mimic the constructor's `return` behavior.
  4104. // See https://es5.github.io/#x13.2.2 for more details.
  4105. return isObject(result) ? result : thisBinding;
  4106. };
  4107. }
  4108. /**
  4109. * Creates a function that wraps `func` to enable currying.
  4110. *
  4111. * @private
  4112. * @param {Function} func The function to wrap.
  4113. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  4114. * for more details.
  4115. * @param {number} arity The arity of `func`.
  4116. * @returns {Function} Returns the new wrapped function.
  4117. */
  4118. function createCurryWrapper(func, bitmask, arity) {
  4119. var Ctor = createCtorWrapper(func);
  4120. function wrapper() {
  4121. var length = arguments.length,
  4122. args = Array(length),
  4123. index = length,
  4124. placeholder = getHolder(wrapper);
  4125. while (index--) {
  4126. args[index] = arguments[index];
  4127. }
  4128. var holders = (length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder)
  4129. ? []
  4130. : replaceHolders(args, placeholder);
  4131. length -= holders.length;
  4132. if (length < arity) {
  4133. return createRecurryWrapper(
  4134. func, bitmask, createHybridWrapper, wrapper.placeholder, undefined,
  4135. args, holders, undefined, undefined, arity - length);
  4136. }
  4137. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  4138. return apply(fn, this, args);
  4139. }
  4140. return wrapper;
  4141. }
  4142. /**
  4143. * Creates a `_.find` or `_.findLast` function.
  4144. *
  4145. * @private
  4146. * @param {Function} findIndexFunc The function to find the collection index.
  4147. * @returns {Function} Returns the new find function.
  4148. */
  4149. function createFind(findIndexFunc) {
  4150. return function(collection, predicate, fromIndex) {
  4151. var iterable = Object(collection);
  4152. predicate = getIteratee(predicate, 3);
  4153. if (!isArrayLike(collection)) {
  4154. var props = keys(collection);
  4155. }
  4156. var index = findIndexFunc(props || collection, function(value, key) {
  4157. if (props) {
  4158. key = value;
  4159. value = iterable[key];
  4160. }
  4161. return predicate(value, key, iterable);
  4162. }, fromIndex);
  4163. return index > -1 ? collection[props ? props[index] : index] : undefined;
  4164. };
  4165. }
  4166. /**
  4167. * Creates a `_.flow` or `_.flowRight` function.
  4168. *
  4169. * @private
  4170. * @param {boolean} [fromRight] Specify iterating from right to left.
  4171. * @returns {Function} Returns the new flow function.
  4172. */
  4173. function createFlow(fromRight) {
  4174. return rest(function(funcs) {
  4175. funcs = baseFlatten(funcs, 1);
  4176. var length = funcs.length,
  4177. index = length,
  4178. prereq = LodashWrapper.prototype.thru;
  4179. if (fromRight) {
  4180. funcs.reverse();
  4181. }
  4182. while (index--) {
  4183. var func = funcs[index];
  4184. if (typeof func != 'function') {
  4185. throw new TypeError(FUNC_ERROR_TEXT);
  4186. }
  4187. if (prereq && !wrapper && getFuncName(func) == 'wrapper') {
  4188. var wrapper = new LodashWrapper([], true);
  4189. }
  4190. }
  4191. index = wrapper ? index : length;
  4192. while (++index < length) {
  4193. func = funcs[index];
  4194. var funcName = getFuncName(func),
  4195. data = funcName == 'wrapper' ? getData(func) : undefined;
  4196. if (data && isLaziable(data[0]) &&
  4197. data[1] == (ARY_FLAG | CURRY_FLAG | PARTIAL_FLAG | REARG_FLAG) &&
  4198. !data[4].length && data[9] == 1
  4199. ) {
  4200. wrapper = wrapper[getFuncName(data[0])].apply(wrapper, data[3]);
  4201. } else {
  4202. wrapper = (func.length == 1 && isLaziable(func))
  4203. ? wrapper[funcName]()
  4204. : wrapper.thru(func);
  4205. }
  4206. }
  4207. return function() {
  4208. var args = arguments,
  4209. value = args[0];
  4210. if (wrapper && args.length == 1 &&
  4211. isArray(value) && value.length >= LARGE_ARRAY_SIZE) {
  4212. return wrapper.plant(value).value();
  4213. }
  4214. var index = 0,
  4215. result = length ? funcs[index].apply(this, args) : value;
  4216. while (++index < length) {
  4217. result = funcs[index].call(this, result);
  4218. }
  4219. return result;
  4220. };
  4221. });
  4222. }
  4223. /**
  4224. * Creates a function that wraps `func` to invoke it with optional `this`
  4225. * binding of `thisArg`, partial application, and currying.
  4226. *
  4227. * @private
  4228. * @param {Function|string} func The function or method name to wrap.
  4229. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  4230. * for more details.
  4231. * @param {*} [thisArg] The `this` binding of `func`.
  4232. * @param {Array} [partials] The arguments to prepend to those provided to
  4233. * the new function.
  4234. * @param {Array} [holders] The `partials` placeholder indexes.
  4235. * @param {Array} [partialsRight] The arguments to append to those provided
  4236. * to the new function.
  4237. * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
  4238. * @param {Array} [argPos] The argument positions of the new function.
  4239. * @param {number} [ary] The arity cap of `func`.
  4240. * @param {number} [arity] The arity of `func`.
  4241. * @returns {Function} Returns the new wrapped function.
  4242. */
  4243. function createHybridWrapper(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
  4244. var isAry = bitmask & ARY_FLAG,
  4245. isBind = bitmask & BIND_FLAG,
  4246. isBindKey = bitmask & BIND_KEY_FLAG,
  4247. isCurried = bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG),
  4248. isFlip = bitmask & FLIP_FLAG,
  4249. Ctor = isBindKey ? undefined : createCtorWrapper(func);
  4250. function wrapper() {
  4251. var length = arguments.length,
  4252. args = Array(length),
  4253. index = length;
  4254. while (index--) {
  4255. args[index] = arguments[index];
  4256. }
  4257. if (isCurried) {
  4258. var placeholder = getHolder(wrapper),
  4259. holdersCount = countHolders(args, placeholder);
  4260. }
  4261. if (partials) {
  4262. args = composeArgs(args, partials, holders, isCurried);
  4263. }
  4264. if (partialsRight) {
  4265. args = composeArgsRight(args, partialsRight, holdersRight, isCurried);
  4266. }
  4267. length -= holdersCount;
  4268. if (isCurried && length < arity) {
  4269. var newHolders = replaceHolders(args, placeholder);
  4270. return createRecurryWrapper(
  4271. func, bitmask, createHybridWrapper, wrapper.placeholder, thisArg,
  4272. args, newHolders, argPos, ary, arity - length
  4273. );
  4274. }
  4275. var thisBinding = isBind ? thisArg : this,
  4276. fn = isBindKey ? thisBinding[func] : func;
  4277. length = args.length;
  4278. if (argPos) {
  4279. args = reorder(args, argPos);
  4280. } else if (isFlip && length > 1) {
  4281. args.reverse();
  4282. }
  4283. if (isAry && ary < length) {
  4284. args.length = ary;
  4285. }
  4286. if (this && this !== root && this instanceof wrapper) {
  4287. fn = Ctor || createCtorWrapper(fn);
  4288. }
  4289. return fn.apply(thisBinding, args);
  4290. }
  4291. return wrapper;
  4292. }
  4293. /**
  4294. * Creates a function like `_.invertBy`.
  4295. *
  4296. * @private
  4297. * @param {Function} setter The function to set accumulator values.
  4298. * @param {Function} toIteratee The function to resolve iteratees.
  4299. * @returns {Function} Returns the new inverter function.
  4300. */
  4301. function createInverter(setter, toIteratee) {
  4302. return function(object, iteratee) {
  4303. return baseInverter(object, setter, toIteratee(iteratee), {});
  4304. };
  4305. }
  4306. /**
  4307. * Creates a function that performs a mathematical operation on two values.
  4308. *
  4309. * @private
  4310. * @param {Function} operator The function to perform the operation.
  4311. * @returns {Function} Returns the new mathematical operation function.
  4312. */
  4313. function createMathOperation(operator) {
  4314. return function(value, other) {
  4315. var result;
  4316. if (value === undefined && other === undefined) {
  4317. return 0;
  4318. }
  4319. if (value !== undefined) {
  4320. result = value;
  4321. }
  4322. if (other !== undefined) {
  4323. if (result === undefined) {
  4324. return other;
  4325. }
  4326. if (typeof value == 'string' || typeof other == 'string') {
  4327. value = baseToString(value);
  4328. other = baseToString(other);
  4329. } else {
  4330. value = baseToNumber(value);
  4331. other = baseToNumber(other);
  4332. }
  4333. result = operator(value, other);
  4334. }
  4335. return result;
  4336. };
  4337. }
  4338. /**
  4339. * Creates a function like `_.over`.
  4340. *
  4341. * @private
  4342. * @param {Function} arrayFunc The function to iterate over iteratees.
  4343. * @returns {Function} Returns the new over function.
  4344. */
  4345. function createOver(arrayFunc) {
  4346. return rest(function(iteratees) {
  4347. iteratees = (iteratees.length == 1 && isArray(iteratees[0]))
  4348. ? arrayMap(iteratees[0], baseUnary(getIteratee()))
  4349. : arrayMap(baseFlatten(iteratees, 1, isFlattenableIteratee), baseUnary(getIteratee()));
  4350. return rest(function(args) {
  4351. var thisArg = this;
  4352. return arrayFunc(iteratees, function(iteratee) {
  4353. return apply(iteratee, thisArg, args);
  4354. });
  4355. });
  4356. });
  4357. }
  4358. /**
  4359. * Creates the padding for `string` based on `length`. The `chars` string
  4360. * is truncated if the number of characters exceeds `length`.
  4361. *
  4362. * @private
  4363. * @param {number} length The padding length.
  4364. * @param {string} [chars=' '] The string used as padding.
  4365. * @returns {string} Returns the padding for `string`.
  4366. */
  4367. function createPadding(length, chars) {
  4368. chars = chars === undefined ? ' ' : baseToString(chars);
  4369. var charsLength = chars.length;
  4370. if (charsLength < 2) {
  4371. return charsLength ? baseRepeat(chars, length) : chars;
  4372. }
  4373. var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
  4374. return reHasComplexSymbol.test(chars)
  4375. ? castSlice(stringToArray(result), 0, length).join('')
  4376. : result.slice(0, length);
  4377. }
  4378. /**
  4379. * Creates a function that wraps `func` to invoke it with the `this` binding
  4380. * of `thisArg` and `partials` prepended to the arguments it receives.
  4381. *
  4382. * @private
  4383. * @param {Function} func The function to wrap.
  4384. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  4385. * for more details.
  4386. * @param {*} thisArg The `this` binding of `func`.
  4387. * @param {Array} partials The arguments to prepend to those provided to
  4388. * the new function.
  4389. * @returns {Function} Returns the new wrapped function.
  4390. */
  4391. function createPartialWrapper(func, bitmask, thisArg, partials) {
  4392. var isBind = bitmask & BIND_FLAG,
  4393. Ctor = createCtorWrapper(func);
  4394. function wrapper() {
  4395. var argsIndex = -1,
  4396. argsLength = arguments.length,
  4397. leftIndex = -1,
  4398. leftLength = partials.length,
  4399. args = Array(leftLength + argsLength),
  4400. fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  4401. while (++leftIndex < leftLength) {
  4402. args[leftIndex] = partials[leftIndex];
  4403. }
  4404. while (argsLength--) {
  4405. args[leftIndex++] = arguments[++argsIndex];
  4406. }
  4407. return apply(fn, isBind ? thisArg : this, args);
  4408. }
  4409. return wrapper;
  4410. }
  4411. /**
  4412. * Creates a `_.range` or `_.rangeRight` function.
  4413. *
  4414. * @private
  4415. * @param {boolean} [fromRight] Specify iterating from right to left.
  4416. * @returns {Function} Returns the new range function.
  4417. */
  4418. function createRange(fromRight) {
  4419. return function(start, end, step) {
  4420. if (step && typeof step != 'number' && isIterateeCall(start, end, step)) {
  4421. end = step = undefined;
  4422. }
  4423. // Ensure the sign of `-0` is preserved.
  4424. start = toNumber(start);
  4425. start = start === start ? start : 0;
  4426. if (end === undefined) {
  4427. end = start;
  4428. start = 0;
  4429. } else {
  4430. end = toNumber(end) || 0;
  4431. }
  4432. step = step === undefined ? (start < end ? 1 : -1) : (toNumber(step) || 0);
  4433. return baseRange(start, end, step, fromRight);
  4434. };
  4435. }
  4436. /**
  4437. * Creates a function that performs a relational operation on two values.
  4438. *
  4439. * @private
  4440. * @param {Function} operator The function to perform the operation.
  4441. * @returns {Function} Returns the new relational operation function.
  4442. */
  4443. function createRelationalOperation(operator) {
  4444. return function(value, other) {
  4445. if (!(typeof value == 'string' && typeof other == 'string')) {
  4446. value = toNumber(value);
  4447. other = toNumber(other);
  4448. }
  4449. return operator(value, other);
  4450. };
  4451. }
  4452. /**
  4453. * Creates a function that wraps `func` to continue currying.
  4454. *
  4455. * @private
  4456. * @param {Function} func The function to wrap.
  4457. * @param {number} bitmask The bitmask of wrapper flags. See `createWrapper`
  4458. * for more details.
  4459. * @param {Function} wrapFunc The function to create the `func` wrapper.
  4460. * @param {*} placeholder The placeholder value.
  4461. * @param {*} [thisArg] The `this` binding of `func`.
  4462. * @param {Array} [partials] The arguments to prepend to those provided to
  4463. * the new function.
  4464. * @param {Array} [holders] The `partials` placeholder indexes.
  4465. * @param {Array} [argPos] The argument positions of the new function.
  4466. * @param {number} [ary] The arity cap of `func`.
  4467. * @param {number} [arity] The arity of `func`.
  4468. * @returns {Function} Returns the new wrapped function.
  4469. */
  4470. function createRecurryWrapper(func, bitmask, wrapFunc, placeholder, thisArg, partials, holders, argPos, ary, arity) {
  4471. var isCurry = bitmask & CURRY_FLAG,
  4472. newHolders = isCurry ? holders : undefined,
  4473. newHoldersRight = isCurry ? undefined : holders,
  4474. newPartials = isCurry ? partials : undefined,
  4475. newPartialsRight = isCurry ? undefined : partials;
  4476. bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
  4477. bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
  4478. if (!(bitmask & CURRY_BOUND_FLAG)) {
  4479. bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
  4480. }
  4481. var newData = [
  4482. func, bitmask, thisArg, newPartials, newHolders, newPartialsRight,
  4483. newHoldersRight, argPos, ary, arity
  4484. ];
  4485. var result = wrapFunc.apply(undefined, newData);
  4486. if (isLaziable(func)) {
  4487. setData(result, newData);
  4488. }
  4489. result.placeholder = placeholder;
  4490. return result;
  4491. }
  4492. /**
  4493. * Creates a function like `_.round`.
  4494. *
  4495. * @private
  4496. * @param {string} methodName The name of the `Math` method to use when rounding.
  4497. * @returns {Function} Returns the new round function.
  4498. */
  4499. function createRound(methodName) {
  4500. var func = Math[methodName];
  4501. return function(number, precision) {
  4502. number = toNumber(number);
  4503. precision = nativeMin(toInteger(precision), 292);
  4504. if (precision) {
  4505. // Shift with exponential notation to avoid floating-point issues.
  4506. // See [MDN](https://mdn.io/round#Examples) for more details.
  4507. var pair = (toString(number) + 'e').split('e'),
  4508. value = func(pair[0] + 'e' + (+pair[1] + precision));
  4509. pair = (toString(value) + 'e').split('e');
  4510. return +(pair[0] + 'e' + (+pair[1] - precision));
  4511. }
  4512. return func(number);
  4513. };
  4514. }
  4515. /**
  4516. * Creates a set of `values`.
  4517. *
  4518. * @private
  4519. * @param {Array} values The values to add to the set.
  4520. * @returns {Object} Returns the new set.
  4521. */
  4522. var createSet = !(Set && (1 / setToArray(new Set([,-0]))[1]) == INFINITY) ? noop : function(values) {
  4523. return new Set(values);
  4524. };
  4525. /**
  4526. * Creates a `_.toPairs` or `_.toPairsIn` function.
  4527. *
  4528. * @private
  4529. * @param {Function} keysFunc The function to get the keys of a given object.
  4530. * @returns {Function} Returns the new pairs function.
  4531. */
  4532. function createToPairs(keysFunc) {
  4533. return function(object) {
  4534. var tag = getTag(object);
  4535. if (tag == mapTag) {
  4536. return mapToArray(object);
  4537. }
  4538. if (tag == setTag) {
  4539. return setToPairs(object);
  4540. }
  4541. return baseToPairs(object, keysFunc(object));
  4542. };
  4543. }
  4544. /**
  4545. * Creates a function that either curries or invokes `func` with optional
  4546. * `this` binding and partially applied arguments.
  4547. *
  4548. * @private
  4549. * @param {Function|string} func The function or method name to wrap.
  4550. * @param {number} bitmask The bitmask of wrapper flags.
  4551. * The bitmask may be composed of the following flags:
  4552. * 1 - `_.bind`
  4553. * 2 - `_.bindKey`
  4554. * 4 - `_.curry` or `_.curryRight` of a bound function
  4555. * 8 - `_.curry`
  4556. * 16 - `_.curryRight`
  4557. * 32 - `_.partial`
  4558. * 64 - `_.partialRight`
  4559. * 128 - `_.rearg`
  4560. * 256 - `_.ary`
  4561. * 512 - `_.flip`
  4562. * @param {*} [thisArg] The `this` binding of `func`.
  4563. * @param {Array} [partials] The arguments to be partially applied.
  4564. * @param {Array} [holders] The `partials` placeholder indexes.
  4565. * @param {Array} [argPos] The argument positions of the new function.
  4566. * @param {number} [ary] The arity cap of `func`.
  4567. * @param {number} [arity] The arity of `func`.
  4568. * @returns {Function} Returns the new wrapped function.
  4569. */
  4570. function createWrapper(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
  4571. var isBindKey = bitmask & BIND_KEY_FLAG;
  4572. if (!isBindKey && typeof func != 'function') {
  4573. throw new TypeError(FUNC_ERROR_TEXT);
  4574. }
  4575. var length = partials ? partials.length : 0;
  4576. if (!length) {
  4577. bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
  4578. partials = holders = undefined;
  4579. }
  4580. ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
  4581. arity = arity === undefined ? arity : toInteger(arity);
  4582. length -= holders ? holders.length : 0;
  4583. if (bitmask & PARTIAL_RIGHT_FLAG) {
  4584. var partialsRight = partials,
  4585. holdersRight = holders;
  4586. partials = holders = undefined;
  4587. }
  4588. var data = isBindKey ? undefined : getData(func);
  4589. var newData = [
  4590. func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
  4591. argPos, ary, arity
  4592. ];
  4593. if (data) {
  4594. mergeData(newData, data);
  4595. }
  4596. func = newData[0];
  4597. bitmask = newData[1];
  4598. thisArg = newData[2];
  4599. partials = newData[3];
  4600. holders = newData[4];
  4601. arity = newData[9] = newData[9] == null
  4602. ? (isBindKey ? 0 : func.length)
  4603. : nativeMax(newData[9] - length, 0);
  4604. if (!arity && bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG)) {
  4605. bitmask &= ~(CURRY_FLAG | CURRY_RIGHT_FLAG);
  4606. }
  4607. if (!bitmask || bitmask == BIND_FLAG) {
  4608. var result = createBaseWrapper(func, bitmask, thisArg);
  4609. } else if (bitmask == CURRY_FLAG || bitmask == CURRY_RIGHT_FLAG) {
  4610. result = createCurryWrapper(func, bitmask, arity);
  4611. } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !holders.length) {
  4612. result = createPartialWrapper(func, bitmask, thisArg, partials);
  4613. } else {
  4614. result = createHybridWrapper.apply(undefined, newData);
  4615. }
  4616. var setter = data ? baseSetData : setData;
  4617. return setter(result, newData);
  4618. }
  4619. /**
  4620. * A specialized version of `baseIsEqualDeep` for arrays with support for
  4621. * partial deep comparisons.
  4622. *
  4623. * @private
  4624. * @param {Array} array The array to compare.
  4625. * @param {Array} other The other array to compare.
  4626. * @param {Function} equalFunc The function to determine equivalents of values.
  4627. * @param {Function} customizer The function to customize comparisons.
  4628. * @param {number} bitmask The bitmask of comparison flags. See `baseIsEqual`
  4629. * for more details.
  4630. * @param {Object} stack Tracks traversed `array` and `other` objects.
  4631. * @returns {boolean} Returns `true` if the arrays are equivalent, else `false`.
  4632. */
  4633. function equalArrays(array, other, equalFunc, customizer, bitmask, stack) {
  4634. var isPartial = bitmask & PARTIAL_COMPARE_FLAG,
  4635. arrLength = array.length,
  4636. othLength = other.length;
  4637. if (arrLength != othLength && !(isPartial && othLength > arrLength)) {
  4638. return false;
  4639. }
  4640. // Assume cyclic values are equal.
  4641. var stacked = stack.get(array);
  4642. if (stacked) {
  4643. return stacked == other;
  4644. }
  4645. var index = -1,
  4646. result = true,
  4647. seen = (bitmask & UNORDERED_COMPARE_FLAG) ? new SetCache : undefined;
  4648. stack.set(array, other);
  4649. // Ignore non-index properties.
  4650. while (++index < arrLength) {
  4651. var arrValue = array[index],
  4652. othValue = other[index];
  4653. if (customizer) {
  4654. var compared = isPartial
  4655. ? customizer(othValue, arrValue, index, other, array, stack)
  4656. : customizer(arrValue, othValue, index, array, other, stack);
  4657. }
  4658. if (compared !== undefined) {
  4659. if (compared) {
  4660. continue;
  4661. }
  4662. result = false;
  4663. break;
  4664. }
  4665. // Recursively compare arrays (susceptible to call stack limits).
  4666. if (seen) {
  4667. if (!arraySome(other, function(othValue, othIndex) {
  4668. if (!seen.has(othIndex) &&
  4669. (arrValue === othValue || equalFunc(arrValue, othValue, customizer, bitmask, stack))) {
  4670. return seen.add(othIndex);
  4671. }
  4672. })) {
  4673. result = false;
  4674. break;
  4675. }
  4676. } else if (!(
  4677. arrValue === othValue ||
  4678. equalFunc(arrValue, othValue, customizer, bitmask, stack)
  4679. )) {
  4680. result = false;
  4681. break;
  4682. }
  4683. }
  4684. stack['delete'](array);
  4685. return result;
  4686. }
  4687. /**
  4688. * A specialized version of `baseIsEqualDeep` for comparing objects of
  4689. * the same `toStringTag`.
  4690. *
  4691. * **Note:** This function only supports comparing values with tags of
  4692. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  4693. *
  4694. * @private
  4695. * @param {Object} object The object to compare.
  4696. * @param {Object} other The other object to compare.
  4697. * @param {string} tag The `toStringTag` of the objects to compare.
  4698. * @param {Function} equalFunc The function to determine equivalents of values.
  4699. * @param {Function} customizer The function to customize comparisons.
  4700. * @param {number} bitmask The bitmask of comparison flags. See `baseIsEqual`
  4701. * for more details.
  4702. * @param {Object} stack Tracks traversed `object` and `other` objects.
  4703. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  4704. */
  4705. function equalByTag(object, other, tag, equalFunc, customizer, bitmask, stack) {
  4706. switch (tag) {
  4707. case dataViewTag:
  4708. if ((object.byteLength != other.byteLength) ||
  4709. (object.byteOffset != other.byteOffset)) {
  4710. return false;
  4711. }
  4712. object = object.buffer;
  4713. other = other.buffer;
  4714. case arrayBufferTag:
  4715. if ((object.byteLength != other.byteLength) ||
  4716. !equalFunc(new Uint8Array(object), new Uint8Array(other))) {
  4717. return false;
  4718. }
  4719. return true;
  4720. case boolTag:
  4721. case dateTag:
  4722. // Coerce dates and booleans to numbers, dates to milliseconds and
  4723. // booleans to `1` or `0` treating invalid dates coerced to `NaN` as
  4724. // not equal.
  4725. return +object == +other;
  4726. case errorTag:
  4727. return object.name == other.name && object.message == other.message;
  4728. case numberTag:
  4729. // Treat `NaN` vs. `NaN` as equal.
  4730. return (object != +object) ? other != +other : object == +other;
  4731. case regexpTag:
  4732. case stringTag:
  4733. // Coerce regexes to strings and treat strings, primitives and objects,
  4734. // as equal. See http://www.ecma-international.org/ecma-262/6.0/#sec-regexp.prototype.tostring
  4735. // for more details.
  4736. return object == (other + '');
  4737. case mapTag:
  4738. var convert = mapToArray;
  4739. case setTag:
  4740. var isPartial = bitmask & PARTIAL_COMPARE_FLAG;
  4741. convert || (convert = setToArray);
  4742. if (object.size != other.size && !isPartial) {
  4743. return false;
  4744. }
  4745. // Assume cyclic values are equal.
  4746. var stacked = stack.get(object);
  4747. if (stacked) {
  4748. return stacked == other;
  4749. }
  4750. bitmask |= UNORDERED_COMPARE_FLAG;
  4751. stack.set(object, other);
  4752. // Recursively compare objects (susceptible to call stack limits).
  4753. return equalArrays(convert(object), convert(other), equalFunc, customizer, bitmask, stack);
  4754. case symbolTag:
  4755. if (symbolValueOf) {
  4756. return symbolValueOf.call(object) == symbolValueOf.call(other);
  4757. }
  4758. }
  4759. return false;
  4760. }
  4761. /**
  4762. * A specialized version of `baseIsEqualDeep` for objects with support for
  4763. * partial deep comparisons.
  4764. *
  4765. * @private
  4766. * @param {Object} object The object to compare.
  4767. * @param {Object} other The other object to compare.
  4768. * @param {Function} equalFunc The function to determine equivalents of values.
  4769. * @param {Function} customizer The function to customize comparisons.
  4770. * @param {number} bitmask The bitmask of comparison flags. See `baseIsEqual`
  4771. * for more details.
  4772. * @param {Object} stack Tracks traversed `object` and `other` objects.
  4773. * @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
  4774. */
  4775. function equalObjects(object, other, equalFunc, customizer, bitmask, stack) {
  4776. var isPartial = bitmask & PARTIAL_COMPARE_FLAG,
  4777. objProps = keys(object),
  4778. objLength = objProps.length,
  4779. othProps = keys(other),
  4780. othLength = othProps.length;
  4781. if (objLength != othLength && !isPartial) {
  4782. return false;
  4783. }
  4784. var index = objLength;
  4785. while (index--) {
  4786. var key = objProps[index];
  4787. if (!(isPartial ? key in other : baseHas(other, key))) {
  4788. return false;
  4789. }
  4790. }
  4791. // Assume cyclic values are equal.
  4792. var stacked = stack.get(object);
  4793. if (stacked) {
  4794. return stacked == other;
  4795. }
  4796. var result = true;
  4797. stack.set(object, other);
  4798. var skipCtor = isPartial;
  4799. while (++index < objLength) {
  4800. key = objProps[index];
  4801. var objValue = object[key],
  4802. othValue = other[key];
  4803. if (customizer) {
  4804. var compared = isPartial
  4805. ? customizer(othValue, objValue, key, other, object, stack)
  4806. : customizer(objValue, othValue, key, object, other, stack);
  4807. }
  4808. // Recursively compare objects (susceptible to call stack limits).
  4809. if (!(compared === undefined
  4810. ? (objValue === othValue || equalFunc(objValue, othValue, customizer, bitmask, stack))
  4811. : compared
  4812. )) {
  4813. result = false;
  4814. break;
  4815. }
  4816. skipCtor || (skipCtor = key == 'constructor');
  4817. }
  4818. if (result && !skipCtor) {
  4819. var objCtor = object.constructor,
  4820. othCtor = other.constructor;
  4821. // Non `Object` object instances with different constructors are not equal.
  4822. if (objCtor != othCtor &&
  4823. ('constructor' in object && 'constructor' in other) &&
  4824. !(typeof objCtor == 'function' && objCtor instanceof objCtor &&
  4825. typeof othCtor == 'function' && othCtor instanceof othCtor)) {
  4826. result = false;
  4827. }
  4828. }
  4829. stack['delete'](object);
  4830. return result;
  4831. }
  4832. /**
  4833. * Creates an array of own enumerable property names and symbols of `object`.
  4834. *
  4835. * @private
  4836. * @param {Object} object The object to query.
  4837. * @returns {Array} Returns the array of property names and symbols.
  4838. */
  4839. function getAllKeys(object) {
  4840. return baseGetAllKeys(object, keys, getSymbols);
  4841. }
  4842. /**
  4843. * Creates an array of own and inherited enumerable property names and
  4844. * symbols of `object`.
  4845. *
  4846. * @private
  4847. * @param {Object} object The object to query.
  4848. * @returns {Array} Returns the array of property names and symbols.
  4849. */
  4850. function getAllKeysIn(object) {
  4851. return baseGetAllKeys(object, keysIn, getSymbolsIn);
  4852. }
  4853. /**
  4854. * Gets metadata for `func`.
  4855. *
  4856. * @private
  4857. * @param {Function} func The function to query.
  4858. * @returns {*} Returns the metadata for `func`.
  4859. */
  4860. var getData = !metaMap ? noop : function(func) {
  4861. return metaMap.get(func);
  4862. };
  4863. /**
  4864. * Gets the name of `func`.
  4865. *
  4866. * @private
  4867. * @param {Function} func The function to query.
  4868. * @returns {string} Returns the function name.
  4869. */
  4870. function getFuncName(func) {
  4871. var result = (func.name + ''),
  4872. array = realNames[result],
  4873. length = hasOwnProperty.call(realNames, result) ? array.length : 0;
  4874. while (length--) {
  4875. var data = array[length],
  4876. otherFunc = data.func;
  4877. if (otherFunc == null || otherFunc == func) {
  4878. return data.name;
  4879. }
  4880. }
  4881. return result;
  4882. }
  4883. /**
  4884. * Gets the argument placeholder value for `func`.
  4885. *
  4886. * @private
  4887. * @param {Function} func The function to inspect.
  4888. * @returns {*} Returns the placeholder value.
  4889. */
  4890. function getHolder(func) {
  4891. var object = hasOwnProperty.call(lodash, 'placeholder') ? lodash : func;
  4892. return object.placeholder;
  4893. }
  4894. /**
  4895. * Gets the appropriate "iteratee" function. If `_.iteratee` is customized,
  4896. * this function returns the custom method, otherwise it returns `baseIteratee`.
  4897. * If arguments are provided, the chosen function is invoked with them and
  4898. * its result is returned.
  4899. *
  4900. * @private
  4901. * @param {*} [value] The value to convert to an iteratee.
  4902. * @param {number} [arity] The arity of the created iteratee.
  4903. * @returns {Function} Returns the chosen function or its result.
  4904. */
  4905. function getIteratee() {
  4906. var result = lodash.iteratee || iteratee;
  4907. result = result === iteratee ? baseIteratee : result;
  4908. return arguments.length ? result(arguments[0], arguments[1]) : result;
  4909. }
  4910. /**
  4911. * Gets the "length" property value of `object`.
  4912. *
  4913. * **Note:** This function is used to avoid a
  4914. * [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792) that affects
  4915. * Safari on at least iOS 8.1-8.3 ARM64.
  4916. *
  4917. * @private
  4918. * @param {Object} object The object to query.
  4919. * @returns {*} Returns the "length" value.
  4920. */
  4921. var getLength = baseProperty('length');
  4922. /**
  4923. * Gets the data for `map`.
  4924. *
  4925. * @private
  4926. * @param {Object} map The map to query.
  4927. * @param {string} key The reference key.
  4928. * @returns {*} Returns the map data.
  4929. */
  4930. function getMapData(map, key) {
  4931. var data = map.__data__;
  4932. return isKeyable(key)
  4933. ? data[typeof key == 'string' ? 'string' : 'hash']
  4934. : data.map;
  4935. }
  4936. /**
  4937. * Gets the property names, values, and compare flags of `object`.
  4938. *
  4939. * @private
  4940. * @param {Object} object The object to query.
  4941. * @returns {Array} Returns the match data of `object`.
  4942. */
  4943. function getMatchData(object) {
  4944. var result = keys(object),
  4945. length = result.length;
  4946. while (length--) {
  4947. var key = result[length],
  4948. value = object[key];
  4949. result[length] = [key, value, isStrictComparable(value)];
  4950. }
  4951. return result;
  4952. }
  4953. /**
  4954. * Gets the native function at `key` of `object`.
  4955. *
  4956. * @private
  4957. * @param {Object} object The object to query.
  4958. * @param {string} key The key of the method to get.
  4959. * @returns {*} Returns the function if it's native, else `undefined`.
  4960. */
  4961. function getNative(object, key) {
  4962. var value = getValue(object, key);
  4963. return baseIsNative(value) ? value : undefined;
  4964. }
  4965. /**
  4966. * Gets the `[[Prototype]]` of `value`.
  4967. *
  4968. * @private
  4969. * @param {*} value The value to query.
  4970. * @returns {null|Object} Returns the `[[Prototype]]`.
  4971. */
  4972. function getPrototype(value) {
  4973. return nativeGetPrototype(Object(value));
  4974. }
  4975. /**
  4976. * Creates an array of the own enumerable symbol properties of `object`.
  4977. *
  4978. * @private
  4979. * @param {Object} object The object to query.
  4980. * @returns {Array} Returns the array of symbols.
  4981. */
  4982. function getSymbols(object) {
  4983. // Coerce `object` to an object to avoid non-object errors in V8.
  4984. // See https://bugs.chromium.org/p/v8/issues/detail?id=3443 for more details.
  4985. return getOwnPropertySymbols(Object(object));
  4986. }
  4987. // Fallback for IE < 11.
  4988. if (!getOwnPropertySymbols) {
  4989. getSymbols = stubArray;
  4990. }
  4991. /**
  4992. * Creates an array of the own and inherited enumerable symbol properties
  4993. * of `object`.
  4994. *
  4995. * @private
  4996. * @param {Object} object The object to query.
  4997. * @returns {Array} Returns the array of symbols.
  4998. */
  4999. var getSymbolsIn = !getOwnPropertySymbols ? getSymbols : function(object) {
  5000. var result = [];
  5001. while (object) {
  5002. arrayPush(result, getSymbols(object));
  5003. object = getPrototype(object);
  5004. }
  5005. return result;
  5006. };
  5007. /**
  5008. * Gets the `toStringTag` of `value`.
  5009. *
  5010. * @private
  5011. * @param {*} value The value to query.
  5012. * @returns {string} Returns the `toStringTag`.
  5013. */
  5014. function getTag(value) {
  5015. return objectToString.call(value);
  5016. }
  5017. // Fallback for data views, maps, sets, and weak maps in IE 11,
  5018. // for data views in Edge, and promises in Node.js.
  5019. if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||
  5020. (Map && getTag(new Map) != mapTag) ||
  5021. (Promise && getTag(Promise.resolve()) != promiseTag) ||
  5022. (Set && getTag(new Set) != setTag) ||
  5023. (WeakMap && getTag(new WeakMap) != weakMapTag)) {
  5024. getTag = function(value) {
  5025. var result = objectToString.call(value),
  5026. Ctor = result == objectTag ? value.constructor : undefined,
  5027. ctorString = Ctor ? toSource(Ctor) : undefined;
  5028. if (ctorString) {
  5029. switch (ctorString) {
  5030. case dataViewCtorString: return dataViewTag;
  5031. case mapCtorString: return mapTag;
  5032. case promiseCtorString: return promiseTag;
  5033. case setCtorString: return setTag;
  5034. case weakMapCtorString: return weakMapTag;
  5035. }
  5036. }
  5037. return result;
  5038. };
  5039. }
  5040. /**
  5041. * Gets the view, applying any `transforms` to the `start` and `end` positions.
  5042. *
  5043. * @private
  5044. * @param {number} start The start of the view.
  5045. * @param {number} end The end of the view.
  5046. * @param {Array} transforms The transformations to apply to the view.
  5047. * @returns {Object} Returns an object containing the `start` and `end`
  5048. * positions of the view.
  5049. */
  5050. function getView(start, end, transforms) {
  5051. var index = -1,
  5052. length = transforms.length;
  5053. while (++index < length) {
  5054. var data = transforms[index],
  5055. size = data.size;
  5056. switch (data.type) {
  5057. case 'drop': start += size; break;
  5058. case 'dropRight': end -= size; break;
  5059. case 'take': end = nativeMin(end, start + size); break;
  5060. case 'takeRight': start = nativeMax(start, end - size); break;
  5061. }
  5062. }
  5063. return { 'start': start, 'end': end };
  5064. }
  5065. /**
  5066. * Checks if `path` exists on `object`.
  5067. *
  5068. * @private
  5069. * @param {Object} object The object to query.
  5070. * @param {Array|string} path The path to check.
  5071. * @param {Function} hasFunc The function to check properties.
  5072. * @returns {boolean} Returns `true` if `path` exists, else `false`.
  5073. */
  5074. function hasPath(object, path, hasFunc) {
  5075. path = isKey(path, object) ? [path] : castPath(path);
  5076. var result,
  5077. index = -1,
  5078. length = path.length;
  5079. while (++index < length) {
  5080. var key = toKey(path[index]);
  5081. if (!(result = object != null && hasFunc(object, key))) {
  5082. break;
  5083. }
  5084. object = object[key];
  5085. }
  5086. if (result) {
  5087. return result;
  5088. }
  5089. var length = object ? object.length : 0;
  5090. return !!length && isLength(length) && isIndex(key, length) &&
  5091. (isArray(object) || isString(object) || isArguments(object));
  5092. }
  5093. /**
  5094. * Initializes an array clone.
  5095. *
  5096. * @private
  5097. * @param {Array} array The array to clone.
  5098. * @returns {Array} Returns the initialized clone.
  5099. */
  5100. function initCloneArray(array) {
  5101. var length = array.length,
  5102. result = array.constructor(length);
  5103. // Add properties assigned by `RegExp#exec`.
  5104. if (length && typeof array[0] == 'string' && hasOwnProperty.call(array, 'index')) {
  5105. result.index = array.index;
  5106. result.input = array.input;
  5107. }
  5108. return result;
  5109. }
  5110. /**
  5111. * Initializes an object clone.
  5112. *
  5113. * @private
  5114. * @param {Object} object The object to clone.
  5115. * @returns {Object} Returns the initialized clone.
  5116. */
  5117. function initCloneObject(object) {
  5118. return (typeof object.constructor == 'function' && !isPrototype(object))
  5119. ? baseCreate(getPrototype(object))
  5120. : {};
  5121. }
  5122. /**
  5123. * Initializes an object clone based on its `toStringTag`.
  5124. *
  5125. * **Note:** This function only supports cloning values with tags of
  5126. * `Boolean`, `Date`, `Error`, `Number`, `RegExp`, or `String`.
  5127. *
  5128. * @private
  5129. * @param {Object} object The object to clone.
  5130. * @param {string} tag The `toStringTag` of the object to clone.
  5131. * @param {Function} cloneFunc The function to clone values.
  5132. * @param {boolean} [isDeep] Specify a deep clone.
  5133. * @returns {Object} Returns the initialized clone.
  5134. */
  5135. function initCloneByTag(object, tag, cloneFunc, isDeep) {
  5136. var Ctor = object.constructor;
  5137. switch (tag) {
  5138. case arrayBufferTag:
  5139. return cloneArrayBuffer(object);
  5140. case boolTag:
  5141. case dateTag:
  5142. return new Ctor(+object);
  5143. case dataViewTag:
  5144. return cloneDataView(object, isDeep);
  5145. case float32Tag: case float64Tag:
  5146. case int8Tag: case int16Tag: case int32Tag:
  5147. case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag:
  5148. return cloneTypedArray(object, isDeep);
  5149. case mapTag:
  5150. return cloneMap(object, isDeep, cloneFunc);
  5151. case numberTag:
  5152. case stringTag:
  5153. return new Ctor(object);
  5154. case regexpTag:
  5155. return cloneRegExp(object);
  5156. case setTag:
  5157. return cloneSet(object, isDeep, cloneFunc);
  5158. case symbolTag:
  5159. return cloneSymbol(object);
  5160. }
  5161. }
  5162. /**
  5163. * Creates an array of index keys for `object` values of arrays,
  5164. * `arguments` objects, and strings, otherwise `null` is returned.
  5165. *
  5166. * @private
  5167. * @param {Object} object The object to query.
  5168. * @returns {Array|null} Returns index keys, else `null`.
  5169. */
  5170. function indexKeys(object) {
  5171. var length = object ? object.length : undefined;
  5172. if (isLength(length) &&
  5173. (isArray(object) || isString(object) || isArguments(object))) {
  5174. return baseTimes(length, String);
  5175. }
  5176. return null;
  5177. }
  5178. /**
  5179. * Checks if `value` is a flattenable `arguments` object or array.
  5180. *
  5181. * @private
  5182. * @param {*} value The value to check.
  5183. * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
  5184. */
  5185. function isFlattenable(value) {
  5186. return isArray(value) || isArguments(value);
  5187. }
  5188. /**
  5189. * Checks if `value` is a flattenable array and not a `_.matchesProperty`
  5190. * iteratee shorthand.
  5191. *
  5192. * @private
  5193. * @param {*} value The value to check.
  5194. * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
  5195. */
  5196. function isFlattenableIteratee(value) {
  5197. return isArray(value) && !(value.length == 2 && !isFunction(value[0]));
  5198. }
  5199. /**
  5200. * Checks if `value` is a valid array-like index.
  5201. *
  5202. * @private
  5203. * @param {*} value The value to check.
  5204. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  5205. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  5206. */
  5207. function isIndex(value, length) {
  5208. length = length == null ? MAX_SAFE_INTEGER : length;
  5209. return !!length &&
  5210. (typeof value == 'number' || reIsUint.test(value)) &&
  5211. (value > -1 && value % 1 == 0 && value < length);
  5212. }
  5213. /**
  5214. * Checks if the given arguments are from an iteratee call.
  5215. *
  5216. * @private
  5217. * @param {*} value The potential iteratee value argument.
  5218. * @param {*} index The potential iteratee index or key argument.
  5219. * @param {*} object The potential iteratee object argument.
  5220. * @returns {boolean} Returns `true` if the arguments are from an iteratee call,
  5221. * else `false`.
  5222. */
  5223. function isIterateeCall(value, index, object) {
  5224. if (!isObject(object)) {
  5225. return false;
  5226. }
  5227. var type = typeof index;
  5228. if (type == 'number'
  5229. ? (isArrayLike(object) && isIndex(index, object.length))
  5230. : (type == 'string' && index in object)
  5231. ) {
  5232. return eq(object[index], value);
  5233. }
  5234. return false;
  5235. }
  5236. /**
  5237. * Checks if `value` is a property name and not a property path.
  5238. *
  5239. * @private
  5240. * @param {*} value The value to check.
  5241. * @param {Object} [object] The object to query keys on.
  5242. * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
  5243. */
  5244. function isKey(value, object) {
  5245. if (isArray(value)) {
  5246. return false;
  5247. }
  5248. var type = typeof value;
  5249. if (type == 'number' || type == 'symbol' || type == 'boolean' ||
  5250. value == null || isSymbol(value)) {
  5251. return true;
  5252. }
  5253. return reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
  5254. (object != null && value in Object(object));
  5255. }
  5256. /**
  5257. * Checks if `value` is suitable for use as unique object key.
  5258. *
  5259. * @private
  5260. * @param {*} value The value to check.
  5261. * @returns {boolean} Returns `true` if `value` is suitable, else `false`.
  5262. */
  5263. function isKeyable(value) {
  5264. var type = typeof value;
  5265. return (type == 'string' || type == 'number' || type == 'symbol' || type == 'boolean')
  5266. ? (value !== '__proto__')
  5267. : (value === null);
  5268. }
  5269. /**
  5270. * Checks if `func` has a lazy counterpart.
  5271. *
  5272. * @private
  5273. * @param {Function} func The function to check.
  5274. * @returns {boolean} Returns `true` if `func` has a lazy counterpart,
  5275. * else `false`.
  5276. */
  5277. function isLaziable(func) {
  5278. var funcName = getFuncName(func),
  5279. other = lodash[funcName];
  5280. if (typeof other != 'function' || !(funcName in LazyWrapper.prototype)) {
  5281. return false;
  5282. }
  5283. if (func === other) {
  5284. return true;
  5285. }
  5286. var data = getData(other);
  5287. return !!data && func === data[0];
  5288. }
  5289. /**
  5290. * Checks if `func` has its source masked.
  5291. *
  5292. * @private
  5293. * @param {Function} func The function to check.
  5294. * @returns {boolean} Returns `true` if `func` is masked, else `false`.
  5295. */
  5296. function isMasked(func) {
  5297. return !!maskSrcKey && (maskSrcKey in func);
  5298. }
  5299. /**
  5300. * Checks if `func` is capable of being masked.
  5301. *
  5302. * @private
  5303. * @param {*} value The value to check.
  5304. * @returns {boolean} Returns `true` if `func` is maskable, else `false`.
  5305. */
  5306. var isMaskable = coreJsData ? isFunction : stubFalse;
  5307. /**
  5308. * Checks if `value` is likely a prototype object.
  5309. *
  5310. * @private
  5311. * @param {*} value The value to check.
  5312. * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
  5313. */
  5314. function isPrototype(value) {
  5315. var Ctor = value && value.constructor,
  5316. proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
  5317. return value === proto;
  5318. }
  5319. /**
  5320. * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
  5321. *
  5322. * @private
  5323. * @param {*} value The value to check.
  5324. * @returns {boolean} Returns `true` if `value` if suitable for strict
  5325. * equality comparisons, else `false`.
  5326. */
  5327. function isStrictComparable(value) {
  5328. return value === value && !isObject(value);
  5329. }
  5330. /**
  5331. * A specialized version of `matchesProperty` for source values suitable
  5332. * for strict equality comparisons, i.e. `===`.
  5333. *
  5334. * @private
  5335. * @param {string} key The key of the property to get.
  5336. * @param {*} srcValue The value to match.
  5337. * @returns {Function} Returns the new spec function.
  5338. */
  5339. function matchesStrictComparable(key, srcValue) {
  5340. return function(object) {
  5341. if (object == null) {
  5342. return false;
  5343. }
  5344. return object[key] === srcValue &&
  5345. (srcValue !== undefined || (key in Object(object)));
  5346. };
  5347. }
  5348. /**
  5349. * Merges the function metadata of `source` into `data`.
  5350. *
  5351. * Merging metadata reduces the number of wrappers used to invoke a function.
  5352. * This is possible because methods like `_.bind`, `_.curry`, and `_.partial`
  5353. * may be applied regardless of execution order. Methods like `_.ary` and
  5354. * `_.rearg` modify function arguments, making the order in which they are
  5355. * executed important, preventing the merging of metadata. However, we make
  5356. * an exception for a safe combined case where curried functions have `_.ary`
  5357. * and or `_.rearg` applied.
  5358. *
  5359. * @private
  5360. * @param {Array} data The destination metadata.
  5361. * @param {Array} source The source metadata.
  5362. * @returns {Array} Returns `data`.
  5363. */
  5364. function mergeData(data, source) {
  5365. var bitmask = data[1],
  5366. srcBitmask = source[1],
  5367. newBitmask = bitmask | srcBitmask,
  5368. isCommon = newBitmask < (BIND_FLAG | BIND_KEY_FLAG | ARY_FLAG);
  5369. var isCombo =
  5370. ((srcBitmask == ARY_FLAG) && (bitmask == CURRY_FLAG)) ||
  5371. ((srcBitmask == ARY_FLAG) && (bitmask == REARG_FLAG) && (data[7].length <= source[8])) ||
  5372. ((srcBitmask == (ARY_FLAG | REARG_FLAG)) && (source[7].length <= source[8]) && (bitmask == CURRY_FLAG));
  5373. // Exit early if metadata can't be merged.
  5374. if (!(isCommon || isCombo)) {
  5375. return data;
  5376. }
  5377. // Use source `thisArg` if available.
  5378. if (srcBitmask & BIND_FLAG) {
  5379. data[2] = source[2];
  5380. // Set when currying a bound function.
  5381. newBitmask |= bitmask & BIND_FLAG ? 0 : CURRY_BOUND_FLAG;
  5382. }
  5383. // Compose partial arguments.
  5384. var value = source[3];
  5385. if (value) {
  5386. var partials = data[3];
  5387. data[3] = partials ? composeArgs(partials, value, source[4]) : value;
  5388. data[4] = partials ? replaceHolders(data[3], PLACEHOLDER) : source[4];
  5389. }
  5390. // Compose partial right arguments.
  5391. value = source[5];
  5392. if (value) {
  5393. partials = data[5];
  5394. data[5] = partials ? composeArgsRight(partials, value, source[6]) : value;
  5395. data[6] = partials ? replaceHolders(data[5], PLACEHOLDER) : source[6];
  5396. }
  5397. // Use source `argPos` if available.
  5398. value = source[7];
  5399. if (value) {
  5400. data[7] = value;
  5401. }
  5402. // Use source `ary` if it's smaller.
  5403. if (srcBitmask & ARY_FLAG) {
  5404. data[8] = data[8] == null ? source[8] : nativeMin(data[8], source[8]);
  5405. }
  5406. // Use source `arity` if one is not provided.
  5407. if (data[9] == null) {
  5408. data[9] = source[9];
  5409. }
  5410. // Use source `func` and merge bitmasks.
  5411. data[0] = source[0];
  5412. data[1] = newBitmask;
  5413. return data;
  5414. }
  5415. /**
  5416. * Used by `_.defaultsDeep` to customize its `_.merge` use.
  5417. *
  5418. * @private
  5419. * @param {*} objValue The destination value.
  5420. * @param {*} srcValue The source value.
  5421. * @param {string} key The key of the property to merge.
  5422. * @param {Object} object The parent object of `objValue`.
  5423. * @param {Object} source The parent object of `srcValue`.
  5424. * @param {Object} [stack] Tracks traversed source values and their merged
  5425. * counterparts.
  5426. * @returns {*} Returns the value to assign.
  5427. */
  5428. function mergeDefaults(objValue, srcValue, key, object, source, stack) {
  5429. if (isObject(objValue) && isObject(srcValue)) {
  5430. baseMerge(objValue, srcValue, undefined, mergeDefaults, stack.set(srcValue, objValue));
  5431. }
  5432. return objValue;
  5433. }
  5434. /**
  5435. * Gets the parent value at `path` of `object`.
  5436. *
  5437. * @private
  5438. * @param {Object} object The object to query.
  5439. * @param {Array} path The path to get the parent value of.
  5440. * @returns {*} Returns the parent value.
  5441. */
  5442. function parent(object, path) {
  5443. return path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  5444. }
  5445. /**
  5446. * Reorder `array` according to the specified indexes where the element at
  5447. * the first index is assigned as the first element, the element at
  5448. * the second index is assigned as the second element, and so on.
  5449. *
  5450. * @private
  5451. * @param {Array} array The array to reorder.
  5452. * @param {Array} indexes The arranged array indexes.
  5453. * @returns {Array} Returns `array`.
  5454. */
  5455. function reorder(array, indexes) {
  5456. var arrLength = array.length,
  5457. length = nativeMin(indexes.length, arrLength),
  5458. oldArray = copyArray(array);
  5459. while (length--) {
  5460. var index = indexes[length];
  5461. array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
  5462. }
  5463. return array;
  5464. }
  5465. /**
  5466. * Sets metadata for `func`.
  5467. *
  5468. * **Note:** If this function becomes hot, i.e. is invoked a lot in a short
  5469. * period of time, it will trip its breaker and transition to an identity
  5470. * function to avoid garbage collection pauses in V8. See
  5471. * [V8 issue 2070](https://bugs.chromium.org/p/v8/issues/detail?id=2070)
  5472. * for more details.
  5473. *
  5474. * @private
  5475. * @param {Function} func The function to associate metadata with.
  5476. * @param {*} data The metadata.
  5477. * @returns {Function} Returns `func`.
  5478. */
  5479. var setData = (function() {
  5480. var count = 0,
  5481. lastCalled = 0;
  5482. return function(key, value) {
  5483. var stamp = now(),
  5484. remaining = HOT_SPAN - (stamp - lastCalled);
  5485. lastCalled = stamp;
  5486. if (remaining > 0) {
  5487. if (++count >= HOT_COUNT) {
  5488. return key;
  5489. }
  5490. } else {
  5491. count = 0;
  5492. }
  5493. return baseSetData(key, value);
  5494. };
  5495. }());
  5496. /**
  5497. * Converts `string` to a property path array.
  5498. *
  5499. * @private
  5500. * @param {string} string The string to convert.
  5501. * @returns {Array} Returns the property path array.
  5502. */
  5503. var stringToPath = memoize(function(string) {
  5504. var result = [];
  5505. toString(string).replace(rePropName, function(match, number, quote, string) {
  5506. result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
  5507. });
  5508. return result;
  5509. });
  5510. /**
  5511. * Converts `value` to a string key if it's not a string or symbol.
  5512. *
  5513. * @private
  5514. * @param {*} value The value to inspect.
  5515. * @returns {string|symbol} Returns the key.
  5516. */
  5517. function toKey(value) {
  5518. if (typeof value == 'string' || isSymbol(value)) {
  5519. return value;
  5520. }
  5521. var result = (value + '');
  5522. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  5523. }
  5524. /**
  5525. * Converts `func` to its source code.
  5526. *
  5527. * @private
  5528. * @param {Function} func The function to process.
  5529. * @returns {string} Returns the source code.
  5530. */
  5531. function toSource(func) {
  5532. if (func != null) {
  5533. try {
  5534. return funcToString.call(func);
  5535. } catch (e) {}
  5536. try {
  5537. return (func + '');
  5538. } catch (e) {}
  5539. }
  5540. return '';
  5541. }
  5542. /**
  5543. * Creates a clone of `wrapper`.
  5544. *
  5545. * @private
  5546. * @param {Object} wrapper The wrapper to clone.
  5547. * @returns {Object} Returns the cloned wrapper.
  5548. */
  5549. function wrapperClone(wrapper) {
  5550. if (wrapper instanceof LazyWrapper) {
  5551. return wrapper.clone();
  5552. }
  5553. var result = new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__);
  5554. result.__actions__ = copyArray(wrapper.__actions__);
  5555. result.__index__ = wrapper.__index__;
  5556. result.__values__ = wrapper.__values__;
  5557. return result;
  5558. }
  5559. /*------------------------------------------------------------------------*/
  5560. /**
  5561. * Creates an array of elements split into groups the length of `size`.
  5562. * If `array` can't be split evenly, the final chunk will be the remaining
  5563. * elements.
  5564. *
  5565. * @static
  5566. * @memberOf _
  5567. * @since 3.0.0
  5568. * @category Array
  5569. * @param {Array} array The array to process.
  5570. * @param {number} [size=1] The length of each chunk
  5571. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  5572. * @returns {Array} Returns the new array of chunks.
  5573. * @example
  5574. *
  5575. * _.chunk(['a', 'b', 'c', 'd'], 2);
  5576. * // => [['a', 'b'], ['c', 'd']]
  5577. *
  5578. * _.chunk(['a', 'b', 'c', 'd'], 3);
  5579. * // => [['a', 'b', 'c'], ['d']]
  5580. */
  5581. function chunk(array, size, guard) {
  5582. if ((guard ? isIterateeCall(array, size, guard) : size === undefined)) {
  5583. size = 1;
  5584. } else {
  5585. size = nativeMax(toInteger(size), 0);
  5586. }
  5587. var length = array ? array.length : 0;
  5588. if (!length || size < 1) {
  5589. return [];
  5590. }
  5591. var index = 0,
  5592. resIndex = 0,
  5593. result = Array(nativeCeil(length / size));
  5594. while (index < length) {
  5595. result[resIndex++] = baseSlice(array, index, (index += size));
  5596. }
  5597. return result;
  5598. }
  5599. /**
  5600. * Creates an array with all falsey values removed. The values `false`, `null`,
  5601. * `0`, `""`, `undefined`, and `NaN` are falsey.
  5602. *
  5603. * @static
  5604. * @memberOf _
  5605. * @since 0.1.0
  5606. * @category Array
  5607. * @param {Array} array The array to compact.
  5608. * @returns {Array} Returns the new array of filtered values.
  5609. * @example
  5610. *
  5611. * _.compact([0, 1, false, 2, '', 3]);
  5612. * // => [1, 2, 3]
  5613. */
  5614. function compact(array) {
  5615. var index = -1,
  5616. length = array ? array.length : 0,
  5617. resIndex = 0,
  5618. result = [];
  5619. while (++index < length) {
  5620. var value = array[index];
  5621. if (value) {
  5622. result[resIndex++] = value;
  5623. }
  5624. }
  5625. return result;
  5626. }
  5627. /**
  5628. * Creates a new array concatenating `array` with any additional arrays
  5629. * and/or values.
  5630. *
  5631. * @static
  5632. * @memberOf _
  5633. * @since 4.0.0
  5634. * @category Array
  5635. * @param {Array} array The array to concatenate.
  5636. * @param {...*} [values] The values to concatenate.
  5637. * @returns {Array} Returns the new concatenated array.
  5638. * @example
  5639. *
  5640. * var array = [1];
  5641. * var other = _.concat(array, 2, [3], [[4]]);
  5642. *
  5643. * console.log(other);
  5644. * // => [1, 2, 3, [4]]
  5645. *
  5646. * console.log(array);
  5647. * // => [1]
  5648. */
  5649. function concat() {
  5650. var length = arguments.length,
  5651. args = Array(length ? length - 1 : 0),
  5652. array = arguments[0],
  5653. index = length;
  5654. while (index--) {
  5655. args[index - 1] = arguments[index];
  5656. }
  5657. return length
  5658. ? arrayPush(isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1))
  5659. : [];
  5660. }
  5661. /**
  5662. * Creates an array of unique `array` values not included in the other given
  5663. * arrays using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  5664. * for equality comparisons. The order of result values is determined by the
  5665. * order they occur in the first array.
  5666. *
  5667. * @static
  5668. * @memberOf _
  5669. * @since 0.1.0
  5670. * @category Array
  5671. * @param {Array} array The array to inspect.
  5672. * @param {...Array} [values] The values to exclude.
  5673. * @returns {Array} Returns the new array of filtered values.
  5674. * @see _.without, _.xor
  5675. * @example
  5676. *
  5677. * _.difference([2, 1], [2, 3]);
  5678. * // => [1]
  5679. */
  5680. var difference = rest(function(array, values) {
  5681. return isArrayLikeObject(array)
  5682. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true))
  5683. : [];
  5684. });
  5685. /**
  5686. * This method is like `_.difference` except that it accepts `iteratee` which
  5687. * is invoked for each element of `array` and `values` to generate the criterion
  5688. * by which they're compared. Result values are chosen from the first array.
  5689. * The iteratee is invoked with one argument: (value).
  5690. *
  5691. * @static
  5692. * @memberOf _
  5693. * @since 4.0.0
  5694. * @category Array
  5695. * @param {Array} array The array to inspect.
  5696. * @param {...Array} [values] The values to exclude.
  5697. * @param {Array|Function|Object|string} [iteratee=_.identity]
  5698. * The iteratee invoked per element.
  5699. * @returns {Array} Returns the new array of filtered values.
  5700. * @example
  5701. *
  5702. * _.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  5703. * // => [1.2]
  5704. *
  5705. * // The `_.property` iteratee shorthand.
  5706. * _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
  5707. * // => [{ 'x': 2 }]
  5708. */
  5709. var differenceBy = rest(function(array, values) {
  5710. var iteratee = last(values);
  5711. if (isArrayLikeObject(iteratee)) {
  5712. iteratee = undefined;
  5713. }
  5714. return isArrayLikeObject(array)
  5715. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), getIteratee(iteratee))
  5716. : [];
  5717. });
  5718. /**
  5719. * This method is like `_.difference` except that it accepts `comparator`
  5720. * which is invoked to compare elements of `array` to `values`. Result values
  5721. * are chosen from the first array. The comparator is invoked with two arguments:
  5722. * (arrVal, othVal).
  5723. *
  5724. * @static
  5725. * @memberOf _
  5726. * @since 4.0.0
  5727. * @category Array
  5728. * @param {Array} array The array to inspect.
  5729. * @param {...Array} [values] The values to exclude.
  5730. * @param {Function} [comparator] The comparator invoked per element.
  5731. * @returns {Array} Returns the new array of filtered values.
  5732. * @example
  5733. *
  5734. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  5735. *
  5736. * _.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
  5737. * // => [{ 'x': 2, 'y': 1 }]
  5738. */
  5739. var differenceWith = rest(function(array, values) {
  5740. var comparator = last(values);
  5741. if (isArrayLikeObject(comparator)) {
  5742. comparator = undefined;
  5743. }
  5744. return isArrayLikeObject(array)
  5745. ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator)
  5746. : [];
  5747. });
  5748. /**
  5749. * Creates a slice of `array` with `n` elements dropped from the beginning.
  5750. *
  5751. * @static
  5752. * @memberOf _
  5753. * @since 0.5.0
  5754. * @category Array
  5755. * @param {Array} array The array to query.
  5756. * @param {number} [n=1] The number of elements to drop.
  5757. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  5758. * @returns {Array} Returns the slice of `array`.
  5759. * @example
  5760. *
  5761. * _.drop([1, 2, 3]);
  5762. * // => [2, 3]
  5763. *
  5764. * _.drop([1, 2, 3], 2);
  5765. * // => [3]
  5766. *
  5767. * _.drop([1, 2, 3], 5);
  5768. * // => []
  5769. *
  5770. * _.drop([1, 2, 3], 0);
  5771. * // => [1, 2, 3]
  5772. */
  5773. function drop(array, n, guard) {
  5774. var length = array ? array.length : 0;
  5775. if (!length) {
  5776. return [];
  5777. }
  5778. n = (guard || n === undefined) ? 1 : toInteger(n);
  5779. return baseSlice(array, n < 0 ? 0 : n, length);
  5780. }
  5781. /**
  5782. * Creates a slice of `array` with `n` elements dropped from the end.
  5783. *
  5784. * @static
  5785. * @memberOf _
  5786. * @since 3.0.0
  5787. * @category Array
  5788. * @param {Array} array The array to query.
  5789. * @param {number} [n=1] The number of elements to drop.
  5790. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  5791. * @returns {Array} Returns the slice of `array`.
  5792. * @example
  5793. *
  5794. * _.dropRight([1, 2, 3]);
  5795. * // => [1, 2]
  5796. *
  5797. * _.dropRight([1, 2, 3], 2);
  5798. * // => [1]
  5799. *
  5800. * _.dropRight([1, 2, 3], 5);
  5801. * // => []
  5802. *
  5803. * _.dropRight([1, 2, 3], 0);
  5804. * // => [1, 2, 3]
  5805. */
  5806. function dropRight(array, n, guard) {
  5807. var length = array ? array.length : 0;
  5808. if (!length) {
  5809. return [];
  5810. }
  5811. n = (guard || n === undefined) ? 1 : toInteger(n);
  5812. n = length - n;
  5813. return baseSlice(array, 0, n < 0 ? 0 : n);
  5814. }
  5815. /**
  5816. * Creates a slice of `array` excluding elements dropped from the end.
  5817. * Elements are dropped until `predicate` returns falsey. The predicate is
  5818. * invoked with three arguments: (value, index, array).
  5819. *
  5820. * @static
  5821. * @memberOf _
  5822. * @since 3.0.0
  5823. * @category Array
  5824. * @param {Array} array The array to query.
  5825. * @param {Array|Function|Object|string} [predicate=_.identity]
  5826. * The function invoked per iteration.
  5827. * @returns {Array} Returns the slice of `array`.
  5828. * @example
  5829. *
  5830. * var users = [
  5831. * { 'user': 'barney', 'active': true },
  5832. * { 'user': 'fred', 'active': false },
  5833. * { 'user': 'pebbles', 'active': false }
  5834. * ];
  5835. *
  5836. * _.dropRightWhile(users, function(o) { return !o.active; });
  5837. * // => objects for ['barney']
  5838. *
  5839. * // The `_.matches` iteratee shorthand.
  5840. * _.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
  5841. * // => objects for ['barney', 'fred']
  5842. *
  5843. * // The `_.matchesProperty` iteratee shorthand.
  5844. * _.dropRightWhile(users, ['active', false]);
  5845. * // => objects for ['barney']
  5846. *
  5847. * // The `_.property` iteratee shorthand.
  5848. * _.dropRightWhile(users, 'active');
  5849. * // => objects for ['barney', 'fred', 'pebbles']
  5850. */
  5851. function dropRightWhile(array, predicate) {
  5852. return (array && array.length)
  5853. ? baseWhile(array, getIteratee(predicate, 3), true, true)
  5854. : [];
  5855. }
  5856. /**
  5857. * Creates a slice of `array` excluding elements dropped from the beginning.
  5858. * Elements are dropped until `predicate` returns falsey. The predicate is
  5859. * invoked with three arguments: (value, index, array).
  5860. *
  5861. * @static
  5862. * @memberOf _
  5863. * @since 3.0.0
  5864. * @category Array
  5865. * @param {Array} array The array to query.
  5866. * @param {Array|Function|Object|string} [predicate=_.identity]
  5867. * The function invoked per iteration.
  5868. * @returns {Array} Returns the slice of `array`.
  5869. * @example
  5870. *
  5871. * var users = [
  5872. * { 'user': 'barney', 'active': false },
  5873. * { 'user': 'fred', 'active': false },
  5874. * { 'user': 'pebbles', 'active': true }
  5875. * ];
  5876. *
  5877. * _.dropWhile(users, function(o) { return !o.active; });
  5878. * // => objects for ['pebbles']
  5879. *
  5880. * // The `_.matches` iteratee shorthand.
  5881. * _.dropWhile(users, { 'user': 'barney', 'active': false });
  5882. * // => objects for ['fred', 'pebbles']
  5883. *
  5884. * // The `_.matchesProperty` iteratee shorthand.
  5885. * _.dropWhile(users, ['active', false]);
  5886. * // => objects for ['pebbles']
  5887. *
  5888. * // The `_.property` iteratee shorthand.
  5889. * _.dropWhile(users, 'active');
  5890. * // => objects for ['barney', 'fred', 'pebbles']
  5891. */
  5892. function dropWhile(array, predicate) {
  5893. return (array && array.length)
  5894. ? baseWhile(array, getIteratee(predicate, 3), true)
  5895. : [];
  5896. }
  5897. /**
  5898. * Fills elements of `array` with `value` from `start` up to, but not
  5899. * including, `end`.
  5900. *
  5901. * **Note:** This method mutates `array`.
  5902. *
  5903. * @static
  5904. * @memberOf _
  5905. * @since 3.2.0
  5906. * @category Array
  5907. * @param {Array} array The array to fill.
  5908. * @param {*} value The value to fill `array` with.
  5909. * @param {number} [start=0] The start position.
  5910. * @param {number} [end=array.length] The end position.
  5911. * @returns {Array} Returns `array`.
  5912. * @example
  5913. *
  5914. * var array = [1, 2, 3];
  5915. *
  5916. * _.fill(array, 'a');
  5917. * console.log(array);
  5918. * // => ['a', 'a', 'a']
  5919. *
  5920. * _.fill(Array(3), 2);
  5921. * // => [2, 2, 2]
  5922. *
  5923. * _.fill([4, 6, 8, 10], '*', 1, 3);
  5924. * // => [4, '*', '*', 10]
  5925. */
  5926. function fill(array, value, start, end) {
  5927. var length = array ? array.length : 0;
  5928. if (!length) {
  5929. return [];
  5930. }
  5931. if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
  5932. start = 0;
  5933. end = length;
  5934. }
  5935. return baseFill(array, value, start, end);
  5936. }
  5937. /**
  5938. * This method is like `_.find` except that it returns the index of the first
  5939. * element `predicate` returns truthy for instead of the element itself.
  5940. *
  5941. * @static
  5942. * @memberOf _
  5943. * @since 1.1.0
  5944. * @category Array
  5945. * @param {Array} array The array to search.
  5946. * @param {Array|Function|Object|string} [predicate=_.identity]
  5947. * The function invoked per iteration.
  5948. * @param {number} [fromIndex=0] The index to search from.
  5949. * @returns {number} Returns the index of the found element, else `-1`.
  5950. * @example
  5951. *
  5952. * var users = [
  5953. * { 'user': 'barney', 'active': false },
  5954. * { 'user': 'fred', 'active': false },
  5955. * { 'user': 'pebbles', 'active': true }
  5956. * ];
  5957. *
  5958. * _.findIndex(users, function(o) { return o.user == 'barney'; });
  5959. * // => 0
  5960. *
  5961. * // The `_.matches` iteratee shorthand.
  5962. * _.findIndex(users, { 'user': 'fred', 'active': false });
  5963. * // => 1
  5964. *
  5965. * // The `_.matchesProperty` iteratee shorthand.
  5966. * _.findIndex(users, ['active', false]);
  5967. * // => 0
  5968. *
  5969. * // The `_.property` iteratee shorthand.
  5970. * _.findIndex(users, 'active');
  5971. * // => 2
  5972. */
  5973. function findIndex(array, predicate, fromIndex) {
  5974. var length = array ? array.length : 0;
  5975. if (!length) {
  5976. return -1;
  5977. }
  5978. var index = fromIndex == null ? 0 : toInteger(fromIndex);
  5979. if (index < 0) {
  5980. index = nativeMax(length + index, 0);
  5981. }
  5982. return baseFindIndex(array, getIteratee(predicate, 3), index);
  5983. }
  5984. /**
  5985. * This method is like `_.findIndex` except that it iterates over elements
  5986. * of `collection` from right to left.
  5987. *
  5988. * @static
  5989. * @memberOf _
  5990. * @since 2.0.0
  5991. * @category Array
  5992. * @param {Array} array The array to search.
  5993. * @param {Array|Function|Object|string} [predicate=_.identity]
  5994. * The function invoked per iteration.
  5995. * @param {number} [fromIndex=array.length-1] The index to search from.
  5996. * @returns {number} Returns the index of the found element, else `-1`.
  5997. * @example
  5998. *
  5999. * var users = [
  6000. * { 'user': 'barney', 'active': true },
  6001. * { 'user': 'fred', 'active': false },
  6002. * { 'user': 'pebbles', 'active': false }
  6003. * ];
  6004. *
  6005. * _.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
  6006. * // => 2
  6007. *
  6008. * // The `_.matches` iteratee shorthand.
  6009. * _.findLastIndex(users, { 'user': 'barney', 'active': true });
  6010. * // => 0
  6011. *
  6012. * // The `_.matchesProperty` iteratee shorthand.
  6013. * _.findLastIndex(users, ['active', false]);
  6014. * // => 2
  6015. *
  6016. * // The `_.property` iteratee shorthand.
  6017. * _.findLastIndex(users, 'active');
  6018. * // => 0
  6019. */
  6020. function findLastIndex(array, predicate, fromIndex) {
  6021. var length = array ? array.length : 0;
  6022. if (!length) {
  6023. return -1;
  6024. }
  6025. var index = length - 1;
  6026. if (fromIndex !== undefined) {
  6027. index = toInteger(fromIndex);
  6028. index = fromIndex < 0
  6029. ? nativeMax(length + index, 0)
  6030. : nativeMin(index, length - 1);
  6031. }
  6032. return baseFindIndex(array, getIteratee(predicate, 3), index, true);
  6033. }
  6034. /**
  6035. * Flattens `array` a single level deep.
  6036. *
  6037. * @static
  6038. * @memberOf _
  6039. * @since 0.1.0
  6040. * @category Array
  6041. * @param {Array} array The array to flatten.
  6042. * @returns {Array} Returns the new flattened array.
  6043. * @example
  6044. *
  6045. * _.flatten([1, [2, [3, [4]], 5]]);
  6046. * // => [1, 2, [3, [4]], 5]
  6047. */
  6048. function flatten(array) {
  6049. var length = array ? array.length : 0;
  6050. return length ? baseFlatten(array, 1) : [];
  6051. }
  6052. /**
  6053. * Recursively flattens `array`.
  6054. *
  6055. * @static
  6056. * @memberOf _
  6057. * @since 3.0.0
  6058. * @category Array
  6059. * @param {Array} array The array to flatten.
  6060. * @returns {Array} Returns the new flattened array.
  6061. * @example
  6062. *
  6063. * _.flattenDeep([1, [2, [3, [4]], 5]]);
  6064. * // => [1, 2, 3, 4, 5]
  6065. */
  6066. function flattenDeep(array) {
  6067. var length = array ? array.length : 0;
  6068. return length ? baseFlatten(array, INFINITY) : [];
  6069. }
  6070. /**
  6071. * Recursively flatten `array` up to `depth` times.
  6072. *
  6073. * @static
  6074. * @memberOf _
  6075. * @since 4.4.0
  6076. * @category Array
  6077. * @param {Array} array The array to flatten.
  6078. * @param {number} [depth=1] The maximum recursion depth.
  6079. * @returns {Array} Returns the new flattened array.
  6080. * @example
  6081. *
  6082. * var array = [1, [2, [3, [4]], 5]];
  6083. *
  6084. * _.flattenDepth(array, 1);
  6085. * // => [1, 2, [3, [4]], 5]
  6086. *
  6087. * _.flattenDepth(array, 2);
  6088. * // => [1, 2, 3, [4], 5]
  6089. */
  6090. function flattenDepth(array, depth) {
  6091. var length = array ? array.length : 0;
  6092. if (!length) {
  6093. return [];
  6094. }
  6095. depth = depth === undefined ? 1 : toInteger(depth);
  6096. return baseFlatten(array, depth);
  6097. }
  6098. /**
  6099. * The inverse of `_.toPairs`; this method returns an object composed
  6100. * from key-value `pairs`.
  6101. *
  6102. * @static
  6103. * @memberOf _
  6104. * @since 4.0.0
  6105. * @category Array
  6106. * @param {Array} pairs The key-value pairs.
  6107. * @returns {Object} Returns the new object.
  6108. * @example
  6109. *
  6110. * _.fromPairs([['fred', 30], ['barney', 40]]);
  6111. * // => { 'fred': 30, 'barney': 40 }
  6112. */
  6113. function fromPairs(pairs) {
  6114. var index = -1,
  6115. length = pairs ? pairs.length : 0,
  6116. result = {};
  6117. while (++index < length) {
  6118. var pair = pairs[index];
  6119. result[pair[0]] = pair[1];
  6120. }
  6121. return result;
  6122. }
  6123. /**
  6124. * Gets the first element of `array`.
  6125. *
  6126. * @static
  6127. * @memberOf _
  6128. * @since 0.1.0
  6129. * @alias first
  6130. * @category Array
  6131. * @param {Array} array The array to query.
  6132. * @returns {*} Returns the first element of `array`.
  6133. * @example
  6134. *
  6135. * _.head([1, 2, 3]);
  6136. * // => 1
  6137. *
  6138. * _.head([]);
  6139. * // => undefined
  6140. */
  6141. function head(array) {
  6142. return (array && array.length) ? array[0] : undefined;
  6143. }
  6144. /**
  6145. * Gets the index at which the first occurrence of `value` is found in `array`
  6146. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  6147. * for equality comparisons. If `fromIndex` is negative, it's used as the
  6148. * offset from the end of `array`.
  6149. *
  6150. * @static
  6151. * @memberOf _
  6152. * @since 0.1.0
  6153. * @category Array
  6154. * @param {Array} array The array to search.
  6155. * @param {*} value The value to search for.
  6156. * @param {number} [fromIndex=0] The index to search from.
  6157. * @returns {number} Returns the index of the matched value, else `-1`.
  6158. * @example
  6159. *
  6160. * _.indexOf([1, 2, 1, 2], 2);
  6161. * // => 1
  6162. *
  6163. * // Search from the `fromIndex`.
  6164. * _.indexOf([1, 2, 1, 2], 2, 2);
  6165. * // => 3
  6166. */
  6167. function indexOf(array, value, fromIndex) {
  6168. var length = array ? array.length : 0;
  6169. if (!length) {
  6170. return -1;
  6171. }
  6172. var index = fromIndex == null ? 0 : toInteger(fromIndex);
  6173. if (index < 0) {
  6174. index = nativeMax(length + index, 0);
  6175. }
  6176. return baseIndexOf(array, value, index);
  6177. }
  6178. /**
  6179. * Gets all but the last element of `array`.
  6180. *
  6181. * @static
  6182. * @memberOf _
  6183. * @since 0.1.0
  6184. * @category Array
  6185. * @param {Array} array The array to query.
  6186. * @returns {Array} Returns the slice of `array`.
  6187. * @example
  6188. *
  6189. * _.initial([1, 2, 3]);
  6190. * // => [1, 2]
  6191. */
  6192. function initial(array) {
  6193. return dropRight(array, 1);
  6194. }
  6195. /**
  6196. * Creates an array of unique values that are included in all given arrays
  6197. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  6198. * for equality comparisons. The order of result values is determined by the
  6199. * order they occur in the first array.
  6200. *
  6201. * @static
  6202. * @memberOf _
  6203. * @since 0.1.0
  6204. * @category Array
  6205. * @param {...Array} [arrays] The arrays to inspect.
  6206. * @returns {Array} Returns the new array of intersecting values.
  6207. * @example
  6208. *
  6209. * _.intersection([2, 1], [2, 3]);
  6210. * // => [2]
  6211. */
  6212. var intersection = rest(function(arrays) {
  6213. var mapped = arrayMap(arrays, castArrayLikeObject);
  6214. return (mapped.length && mapped[0] === arrays[0])
  6215. ? baseIntersection(mapped)
  6216. : [];
  6217. });
  6218. /**
  6219. * This method is like `_.intersection` except that it accepts `iteratee`
  6220. * which is invoked for each element of each `arrays` to generate the criterion
  6221. * by which they're compared. Result values are chosen from the first array.
  6222. * The iteratee is invoked with one argument: (value).
  6223. *
  6224. * @static
  6225. * @memberOf _
  6226. * @since 4.0.0
  6227. * @category Array
  6228. * @param {...Array} [arrays] The arrays to inspect.
  6229. * @param {Array|Function|Object|string} [iteratee=_.identity]
  6230. * The iteratee invoked per element.
  6231. * @returns {Array} Returns the new array of intersecting values.
  6232. * @example
  6233. *
  6234. * _.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  6235. * // => [2.1]
  6236. *
  6237. * // The `_.property` iteratee shorthand.
  6238. * _.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  6239. * // => [{ 'x': 1 }]
  6240. */
  6241. var intersectionBy = rest(function(arrays) {
  6242. var iteratee = last(arrays),
  6243. mapped = arrayMap(arrays, castArrayLikeObject);
  6244. if (iteratee === last(mapped)) {
  6245. iteratee = undefined;
  6246. } else {
  6247. mapped.pop();
  6248. }
  6249. return (mapped.length && mapped[0] === arrays[0])
  6250. ? baseIntersection(mapped, getIteratee(iteratee))
  6251. : [];
  6252. });
  6253. /**
  6254. * This method is like `_.intersection` except that it accepts `comparator`
  6255. * which is invoked to compare elements of `arrays`. Result values are chosen
  6256. * from the first array. The comparator is invoked with two arguments:
  6257. * (arrVal, othVal).
  6258. *
  6259. * @static
  6260. * @memberOf _
  6261. * @since 4.0.0
  6262. * @category Array
  6263. * @param {...Array} [arrays] The arrays to inspect.
  6264. * @param {Function} [comparator] The comparator invoked per element.
  6265. * @returns {Array} Returns the new array of intersecting values.
  6266. * @example
  6267. *
  6268. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  6269. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  6270. *
  6271. * _.intersectionWith(objects, others, _.isEqual);
  6272. * // => [{ 'x': 1, 'y': 2 }]
  6273. */
  6274. var intersectionWith = rest(function(arrays) {
  6275. var comparator = last(arrays),
  6276. mapped = arrayMap(arrays, castArrayLikeObject);
  6277. if (comparator === last(mapped)) {
  6278. comparator = undefined;
  6279. } else {
  6280. mapped.pop();
  6281. }
  6282. return (mapped.length && mapped[0] === arrays[0])
  6283. ? baseIntersection(mapped, undefined, comparator)
  6284. : [];
  6285. });
  6286. /**
  6287. * Converts all elements in `array` into a string separated by `separator`.
  6288. *
  6289. * @static
  6290. * @memberOf _
  6291. * @since 4.0.0
  6292. * @category Array
  6293. * @param {Array} array The array to convert.
  6294. * @param {string} [separator=','] The element separator.
  6295. * @returns {string} Returns the joined string.
  6296. * @example
  6297. *
  6298. * _.join(['a', 'b', 'c'], '~');
  6299. * // => 'a~b~c'
  6300. */
  6301. function join(array, separator) {
  6302. return array ? nativeJoin.call(array, separator) : '';
  6303. }
  6304. /**
  6305. * Gets the last element of `array`.
  6306. *
  6307. * @static
  6308. * @memberOf _
  6309. * @since 0.1.0
  6310. * @category Array
  6311. * @param {Array} array The array to query.
  6312. * @returns {*} Returns the last element of `array`.
  6313. * @example
  6314. *
  6315. * _.last([1, 2, 3]);
  6316. * // => 3
  6317. */
  6318. function last(array) {
  6319. var length = array ? array.length : 0;
  6320. return length ? array[length - 1] : undefined;
  6321. }
  6322. /**
  6323. * This method is like `_.indexOf` except that it iterates over elements of
  6324. * `array` from right to left.
  6325. *
  6326. * @static
  6327. * @memberOf _
  6328. * @since 0.1.0
  6329. * @category Array
  6330. * @param {Array} array The array to search.
  6331. * @param {*} value The value to search for.
  6332. * @param {number} [fromIndex=array.length-1] The index to search from.
  6333. * @returns {number} Returns the index of the matched value, else `-1`.
  6334. * @example
  6335. *
  6336. * _.lastIndexOf([1, 2, 1, 2], 2);
  6337. * // => 3
  6338. *
  6339. * // Search from the `fromIndex`.
  6340. * _.lastIndexOf([1, 2, 1, 2], 2, 2);
  6341. * // => 1
  6342. */
  6343. function lastIndexOf(array, value, fromIndex) {
  6344. var length = array ? array.length : 0;
  6345. if (!length) {
  6346. return -1;
  6347. }
  6348. var index = length;
  6349. if (fromIndex !== undefined) {
  6350. index = toInteger(fromIndex);
  6351. index = (
  6352. index < 0
  6353. ? nativeMax(length + index, 0)
  6354. : nativeMin(index, length - 1)
  6355. ) + 1;
  6356. }
  6357. if (value !== value) {
  6358. return indexOfNaN(array, index - 1, true);
  6359. }
  6360. while (index--) {
  6361. if (array[index] === value) {
  6362. return index;
  6363. }
  6364. }
  6365. return -1;
  6366. }
  6367. /**
  6368. * Gets the element at index `n` of `array`. If `n` is negative, the nth
  6369. * element from the end is returned.
  6370. *
  6371. * @static
  6372. * @memberOf _
  6373. * @since 4.11.0
  6374. * @category Array
  6375. * @param {Array} array The array to query.
  6376. * @param {number} [n=0] The index of the element to return.
  6377. * @returns {*} Returns the nth element of `array`.
  6378. * @example
  6379. *
  6380. * var array = ['a', 'b', 'c', 'd'];
  6381. *
  6382. * _.nth(array, 1);
  6383. * // => 'b'
  6384. *
  6385. * _.nth(array, -2);
  6386. * // => 'c';
  6387. */
  6388. function nth(array, n) {
  6389. return (array && array.length) ? baseNth(array, toInteger(n)) : undefined;
  6390. }
  6391. /**
  6392. * Removes all given values from `array` using
  6393. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  6394. * for equality comparisons.
  6395. *
  6396. * **Note:** Unlike `_.without`, this method mutates `array`. Use `_.remove`
  6397. * to remove elements from an array by predicate.
  6398. *
  6399. * @static
  6400. * @memberOf _
  6401. * @since 2.0.0
  6402. * @category Array
  6403. * @param {Array} array The array to modify.
  6404. * @param {...*} [values] The values to remove.
  6405. * @returns {Array} Returns `array`.
  6406. * @example
  6407. *
  6408. * var array = ['a', 'b', 'c', 'a', 'b', 'c'];
  6409. *
  6410. * _.pull(array, 'a', 'c');
  6411. * console.log(array);
  6412. * // => ['b', 'b']
  6413. */
  6414. var pull = rest(pullAll);
  6415. /**
  6416. * This method is like `_.pull` except that it accepts an array of values to remove.
  6417. *
  6418. * **Note:** Unlike `_.difference`, this method mutates `array`.
  6419. *
  6420. * @static
  6421. * @memberOf _
  6422. * @since 4.0.0
  6423. * @category Array
  6424. * @param {Array} array The array to modify.
  6425. * @param {Array} values The values to remove.
  6426. * @returns {Array} Returns `array`.
  6427. * @example
  6428. *
  6429. * var array = ['a', 'b', 'c', 'a', 'b', 'c'];
  6430. *
  6431. * _.pullAll(array, ['a', 'c']);
  6432. * console.log(array);
  6433. * // => ['b', 'b']
  6434. */
  6435. function pullAll(array, values) {
  6436. return (array && array.length && values && values.length)
  6437. ? basePullAll(array, values)
  6438. : array;
  6439. }
  6440. /**
  6441. * This method is like `_.pullAll` except that it accepts `iteratee` which is
  6442. * invoked for each element of `array` and `values` to generate the criterion
  6443. * by which they're compared. The iteratee is invoked with one argument: (value).
  6444. *
  6445. * **Note:** Unlike `_.differenceBy`, this method mutates `array`.
  6446. *
  6447. * @static
  6448. * @memberOf _
  6449. * @since 4.0.0
  6450. * @category Array
  6451. * @param {Array} array The array to modify.
  6452. * @param {Array} values The values to remove.
  6453. * @param {Array|Function|Object|string} [iteratee=_.identity]
  6454. * The iteratee invoked per element.
  6455. * @returns {Array} Returns `array`.
  6456. * @example
  6457. *
  6458. * var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
  6459. *
  6460. * _.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
  6461. * console.log(array);
  6462. * // => [{ 'x': 2 }]
  6463. */
  6464. function pullAllBy(array, values, iteratee) {
  6465. return (array && array.length && values && values.length)
  6466. ? basePullAll(array, values, getIteratee(iteratee))
  6467. : array;
  6468. }
  6469. /**
  6470. * This method is like `_.pullAll` except that it accepts `comparator` which
  6471. * is invoked to compare elements of `array` to `values`. The comparator is
  6472. * invoked with two arguments: (arrVal, othVal).
  6473. *
  6474. * **Note:** Unlike `_.differenceWith`, this method mutates `array`.
  6475. *
  6476. * @static
  6477. * @memberOf _
  6478. * @since 4.6.0
  6479. * @category Array
  6480. * @param {Array} array The array to modify.
  6481. * @param {Array} values The values to remove.
  6482. * @param {Function} [comparator] The comparator invoked per element.
  6483. * @returns {Array} Returns `array`.
  6484. * @example
  6485. *
  6486. * var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
  6487. *
  6488. * _.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
  6489. * console.log(array);
  6490. * // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]
  6491. */
  6492. function pullAllWith(array, values, comparator) {
  6493. return (array && array.length && values && values.length)
  6494. ? basePullAll(array, values, undefined, comparator)
  6495. : array;
  6496. }
  6497. /**
  6498. * Removes elements from `array` corresponding to `indexes` and returns an
  6499. * array of removed elements.
  6500. *
  6501. * **Note:** Unlike `_.at`, this method mutates `array`.
  6502. *
  6503. * @static
  6504. * @memberOf _
  6505. * @since 3.0.0
  6506. * @category Array
  6507. * @param {Array} array The array to modify.
  6508. * @param {...(number|number[])} [indexes] The indexes of elements to remove.
  6509. * @returns {Array} Returns the new array of removed elements.
  6510. * @example
  6511. *
  6512. * var array = ['a', 'b', 'c', 'd'];
  6513. * var pulled = _.pullAt(array, [1, 3]);
  6514. *
  6515. * console.log(array);
  6516. * // => ['a', 'c']
  6517. *
  6518. * console.log(pulled);
  6519. * // => ['b', 'd']
  6520. */
  6521. var pullAt = rest(function(array, indexes) {
  6522. indexes = baseFlatten(indexes, 1);
  6523. var length = array ? array.length : 0,
  6524. result = baseAt(array, indexes);
  6525. basePullAt(array, arrayMap(indexes, function(index) {
  6526. return isIndex(index, length) ? +index : index;
  6527. }).sort(compareAscending));
  6528. return result;
  6529. });
  6530. /**
  6531. * Removes all elements from `array` that `predicate` returns truthy for
  6532. * and returns an array of the removed elements. The predicate is invoked
  6533. * with three arguments: (value, index, array).
  6534. *
  6535. * **Note:** Unlike `_.filter`, this method mutates `array`. Use `_.pull`
  6536. * to pull elements from an array by value.
  6537. *
  6538. * @static
  6539. * @memberOf _
  6540. * @since 2.0.0
  6541. * @category Array
  6542. * @param {Array} array The array to modify.
  6543. * @param {Array|Function|Object|string} [predicate=_.identity]
  6544. * The function invoked per iteration.
  6545. * @returns {Array} Returns the new array of removed elements.
  6546. * @example
  6547. *
  6548. * var array = [1, 2, 3, 4];
  6549. * var evens = _.remove(array, function(n) {
  6550. * return n % 2 == 0;
  6551. * });
  6552. *
  6553. * console.log(array);
  6554. * // => [1, 3]
  6555. *
  6556. * console.log(evens);
  6557. * // => [2, 4]
  6558. */
  6559. function remove(array, predicate) {
  6560. var result = [];
  6561. if (!(array && array.length)) {
  6562. return result;
  6563. }
  6564. var index = -1,
  6565. indexes = [],
  6566. length = array.length;
  6567. predicate = getIteratee(predicate, 3);
  6568. while (++index < length) {
  6569. var value = array[index];
  6570. if (predicate(value, index, array)) {
  6571. result.push(value);
  6572. indexes.push(index);
  6573. }
  6574. }
  6575. basePullAt(array, indexes);
  6576. return result;
  6577. }
  6578. /**
  6579. * Reverses `array` so that the first element becomes the last, the second
  6580. * element becomes the second to last, and so on.
  6581. *
  6582. * **Note:** This method mutates `array` and is based on
  6583. * [`Array#reverse`](https://mdn.io/Array/reverse).
  6584. *
  6585. * @static
  6586. * @memberOf _
  6587. * @since 4.0.0
  6588. * @category Array
  6589. * @param {Array} array The array to modify.
  6590. * @returns {Array} Returns `array`.
  6591. * @example
  6592. *
  6593. * var array = [1, 2, 3];
  6594. *
  6595. * _.reverse(array);
  6596. * // => [3, 2, 1]
  6597. *
  6598. * console.log(array);
  6599. * // => [3, 2, 1]
  6600. */
  6601. function reverse(array) {
  6602. return array ? nativeReverse.call(array) : array;
  6603. }
  6604. /**
  6605. * Creates a slice of `array` from `start` up to, but not including, `end`.
  6606. *
  6607. * **Note:** This method is used instead of
  6608. * [`Array#slice`](https://mdn.io/Array/slice) to ensure dense arrays are
  6609. * returned.
  6610. *
  6611. * @static
  6612. * @memberOf _
  6613. * @since 3.0.0
  6614. * @category Array
  6615. * @param {Array} array The array to slice.
  6616. * @param {number} [start=0] The start position.
  6617. * @param {number} [end=array.length] The end position.
  6618. * @returns {Array} Returns the slice of `array`.
  6619. */
  6620. function slice(array, start, end) {
  6621. var length = array ? array.length : 0;
  6622. if (!length) {
  6623. return [];
  6624. }
  6625. if (end && typeof end != 'number' && isIterateeCall(array, start, end)) {
  6626. start = 0;
  6627. end = length;
  6628. }
  6629. else {
  6630. start = start == null ? 0 : toInteger(start);
  6631. end = end === undefined ? length : toInteger(end);
  6632. }
  6633. return baseSlice(array, start, end);
  6634. }
  6635. /**
  6636. * Uses a binary search to determine the lowest index at which `value`
  6637. * should be inserted into `array` in order to maintain its sort order.
  6638. *
  6639. * @static
  6640. * @memberOf _
  6641. * @since 0.1.0
  6642. * @category Array
  6643. * @param {Array} array The sorted array to inspect.
  6644. * @param {*} value The value to evaluate.
  6645. * @returns {number} Returns the index at which `value` should be inserted
  6646. * into `array`.
  6647. * @example
  6648. *
  6649. * _.sortedIndex([30, 50], 40);
  6650. * // => 1
  6651. */
  6652. function sortedIndex(array, value) {
  6653. return baseSortedIndex(array, value);
  6654. }
  6655. /**
  6656. * This method is like `_.sortedIndex` except that it accepts `iteratee`
  6657. * which is invoked for `value` and each element of `array` to compute their
  6658. * sort ranking. The iteratee is invoked with one argument: (value).
  6659. *
  6660. * @static
  6661. * @memberOf _
  6662. * @since 4.0.0
  6663. * @category Array
  6664. * @param {Array} array The sorted array to inspect.
  6665. * @param {*} value The value to evaluate.
  6666. * @param {Array|Function|Object|string} [iteratee=_.identity]
  6667. * The iteratee invoked per element.
  6668. * @returns {number} Returns the index at which `value` should be inserted
  6669. * into `array`.
  6670. * @example
  6671. *
  6672. * var objects = [{ 'x': 4 }, { 'x': 5 }];
  6673. *
  6674. * _.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
  6675. * // => 0
  6676. *
  6677. * // The `_.property` iteratee shorthand.
  6678. * _.sortedIndexBy(objects, { 'x': 4 }, 'x');
  6679. * // => 0
  6680. */
  6681. function sortedIndexBy(array, value, iteratee) {
  6682. return baseSortedIndexBy(array, value, getIteratee(iteratee));
  6683. }
  6684. /**
  6685. * This method is like `_.indexOf` except that it performs a binary
  6686. * search on a sorted `array`.
  6687. *
  6688. * @static
  6689. * @memberOf _
  6690. * @since 4.0.0
  6691. * @category Array
  6692. * @param {Array} array The array to search.
  6693. * @param {*} value The value to search for.
  6694. * @returns {number} Returns the index of the matched value, else `-1`.
  6695. * @example
  6696. *
  6697. * _.sortedIndexOf([4, 5, 5, 5, 6], 5);
  6698. * // => 1
  6699. */
  6700. function sortedIndexOf(array, value) {
  6701. var length = array ? array.length : 0;
  6702. if (length) {
  6703. var index = baseSortedIndex(array, value);
  6704. if (index < length && eq(array[index], value)) {
  6705. return index;
  6706. }
  6707. }
  6708. return -1;
  6709. }
  6710. /**
  6711. * This method is like `_.sortedIndex` except that it returns the highest
  6712. * index at which `value` should be inserted into `array` in order to
  6713. * maintain its sort order.
  6714. *
  6715. * @static
  6716. * @memberOf _
  6717. * @since 3.0.0
  6718. * @category Array
  6719. * @param {Array} array The sorted array to inspect.
  6720. * @param {*} value The value to evaluate.
  6721. * @returns {number} Returns the index at which `value` should be inserted
  6722. * into `array`.
  6723. * @example
  6724. *
  6725. * _.sortedLastIndex([4, 5, 5, 5, 6], 5);
  6726. * // => 4
  6727. */
  6728. function sortedLastIndex(array, value) {
  6729. return baseSortedIndex(array, value, true);
  6730. }
  6731. /**
  6732. * This method is like `_.sortedLastIndex` except that it accepts `iteratee`
  6733. * which is invoked for `value` and each element of `array` to compute their
  6734. * sort ranking. The iteratee is invoked with one argument: (value).
  6735. *
  6736. * @static
  6737. * @memberOf _
  6738. * @since 4.0.0
  6739. * @category Array
  6740. * @param {Array} array The sorted array to inspect.
  6741. * @param {*} value The value to evaluate.
  6742. * @param {Array|Function|Object|string} [iteratee=_.identity]
  6743. * The iteratee invoked per element.
  6744. * @returns {number} Returns the index at which `value` should be inserted
  6745. * into `array`.
  6746. * @example
  6747. *
  6748. * var objects = [{ 'x': 4 }, { 'x': 5 }];
  6749. *
  6750. * _.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
  6751. * // => 1
  6752. *
  6753. * // The `_.property` iteratee shorthand.
  6754. * _.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
  6755. * // => 1
  6756. */
  6757. function sortedLastIndexBy(array, value, iteratee) {
  6758. return baseSortedIndexBy(array, value, getIteratee(iteratee), true);
  6759. }
  6760. /**
  6761. * This method is like `_.lastIndexOf` except that it performs a binary
  6762. * search on a sorted `array`.
  6763. *
  6764. * @static
  6765. * @memberOf _
  6766. * @since 4.0.0
  6767. * @category Array
  6768. * @param {Array} array The array to search.
  6769. * @param {*} value The value to search for.
  6770. * @returns {number} Returns the index of the matched value, else `-1`.
  6771. * @example
  6772. *
  6773. * _.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
  6774. * // => 3
  6775. */
  6776. function sortedLastIndexOf(array, value) {
  6777. var length = array ? array.length : 0;
  6778. if (length) {
  6779. var index = baseSortedIndex(array, value, true) - 1;
  6780. if (eq(array[index], value)) {
  6781. return index;
  6782. }
  6783. }
  6784. return -1;
  6785. }
  6786. /**
  6787. * This method is like `_.uniq` except that it's designed and optimized
  6788. * for sorted arrays.
  6789. *
  6790. * @static
  6791. * @memberOf _
  6792. * @since 4.0.0
  6793. * @category Array
  6794. * @param {Array} array The array to inspect.
  6795. * @returns {Array} Returns the new duplicate free array.
  6796. * @example
  6797. *
  6798. * _.sortedUniq([1, 1, 2]);
  6799. * // => [1, 2]
  6800. */
  6801. function sortedUniq(array) {
  6802. return (array && array.length)
  6803. ? baseSortedUniq(array)
  6804. : [];
  6805. }
  6806. /**
  6807. * This method is like `_.uniqBy` except that it's designed and optimized
  6808. * for sorted arrays.
  6809. *
  6810. * @static
  6811. * @memberOf _
  6812. * @since 4.0.0
  6813. * @category Array
  6814. * @param {Array} array The array to inspect.
  6815. * @param {Function} [iteratee] The iteratee invoked per element.
  6816. * @returns {Array} Returns the new duplicate free array.
  6817. * @example
  6818. *
  6819. * _.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
  6820. * // => [1.1, 2.3]
  6821. */
  6822. function sortedUniqBy(array, iteratee) {
  6823. return (array && array.length)
  6824. ? baseSortedUniq(array, getIteratee(iteratee))
  6825. : [];
  6826. }
  6827. /**
  6828. * Gets all but the first element of `array`.
  6829. *
  6830. * @static
  6831. * @memberOf _
  6832. * @since 4.0.0
  6833. * @category Array
  6834. * @param {Array} array The array to query.
  6835. * @returns {Array} Returns the slice of `array`.
  6836. * @example
  6837. *
  6838. * _.tail([1, 2, 3]);
  6839. * // => [2, 3]
  6840. */
  6841. function tail(array) {
  6842. return drop(array, 1);
  6843. }
  6844. /**
  6845. * Creates a slice of `array` with `n` elements taken from the beginning.
  6846. *
  6847. * @static
  6848. * @memberOf _
  6849. * @since 0.1.0
  6850. * @category Array
  6851. * @param {Array} array The array to query.
  6852. * @param {number} [n=1] The number of elements to take.
  6853. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  6854. * @returns {Array} Returns the slice of `array`.
  6855. * @example
  6856. *
  6857. * _.take([1, 2, 3]);
  6858. * // => [1]
  6859. *
  6860. * _.take([1, 2, 3], 2);
  6861. * // => [1, 2]
  6862. *
  6863. * _.take([1, 2, 3], 5);
  6864. * // => [1, 2, 3]
  6865. *
  6866. * _.take([1, 2, 3], 0);
  6867. * // => []
  6868. */
  6869. function take(array, n, guard) {
  6870. if (!(array && array.length)) {
  6871. return [];
  6872. }
  6873. n = (guard || n === undefined) ? 1 : toInteger(n);
  6874. return baseSlice(array, 0, n < 0 ? 0 : n);
  6875. }
  6876. /**
  6877. * Creates a slice of `array` with `n` elements taken from the end.
  6878. *
  6879. * @static
  6880. * @memberOf _
  6881. * @since 3.0.0
  6882. * @category Array
  6883. * @param {Array} array The array to query.
  6884. * @param {number} [n=1] The number of elements to take.
  6885. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  6886. * @returns {Array} Returns the slice of `array`.
  6887. * @example
  6888. *
  6889. * _.takeRight([1, 2, 3]);
  6890. * // => [3]
  6891. *
  6892. * _.takeRight([1, 2, 3], 2);
  6893. * // => [2, 3]
  6894. *
  6895. * _.takeRight([1, 2, 3], 5);
  6896. * // => [1, 2, 3]
  6897. *
  6898. * _.takeRight([1, 2, 3], 0);
  6899. * // => []
  6900. */
  6901. function takeRight(array, n, guard) {
  6902. var length = array ? array.length : 0;
  6903. if (!length) {
  6904. return [];
  6905. }
  6906. n = (guard || n === undefined) ? 1 : toInteger(n);
  6907. n = length - n;
  6908. return baseSlice(array, n < 0 ? 0 : n, length);
  6909. }
  6910. /**
  6911. * Creates a slice of `array` with elements taken from the end. Elements are
  6912. * taken until `predicate` returns falsey. The predicate is invoked with
  6913. * three arguments: (value, index, array).
  6914. *
  6915. * @static
  6916. * @memberOf _
  6917. * @since 3.0.0
  6918. * @category Array
  6919. * @param {Array} array The array to query.
  6920. * @param {Array|Function|Object|string} [predicate=_.identity]
  6921. * The function invoked per iteration.
  6922. * @returns {Array} Returns the slice of `array`.
  6923. * @example
  6924. *
  6925. * var users = [
  6926. * { 'user': 'barney', 'active': true },
  6927. * { 'user': 'fred', 'active': false },
  6928. * { 'user': 'pebbles', 'active': false }
  6929. * ];
  6930. *
  6931. * _.takeRightWhile(users, function(o) { return !o.active; });
  6932. * // => objects for ['fred', 'pebbles']
  6933. *
  6934. * // The `_.matches` iteratee shorthand.
  6935. * _.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
  6936. * // => objects for ['pebbles']
  6937. *
  6938. * // The `_.matchesProperty` iteratee shorthand.
  6939. * _.takeRightWhile(users, ['active', false]);
  6940. * // => objects for ['fred', 'pebbles']
  6941. *
  6942. * // The `_.property` iteratee shorthand.
  6943. * _.takeRightWhile(users, 'active');
  6944. * // => []
  6945. */
  6946. function takeRightWhile(array, predicate) {
  6947. return (array && array.length)
  6948. ? baseWhile(array, getIteratee(predicate, 3), false, true)
  6949. : [];
  6950. }
  6951. /**
  6952. * Creates a slice of `array` with elements taken from the beginning. Elements
  6953. * are taken until `predicate` returns falsey. The predicate is invoked with
  6954. * three arguments: (value, index, array).
  6955. *
  6956. * @static
  6957. * @memberOf _
  6958. * @since 3.0.0
  6959. * @category Array
  6960. * @param {Array} array The array to query.
  6961. * @param {Array|Function|Object|string} [predicate=_.identity]
  6962. * The function invoked per iteration.
  6963. * @returns {Array} Returns the slice of `array`.
  6964. * @example
  6965. *
  6966. * var users = [
  6967. * { 'user': 'barney', 'active': false },
  6968. * { 'user': 'fred', 'active': false},
  6969. * { 'user': 'pebbles', 'active': true }
  6970. * ];
  6971. *
  6972. * _.takeWhile(users, function(o) { return !o.active; });
  6973. * // => objects for ['barney', 'fred']
  6974. *
  6975. * // The `_.matches` iteratee shorthand.
  6976. * _.takeWhile(users, { 'user': 'barney', 'active': false });
  6977. * // => objects for ['barney']
  6978. *
  6979. * // The `_.matchesProperty` iteratee shorthand.
  6980. * _.takeWhile(users, ['active', false]);
  6981. * // => objects for ['barney', 'fred']
  6982. *
  6983. * // The `_.property` iteratee shorthand.
  6984. * _.takeWhile(users, 'active');
  6985. * // => []
  6986. */
  6987. function takeWhile(array, predicate) {
  6988. return (array && array.length)
  6989. ? baseWhile(array, getIteratee(predicate, 3))
  6990. : [];
  6991. }
  6992. /**
  6993. * Creates an array of unique values, in order, from all given arrays using
  6994. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  6995. * for equality comparisons.
  6996. *
  6997. * @static
  6998. * @memberOf _
  6999. * @since 0.1.0
  7000. * @category Array
  7001. * @param {...Array} [arrays] The arrays to inspect.
  7002. * @returns {Array} Returns the new array of combined values.
  7003. * @example
  7004. *
  7005. * _.union([2], [1, 2]);
  7006. * // => [2, 1]
  7007. */
  7008. var union = rest(function(arrays) {
  7009. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true));
  7010. });
  7011. /**
  7012. * This method is like `_.union` except that it accepts `iteratee` which is
  7013. * invoked for each element of each `arrays` to generate the criterion by
  7014. * which uniqueness is computed. The iteratee is invoked with one argument:
  7015. * (value).
  7016. *
  7017. * @static
  7018. * @memberOf _
  7019. * @since 4.0.0
  7020. * @category Array
  7021. * @param {...Array} [arrays] The arrays to inspect.
  7022. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7023. * The iteratee invoked per element.
  7024. * @returns {Array} Returns the new array of combined values.
  7025. * @example
  7026. *
  7027. * _.unionBy([2.1], [1.2, 2.3], Math.floor);
  7028. * // => [2.1, 1.2]
  7029. *
  7030. * // The `_.property` iteratee shorthand.
  7031. * _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  7032. * // => [{ 'x': 1 }, { 'x': 2 }]
  7033. */
  7034. var unionBy = rest(function(arrays) {
  7035. var iteratee = last(arrays);
  7036. if (isArrayLikeObject(iteratee)) {
  7037. iteratee = undefined;
  7038. }
  7039. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), getIteratee(iteratee));
  7040. });
  7041. /**
  7042. * This method is like `_.union` except that it accepts `comparator` which
  7043. * is invoked to compare elements of `arrays`. The comparator is invoked
  7044. * with two arguments: (arrVal, othVal).
  7045. *
  7046. * @static
  7047. * @memberOf _
  7048. * @since 4.0.0
  7049. * @category Array
  7050. * @param {...Array} [arrays] The arrays to inspect.
  7051. * @param {Function} [comparator] The comparator invoked per element.
  7052. * @returns {Array} Returns the new array of combined values.
  7053. * @example
  7054. *
  7055. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  7056. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  7057. *
  7058. * _.unionWith(objects, others, _.isEqual);
  7059. * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  7060. */
  7061. var unionWith = rest(function(arrays) {
  7062. var comparator = last(arrays);
  7063. if (isArrayLikeObject(comparator)) {
  7064. comparator = undefined;
  7065. }
  7066. return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator);
  7067. });
  7068. /**
  7069. * Creates a duplicate-free version of an array, using
  7070. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  7071. * for equality comparisons, in which only the first occurrence of each
  7072. * element is kept.
  7073. *
  7074. * @static
  7075. * @memberOf _
  7076. * @since 0.1.0
  7077. * @category Array
  7078. * @param {Array} array The array to inspect.
  7079. * @returns {Array} Returns the new duplicate free array.
  7080. * @example
  7081. *
  7082. * _.uniq([2, 1, 2]);
  7083. * // => [2, 1]
  7084. */
  7085. function uniq(array) {
  7086. return (array && array.length)
  7087. ? baseUniq(array)
  7088. : [];
  7089. }
  7090. /**
  7091. * This method is like `_.uniq` except that it accepts `iteratee` which is
  7092. * invoked for each element in `array` to generate the criterion by which
  7093. * uniqueness is computed. The iteratee is invoked with one argument: (value).
  7094. *
  7095. * @static
  7096. * @memberOf _
  7097. * @since 4.0.0
  7098. * @category Array
  7099. * @param {Array} array The array to inspect.
  7100. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7101. * The iteratee invoked per element.
  7102. * @returns {Array} Returns the new duplicate free array.
  7103. * @example
  7104. *
  7105. * _.uniqBy([2.1, 1.2, 2.3], Math.floor);
  7106. * // => [2.1, 1.2]
  7107. *
  7108. * // The `_.property` iteratee shorthand.
  7109. * _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
  7110. * // => [{ 'x': 1 }, { 'x': 2 }]
  7111. */
  7112. function uniqBy(array, iteratee) {
  7113. return (array && array.length)
  7114. ? baseUniq(array, getIteratee(iteratee))
  7115. : [];
  7116. }
  7117. /**
  7118. * This method is like `_.uniq` except that it accepts `comparator` which
  7119. * is invoked to compare elements of `array`. The comparator is invoked with
  7120. * two arguments: (arrVal, othVal).
  7121. *
  7122. * @static
  7123. * @memberOf _
  7124. * @since 4.0.0
  7125. * @category Array
  7126. * @param {Array} array The array to inspect.
  7127. * @param {Function} [comparator] The comparator invoked per element.
  7128. * @returns {Array} Returns the new duplicate free array.
  7129. * @example
  7130. *
  7131. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
  7132. *
  7133. * _.uniqWith(objects, _.isEqual);
  7134. * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
  7135. */
  7136. function uniqWith(array, comparator) {
  7137. return (array && array.length)
  7138. ? baseUniq(array, undefined, comparator)
  7139. : [];
  7140. }
  7141. /**
  7142. * This method is like `_.zip` except that it accepts an array of grouped
  7143. * elements and creates an array regrouping the elements to their pre-zip
  7144. * configuration.
  7145. *
  7146. * @static
  7147. * @memberOf _
  7148. * @since 1.2.0
  7149. * @category Array
  7150. * @param {Array} array The array of grouped elements to process.
  7151. * @returns {Array} Returns the new array of regrouped elements.
  7152. * @example
  7153. *
  7154. * var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
  7155. * // => [['fred', 30, true], ['barney', 40, false]]
  7156. *
  7157. * _.unzip(zipped);
  7158. * // => [['fred', 'barney'], [30, 40], [true, false]]
  7159. */
  7160. function unzip(array) {
  7161. if (!(array && array.length)) {
  7162. return [];
  7163. }
  7164. var length = 0;
  7165. array = arrayFilter(array, function(group) {
  7166. if (isArrayLikeObject(group)) {
  7167. length = nativeMax(group.length, length);
  7168. return true;
  7169. }
  7170. });
  7171. return baseTimes(length, function(index) {
  7172. return arrayMap(array, baseProperty(index));
  7173. });
  7174. }
  7175. /**
  7176. * This method is like `_.unzip` except that it accepts `iteratee` to specify
  7177. * how regrouped values should be combined. The iteratee is invoked with the
  7178. * elements of each group: (...group).
  7179. *
  7180. * @static
  7181. * @memberOf _
  7182. * @since 3.8.0
  7183. * @category Array
  7184. * @param {Array} array The array of grouped elements to process.
  7185. * @param {Function} [iteratee=_.identity] The function to combine
  7186. * regrouped values.
  7187. * @returns {Array} Returns the new array of regrouped elements.
  7188. * @example
  7189. *
  7190. * var zipped = _.zip([1, 2], [10, 20], [100, 200]);
  7191. * // => [[1, 10, 100], [2, 20, 200]]
  7192. *
  7193. * _.unzipWith(zipped, _.add);
  7194. * // => [3, 30, 300]
  7195. */
  7196. function unzipWith(array, iteratee) {
  7197. if (!(array && array.length)) {
  7198. return [];
  7199. }
  7200. var result = unzip(array);
  7201. if (iteratee == null) {
  7202. return result;
  7203. }
  7204. return arrayMap(result, function(group) {
  7205. return apply(iteratee, undefined, group);
  7206. });
  7207. }
  7208. /**
  7209. * Creates an array excluding all given values using
  7210. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  7211. * for equality comparisons.
  7212. *
  7213. * @static
  7214. * @memberOf _
  7215. * @since 0.1.0
  7216. * @category Array
  7217. * @param {Array} array The array to inspect.
  7218. * @param {...*} [values] The values to exclude.
  7219. * @returns {Array} Returns the new array of filtered values.
  7220. * @see _.difference, _.xor
  7221. * @example
  7222. *
  7223. * _.without([2, 1, 2, 3], 1, 2);
  7224. * // => [3]
  7225. */
  7226. var without = rest(function(array, values) {
  7227. return isArrayLikeObject(array)
  7228. ? baseDifference(array, values)
  7229. : [];
  7230. });
  7231. /**
  7232. * Creates an array of unique values that is the
  7233. * [symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference)
  7234. * of the given arrays. The order of result values is determined by the order
  7235. * they occur in the arrays.
  7236. *
  7237. * @static
  7238. * @memberOf _
  7239. * @since 2.4.0
  7240. * @category Array
  7241. * @param {...Array} [arrays] The arrays to inspect.
  7242. * @returns {Array} Returns the new array of filtered values.
  7243. * @see _.difference, _.without
  7244. * @example
  7245. *
  7246. * _.xor([2, 1], [2, 3]);
  7247. * // => [1, 3]
  7248. */
  7249. var xor = rest(function(arrays) {
  7250. return baseXor(arrayFilter(arrays, isArrayLikeObject));
  7251. });
  7252. /**
  7253. * This method is like `_.xor` except that it accepts `iteratee` which is
  7254. * invoked for each element of each `arrays` to generate the criterion by
  7255. * which by which they're compared. The iteratee is invoked with one argument:
  7256. * (value).
  7257. *
  7258. * @static
  7259. * @memberOf _
  7260. * @since 4.0.0
  7261. * @category Array
  7262. * @param {...Array} [arrays] The arrays to inspect.
  7263. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7264. * The iteratee invoked per element.
  7265. * @returns {Array} Returns the new array of filtered values.
  7266. * @example
  7267. *
  7268. * _.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
  7269. * // => [1.2, 3.4]
  7270. *
  7271. * // The `_.property` iteratee shorthand.
  7272. * _.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
  7273. * // => [{ 'x': 2 }]
  7274. */
  7275. var xorBy = rest(function(arrays) {
  7276. var iteratee = last(arrays);
  7277. if (isArrayLikeObject(iteratee)) {
  7278. iteratee = undefined;
  7279. }
  7280. return baseXor(arrayFilter(arrays, isArrayLikeObject), getIteratee(iteratee));
  7281. });
  7282. /**
  7283. * This method is like `_.xor` except that it accepts `comparator` which is
  7284. * invoked to compare elements of `arrays`. The comparator is invoked with
  7285. * two arguments: (arrVal, othVal).
  7286. *
  7287. * @static
  7288. * @memberOf _
  7289. * @since 4.0.0
  7290. * @category Array
  7291. * @param {...Array} [arrays] The arrays to inspect.
  7292. * @param {Function} [comparator] The comparator invoked per element.
  7293. * @returns {Array} Returns the new array of filtered values.
  7294. * @example
  7295. *
  7296. * var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
  7297. * var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
  7298. *
  7299. * _.xorWith(objects, others, _.isEqual);
  7300. * // => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  7301. */
  7302. var xorWith = rest(function(arrays) {
  7303. var comparator = last(arrays);
  7304. if (isArrayLikeObject(comparator)) {
  7305. comparator = undefined;
  7306. }
  7307. return baseXor(arrayFilter(arrays, isArrayLikeObject), undefined, comparator);
  7308. });
  7309. /**
  7310. * Creates an array of grouped elements, the first of which contains the
  7311. * first elements of the given arrays, the second of which contains the
  7312. * second elements of the given arrays, and so on.
  7313. *
  7314. * @static
  7315. * @memberOf _
  7316. * @since 0.1.0
  7317. * @category Array
  7318. * @param {...Array} [arrays] The arrays to process.
  7319. * @returns {Array} Returns the new array of grouped elements.
  7320. * @example
  7321. *
  7322. * _.zip(['fred', 'barney'], [30, 40], [true, false]);
  7323. * // => [['fred', 30, true], ['barney', 40, false]]
  7324. */
  7325. var zip = rest(unzip);
  7326. /**
  7327. * This method is like `_.fromPairs` except that it accepts two arrays,
  7328. * one of property identifiers and one of corresponding values.
  7329. *
  7330. * @static
  7331. * @memberOf _
  7332. * @since 0.4.0
  7333. * @category Array
  7334. * @param {Array} [props=[]] The property identifiers.
  7335. * @param {Array} [values=[]] The property values.
  7336. * @returns {Object} Returns the new object.
  7337. * @example
  7338. *
  7339. * _.zipObject(['a', 'b'], [1, 2]);
  7340. * // => { 'a': 1, 'b': 2 }
  7341. */
  7342. function zipObject(props, values) {
  7343. return baseZipObject(props || [], values || [], assignValue);
  7344. }
  7345. /**
  7346. * This method is like `_.zipObject` except that it supports property paths.
  7347. *
  7348. * @static
  7349. * @memberOf _
  7350. * @since 4.1.0
  7351. * @category Array
  7352. * @param {Array} [props=[]] The property identifiers.
  7353. * @param {Array} [values=[]] The property values.
  7354. * @returns {Object} Returns the new object.
  7355. * @example
  7356. *
  7357. * _.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
  7358. * // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
  7359. */
  7360. function zipObjectDeep(props, values) {
  7361. return baseZipObject(props || [], values || [], baseSet);
  7362. }
  7363. /**
  7364. * This method is like `_.zip` except that it accepts `iteratee` to specify
  7365. * how grouped values should be combined. The iteratee is invoked with the
  7366. * elements of each group: (...group).
  7367. *
  7368. * @static
  7369. * @memberOf _
  7370. * @since 3.8.0
  7371. * @category Array
  7372. * @param {...Array} [arrays] The arrays to process.
  7373. * @param {Function} [iteratee=_.identity] The function to combine grouped values.
  7374. * @returns {Array} Returns the new array of grouped elements.
  7375. * @example
  7376. *
  7377. * _.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
  7378. * return a + b + c;
  7379. * });
  7380. * // => [111, 222]
  7381. */
  7382. var zipWith = rest(function(arrays) {
  7383. var length = arrays.length,
  7384. iteratee = length > 1 ? arrays[length - 1] : undefined;
  7385. iteratee = typeof iteratee == 'function' ? (arrays.pop(), iteratee) : undefined;
  7386. return unzipWith(arrays, iteratee);
  7387. });
  7388. /*------------------------------------------------------------------------*/
  7389. /**
  7390. * Creates a `lodash` wrapper instance that wraps `value` with explicit method
  7391. * chain sequences enabled. The result of such sequences must be unwrapped
  7392. * with `_#value`.
  7393. *
  7394. * @static
  7395. * @memberOf _
  7396. * @since 1.3.0
  7397. * @category Seq
  7398. * @param {*} value The value to wrap.
  7399. * @returns {Object} Returns the new `lodash` wrapper instance.
  7400. * @example
  7401. *
  7402. * var users = [
  7403. * { 'user': 'barney', 'age': 36 },
  7404. * { 'user': 'fred', 'age': 40 },
  7405. * { 'user': 'pebbles', 'age': 1 }
  7406. * ];
  7407. *
  7408. * var youngest = _
  7409. * .chain(users)
  7410. * .sortBy('age')
  7411. * .map(function(o) {
  7412. * return o.user + ' is ' + o.age;
  7413. * })
  7414. * .head()
  7415. * .value();
  7416. * // => 'pebbles is 1'
  7417. */
  7418. function chain(value) {
  7419. var result = lodash(value);
  7420. result.__chain__ = true;
  7421. return result;
  7422. }
  7423. /**
  7424. * This method invokes `interceptor` and returns `value`. The interceptor
  7425. * is invoked with one argument; (value). The purpose of this method is to
  7426. * "tap into" a method chain sequence in order to modify intermediate results.
  7427. *
  7428. * @static
  7429. * @memberOf _
  7430. * @since 0.1.0
  7431. * @category Seq
  7432. * @param {*} value The value to provide to `interceptor`.
  7433. * @param {Function} interceptor The function to invoke.
  7434. * @returns {*} Returns `value`.
  7435. * @example
  7436. *
  7437. * _([1, 2, 3])
  7438. * .tap(function(array) {
  7439. * // Mutate input array.
  7440. * array.pop();
  7441. * })
  7442. * .reverse()
  7443. * .value();
  7444. * // => [2, 1]
  7445. */
  7446. function tap(value, interceptor) {
  7447. interceptor(value);
  7448. return value;
  7449. }
  7450. /**
  7451. * This method is like `_.tap` except that it returns the result of `interceptor`.
  7452. * The purpose of this method is to "pass thru" values replacing intermediate
  7453. * results in a method chain sequence.
  7454. *
  7455. * @static
  7456. * @memberOf _
  7457. * @since 3.0.0
  7458. * @category Seq
  7459. * @param {*} value The value to provide to `interceptor`.
  7460. * @param {Function} interceptor The function to invoke.
  7461. * @returns {*} Returns the result of `interceptor`.
  7462. * @example
  7463. *
  7464. * _(' abc ')
  7465. * .chain()
  7466. * .trim()
  7467. * .thru(function(value) {
  7468. * return [value];
  7469. * })
  7470. * .value();
  7471. * // => ['abc']
  7472. */
  7473. function thru(value, interceptor) {
  7474. return interceptor(value);
  7475. }
  7476. /**
  7477. * This method is the wrapper version of `_.at`.
  7478. *
  7479. * @name at
  7480. * @memberOf _
  7481. * @since 1.0.0
  7482. * @category Seq
  7483. * @param {...(string|string[])} [paths] The property paths of elements to pick.
  7484. * @returns {Object} Returns the new `lodash` wrapper instance.
  7485. * @example
  7486. *
  7487. * var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
  7488. *
  7489. * _(object).at(['a[0].b.c', 'a[1]']).value();
  7490. * // => [3, 4]
  7491. */
  7492. var wrapperAt = rest(function(paths) {
  7493. paths = baseFlatten(paths, 1);
  7494. var length = paths.length,
  7495. start = length ? paths[0] : 0,
  7496. value = this.__wrapped__,
  7497. interceptor = function(object) { return baseAt(object, paths); };
  7498. if (length > 1 || this.__actions__.length ||
  7499. !(value instanceof LazyWrapper) || !isIndex(start)) {
  7500. return this.thru(interceptor);
  7501. }
  7502. value = value.slice(start, +start + (length ? 1 : 0));
  7503. value.__actions__.push({
  7504. 'func': thru,
  7505. 'args': [interceptor],
  7506. 'thisArg': undefined
  7507. });
  7508. return new LodashWrapper(value, this.__chain__).thru(function(array) {
  7509. if (length && !array.length) {
  7510. array.push(undefined);
  7511. }
  7512. return array;
  7513. });
  7514. });
  7515. /**
  7516. * Creates a `lodash` wrapper instance with explicit method chain sequences enabled.
  7517. *
  7518. * @name chain
  7519. * @memberOf _
  7520. * @since 0.1.0
  7521. * @category Seq
  7522. * @returns {Object} Returns the new `lodash` wrapper instance.
  7523. * @example
  7524. *
  7525. * var users = [
  7526. * { 'user': 'barney', 'age': 36 },
  7527. * { 'user': 'fred', 'age': 40 }
  7528. * ];
  7529. *
  7530. * // A sequence without explicit chaining.
  7531. * _(users).head();
  7532. * // => { 'user': 'barney', 'age': 36 }
  7533. *
  7534. * // A sequence with explicit chaining.
  7535. * _(users)
  7536. * .chain()
  7537. * .head()
  7538. * .pick('user')
  7539. * .value();
  7540. * // => { 'user': 'barney' }
  7541. */
  7542. function wrapperChain() {
  7543. return chain(this);
  7544. }
  7545. /**
  7546. * Executes the chain sequence and returns the wrapped result.
  7547. *
  7548. * @name commit
  7549. * @memberOf _
  7550. * @since 3.2.0
  7551. * @category Seq
  7552. * @returns {Object} Returns the new `lodash` wrapper instance.
  7553. * @example
  7554. *
  7555. * var array = [1, 2];
  7556. * var wrapped = _(array).push(3);
  7557. *
  7558. * console.log(array);
  7559. * // => [1, 2]
  7560. *
  7561. * wrapped = wrapped.commit();
  7562. * console.log(array);
  7563. * // => [1, 2, 3]
  7564. *
  7565. * wrapped.last();
  7566. * // => 3
  7567. *
  7568. * console.log(array);
  7569. * // => [1, 2, 3]
  7570. */
  7571. function wrapperCommit() {
  7572. return new LodashWrapper(this.value(), this.__chain__);
  7573. }
  7574. /**
  7575. * Gets the next value on a wrapped object following the
  7576. * [iterator protocol](https://mdn.io/iteration_protocols#iterator).
  7577. *
  7578. * @name next
  7579. * @memberOf _
  7580. * @since 4.0.0
  7581. * @category Seq
  7582. * @returns {Object} Returns the next iterator value.
  7583. * @example
  7584. *
  7585. * var wrapped = _([1, 2]);
  7586. *
  7587. * wrapped.next();
  7588. * // => { 'done': false, 'value': 1 }
  7589. *
  7590. * wrapped.next();
  7591. * // => { 'done': false, 'value': 2 }
  7592. *
  7593. * wrapped.next();
  7594. * // => { 'done': true, 'value': undefined }
  7595. */
  7596. function wrapperNext() {
  7597. if (this.__values__ === undefined) {
  7598. this.__values__ = toArray(this.value());
  7599. }
  7600. var done = this.__index__ >= this.__values__.length,
  7601. value = done ? undefined : this.__values__[this.__index__++];
  7602. return { 'done': done, 'value': value };
  7603. }
  7604. /**
  7605. * Enables the wrapper to be iterable.
  7606. *
  7607. * @name Symbol.iterator
  7608. * @memberOf _
  7609. * @since 4.0.0
  7610. * @category Seq
  7611. * @returns {Object} Returns the wrapper object.
  7612. * @example
  7613. *
  7614. * var wrapped = _([1, 2]);
  7615. *
  7616. * wrapped[Symbol.iterator]() === wrapped;
  7617. * // => true
  7618. *
  7619. * Array.from(wrapped);
  7620. * // => [1, 2]
  7621. */
  7622. function wrapperToIterator() {
  7623. return this;
  7624. }
  7625. /**
  7626. * Creates a clone of the chain sequence planting `value` as the wrapped value.
  7627. *
  7628. * @name plant
  7629. * @memberOf _
  7630. * @since 3.2.0
  7631. * @category Seq
  7632. * @param {*} value The value to plant.
  7633. * @returns {Object} Returns the new `lodash` wrapper instance.
  7634. * @example
  7635. *
  7636. * function square(n) {
  7637. * return n * n;
  7638. * }
  7639. *
  7640. * var wrapped = _([1, 2]).map(square);
  7641. * var other = wrapped.plant([3, 4]);
  7642. *
  7643. * other.value();
  7644. * // => [9, 16]
  7645. *
  7646. * wrapped.value();
  7647. * // => [1, 4]
  7648. */
  7649. function wrapperPlant(value) {
  7650. var result,
  7651. parent = this;
  7652. while (parent instanceof baseLodash) {
  7653. var clone = wrapperClone(parent);
  7654. clone.__index__ = 0;
  7655. clone.__values__ = undefined;
  7656. if (result) {
  7657. previous.__wrapped__ = clone;
  7658. } else {
  7659. result = clone;
  7660. }
  7661. var previous = clone;
  7662. parent = parent.__wrapped__;
  7663. }
  7664. previous.__wrapped__ = value;
  7665. return result;
  7666. }
  7667. /**
  7668. * This method is the wrapper version of `_.reverse`.
  7669. *
  7670. * **Note:** This method mutates the wrapped array.
  7671. *
  7672. * @name reverse
  7673. * @memberOf _
  7674. * @since 0.1.0
  7675. * @category Seq
  7676. * @returns {Object} Returns the new `lodash` wrapper instance.
  7677. * @example
  7678. *
  7679. * var array = [1, 2, 3];
  7680. *
  7681. * _(array).reverse().value()
  7682. * // => [3, 2, 1]
  7683. *
  7684. * console.log(array);
  7685. * // => [3, 2, 1]
  7686. */
  7687. function wrapperReverse() {
  7688. var value = this.__wrapped__;
  7689. if (value instanceof LazyWrapper) {
  7690. var wrapped = value;
  7691. if (this.__actions__.length) {
  7692. wrapped = new LazyWrapper(this);
  7693. }
  7694. wrapped = wrapped.reverse();
  7695. wrapped.__actions__.push({
  7696. 'func': thru,
  7697. 'args': [reverse],
  7698. 'thisArg': undefined
  7699. });
  7700. return new LodashWrapper(wrapped, this.__chain__);
  7701. }
  7702. return this.thru(reverse);
  7703. }
  7704. /**
  7705. * Executes the chain sequence to resolve the unwrapped value.
  7706. *
  7707. * @name value
  7708. * @memberOf _
  7709. * @since 0.1.0
  7710. * @alias toJSON, valueOf
  7711. * @category Seq
  7712. * @returns {*} Returns the resolved unwrapped value.
  7713. * @example
  7714. *
  7715. * _([1, 2, 3]).value();
  7716. * // => [1, 2, 3]
  7717. */
  7718. function wrapperValue() {
  7719. return baseWrapperValue(this.__wrapped__, this.__actions__);
  7720. }
  7721. /*------------------------------------------------------------------------*/
  7722. /**
  7723. * Creates an object composed of keys generated from the results of running
  7724. * each element of `collection` thru `iteratee`. The corresponding value of
  7725. * each key is the number of times the key was returned by `iteratee`. The
  7726. * iteratee is invoked with one argument: (value).
  7727. *
  7728. * @static
  7729. * @memberOf _
  7730. * @since 0.5.0
  7731. * @category Collection
  7732. * @param {Array|Object} collection The collection to iterate over.
  7733. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7734. * The iteratee to transform keys.
  7735. * @returns {Object} Returns the composed aggregate object.
  7736. * @example
  7737. *
  7738. * _.countBy([6.1, 4.2, 6.3], Math.floor);
  7739. * // => { '4': 1, '6': 2 }
  7740. *
  7741. * // The `_.property` iteratee shorthand.
  7742. * _.countBy(['one', 'two', 'three'], 'length');
  7743. * // => { '3': 2, '5': 1 }
  7744. */
  7745. var countBy = createAggregator(function(result, value, key) {
  7746. hasOwnProperty.call(result, key) ? ++result[key] : (result[key] = 1);
  7747. });
  7748. /**
  7749. * Checks if `predicate` returns truthy for **all** elements of `collection`.
  7750. * Iteration is stopped once `predicate` returns falsey. The predicate is
  7751. * invoked with three arguments: (value, index|key, collection).
  7752. *
  7753. * @static
  7754. * @memberOf _
  7755. * @since 0.1.0
  7756. * @category Collection
  7757. * @param {Array|Object} collection The collection to iterate over.
  7758. * @param {Array|Function|Object|string} [predicate=_.identity]
  7759. * The function invoked per iteration.
  7760. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  7761. * @returns {boolean} Returns `true` if all elements pass the predicate check,
  7762. * else `false`.
  7763. * @example
  7764. *
  7765. * _.every([true, 1, null, 'yes'], Boolean);
  7766. * // => false
  7767. *
  7768. * var users = [
  7769. * { 'user': 'barney', 'age': 36, 'active': false },
  7770. * { 'user': 'fred', 'age': 40, 'active': false }
  7771. * ];
  7772. *
  7773. * // The `_.matches` iteratee shorthand.
  7774. * _.every(users, { 'user': 'barney', 'active': false });
  7775. * // => false
  7776. *
  7777. * // The `_.matchesProperty` iteratee shorthand.
  7778. * _.every(users, ['active', false]);
  7779. * // => true
  7780. *
  7781. * // The `_.property` iteratee shorthand.
  7782. * _.every(users, 'active');
  7783. * // => false
  7784. */
  7785. function every(collection, predicate, guard) {
  7786. var func = isArray(collection) ? arrayEvery : baseEvery;
  7787. if (guard && isIterateeCall(collection, predicate, guard)) {
  7788. predicate = undefined;
  7789. }
  7790. return func(collection, getIteratee(predicate, 3));
  7791. }
  7792. /**
  7793. * Iterates over elements of `collection`, returning an array of all elements
  7794. * `predicate` returns truthy for. The predicate is invoked with three
  7795. * arguments: (value, index|key, collection).
  7796. *
  7797. * @static
  7798. * @memberOf _
  7799. * @since 0.1.0
  7800. * @category Collection
  7801. * @param {Array|Object} collection The collection to iterate over.
  7802. * @param {Array|Function|Object|string} [predicate=_.identity]
  7803. * The function invoked per iteration.
  7804. * @returns {Array} Returns the new filtered array.
  7805. * @see _.reject
  7806. * @example
  7807. *
  7808. * var users = [
  7809. * { 'user': 'barney', 'age': 36, 'active': true },
  7810. * { 'user': 'fred', 'age': 40, 'active': false }
  7811. * ];
  7812. *
  7813. * _.filter(users, function(o) { return !o.active; });
  7814. * // => objects for ['fred']
  7815. *
  7816. * // The `_.matches` iteratee shorthand.
  7817. * _.filter(users, { 'age': 36, 'active': true });
  7818. * // => objects for ['barney']
  7819. *
  7820. * // The `_.matchesProperty` iteratee shorthand.
  7821. * _.filter(users, ['active', false]);
  7822. * // => objects for ['fred']
  7823. *
  7824. * // The `_.property` iteratee shorthand.
  7825. * _.filter(users, 'active');
  7826. * // => objects for ['barney']
  7827. */
  7828. function filter(collection, predicate) {
  7829. var func = isArray(collection) ? arrayFilter : baseFilter;
  7830. return func(collection, getIteratee(predicate, 3));
  7831. }
  7832. /**
  7833. * Iterates over elements of `collection`, returning the first element
  7834. * `predicate` returns truthy for. The predicate is invoked with three
  7835. * arguments: (value, index|key, collection).
  7836. *
  7837. * @static
  7838. * @memberOf _
  7839. * @since 0.1.0
  7840. * @category Collection
  7841. * @param {Array|Object} collection The collection to search.
  7842. * @param {Array|Function|Object|string} [predicate=_.identity]
  7843. * The function invoked per iteration.
  7844. * @param {number} [fromIndex=0] The index to search from.
  7845. * @returns {*} Returns the matched element, else `undefined`.
  7846. * @example
  7847. *
  7848. * var users = [
  7849. * { 'user': 'barney', 'age': 36, 'active': true },
  7850. * { 'user': 'fred', 'age': 40, 'active': false },
  7851. * { 'user': 'pebbles', 'age': 1, 'active': true }
  7852. * ];
  7853. *
  7854. * _.find(users, function(o) { return o.age < 40; });
  7855. * // => object for 'barney'
  7856. *
  7857. * // The `_.matches` iteratee shorthand.
  7858. * _.find(users, { 'age': 1, 'active': true });
  7859. * // => object for 'pebbles'
  7860. *
  7861. * // The `_.matchesProperty` iteratee shorthand.
  7862. * _.find(users, ['active', false]);
  7863. * // => object for 'fred'
  7864. *
  7865. * // The `_.property` iteratee shorthand.
  7866. * _.find(users, 'active');
  7867. * // => object for 'barney'
  7868. */
  7869. var find = createFind(findIndex);
  7870. /**
  7871. * This method is like `_.find` except that it iterates over elements of
  7872. * `collection` from right to left.
  7873. *
  7874. * @static
  7875. * @memberOf _
  7876. * @since 2.0.0
  7877. * @category Collection
  7878. * @param {Array|Object} collection The collection to search.
  7879. * @param {Array|Function|Object|string} [predicate=_.identity]
  7880. * The function invoked per iteration.
  7881. * @param {number} [fromIndex=collection.length-1] The index to search from.
  7882. * @returns {*} Returns the matched element, else `undefined`.
  7883. * @example
  7884. *
  7885. * _.findLast([1, 2, 3, 4], function(n) {
  7886. * return n % 2 == 1;
  7887. * });
  7888. * // => 3
  7889. */
  7890. var findLast = createFind(findLastIndex);
  7891. /**
  7892. * Creates a flattened array of values by running each element in `collection`
  7893. * thru `iteratee` and flattening the mapped results. The iteratee is invoked
  7894. * with three arguments: (value, index|key, collection).
  7895. *
  7896. * @static
  7897. * @memberOf _
  7898. * @since 4.0.0
  7899. * @category Collection
  7900. * @param {Array|Object} collection The collection to iterate over.
  7901. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7902. * The function invoked per iteration.
  7903. * @returns {Array} Returns the new flattened array.
  7904. * @example
  7905. *
  7906. * function duplicate(n) {
  7907. * return [n, n];
  7908. * }
  7909. *
  7910. * _.flatMap([1, 2], duplicate);
  7911. * // => [1, 1, 2, 2]
  7912. */
  7913. function flatMap(collection, iteratee) {
  7914. return baseFlatten(map(collection, iteratee), 1);
  7915. }
  7916. /**
  7917. * This method is like `_.flatMap` except that it recursively flattens the
  7918. * mapped results.
  7919. *
  7920. * @static
  7921. * @memberOf _
  7922. * @since 4.7.0
  7923. * @category Collection
  7924. * @param {Array|Object} collection The collection to iterate over.
  7925. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7926. * The function invoked per iteration.
  7927. * @returns {Array} Returns the new flattened array.
  7928. * @example
  7929. *
  7930. * function duplicate(n) {
  7931. * return [[[n, n]]];
  7932. * }
  7933. *
  7934. * _.flatMapDeep([1, 2], duplicate);
  7935. * // => [1, 1, 2, 2]
  7936. */
  7937. function flatMapDeep(collection, iteratee) {
  7938. return baseFlatten(map(collection, iteratee), INFINITY);
  7939. }
  7940. /**
  7941. * This method is like `_.flatMap` except that it recursively flattens the
  7942. * mapped results up to `depth` times.
  7943. *
  7944. * @static
  7945. * @memberOf _
  7946. * @since 4.7.0
  7947. * @category Collection
  7948. * @param {Array|Object} collection The collection to iterate over.
  7949. * @param {Array|Function|Object|string} [iteratee=_.identity]
  7950. * The function invoked per iteration.
  7951. * @param {number} [depth=1] The maximum recursion depth.
  7952. * @returns {Array} Returns the new flattened array.
  7953. * @example
  7954. *
  7955. * function duplicate(n) {
  7956. * return [[[n, n]]];
  7957. * }
  7958. *
  7959. * _.flatMapDepth([1, 2], duplicate, 2);
  7960. * // => [[1, 1], [2, 2]]
  7961. */
  7962. function flatMapDepth(collection, iteratee, depth) {
  7963. depth = depth === undefined ? 1 : toInteger(depth);
  7964. return baseFlatten(map(collection, iteratee), depth);
  7965. }
  7966. /**
  7967. * Iterates over elements of `collection` and invokes `iteratee` for each element.
  7968. * The iteratee is invoked with three arguments: (value, index|key, collection).
  7969. * Iteratee functions may exit iteration early by explicitly returning `false`.
  7970. *
  7971. * **Note:** As with other "Collections" methods, objects with a "length"
  7972. * property are iterated like arrays. To avoid this behavior use `_.forIn`
  7973. * or `_.forOwn` for object iteration.
  7974. *
  7975. * @static
  7976. * @memberOf _
  7977. * @since 0.1.0
  7978. * @alias each
  7979. * @category Collection
  7980. * @param {Array|Object} collection The collection to iterate over.
  7981. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  7982. * @returns {Array|Object} Returns `collection`.
  7983. * @see _.forEachRight
  7984. * @example
  7985. *
  7986. * _([1, 2]).forEach(function(value) {
  7987. * console.log(value);
  7988. * });
  7989. * // => Logs `1` then `2`.
  7990. *
  7991. * _.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  7992. * console.log(key);
  7993. * });
  7994. * // => Logs 'a' then 'b' (iteration order is not guaranteed).
  7995. */
  7996. function forEach(collection, iteratee) {
  7997. var func = isArray(collection) ? arrayEach : baseEach;
  7998. return func(collection, getIteratee(iteratee, 3));
  7999. }
  8000. /**
  8001. * This method is like `_.forEach` except that it iterates over elements of
  8002. * `collection` from right to left.
  8003. *
  8004. * @static
  8005. * @memberOf _
  8006. * @since 2.0.0
  8007. * @alias eachRight
  8008. * @category Collection
  8009. * @param {Array|Object} collection The collection to iterate over.
  8010. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8011. * @returns {Array|Object} Returns `collection`.
  8012. * @see _.forEach
  8013. * @example
  8014. *
  8015. * _.forEachRight([1, 2], function(value) {
  8016. * console.log(value);
  8017. * });
  8018. * // => Logs `2` then `1`.
  8019. */
  8020. function forEachRight(collection, iteratee) {
  8021. var func = isArray(collection) ? arrayEachRight : baseEachRight;
  8022. return func(collection, getIteratee(iteratee, 3));
  8023. }
  8024. /**
  8025. * Creates an object composed of keys generated from the results of running
  8026. * each element of `collection` thru `iteratee`. The order of grouped values
  8027. * is determined by the order they occur in `collection`. The corresponding
  8028. * value of each key is an array of elements responsible for generating the
  8029. * key. The iteratee is invoked with one argument: (value).
  8030. *
  8031. * @static
  8032. * @memberOf _
  8033. * @since 0.1.0
  8034. * @category Collection
  8035. * @param {Array|Object} collection The collection to iterate over.
  8036. * @param {Array|Function|Object|string} [iteratee=_.identity]
  8037. * The iteratee to transform keys.
  8038. * @returns {Object} Returns the composed aggregate object.
  8039. * @example
  8040. *
  8041. * _.groupBy([6.1, 4.2, 6.3], Math.floor);
  8042. * // => { '4': [4.2], '6': [6.1, 6.3] }
  8043. *
  8044. * // The `_.property` iteratee shorthand.
  8045. * _.groupBy(['one', 'two', 'three'], 'length');
  8046. * // => { '3': ['one', 'two'], '5': ['three'] }
  8047. */
  8048. var groupBy = createAggregator(function(result, value, key) {
  8049. if (hasOwnProperty.call(result, key)) {
  8050. result[key].push(value);
  8051. } else {
  8052. result[key] = [value];
  8053. }
  8054. });
  8055. /**
  8056. * Checks if `value` is in `collection`. If `collection` is a string, it's
  8057. * checked for a substring of `value`, otherwise
  8058. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  8059. * is used for equality comparisons. If `fromIndex` is negative, it's used as
  8060. * the offset from the end of `collection`.
  8061. *
  8062. * @static
  8063. * @memberOf _
  8064. * @since 0.1.0
  8065. * @category Collection
  8066. * @param {Array|Object|string} collection The collection to search.
  8067. * @param {*} value The value to search for.
  8068. * @param {number} [fromIndex=0] The index to search from.
  8069. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`.
  8070. * @returns {boolean} Returns `true` if `value` is found, else `false`.
  8071. * @example
  8072. *
  8073. * _.includes([1, 2, 3], 1);
  8074. * // => true
  8075. *
  8076. * _.includes([1, 2, 3], 1, 2);
  8077. * // => false
  8078. *
  8079. * _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
  8080. * // => true
  8081. *
  8082. * _.includes('pebbles', 'eb');
  8083. * // => true
  8084. */
  8085. function includes(collection, value, fromIndex, guard) {
  8086. collection = isArrayLike(collection) ? collection : values(collection);
  8087. fromIndex = (fromIndex && !guard) ? toInteger(fromIndex) : 0;
  8088. var length = collection.length;
  8089. if (fromIndex < 0) {
  8090. fromIndex = nativeMax(length + fromIndex, 0);
  8091. }
  8092. return isString(collection)
  8093. ? (fromIndex <= length && collection.indexOf(value, fromIndex) > -1)
  8094. : (!!length && baseIndexOf(collection, value, fromIndex) > -1);
  8095. }
  8096. /**
  8097. * Invokes the method at `path` of each element in `collection`, returning
  8098. * an array of the results of each invoked method. Any additional arguments
  8099. * are provided to each invoked method. If `methodName` is a function, it's
  8100. * invoked for and `this` bound to, each element in `collection`.
  8101. *
  8102. * @static
  8103. * @memberOf _
  8104. * @since 4.0.0
  8105. * @category Collection
  8106. * @param {Array|Object} collection The collection to iterate over.
  8107. * @param {Array|Function|string} path The path of the method to invoke or
  8108. * the function invoked per iteration.
  8109. * @param {...*} [args] The arguments to invoke each method with.
  8110. * @returns {Array} Returns the array of results.
  8111. * @example
  8112. *
  8113. * _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
  8114. * // => [[1, 5, 7], [1, 2, 3]]
  8115. *
  8116. * _.invokeMap([123, 456], String.prototype.split, '');
  8117. * // => [['1', '2', '3'], ['4', '5', '6']]
  8118. */
  8119. var invokeMap = rest(function(collection, path, args) {
  8120. var index = -1,
  8121. isFunc = typeof path == 'function',
  8122. isProp = isKey(path),
  8123. result = isArrayLike(collection) ? Array(collection.length) : [];
  8124. baseEach(collection, function(value) {
  8125. var func = isFunc ? path : ((isProp && value != null) ? value[path] : undefined);
  8126. result[++index] = func ? apply(func, value, args) : baseInvoke(value, path, args);
  8127. });
  8128. return result;
  8129. });
  8130. /**
  8131. * Creates an object composed of keys generated from the results of running
  8132. * each element of `collection` thru `iteratee`. The corresponding value of
  8133. * each key is the last element responsible for generating the key. The
  8134. * iteratee is invoked with one argument: (value).
  8135. *
  8136. * @static
  8137. * @memberOf _
  8138. * @since 4.0.0
  8139. * @category Collection
  8140. * @param {Array|Object} collection The collection to iterate over.
  8141. * @param {Array|Function|Object|string} [iteratee=_.identity]
  8142. * The iteratee to transform keys.
  8143. * @returns {Object} Returns the composed aggregate object.
  8144. * @example
  8145. *
  8146. * var array = [
  8147. * { 'dir': 'left', 'code': 97 },
  8148. * { 'dir': 'right', 'code': 100 }
  8149. * ];
  8150. *
  8151. * _.keyBy(array, function(o) {
  8152. * return String.fromCharCode(o.code);
  8153. * });
  8154. * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
  8155. *
  8156. * _.keyBy(array, 'dir');
  8157. * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
  8158. */
  8159. var keyBy = createAggregator(function(result, value, key) {
  8160. result[key] = value;
  8161. });
  8162. /**
  8163. * Creates an array of values by running each element in `collection` thru
  8164. * `iteratee`. The iteratee is invoked with three arguments:
  8165. * (value, index|key, collection).
  8166. *
  8167. * Many lodash methods are guarded to work as iteratees for methods like
  8168. * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
  8169. *
  8170. * The guarded methods are:
  8171. * `ary`, `chunk`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
  8172. * `fill`, `invert`, `parseInt`, `random`, `range`, `rangeRight`, `repeat`,
  8173. * `sampleSize`, `slice`, `some`, `sortBy`, `split`, `take`, `takeRight`,
  8174. * `template`, `trim`, `trimEnd`, `trimStart`, and `words`
  8175. *
  8176. * @static
  8177. * @memberOf _
  8178. * @since 0.1.0
  8179. * @category Collection
  8180. * @param {Array|Object} collection The collection to iterate over.
  8181. * @param {Array|Function|Object|string} [iteratee=_.identity]
  8182. * The function invoked per iteration.
  8183. * @returns {Array} Returns the new mapped array.
  8184. * @example
  8185. *
  8186. * function square(n) {
  8187. * return n * n;
  8188. * }
  8189. *
  8190. * _.map([4, 8], square);
  8191. * // => [16, 64]
  8192. *
  8193. * _.map({ 'a': 4, 'b': 8 }, square);
  8194. * // => [16, 64] (iteration order is not guaranteed)
  8195. *
  8196. * var users = [
  8197. * { 'user': 'barney' },
  8198. * { 'user': 'fred' }
  8199. * ];
  8200. *
  8201. * // The `_.property` iteratee shorthand.
  8202. * _.map(users, 'user');
  8203. * // => ['barney', 'fred']
  8204. */
  8205. function map(collection, iteratee) {
  8206. var func = isArray(collection) ? arrayMap : baseMap;
  8207. return func(collection, getIteratee(iteratee, 3));
  8208. }
  8209. /**
  8210. * This method is like `_.sortBy` except that it allows specifying the sort
  8211. * orders of the iteratees to sort by. If `orders` is unspecified, all values
  8212. * are sorted in ascending order. Otherwise, specify an order of "desc" for
  8213. * descending or "asc" for ascending sort order of corresponding values.
  8214. *
  8215. * @static
  8216. * @memberOf _
  8217. * @since 4.0.0
  8218. * @category Collection
  8219. * @param {Array|Object} collection The collection to iterate over.
  8220. * @param {Array[]|Function[]|Object[]|string[]} [iteratees=[_.identity]]
  8221. * The iteratees to sort by.
  8222. * @param {string[]} [orders] The sort orders of `iteratees`.
  8223. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`.
  8224. * @returns {Array} Returns the new sorted array.
  8225. * @example
  8226. *
  8227. * var users = [
  8228. * { 'user': 'fred', 'age': 48 },
  8229. * { 'user': 'barney', 'age': 34 },
  8230. * { 'user': 'fred', 'age': 40 },
  8231. * { 'user': 'barney', 'age': 36 }
  8232. * ];
  8233. *
  8234. * // Sort by `user` in ascending order and by `age` in descending order.
  8235. * _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
  8236. * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
  8237. */
  8238. function orderBy(collection, iteratees, orders, guard) {
  8239. if (collection == null) {
  8240. return [];
  8241. }
  8242. if (!isArray(iteratees)) {
  8243. iteratees = iteratees == null ? [] : [iteratees];
  8244. }
  8245. orders = guard ? undefined : orders;
  8246. if (!isArray(orders)) {
  8247. orders = orders == null ? [] : [orders];
  8248. }
  8249. return baseOrderBy(collection, iteratees, orders);
  8250. }
  8251. /**
  8252. * Creates an array of elements split into two groups, the first of which
  8253. * contains elements `predicate` returns truthy for, the second of which
  8254. * contains elements `predicate` returns falsey for. The predicate is
  8255. * invoked with one argument: (value).
  8256. *
  8257. * @static
  8258. * @memberOf _
  8259. * @since 3.0.0
  8260. * @category Collection
  8261. * @param {Array|Object} collection The collection to iterate over.
  8262. * @param {Array|Function|Object|string} [predicate=_.identity]
  8263. * The function invoked per iteration.
  8264. * @returns {Array} Returns the array of grouped elements.
  8265. * @example
  8266. *
  8267. * var users = [
  8268. * { 'user': 'barney', 'age': 36, 'active': false },
  8269. * { 'user': 'fred', 'age': 40, 'active': true },
  8270. * { 'user': 'pebbles', 'age': 1, 'active': false }
  8271. * ];
  8272. *
  8273. * _.partition(users, function(o) { return o.active; });
  8274. * // => objects for [['fred'], ['barney', 'pebbles']]
  8275. *
  8276. * // The `_.matches` iteratee shorthand.
  8277. * _.partition(users, { 'age': 1, 'active': false });
  8278. * // => objects for [['pebbles'], ['barney', 'fred']]
  8279. *
  8280. * // The `_.matchesProperty` iteratee shorthand.
  8281. * _.partition(users, ['active', false]);
  8282. * // => objects for [['barney', 'pebbles'], ['fred']]
  8283. *
  8284. * // The `_.property` iteratee shorthand.
  8285. * _.partition(users, 'active');
  8286. * // => objects for [['fred'], ['barney', 'pebbles']]
  8287. */
  8288. var partition = createAggregator(function(result, value, key) {
  8289. result[key ? 0 : 1].push(value);
  8290. }, function() { return [[], []]; });
  8291. /**
  8292. * Reduces `collection` to a value which is the accumulated result of running
  8293. * each element in `collection` thru `iteratee`, where each successive
  8294. * invocation is supplied the return value of the previous. If `accumulator`
  8295. * is not given, the first element of `collection` is used as the initial
  8296. * value. The iteratee is invoked with four arguments:
  8297. * (accumulator, value, index|key, collection).
  8298. *
  8299. * Many lodash methods are guarded to work as iteratees for methods like
  8300. * `_.reduce`, `_.reduceRight`, and `_.transform`.
  8301. *
  8302. * The guarded methods are:
  8303. * `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`,
  8304. * and `sortBy`
  8305. *
  8306. * @static
  8307. * @memberOf _
  8308. * @since 0.1.0
  8309. * @category Collection
  8310. * @param {Array|Object} collection The collection to iterate over.
  8311. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8312. * @param {*} [accumulator] The initial value.
  8313. * @returns {*} Returns the accumulated value.
  8314. * @see _.reduceRight
  8315. * @example
  8316. *
  8317. * _.reduce([1, 2], function(sum, n) {
  8318. * return sum + n;
  8319. * }, 0);
  8320. * // => 3
  8321. *
  8322. * _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
  8323. * (result[value] || (result[value] = [])).push(key);
  8324. * return result;
  8325. * }, {});
  8326. * // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
  8327. */
  8328. function reduce(collection, iteratee, accumulator) {
  8329. var func = isArray(collection) ? arrayReduce : baseReduce,
  8330. initAccum = arguments.length < 3;
  8331. return func(collection, getIteratee(iteratee, 4), accumulator, initAccum, baseEach);
  8332. }
  8333. /**
  8334. * This method is like `_.reduce` except that it iterates over elements of
  8335. * `collection` from right to left.
  8336. *
  8337. * @static
  8338. * @memberOf _
  8339. * @since 0.1.0
  8340. * @category Collection
  8341. * @param {Array|Object} collection The collection to iterate over.
  8342. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  8343. * @param {*} [accumulator] The initial value.
  8344. * @returns {*} Returns the accumulated value.
  8345. * @see _.reduce
  8346. * @example
  8347. *
  8348. * var array = [[0, 1], [2, 3], [4, 5]];
  8349. *
  8350. * _.reduceRight(array, function(flattened, other) {
  8351. * return flattened.concat(other);
  8352. * }, []);
  8353. * // => [4, 5, 2, 3, 0, 1]
  8354. */
  8355. function reduceRight(collection, iteratee, accumulator) {
  8356. var func = isArray(collection) ? arrayReduceRight : baseReduce,
  8357. initAccum = arguments.length < 3;
  8358. return func(collection, getIteratee(iteratee, 4), accumulator, initAccum, baseEachRight);
  8359. }
  8360. /**
  8361. * The opposite of `_.filter`; this method returns the elements of `collection`
  8362. * that `predicate` does **not** return truthy for.
  8363. *
  8364. * @static
  8365. * @memberOf _
  8366. * @since 0.1.0
  8367. * @category Collection
  8368. * @param {Array|Object} collection The collection to iterate over.
  8369. * @param {Array|Function|Object|string} [predicate=_.identity]
  8370. * The function invoked per iteration.
  8371. * @returns {Array} Returns the new filtered array.
  8372. * @see _.filter
  8373. * @example
  8374. *
  8375. * var users = [
  8376. * { 'user': 'barney', 'age': 36, 'active': false },
  8377. * { 'user': 'fred', 'age': 40, 'active': true }
  8378. * ];
  8379. *
  8380. * _.reject(users, function(o) { return !o.active; });
  8381. * // => objects for ['fred']
  8382. *
  8383. * // The `_.matches` iteratee shorthand.
  8384. * _.reject(users, { 'age': 40, 'active': true });
  8385. * // => objects for ['barney']
  8386. *
  8387. * // The `_.matchesProperty` iteratee shorthand.
  8388. * _.reject(users, ['active', false]);
  8389. * // => objects for ['fred']
  8390. *
  8391. * // The `_.property` iteratee shorthand.
  8392. * _.reject(users, 'active');
  8393. * // => objects for ['barney']
  8394. */
  8395. function reject(collection, predicate) {
  8396. var func = isArray(collection) ? arrayFilter : baseFilter;
  8397. predicate = getIteratee(predicate, 3);
  8398. return func(collection, function(value, index, collection) {
  8399. return !predicate(value, index, collection);
  8400. });
  8401. }
  8402. /**
  8403. * Gets a random element from `collection`.
  8404. *
  8405. * @static
  8406. * @memberOf _
  8407. * @since 2.0.0
  8408. * @category Collection
  8409. * @param {Array|Object} collection The collection to sample.
  8410. * @returns {*} Returns the random element.
  8411. * @example
  8412. *
  8413. * _.sample([1, 2, 3, 4]);
  8414. * // => 2
  8415. */
  8416. function sample(collection) {
  8417. var array = isArrayLike(collection) ? collection : values(collection),
  8418. length = array.length;
  8419. return length > 0 ? array[baseRandom(0, length - 1)] : undefined;
  8420. }
  8421. /**
  8422. * Gets `n` random elements at unique keys from `collection` up to the
  8423. * size of `collection`.
  8424. *
  8425. * @static
  8426. * @memberOf _
  8427. * @since 4.0.0
  8428. * @category Collection
  8429. * @param {Array|Object} collection The collection to sample.
  8430. * @param {number} [n=1] The number of elements to sample.
  8431. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  8432. * @returns {Array} Returns the random elements.
  8433. * @example
  8434. *
  8435. * _.sampleSize([1, 2, 3], 2);
  8436. * // => [3, 1]
  8437. *
  8438. * _.sampleSize([1, 2, 3], 4);
  8439. * // => [2, 3, 1]
  8440. */
  8441. function sampleSize(collection, n, guard) {
  8442. var index = -1,
  8443. result = toArray(collection),
  8444. length = result.length,
  8445. lastIndex = length - 1;
  8446. if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
  8447. n = 1;
  8448. } else {
  8449. n = baseClamp(toInteger(n), 0, length);
  8450. }
  8451. while (++index < n) {
  8452. var rand = baseRandom(index, lastIndex),
  8453. value = result[rand];
  8454. result[rand] = result[index];
  8455. result[index] = value;
  8456. }
  8457. result.length = n;
  8458. return result;
  8459. }
  8460. /**
  8461. * Creates an array of shuffled values, using a version of the
  8462. * [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
  8463. *
  8464. * @static
  8465. * @memberOf _
  8466. * @since 0.1.0
  8467. * @category Collection
  8468. * @param {Array|Object} collection The collection to shuffle.
  8469. * @returns {Array} Returns the new shuffled array.
  8470. * @example
  8471. *
  8472. * _.shuffle([1, 2, 3, 4]);
  8473. * // => [4, 1, 3, 2]
  8474. */
  8475. function shuffle(collection) {
  8476. return sampleSize(collection, MAX_ARRAY_LENGTH);
  8477. }
  8478. /**
  8479. * Gets the size of `collection` by returning its length for array-like
  8480. * values or the number of own enumerable string keyed properties for objects.
  8481. *
  8482. * @static
  8483. * @memberOf _
  8484. * @since 0.1.0
  8485. * @category Collection
  8486. * @param {Array|Object} collection The collection to inspect.
  8487. * @returns {number} Returns the collection size.
  8488. * @example
  8489. *
  8490. * _.size([1, 2, 3]);
  8491. * // => 3
  8492. *
  8493. * _.size({ 'a': 1, 'b': 2 });
  8494. * // => 2
  8495. *
  8496. * _.size('pebbles');
  8497. * // => 7
  8498. */
  8499. function size(collection) {
  8500. if (collection == null) {
  8501. return 0;
  8502. }
  8503. if (isArrayLike(collection)) {
  8504. var result = collection.length;
  8505. return (result && isString(collection)) ? stringSize(collection) : result;
  8506. }
  8507. if (isObjectLike(collection)) {
  8508. var tag = getTag(collection);
  8509. if (tag == mapTag || tag == setTag) {
  8510. return collection.size;
  8511. }
  8512. }
  8513. return keys(collection).length;
  8514. }
  8515. /**
  8516. * Checks if `predicate` returns truthy for **any** element of `collection`.
  8517. * Iteration is stopped once `predicate` returns truthy. The predicate is
  8518. * invoked with three arguments: (value, index|key, collection).
  8519. *
  8520. * @static
  8521. * @memberOf _
  8522. * @since 0.1.0
  8523. * @category Collection
  8524. * @param {Array|Object} collection The collection to iterate over.
  8525. * @param {Array|Function|Object|string} [predicate=_.identity]
  8526. * The function invoked per iteration.
  8527. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  8528. * @returns {boolean} Returns `true` if any element passes the predicate check,
  8529. * else `false`.
  8530. * @example
  8531. *
  8532. * _.some([null, 0, 'yes', false], Boolean);
  8533. * // => true
  8534. *
  8535. * var users = [
  8536. * { 'user': 'barney', 'active': true },
  8537. * { 'user': 'fred', 'active': false }
  8538. * ];
  8539. *
  8540. * // The `_.matches` iteratee shorthand.
  8541. * _.some(users, { 'user': 'barney', 'active': false });
  8542. * // => false
  8543. *
  8544. * // The `_.matchesProperty` iteratee shorthand.
  8545. * _.some(users, ['active', false]);
  8546. * // => true
  8547. *
  8548. * // The `_.property` iteratee shorthand.
  8549. * _.some(users, 'active');
  8550. * // => true
  8551. */
  8552. function some(collection, predicate, guard) {
  8553. var func = isArray(collection) ? arraySome : baseSome;
  8554. if (guard && isIterateeCall(collection, predicate, guard)) {
  8555. predicate = undefined;
  8556. }
  8557. return func(collection, getIteratee(predicate, 3));
  8558. }
  8559. /**
  8560. * Creates an array of elements, sorted in ascending order by the results of
  8561. * running each element in a collection thru each iteratee. This method
  8562. * performs a stable sort, that is, it preserves the original sort order of
  8563. * equal elements. The iteratees are invoked with one argument: (value).
  8564. *
  8565. * @static
  8566. * @memberOf _
  8567. * @since 0.1.0
  8568. * @category Collection
  8569. * @param {Array|Object} collection The collection to iterate over.
  8570. * @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
  8571. * [iteratees=[_.identity]] The iteratees to sort by.
  8572. * @returns {Array} Returns the new sorted array.
  8573. * @example
  8574. *
  8575. * var users = [
  8576. * { 'user': 'fred', 'age': 48 },
  8577. * { 'user': 'barney', 'age': 36 },
  8578. * { 'user': 'fred', 'age': 40 },
  8579. * { 'user': 'barney', 'age': 34 }
  8580. * ];
  8581. *
  8582. * _.sortBy(users, function(o) { return o.user; });
  8583. * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
  8584. *
  8585. * _.sortBy(users, ['user', 'age']);
  8586. * // => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
  8587. *
  8588. * _.sortBy(users, 'user', function(o) {
  8589. * return Math.floor(o.age / 10);
  8590. * });
  8591. * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
  8592. */
  8593. var sortBy = rest(function(collection, iteratees) {
  8594. if (collection == null) {
  8595. return [];
  8596. }
  8597. var length = iteratees.length;
  8598. if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) {
  8599. iteratees = [];
  8600. } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) {
  8601. iteratees = [iteratees[0]];
  8602. }
  8603. iteratees = (iteratees.length == 1 && isArray(iteratees[0]))
  8604. ? iteratees[0]
  8605. : baseFlatten(iteratees, 1, isFlattenableIteratee);
  8606. return baseOrderBy(collection, iteratees, []);
  8607. });
  8608. /*------------------------------------------------------------------------*/
  8609. /**
  8610. * Gets the timestamp of the number of milliseconds that have elapsed since
  8611. * the Unix epoch (1 January 1970 00:00:00 UTC).
  8612. *
  8613. * @static
  8614. * @memberOf _
  8615. * @since 2.4.0
  8616. * @category Date
  8617. * @returns {number} Returns the timestamp.
  8618. * @example
  8619. *
  8620. * _.defer(function(stamp) {
  8621. * console.log(_.now() - stamp);
  8622. * }, _.now());
  8623. * // => Logs the number of milliseconds it took for the deferred invocation.
  8624. */
  8625. function now() {
  8626. return Date.now();
  8627. }
  8628. /*------------------------------------------------------------------------*/
  8629. /**
  8630. * The opposite of `_.before`; this method creates a function that invokes
  8631. * `func` once it's called `n` or more times.
  8632. *
  8633. * @static
  8634. * @memberOf _
  8635. * @since 0.1.0
  8636. * @category Function
  8637. * @param {number} n The number of calls before `func` is invoked.
  8638. * @param {Function} func The function to restrict.
  8639. * @returns {Function} Returns the new restricted function.
  8640. * @example
  8641. *
  8642. * var saves = ['profile', 'settings'];
  8643. *
  8644. * var done = _.after(saves.length, function() {
  8645. * console.log('done saving!');
  8646. * });
  8647. *
  8648. * _.forEach(saves, function(type) {
  8649. * asyncSave({ 'type': type, 'complete': done });
  8650. * });
  8651. * // => Logs 'done saving!' after the two async saves have completed.
  8652. */
  8653. function after(n, func) {
  8654. if (typeof func != 'function') {
  8655. throw new TypeError(FUNC_ERROR_TEXT);
  8656. }
  8657. n = toInteger(n);
  8658. return function() {
  8659. if (--n < 1) {
  8660. return func.apply(this, arguments);
  8661. }
  8662. };
  8663. }
  8664. /**
  8665. * Creates a function that invokes `func`, with up to `n` arguments,
  8666. * ignoring any additional arguments.
  8667. *
  8668. * @static
  8669. * @memberOf _
  8670. * @since 3.0.0
  8671. * @category Function
  8672. * @param {Function} func The function to cap arguments for.
  8673. * @param {number} [n=func.length] The arity cap.
  8674. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  8675. * @returns {Function} Returns the new capped function.
  8676. * @example
  8677. *
  8678. * _.map(['6', '8', '10'], _.ary(parseInt, 1));
  8679. * // => [6, 8, 10]
  8680. */
  8681. function ary(func, n, guard) {
  8682. n = guard ? undefined : n;
  8683. n = (func && n == null) ? func.length : n;
  8684. return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n);
  8685. }
  8686. /**
  8687. * Creates a function that invokes `func`, with the `this` binding and arguments
  8688. * of the created function, while it's called less than `n` times. Subsequent
  8689. * calls to the created function return the result of the last `func` invocation.
  8690. *
  8691. * @static
  8692. * @memberOf _
  8693. * @since 3.0.0
  8694. * @category Function
  8695. * @param {number} n The number of calls at which `func` is no longer invoked.
  8696. * @param {Function} func The function to restrict.
  8697. * @returns {Function} Returns the new restricted function.
  8698. * @example
  8699. *
  8700. * jQuery(element).on('click', _.before(5, addContactToList));
  8701. * // => allows adding up to 4 contacts to the list
  8702. */
  8703. function before(n, func) {
  8704. var result;
  8705. if (typeof func != 'function') {
  8706. throw new TypeError(FUNC_ERROR_TEXT);
  8707. }
  8708. n = toInteger(n);
  8709. return function() {
  8710. if (--n > 0) {
  8711. result = func.apply(this, arguments);
  8712. }
  8713. if (n <= 1) {
  8714. func = undefined;
  8715. }
  8716. return result;
  8717. };
  8718. }
  8719. /**
  8720. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  8721. * and `partials` prepended to the arguments it receives.
  8722. *
  8723. * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
  8724. * may be used as a placeholder for partially applied arguments.
  8725. *
  8726. * **Note:** Unlike native `Function#bind`, this method doesn't set the "length"
  8727. * property of bound functions.
  8728. *
  8729. * @static
  8730. * @memberOf _
  8731. * @since 0.1.0
  8732. * @category Function
  8733. * @param {Function} func The function to bind.
  8734. * @param {*} thisArg The `this` binding of `func`.
  8735. * @param {...*} [partials] The arguments to be partially applied.
  8736. * @returns {Function} Returns the new bound function.
  8737. * @example
  8738. *
  8739. * var greet = function(greeting, punctuation) {
  8740. * return greeting + ' ' + this.user + punctuation;
  8741. * };
  8742. *
  8743. * var object = { 'user': 'fred' };
  8744. *
  8745. * var bound = _.bind(greet, object, 'hi');
  8746. * bound('!');
  8747. * // => 'hi fred!'
  8748. *
  8749. * // Bound with placeholders.
  8750. * var bound = _.bind(greet, object, _, '!');
  8751. * bound('hi');
  8752. * // => 'hi fred!'
  8753. */
  8754. var bind = rest(function(func, thisArg, partials) {
  8755. var bitmask = BIND_FLAG;
  8756. if (partials.length) {
  8757. var holders = replaceHolders(partials, getHolder(bind));
  8758. bitmask |= PARTIAL_FLAG;
  8759. }
  8760. return createWrapper(func, bitmask, thisArg, partials, holders);
  8761. });
  8762. /**
  8763. * Creates a function that invokes the method at `object[key]` with `partials`
  8764. * prepended to the arguments it receives.
  8765. *
  8766. * This method differs from `_.bind` by allowing bound functions to reference
  8767. * methods that may be redefined or don't yet exist. See
  8768. * [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)
  8769. * for more details.
  8770. *
  8771. * The `_.bindKey.placeholder` value, which defaults to `_` in monolithic
  8772. * builds, may be used as a placeholder for partially applied arguments.
  8773. *
  8774. * @static
  8775. * @memberOf _
  8776. * @since 0.10.0
  8777. * @category Function
  8778. * @param {Object} object The object to invoke the method on.
  8779. * @param {string} key The key of the method.
  8780. * @param {...*} [partials] The arguments to be partially applied.
  8781. * @returns {Function} Returns the new bound function.
  8782. * @example
  8783. *
  8784. * var object = {
  8785. * 'user': 'fred',
  8786. * 'greet': function(greeting, punctuation) {
  8787. * return greeting + ' ' + this.user + punctuation;
  8788. * }
  8789. * };
  8790. *
  8791. * var bound = _.bindKey(object, 'greet', 'hi');
  8792. * bound('!');
  8793. * // => 'hi fred!'
  8794. *
  8795. * object.greet = function(greeting, punctuation) {
  8796. * return greeting + 'ya ' + this.user + punctuation;
  8797. * };
  8798. *
  8799. * bound('!');
  8800. * // => 'hiya fred!'
  8801. *
  8802. * // Bound with placeholders.
  8803. * var bound = _.bindKey(object, 'greet', _, '!');
  8804. * bound('hi');
  8805. * // => 'hiya fred!'
  8806. */
  8807. var bindKey = rest(function(object, key, partials) {
  8808. var bitmask = BIND_FLAG | BIND_KEY_FLAG;
  8809. if (partials.length) {
  8810. var holders = replaceHolders(partials, getHolder(bindKey));
  8811. bitmask |= PARTIAL_FLAG;
  8812. }
  8813. return createWrapper(key, bitmask, object, partials, holders);
  8814. });
  8815. /**
  8816. * Creates a function that accepts arguments of `func` and either invokes
  8817. * `func` returning its result, if at least `arity` number of arguments have
  8818. * been provided, or returns a function that accepts the remaining `func`
  8819. * arguments, and so on. The arity of `func` may be specified if `func.length`
  8820. * is not sufficient.
  8821. *
  8822. * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
  8823. * may be used as a placeholder for provided arguments.
  8824. *
  8825. * **Note:** This method doesn't set the "length" property of curried functions.
  8826. *
  8827. * @static
  8828. * @memberOf _
  8829. * @since 2.0.0
  8830. * @category Function
  8831. * @param {Function} func The function to curry.
  8832. * @param {number} [arity=func.length] The arity of `func`.
  8833. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  8834. * @returns {Function} Returns the new curried function.
  8835. * @example
  8836. *
  8837. * var abc = function(a, b, c) {
  8838. * return [a, b, c];
  8839. * };
  8840. *
  8841. * var curried = _.curry(abc);
  8842. *
  8843. * curried(1)(2)(3);
  8844. * // => [1, 2, 3]
  8845. *
  8846. * curried(1, 2)(3);
  8847. * // => [1, 2, 3]
  8848. *
  8849. * curried(1, 2, 3);
  8850. * // => [1, 2, 3]
  8851. *
  8852. * // Curried with placeholders.
  8853. * curried(1)(_, 3)(2);
  8854. * // => [1, 2, 3]
  8855. */
  8856. function curry(func, arity, guard) {
  8857. arity = guard ? undefined : arity;
  8858. var result = createWrapper(func, CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
  8859. result.placeholder = curry.placeholder;
  8860. return result;
  8861. }
  8862. /**
  8863. * This method is like `_.curry` except that arguments are applied to `func`
  8864. * in the manner of `_.partialRight` instead of `_.partial`.
  8865. *
  8866. * The `_.curryRight.placeholder` value, which defaults to `_` in monolithic
  8867. * builds, may be used as a placeholder for provided arguments.
  8868. *
  8869. * **Note:** This method doesn't set the "length" property of curried functions.
  8870. *
  8871. * @static
  8872. * @memberOf _
  8873. * @since 3.0.0
  8874. * @category Function
  8875. * @param {Function} func The function to curry.
  8876. * @param {number} [arity=func.length] The arity of `func`.
  8877. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  8878. * @returns {Function} Returns the new curried function.
  8879. * @example
  8880. *
  8881. * var abc = function(a, b, c) {
  8882. * return [a, b, c];
  8883. * };
  8884. *
  8885. * var curried = _.curryRight(abc);
  8886. *
  8887. * curried(3)(2)(1);
  8888. * // => [1, 2, 3]
  8889. *
  8890. * curried(2, 3)(1);
  8891. * // => [1, 2, 3]
  8892. *
  8893. * curried(1, 2, 3);
  8894. * // => [1, 2, 3]
  8895. *
  8896. * // Curried with placeholders.
  8897. * curried(3)(1, _)(2);
  8898. * // => [1, 2, 3]
  8899. */
  8900. function curryRight(func, arity, guard) {
  8901. arity = guard ? undefined : arity;
  8902. var result = createWrapper(func, CURRY_RIGHT_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
  8903. result.placeholder = curryRight.placeholder;
  8904. return result;
  8905. }
  8906. /**
  8907. * Creates a debounced function that delays invoking `func` until after `wait`
  8908. * milliseconds have elapsed since the last time the debounced function was
  8909. * invoked. The debounced function comes with a `cancel` method to cancel
  8910. * delayed `func` invocations and a `flush` method to immediately invoke them.
  8911. * Provide an options object to indicate whether `func` should be invoked on
  8912. * the leading and/or trailing edge of the `wait` timeout. The `func` is invoked
  8913. * with the last arguments provided to the debounced function. Subsequent calls
  8914. * to the debounced function return the result of the last `func` invocation.
  8915. *
  8916. * **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
  8917. * on the trailing edge of the timeout only if the debounced function is
  8918. * invoked more than once during the `wait` timeout.
  8919. *
  8920. * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
  8921. * for details over the differences between `_.debounce` and `_.throttle`.
  8922. *
  8923. * @static
  8924. * @memberOf _
  8925. * @since 0.1.0
  8926. * @category Function
  8927. * @param {Function} func The function to debounce.
  8928. * @param {number} [wait=0] The number of milliseconds to delay.
  8929. * @param {Object} [options={}] The options object.
  8930. * @param {boolean} [options.leading=false]
  8931. * Specify invoking on the leading edge of the timeout.
  8932. * @param {number} [options.maxWait]
  8933. * The maximum time `func` is allowed to be delayed before it's invoked.
  8934. * @param {boolean} [options.trailing=true]
  8935. * Specify invoking on the trailing edge of the timeout.
  8936. * @returns {Function} Returns the new debounced function.
  8937. * @example
  8938. *
  8939. * // Avoid costly calculations while the window size is in flux.
  8940. * jQuery(window).on('resize', _.debounce(calculateLayout, 150));
  8941. *
  8942. * // Invoke `sendMail` when clicked, debouncing subsequent calls.
  8943. * jQuery(element).on('click', _.debounce(sendMail, 300, {
  8944. * 'leading': true,
  8945. * 'trailing': false
  8946. * }));
  8947. *
  8948. * // Ensure `batchLog` is invoked once after 1 second of debounced calls.
  8949. * var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 });
  8950. * var source = new EventSource('/stream');
  8951. * jQuery(source).on('message', debounced);
  8952. *
  8953. * // Cancel the trailing debounced invocation.
  8954. * jQuery(window).on('popstate', debounced.cancel);
  8955. */
  8956. function debounce(func, wait, options) {
  8957. var lastArgs,
  8958. lastThis,
  8959. maxWait,
  8960. result,
  8961. timerId,
  8962. lastCallTime,
  8963. lastInvokeTime = 0,
  8964. leading = false,
  8965. maxing = false,
  8966. trailing = true;
  8967. if (typeof func != 'function') {
  8968. throw new TypeError(FUNC_ERROR_TEXT);
  8969. }
  8970. wait = toNumber(wait) || 0;
  8971. if (isObject(options)) {
  8972. leading = !!options.leading;
  8973. maxing = 'maxWait' in options;
  8974. maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
  8975. trailing = 'trailing' in options ? !!options.trailing : trailing;
  8976. }
  8977. function invokeFunc(time) {
  8978. var args = lastArgs,
  8979. thisArg = lastThis;
  8980. lastArgs = lastThis = undefined;
  8981. lastInvokeTime = time;
  8982. result = func.apply(thisArg, args);
  8983. return result;
  8984. }
  8985. function leadingEdge(time) {
  8986. // Reset any `maxWait` timer.
  8987. lastInvokeTime = time;
  8988. // Start the timer for the trailing edge.
  8989. timerId = setTimeout(timerExpired, wait);
  8990. // Invoke the leading edge.
  8991. return leading ? invokeFunc(time) : result;
  8992. }
  8993. function remainingWait(time) {
  8994. var timeSinceLastCall = time - lastCallTime,
  8995. timeSinceLastInvoke = time - lastInvokeTime,
  8996. result = wait - timeSinceLastCall;
  8997. return maxing ? nativeMin(result, maxWait - timeSinceLastInvoke) : result;
  8998. }
  8999. function shouldInvoke(time) {
  9000. var timeSinceLastCall = time - lastCallTime,
  9001. timeSinceLastInvoke = time - lastInvokeTime;
  9002. // Either this is the first call, activity has stopped and we're at the
  9003. // trailing edge, the system time has gone backwards and we're treating
  9004. // it as the trailing edge, or we've hit the `maxWait` limit.
  9005. return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
  9006. (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait));
  9007. }
  9008. function timerExpired() {
  9009. var time = now();
  9010. if (shouldInvoke(time)) {
  9011. return trailingEdge(time);
  9012. }
  9013. // Restart the timer.
  9014. timerId = setTimeout(timerExpired, remainingWait(time));
  9015. }
  9016. function trailingEdge(time) {
  9017. timerId = undefined;
  9018. // Only invoke if we have `lastArgs` which means `func` has been
  9019. // debounced at least once.
  9020. if (trailing && lastArgs) {
  9021. return invokeFunc(time);
  9022. }
  9023. lastArgs = lastThis = undefined;
  9024. return result;
  9025. }
  9026. function cancel() {
  9027. lastInvokeTime = 0;
  9028. lastArgs = lastCallTime = lastThis = timerId = undefined;
  9029. }
  9030. function flush() {
  9031. return timerId === undefined ? result : trailingEdge(now());
  9032. }
  9033. function debounced() {
  9034. var time = now(),
  9035. isInvoking = shouldInvoke(time);
  9036. lastArgs = arguments;
  9037. lastThis = this;
  9038. lastCallTime = time;
  9039. if (isInvoking) {
  9040. if (timerId === undefined) {
  9041. return leadingEdge(lastCallTime);
  9042. }
  9043. if (maxing) {
  9044. // Handle invocations in a tight loop.
  9045. timerId = setTimeout(timerExpired, wait);
  9046. return invokeFunc(lastCallTime);
  9047. }
  9048. }
  9049. if (timerId === undefined) {
  9050. timerId = setTimeout(timerExpired, wait);
  9051. }
  9052. return result;
  9053. }
  9054. debounced.cancel = cancel;
  9055. debounced.flush = flush;
  9056. return debounced;
  9057. }
  9058. /**
  9059. * Defers invoking the `func` until the current call stack has cleared. Any
  9060. * additional arguments are provided to `func` when it's invoked.
  9061. *
  9062. * @static
  9063. * @memberOf _
  9064. * @since 0.1.0
  9065. * @category Function
  9066. * @param {Function} func The function to defer.
  9067. * @param {...*} [args] The arguments to invoke `func` with.
  9068. * @returns {number} Returns the timer id.
  9069. * @example
  9070. *
  9071. * _.defer(function(text) {
  9072. * console.log(text);
  9073. * }, 'deferred');
  9074. * // => Logs 'deferred' after one or more milliseconds.
  9075. */
  9076. var defer = rest(function(func, args) {
  9077. return baseDelay(func, 1, args);
  9078. });
  9079. /**
  9080. * Invokes `func` after `wait` milliseconds. Any additional arguments are
  9081. * provided to `func` when it's invoked.
  9082. *
  9083. * @static
  9084. * @memberOf _
  9085. * @since 0.1.0
  9086. * @category Function
  9087. * @param {Function} func The function to delay.
  9088. * @param {number} wait The number of milliseconds to delay invocation.
  9089. * @param {...*} [args] The arguments to invoke `func` with.
  9090. * @returns {number} Returns the timer id.
  9091. * @example
  9092. *
  9093. * _.delay(function(text) {
  9094. * console.log(text);
  9095. * }, 1000, 'later');
  9096. * // => Logs 'later' after one second.
  9097. */
  9098. var delay = rest(function(func, wait, args) {
  9099. return baseDelay(func, toNumber(wait) || 0, args);
  9100. });
  9101. /**
  9102. * Creates a function that invokes `func` with arguments reversed.
  9103. *
  9104. * @static
  9105. * @memberOf _
  9106. * @since 4.0.0
  9107. * @category Function
  9108. * @param {Function} func The function to flip arguments for.
  9109. * @returns {Function} Returns the new flipped function.
  9110. * @example
  9111. *
  9112. * var flipped = _.flip(function() {
  9113. * return _.toArray(arguments);
  9114. * });
  9115. *
  9116. * flipped('a', 'b', 'c', 'd');
  9117. * // => ['d', 'c', 'b', 'a']
  9118. */
  9119. function flip(func) {
  9120. return createWrapper(func, FLIP_FLAG);
  9121. }
  9122. /**
  9123. * Creates a function that memoizes the result of `func`. If `resolver` is
  9124. * provided, it determines the cache key for storing the result based on the
  9125. * arguments provided to the memoized function. By default, the first argument
  9126. * provided to the memoized function is used as the map cache key. The `func`
  9127. * is invoked with the `this` binding of the memoized function.
  9128. *
  9129. * **Note:** The cache is exposed as the `cache` property on the memoized
  9130. * function. Its creation may be customized by replacing the `_.memoize.Cache`
  9131. * constructor with one whose instances implement the
  9132. * [`Map`](http://ecma-international.org/ecma-262/6.0/#sec-properties-of-the-map-prototype-object)
  9133. * method interface of `delete`, `get`, `has`, and `set`.
  9134. *
  9135. * @static
  9136. * @memberOf _
  9137. * @since 0.1.0
  9138. * @category Function
  9139. * @param {Function} func The function to have its output memoized.
  9140. * @param {Function} [resolver] The function to resolve the cache key.
  9141. * @returns {Function} Returns the new memoized function.
  9142. * @example
  9143. *
  9144. * var object = { 'a': 1, 'b': 2 };
  9145. * var other = { 'c': 3, 'd': 4 };
  9146. *
  9147. * var values = _.memoize(_.values);
  9148. * values(object);
  9149. * // => [1, 2]
  9150. *
  9151. * values(other);
  9152. * // => [3, 4]
  9153. *
  9154. * object.a = 2;
  9155. * values(object);
  9156. * // => [1, 2]
  9157. *
  9158. * // Modify the result cache.
  9159. * values.cache.set(object, ['a', 'b']);
  9160. * values(object);
  9161. * // => ['a', 'b']
  9162. *
  9163. * // Replace `_.memoize.Cache`.
  9164. * _.memoize.Cache = WeakMap;
  9165. */
  9166. function memoize(func, resolver) {
  9167. if (typeof func != 'function' || (resolver && typeof resolver != 'function')) {
  9168. throw new TypeError(FUNC_ERROR_TEXT);
  9169. }
  9170. var memoized = function() {
  9171. var args = arguments,
  9172. key = resolver ? resolver.apply(this, args) : args[0],
  9173. cache = memoized.cache;
  9174. if (cache.has(key)) {
  9175. return cache.get(key);
  9176. }
  9177. var result = func.apply(this, args);
  9178. memoized.cache = cache.set(key, result);
  9179. return result;
  9180. };
  9181. memoized.cache = new (memoize.Cache || MapCache);
  9182. return memoized;
  9183. }
  9184. // Assign cache to `_.memoize`.
  9185. memoize.Cache = MapCache;
  9186. /**
  9187. * Creates a function that negates the result of the predicate `func`. The
  9188. * `func` predicate is invoked with the `this` binding and arguments of the
  9189. * created function.
  9190. *
  9191. * @static
  9192. * @memberOf _
  9193. * @since 3.0.0
  9194. * @category Function
  9195. * @param {Function} predicate The predicate to negate.
  9196. * @returns {Function} Returns the new negated function.
  9197. * @example
  9198. *
  9199. * function isEven(n) {
  9200. * return n % 2 == 0;
  9201. * }
  9202. *
  9203. * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));
  9204. * // => [1, 3, 5]
  9205. */
  9206. function negate(predicate) {
  9207. if (typeof predicate != 'function') {
  9208. throw new TypeError(FUNC_ERROR_TEXT);
  9209. }
  9210. return function() {
  9211. return !predicate.apply(this, arguments);
  9212. };
  9213. }
  9214. /**
  9215. * Creates a function that is restricted to invoking `func` once. Repeat calls
  9216. * to the function return the value of the first invocation. The `func` is
  9217. * invoked with the `this` binding and arguments of the created function.
  9218. *
  9219. * @static
  9220. * @memberOf _
  9221. * @since 0.1.0
  9222. * @category Function
  9223. * @param {Function} func The function to restrict.
  9224. * @returns {Function} Returns the new restricted function.
  9225. * @example
  9226. *
  9227. * var initialize = _.once(createApplication);
  9228. * initialize();
  9229. * initialize();
  9230. * // `initialize` invokes `createApplication` once
  9231. */
  9232. function once(func) {
  9233. return before(2, func);
  9234. }
  9235. /**
  9236. * Creates a function that invokes `func` with arguments transformed by
  9237. * corresponding `transforms`.
  9238. *
  9239. * @static
  9240. * @since 4.0.0
  9241. * @memberOf _
  9242. * @category Function
  9243. * @param {Function} func The function to wrap.
  9244. * @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
  9245. * [transforms[_.identity]] The functions to transform.
  9246. * @returns {Function} Returns the new function.
  9247. * @example
  9248. *
  9249. * function doubled(n) {
  9250. * return n * 2;
  9251. * }
  9252. *
  9253. * function square(n) {
  9254. * return n * n;
  9255. * }
  9256. *
  9257. * var func = _.overArgs(function(x, y) {
  9258. * return [x, y];
  9259. * }, [square, doubled]);
  9260. *
  9261. * func(9, 3);
  9262. * // => [81, 6]
  9263. *
  9264. * func(10, 5);
  9265. * // => [100, 10]
  9266. */
  9267. var overArgs = rest(function(func, transforms) {
  9268. transforms = (transforms.length == 1 && isArray(transforms[0]))
  9269. ? arrayMap(transforms[0], baseUnary(getIteratee()))
  9270. : arrayMap(baseFlatten(transforms, 1, isFlattenableIteratee), baseUnary(getIteratee()));
  9271. var funcsLength = transforms.length;
  9272. return rest(function(args) {
  9273. var index = -1,
  9274. length = nativeMin(args.length, funcsLength);
  9275. while (++index < length) {
  9276. args[index] = transforms[index].call(this, args[index]);
  9277. }
  9278. return apply(func, this, args);
  9279. });
  9280. });
  9281. /**
  9282. * Creates a function that invokes `func` with `partials` prepended to the
  9283. * arguments it receives. This method is like `_.bind` except it does **not**
  9284. * alter the `this` binding.
  9285. *
  9286. * The `_.partial.placeholder` value, which defaults to `_` in monolithic
  9287. * builds, may be used as a placeholder for partially applied arguments.
  9288. *
  9289. * **Note:** This method doesn't set the "length" property of partially
  9290. * applied functions.
  9291. *
  9292. * @static
  9293. * @memberOf _
  9294. * @since 0.2.0
  9295. * @category Function
  9296. * @param {Function} func The function to partially apply arguments to.
  9297. * @param {...*} [partials] The arguments to be partially applied.
  9298. * @returns {Function} Returns the new partially applied function.
  9299. * @example
  9300. *
  9301. * var greet = function(greeting, name) {
  9302. * return greeting + ' ' + name;
  9303. * };
  9304. *
  9305. * var sayHelloTo = _.partial(greet, 'hello');
  9306. * sayHelloTo('fred');
  9307. * // => 'hello fred'
  9308. *
  9309. * // Partially applied with placeholders.
  9310. * var greetFred = _.partial(greet, _, 'fred');
  9311. * greetFred('hi');
  9312. * // => 'hi fred'
  9313. */
  9314. var partial = rest(function(func, partials) {
  9315. var holders = replaceHolders(partials, getHolder(partial));
  9316. return createWrapper(func, PARTIAL_FLAG, undefined, partials, holders);
  9317. });
  9318. /**
  9319. * This method is like `_.partial` except that partially applied arguments
  9320. * are appended to the arguments it receives.
  9321. *
  9322. * The `_.partialRight.placeholder` value, which defaults to `_` in monolithic
  9323. * builds, may be used as a placeholder for partially applied arguments.
  9324. *
  9325. * **Note:** This method doesn't set the "length" property of partially
  9326. * applied functions.
  9327. *
  9328. * @static
  9329. * @memberOf _
  9330. * @since 1.0.0
  9331. * @category Function
  9332. * @param {Function} func The function to partially apply arguments to.
  9333. * @param {...*} [partials] The arguments to be partially applied.
  9334. * @returns {Function} Returns the new partially applied function.
  9335. * @example
  9336. *
  9337. * var greet = function(greeting, name) {
  9338. * return greeting + ' ' + name;
  9339. * };
  9340. *
  9341. * var greetFred = _.partialRight(greet, 'fred');
  9342. * greetFred('hi');
  9343. * // => 'hi fred'
  9344. *
  9345. * // Partially applied with placeholders.
  9346. * var sayHelloTo = _.partialRight(greet, 'hello', _);
  9347. * sayHelloTo('fred');
  9348. * // => 'hello fred'
  9349. */
  9350. var partialRight = rest(function(func, partials) {
  9351. var holders = replaceHolders(partials, getHolder(partialRight));
  9352. return createWrapper(func, PARTIAL_RIGHT_FLAG, undefined, partials, holders);
  9353. });
  9354. /**
  9355. * Creates a function that invokes `func` with arguments arranged according
  9356. * to the specified `indexes` where the argument value at the first index is
  9357. * provided as the first argument, the argument value at the second index is
  9358. * provided as the second argument, and so on.
  9359. *
  9360. * @static
  9361. * @memberOf _
  9362. * @since 3.0.0
  9363. * @category Function
  9364. * @param {Function} func The function to rearrange arguments for.
  9365. * @param {...(number|number[])} indexes The arranged argument indexes.
  9366. * @returns {Function} Returns the new function.
  9367. * @example
  9368. *
  9369. * var rearged = _.rearg(function(a, b, c) {
  9370. * return [a, b, c];
  9371. * }, [2, 0, 1]);
  9372. *
  9373. * rearged('b', 'c', 'a')
  9374. * // => ['a', 'b', 'c']
  9375. */
  9376. var rearg = rest(function(func, indexes) {
  9377. return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes, 1));
  9378. });
  9379. /**
  9380. * Creates a function that invokes `func` with the `this` binding of the
  9381. * created function and arguments from `start` and beyond provided as
  9382. * an array.
  9383. *
  9384. * **Note:** This method is based on the
  9385. * [rest parameter](https://mdn.io/rest_parameters).
  9386. *
  9387. * @static
  9388. * @memberOf _
  9389. * @since 4.0.0
  9390. * @category Function
  9391. * @param {Function} func The function to apply a rest parameter to.
  9392. * @param {number} [start=func.length-1] The start position of the rest parameter.
  9393. * @returns {Function} Returns the new function.
  9394. * @example
  9395. *
  9396. * var say = _.rest(function(what, names) {
  9397. * return what + ' ' + _.initial(names).join(', ') +
  9398. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  9399. * });
  9400. *
  9401. * say('hello', 'fred', 'barney', 'pebbles');
  9402. * // => 'hello fred, barney, & pebbles'
  9403. */
  9404. function rest(func, start) {
  9405. if (typeof func != 'function') {
  9406. throw new TypeError(FUNC_ERROR_TEXT);
  9407. }
  9408. start = nativeMax(start === undefined ? (func.length - 1) : toInteger(start), 0);
  9409. return function() {
  9410. var args = arguments,
  9411. index = -1,
  9412. length = nativeMax(args.length - start, 0),
  9413. array = Array(length);
  9414. while (++index < length) {
  9415. array[index] = args[start + index];
  9416. }
  9417. switch (start) {
  9418. case 0: return func.call(this, array);
  9419. case 1: return func.call(this, args[0], array);
  9420. case 2: return func.call(this, args[0], args[1], array);
  9421. }
  9422. var otherArgs = Array(start + 1);
  9423. index = -1;
  9424. while (++index < start) {
  9425. otherArgs[index] = args[index];
  9426. }
  9427. otherArgs[start] = array;
  9428. return apply(func, this, otherArgs);
  9429. };
  9430. }
  9431. /**
  9432. * Creates a function that invokes `func` with the `this` binding of the
  9433. * create function and an array of arguments much like
  9434. * [`Function#apply`](http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.apply).
  9435. *
  9436. * **Note:** This method is based on the
  9437. * [spread operator](https://mdn.io/spread_operator).
  9438. *
  9439. * @static
  9440. * @memberOf _
  9441. * @since 3.2.0
  9442. * @category Function
  9443. * @param {Function} func The function to spread arguments over.
  9444. * @param {number} [start=0] The start position of the spread.
  9445. * @returns {Function} Returns the new function.
  9446. * @example
  9447. *
  9448. * var say = _.spread(function(who, what) {
  9449. * return who + ' says ' + what;
  9450. * });
  9451. *
  9452. * say(['fred', 'hello']);
  9453. * // => 'fred says hello'
  9454. *
  9455. * var numbers = Promise.all([
  9456. * Promise.resolve(40),
  9457. * Promise.resolve(36)
  9458. * ]);
  9459. *
  9460. * numbers.then(_.spread(function(x, y) {
  9461. * return x + y;
  9462. * }));
  9463. * // => a Promise of 76
  9464. */
  9465. function spread(func, start) {
  9466. if (typeof func != 'function') {
  9467. throw new TypeError(FUNC_ERROR_TEXT);
  9468. }
  9469. start = start === undefined ? 0 : nativeMax(toInteger(start), 0);
  9470. return rest(function(args) {
  9471. var array = args[start],
  9472. otherArgs = castSlice(args, 0, start);
  9473. if (array) {
  9474. arrayPush(otherArgs, array);
  9475. }
  9476. return apply(func, this, otherArgs);
  9477. });
  9478. }
  9479. /**
  9480. * Creates a throttled function that only invokes `func` at most once per
  9481. * every `wait` milliseconds. The throttled function comes with a `cancel`
  9482. * method to cancel delayed `func` invocations and a `flush` method to
  9483. * immediately invoke them. Provide an options object to indicate whether
  9484. * `func` should be invoked on the leading and/or trailing edge of the `wait`
  9485. * timeout. The `func` is invoked with the last arguments provided to the
  9486. * throttled function. Subsequent calls to the throttled function return the
  9487. * result of the last `func` invocation.
  9488. *
  9489. * **Note:** If `leading` and `trailing` options are `true`, `func` is
  9490. * invoked on the trailing edge of the timeout only if the throttled function
  9491. * is invoked more than once during the `wait` timeout.
  9492. *
  9493. * See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
  9494. * for details over the differences between `_.throttle` and `_.debounce`.
  9495. *
  9496. * @static
  9497. * @memberOf _
  9498. * @since 0.1.0
  9499. * @category Function
  9500. * @param {Function} func The function to throttle.
  9501. * @param {number} [wait=0] The number of milliseconds to throttle invocations to.
  9502. * @param {Object} [options={}] The options object.
  9503. * @param {boolean} [options.leading=true]
  9504. * Specify invoking on the leading edge of the timeout.
  9505. * @param {boolean} [options.trailing=true]
  9506. * Specify invoking on the trailing edge of the timeout.
  9507. * @returns {Function} Returns the new throttled function.
  9508. * @example
  9509. *
  9510. * // Avoid excessively updating the position while scrolling.
  9511. * jQuery(window).on('scroll', _.throttle(updatePosition, 100));
  9512. *
  9513. * // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
  9514. * var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
  9515. * jQuery(element).on('click', throttled);
  9516. *
  9517. * // Cancel the trailing throttled invocation.
  9518. * jQuery(window).on('popstate', throttled.cancel);
  9519. */
  9520. function throttle(func, wait, options) {
  9521. var leading = true,
  9522. trailing = true;
  9523. if (typeof func != 'function') {
  9524. throw new TypeError(FUNC_ERROR_TEXT);
  9525. }
  9526. if (isObject(options)) {
  9527. leading = 'leading' in options ? !!options.leading : leading;
  9528. trailing = 'trailing' in options ? !!options.trailing : trailing;
  9529. }
  9530. return debounce(func, wait, {
  9531. 'leading': leading,
  9532. 'maxWait': wait,
  9533. 'trailing': trailing
  9534. });
  9535. }
  9536. /**
  9537. * Creates a function that accepts up to one argument, ignoring any
  9538. * additional arguments.
  9539. *
  9540. * @static
  9541. * @memberOf _
  9542. * @since 4.0.0
  9543. * @category Function
  9544. * @param {Function} func The function to cap arguments for.
  9545. * @returns {Function} Returns the new capped function.
  9546. * @example
  9547. *
  9548. * _.map(['6', '8', '10'], _.unary(parseInt));
  9549. * // => [6, 8, 10]
  9550. */
  9551. function unary(func) {
  9552. return ary(func, 1);
  9553. }
  9554. /**
  9555. * Creates a function that provides `value` to the wrapper function as its
  9556. * first argument. Any additional arguments provided to the function are
  9557. * appended to those provided to the wrapper function. The wrapper is invoked
  9558. * with the `this` binding of the created function.
  9559. *
  9560. * @static
  9561. * @memberOf _
  9562. * @since 0.1.0
  9563. * @category Function
  9564. * @param {*} value The value to wrap.
  9565. * @param {Function} [wrapper=identity] The wrapper function.
  9566. * @returns {Function} Returns the new function.
  9567. * @example
  9568. *
  9569. * var p = _.wrap(_.escape, function(func, text) {
  9570. * return '<p>' + func(text) + '</p>';
  9571. * });
  9572. *
  9573. * p('fred, barney, & pebbles');
  9574. * // => '<p>fred, barney, &amp; pebbles</p>'
  9575. */
  9576. function wrap(value, wrapper) {
  9577. wrapper = wrapper == null ? identity : wrapper;
  9578. return partial(wrapper, value);
  9579. }
  9580. /*------------------------------------------------------------------------*/
  9581. /**
  9582. * Casts `value` as an array if it's not one.
  9583. *
  9584. * @static
  9585. * @memberOf _
  9586. * @since 4.4.0
  9587. * @category Lang
  9588. * @param {*} value The value to inspect.
  9589. * @returns {Array} Returns the cast array.
  9590. * @example
  9591. *
  9592. * _.castArray(1);
  9593. * // => [1]
  9594. *
  9595. * _.castArray({ 'a': 1 });
  9596. * // => [{ 'a': 1 }]
  9597. *
  9598. * _.castArray('abc');
  9599. * // => ['abc']
  9600. *
  9601. * _.castArray(null);
  9602. * // => [null]
  9603. *
  9604. * _.castArray(undefined);
  9605. * // => [undefined]
  9606. *
  9607. * _.castArray();
  9608. * // => []
  9609. *
  9610. * var array = [1, 2, 3];
  9611. * console.log(_.castArray(array) === array);
  9612. * // => true
  9613. */
  9614. function castArray() {
  9615. if (!arguments.length) {
  9616. return [];
  9617. }
  9618. var value = arguments[0];
  9619. return isArray(value) ? value : [value];
  9620. }
  9621. /**
  9622. * Creates a shallow clone of `value`.
  9623. *
  9624. * **Note:** This method is loosely based on the
  9625. * [structured clone algorithm](https://mdn.io/Structured_clone_algorithm)
  9626. * and supports cloning arrays, array buffers, booleans, date objects, maps,
  9627. * numbers, `Object` objects, regexes, sets, strings, symbols, and typed
  9628. * arrays. The own enumerable properties of `arguments` objects are cloned
  9629. * as plain objects. An empty object is returned for uncloneable values such
  9630. * as error objects, functions, DOM nodes, and WeakMaps.
  9631. *
  9632. * @static
  9633. * @memberOf _
  9634. * @since 0.1.0
  9635. * @category Lang
  9636. * @param {*} value The value to clone.
  9637. * @returns {*} Returns the cloned value.
  9638. * @see _.cloneDeep
  9639. * @example
  9640. *
  9641. * var objects = [{ 'a': 1 }, { 'b': 2 }];
  9642. *
  9643. * var shallow = _.clone(objects);
  9644. * console.log(shallow[0] === objects[0]);
  9645. * // => true
  9646. */
  9647. function clone(value) {
  9648. return baseClone(value, false, true);
  9649. }
  9650. /**
  9651. * This method is like `_.clone` except that it accepts `customizer` which
  9652. * is invoked to produce the cloned value. If `customizer` returns `undefined`,
  9653. * cloning is handled by the method instead. The `customizer` is invoked with
  9654. * up to four arguments; (value [, index|key, object, stack]).
  9655. *
  9656. * @static
  9657. * @memberOf _
  9658. * @since 4.0.0
  9659. * @category Lang
  9660. * @param {*} value The value to clone.
  9661. * @param {Function} [customizer] The function to customize cloning.
  9662. * @returns {*} Returns the cloned value.
  9663. * @see _.cloneDeepWith
  9664. * @example
  9665. *
  9666. * function customizer(value) {
  9667. * if (_.isElement(value)) {
  9668. * return value.cloneNode(false);
  9669. * }
  9670. * }
  9671. *
  9672. * var el = _.cloneWith(document.body, customizer);
  9673. *
  9674. * console.log(el === document.body);
  9675. * // => false
  9676. * console.log(el.nodeName);
  9677. * // => 'BODY'
  9678. * console.log(el.childNodes.length);
  9679. * // => 0
  9680. */
  9681. function cloneWith(value, customizer) {
  9682. return baseClone(value, false, true, customizer);
  9683. }
  9684. /**
  9685. * This method is like `_.clone` except that it recursively clones `value`.
  9686. *
  9687. * @static
  9688. * @memberOf _
  9689. * @since 1.0.0
  9690. * @category Lang
  9691. * @param {*} value The value to recursively clone.
  9692. * @returns {*} Returns the deep cloned value.
  9693. * @see _.clone
  9694. * @example
  9695. *
  9696. * var objects = [{ 'a': 1 }, { 'b': 2 }];
  9697. *
  9698. * var deep = _.cloneDeep(objects);
  9699. * console.log(deep[0] === objects[0]);
  9700. * // => false
  9701. */
  9702. function cloneDeep(value) {
  9703. return baseClone(value, true, true);
  9704. }
  9705. /**
  9706. * This method is like `_.cloneWith` except that it recursively clones `value`.
  9707. *
  9708. * @static
  9709. * @memberOf _
  9710. * @since 4.0.0
  9711. * @category Lang
  9712. * @param {*} value The value to recursively clone.
  9713. * @param {Function} [customizer] The function to customize cloning.
  9714. * @returns {*} Returns the deep cloned value.
  9715. * @see _.cloneWith
  9716. * @example
  9717. *
  9718. * function customizer(value) {
  9719. * if (_.isElement(value)) {
  9720. * return value.cloneNode(true);
  9721. * }
  9722. * }
  9723. *
  9724. * var el = _.cloneDeepWith(document.body, customizer);
  9725. *
  9726. * console.log(el === document.body);
  9727. * // => false
  9728. * console.log(el.nodeName);
  9729. * // => 'BODY'
  9730. * console.log(el.childNodes.length);
  9731. * // => 20
  9732. */
  9733. function cloneDeepWith(value, customizer) {
  9734. return baseClone(value, true, true, customizer);
  9735. }
  9736. /**
  9737. * Performs a
  9738. * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  9739. * comparison between two values to determine if they are equivalent.
  9740. *
  9741. * @static
  9742. * @memberOf _
  9743. * @since 4.0.0
  9744. * @category Lang
  9745. * @param {*} value The value to compare.
  9746. * @param {*} other The other value to compare.
  9747. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  9748. * @example
  9749. *
  9750. * var object = { 'user': 'fred' };
  9751. * var other = { 'user': 'fred' };
  9752. *
  9753. * _.eq(object, object);
  9754. * // => true
  9755. *
  9756. * _.eq(object, other);
  9757. * // => false
  9758. *
  9759. * _.eq('a', 'a');
  9760. * // => true
  9761. *
  9762. * _.eq('a', Object('a'));
  9763. * // => false
  9764. *
  9765. * _.eq(NaN, NaN);
  9766. * // => true
  9767. */
  9768. function eq(value, other) {
  9769. return value === other || (value !== value && other !== other);
  9770. }
  9771. /**
  9772. * Checks if `value` is greater than `other`.
  9773. *
  9774. * @static
  9775. * @memberOf _
  9776. * @since 3.9.0
  9777. * @category Lang
  9778. * @param {*} value The value to compare.
  9779. * @param {*} other The other value to compare.
  9780. * @returns {boolean} Returns `true` if `value` is greater than `other`,
  9781. * else `false`.
  9782. * @see _.lt
  9783. * @example
  9784. *
  9785. * _.gt(3, 1);
  9786. * // => true
  9787. *
  9788. * _.gt(3, 3);
  9789. * // => false
  9790. *
  9791. * _.gt(1, 3);
  9792. * // => false
  9793. */
  9794. var gt = createRelationalOperation(baseGt);
  9795. /**
  9796. * Checks if `value` is greater than or equal to `other`.
  9797. *
  9798. * @static
  9799. * @memberOf _
  9800. * @since 3.9.0
  9801. * @category Lang
  9802. * @param {*} value The value to compare.
  9803. * @param {*} other The other value to compare.
  9804. * @returns {boolean} Returns `true` if `value` is greater than or equal to
  9805. * `other`, else `false`.
  9806. * @see _.lte
  9807. * @example
  9808. *
  9809. * _.gte(3, 1);
  9810. * // => true
  9811. *
  9812. * _.gte(3, 3);
  9813. * // => true
  9814. *
  9815. * _.gte(1, 3);
  9816. * // => false
  9817. */
  9818. var gte = createRelationalOperation(function(value, other) {
  9819. return value >= other;
  9820. });
  9821. /**
  9822. * Checks if `value` is likely an `arguments` object.
  9823. *
  9824. * @static
  9825. * @memberOf _
  9826. * @since 0.1.0
  9827. * @category Lang
  9828. * @param {*} value The value to check.
  9829. * @returns {boolean} Returns `true` if `value` is correctly classified,
  9830. * else `false`.
  9831. * @example
  9832. *
  9833. * _.isArguments(function() { return arguments; }());
  9834. * // => true
  9835. *
  9836. * _.isArguments([1, 2, 3]);
  9837. * // => false
  9838. */
  9839. function isArguments(value) {
  9840. // Safari 8.1 incorrectly makes `arguments.callee` enumerable in strict mode.
  9841. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  9842. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  9843. }
  9844. /**
  9845. * Checks if `value` is classified as an `Array` object.
  9846. *
  9847. * @static
  9848. * @memberOf _
  9849. * @since 0.1.0
  9850. * @type {Function}
  9851. * @category Lang
  9852. * @param {*} value The value to check.
  9853. * @returns {boolean} Returns `true` if `value` is correctly classified,
  9854. * else `false`.
  9855. * @example
  9856. *
  9857. * _.isArray([1, 2, 3]);
  9858. * // => true
  9859. *
  9860. * _.isArray(document.body.children);
  9861. * // => false
  9862. *
  9863. * _.isArray('abc');
  9864. * // => false
  9865. *
  9866. * _.isArray(_.noop);
  9867. * // => false
  9868. */
  9869. var isArray = Array.isArray;
  9870. /**
  9871. * Checks if `value` is classified as an `ArrayBuffer` object.
  9872. *
  9873. * @static
  9874. * @memberOf _
  9875. * @since 4.3.0
  9876. * @category Lang
  9877. * @param {*} value The value to check.
  9878. * @returns {boolean} Returns `true` if `value` is correctly classified,
  9879. * else `false`.
  9880. * @example
  9881. *
  9882. * _.isArrayBuffer(new ArrayBuffer(2));
  9883. * // => true
  9884. *
  9885. * _.isArrayBuffer(new Array(2));
  9886. * // => false
  9887. */
  9888. function isArrayBuffer(value) {
  9889. return isObjectLike(value) && objectToString.call(value) == arrayBufferTag;
  9890. }
  9891. /**
  9892. * Checks if `value` is array-like. A value is considered array-like if it's
  9893. * not a function and has a `value.length` that's an integer greater than or
  9894. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  9895. *
  9896. * @static
  9897. * @memberOf _
  9898. * @since 4.0.0
  9899. * @category Lang
  9900. * @param {*} value The value to check.
  9901. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  9902. * @example
  9903. *
  9904. * _.isArrayLike([1, 2, 3]);
  9905. * // => true
  9906. *
  9907. * _.isArrayLike(document.body.children);
  9908. * // => true
  9909. *
  9910. * _.isArrayLike('abc');
  9911. * // => true
  9912. *
  9913. * _.isArrayLike(_.noop);
  9914. * // => false
  9915. */
  9916. function isArrayLike(value) {
  9917. return value != null && isLength(getLength(value)) && !isFunction(value);
  9918. }
  9919. /**
  9920. * This method is like `_.isArrayLike` except that it also checks if `value`
  9921. * is an object.
  9922. *
  9923. * @static
  9924. * @memberOf _
  9925. * @since 4.0.0
  9926. * @category Lang
  9927. * @param {*} value The value to check.
  9928. * @returns {boolean} Returns `true` if `value` is an array-like object,
  9929. * else `false`.
  9930. * @example
  9931. *
  9932. * _.isArrayLikeObject([1, 2, 3]);
  9933. * // => true
  9934. *
  9935. * _.isArrayLikeObject(document.body.children);
  9936. * // => true
  9937. *
  9938. * _.isArrayLikeObject('abc');
  9939. * // => false
  9940. *
  9941. * _.isArrayLikeObject(_.noop);
  9942. * // => false
  9943. */
  9944. function isArrayLikeObject(value) {
  9945. return isObjectLike(value) && isArrayLike(value);
  9946. }
  9947. /**
  9948. * Checks if `value` is classified as a boolean primitive or object.
  9949. *
  9950. * @static
  9951. * @memberOf _
  9952. * @since 0.1.0
  9953. * @category Lang
  9954. * @param {*} value The value to check.
  9955. * @returns {boolean} Returns `true` if `value` is correctly classified,
  9956. * else `false`.
  9957. * @example
  9958. *
  9959. * _.isBoolean(false);
  9960. * // => true
  9961. *
  9962. * _.isBoolean(null);
  9963. * // => false
  9964. */
  9965. function isBoolean(value) {
  9966. return value === true || value === false ||
  9967. (isObjectLike(value) && objectToString.call(value) == boolTag);
  9968. }
  9969. /**
  9970. * Checks if `value` is a buffer.
  9971. *
  9972. * @static
  9973. * @memberOf _
  9974. * @since 4.3.0
  9975. * @category Lang
  9976. * @param {*} value The value to check.
  9977. * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
  9978. * @example
  9979. *
  9980. * _.isBuffer(new Buffer(2));
  9981. * // => true
  9982. *
  9983. * _.isBuffer(new Uint8Array(2));
  9984. * // => false
  9985. */
  9986. var isBuffer = !Buffer ? stubFalse : function(value) {
  9987. return value instanceof Buffer;
  9988. };
  9989. /**
  9990. * Checks if `value` is classified as a `Date` object.
  9991. *
  9992. * @static
  9993. * @memberOf _
  9994. * @since 0.1.0
  9995. * @category Lang
  9996. * @param {*} value The value to check.
  9997. * @returns {boolean} Returns `true` if `value` is correctly classified,
  9998. * else `false`.
  9999. * @example
  10000. *
  10001. * _.isDate(new Date);
  10002. * // => true
  10003. *
  10004. * _.isDate('Mon April 23 2012');
  10005. * // => false
  10006. */
  10007. function isDate(value) {
  10008. return isObjectLike(value) && objectToString.call(value) == dateTag;
  10009. }
  10010. /**
  10011. * Checks if `value` is likely a DOM element.
  10012. *
  10013. * @static
  10014. * @memberOf _
  10015. * @since 0.1.0
  10016. * @category Lang
  10017. * @param {*} value The value to check.
  10018. * @returns {boolean} Returns `true` if `value` is a DOM element,
  10019. * else `false`.
  10020. * @example
  10021. *
  10022. * _.isElement(document.body);
  10023. * // => true
  10024. *
  10025. * _.isElement('<body>');
  10026. * // => false
  10027. */
  10028. function isElement(value) {
  10029. return !!value && value.nodeType === 1 && isObjectLike(value) && !isPlainObject(value);
  10030. }
  10031. /**
  10032. * Checks if `value` is an empty object, collection, map, or set.
  10033. *
  10034. * Objects are considered empty if they have no own enumerable string keyed
  10035. * properties.
  10036. *
  10037. * Array-like values such as `arguments` objects, arrays, buffers, strings, or
  10038. * jQuery-like collections are considered empty if they have a `length` of `0`.
  10039. * Similarly, maps and sets are considered empty if they have a `size` of `0`.
  10040. *
  10041. * @static
  10042. * @memberOf _
  10043. * @since 0.1.0
  10044. * @category Lang
  10045. * @param {*} value The value to check.
  10046. * @returns {boolean} Returns `true` if `value` is empty, else `false`.
  10047. * @example
  10048. *
  10049. * _.isEmpty(null);
  10050. * // => true
  10051. *
  10052. * _.isEmpty(true);
  10053. * // => true
  10054. *
  10055. * _.isEmpty(1);
  10056. * // => true
  10057. *
  10058. * _.isEmpty([1, 2, 3]);
  10059. * // => false
  10060. *
  10061. * _.isEmpty({ 'a': 1 });
  10062. * // => false
  10063. */
  10064. function isEmpty(value) {
  10065. if (isArrayLike(value) &&
  10066. (isArray(value) || isString(value) || isFunction(value.splice) ||
  10067. isArguments(value) || isBuffer(value))) {
  10068. return !value.length;
  10069. }
  10070. if (isObjectLike(value)) {
  10071. var tag = getTag(value);
  10072. if (tag == mapTag || tag == setTag) {
  10073. return !value.size;
  10074. }
  10075. }
  10076. for (var key in value) {
  10077. if (hasOwnProperty.call(value, key)) {
  10078. return false;
  10079. }
  10080. }
  10081. return !(nonEnumShadows && keys(value).length);
  10082. }
  10083. /**
  10084. * Performs a deep comparison between two values to determine if they are
  10085. * equivalent.
  10086. *
  10087. * **Note:** This method supports comparing arrays, array buffers, booleans,
  10088. * date objects, error objects, maps, numbers, `Object` objects, regexes,
  10089. * sets, strings, symbols, and typed arrays. `Object` objects are compared
  10090. * by their own, not inherited, enumerable properties. Functions and DOM
  10091. * nodes are **not** supported.
  10092. *
  10093. * @static
  10094. * @memberOf _
  10095. * @since 0.1.0
  10096. * @category Lang
  10097. * @param {*} value The value to compare.
  10098. * @param {*} other The other value to compare.
  10099. * @returns {boolean} Returns `true` if the values are equivalent,
  10100. * else `false`.
  10101. * @example
  10102. *
  10103. * var object = { 'user': 'fred' };
  10104. * var other = { 'user': 'fred' };
  10105. *
  10106. * _.isEqual(object, other);
  10107. * // => true
  10108. *
  10109. * object === other;
  10110. * // => false
  10111. */
  10112. function isEqual(value, other) {
  10113. return baseIsEqual(value, other);
  10114. }
  10115. /**
  10116. * This method is like `_.isEqual` except that it accepts `customizer` which
  10117. * is invoked to compare values. If `customizer` returns `undefined`, comparisons
  10118. * are handled by the method instead. The `customizer` is invoked with up to
  10119. * six arguments: (objValue, othValue [, index|key, object, other, stack]).
  10120. *
  10121. * @static
  10122. * @memberOf _
  10123. * @since 4.0.0
  10124. * @category Lang
  10125. * @param {*} value The value to compare.
  10126. * @param {*} other The other value to compare.
  10127. * @param {Function} [customizer] The function to customize comparisons.
  10128. * @returns {boolean} Returns `true` if the values are equivalent,
  10129. * else `false`.
  10130. * @example
  10131. *
  10132. * function isGreeting(value) {
  10133. * return /^h(?:i|ello)$/.test(value);
  10134. * }
  10135. *
  10136. * function customizer(objValue, othValue) {
  10137. * if (isGreeting(objValue) && isGreeting(othValue)) {
  10138. * return true;
  10139. * }
  10140. * }
  10141. *
  10142. * var array = ['hello', 'goodbye'];
  10143. * var other = ['hi', 'goodbye'];
  10144. *
  10145. * _.isEqualWith(array, other, customizer);
  10146. * // => true
  10147. */
  10148. function isEqualWith(value, other, customizer) {
  10149. customizer = typeof customizer == 'function' ? customizer : undefined;
  10150. var result = customizer ? customizer(value, other) : undefined;
  10151. return result === undefined ? baseIsEqual(value, other, customizer) : !!result;
  10152. }
  10153. /**
  10154. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  10155. * `SyntaxError`, `TypeError`, or `URIError` object.
  10156. *
  10157. * @static
  10158. * @memberOf _
  10159. * @since 3.0.0
  10160. * @category Lang
  10161. * @param {*} value The value to check.
  10162. * @returns {boolean} Returns `true` if `value` is an error object,
  10163. * else `false`.
  10164. * @example
  10165. *
  10166. * _.isError(new Error);
  10167. * // => true
  10168. *
  10169. * _.isError(Error);
  10170. * // => false
  10171. */
  10172. function isError(value) {
  10173. if (!isObjectLike(value)) {
  10174. return false;
  10175. }
  10176. return (objectToString.call(value) == errorTag) ||
  10177. (typeof value.message == 'string' && typeof value.name == 'string');
  10178. }
  10179. /**
  10180. * Checks if `value` is a finite primitive number.
  10181. *
  10182. * **Note:** This method is based on
  10183. * [`Number.isFinite`](https://mdn.io/Number/isFinite).
  10184. *
  10185. * @static
  10186. * @memberOf _
  10187. * @since 0.1.0
  10188. * @category Lang
  10189. * @param {*} value The value to check.
  10190. * @returns {boolean} Returns `true` if `value` is a finite number,
  10191. * else `false`.
  10192. * @example
  10193. *
  10194. * _.isFinite(3);
  10195. * // => true
  10196. *
  10197. * _.isFinite(Number.MIN_VALUE);
  10198. * // => true
  10199. *
  10200. * _.isFinite(Infinity);
  10201. * // => false
  10202. *
  10203. * _.isFinite('3');
  10204. * // => false
  10205. */
  10206. function isFinite(value) {
  10207. return typeof value == 'number' && nativeIsFinite(value);
  10208. }
  10209. /**
  10210. * Checks if `value` is classified as a `Function` object.
  10211. *
  10212. * @static
  10213. * @memberOf _
  10214. * @since 0.1.0
  10215. * @category Lang
  10216. * @param {*} value The value to check.
  10217. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10218. * else `false`.
  10219. * @example
  10220. *
  10221. * _.isFunction(_);
  10222. * // => true
  10223. *
  10224. * _.isFunction(/abc/);
  10225. * // => false
  10226. */
  10227. function isFunction(value) {
  10228. // The use of `Object#toString` avoids issues with the `typeof` operator
  10229. // in Safari 8 which returns 'object' for typed array and weak map constructors,
  10230. // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  10231. var tag = isObject(value) ? objectToString.call(value) : '';
  10232. return tag == funcTag || tag == genTag;
  10233. }
  10234. /**
  10235. * Checks if `value` is an integer.
  10236. *
  10237. * **Note:** This method is based on
  10238. * [`Number.isInteger`](https://mdn.io/Number/isInteger).
  10239. *
  10240. * @static
  10241. * @memberOf _
  10242. * @since 4.0.0
  10243. * @category Lang
  10244. * @param {*} value The value to check.
  10245. * @returns {boolean} Returns `true` if `value` is an integer, else `false`.
  10246. * @example
  10247. *
  10248. * _.isInteger(3);
  10249. * // => true
  10250. *
  10251. * _.isInteger(Number.MIN_VALUE);
  10252. * // => false
  10253. *
  10254. * _.isInteger(Infinity);
  10255. * // => false
  10256. *
  10257. * _.isInteger('3');
  10258. * // => false
  10259. */
  10260. function isInteger(value) {
  10261. return typeof value == 'number' && value == toInteger(value);
  10262. }
  10263. /**
  10264. * Checks if `value` is a valid array-like length.
  10265. *
  10266. * **Note:** This function is loosely based on
  10267. * [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  10268. *
  10269. * @static
  10270. * @memberOf _
  10271. * @since 4.0.0
  10272. * @category Lang
  10273. * @param {*} value The value to check.
  10274. * @returns {boolean} Returns `true` if `value` is a valid length,
  10275. * else `false`.
  10276. * @example
  10277. *
  10278. * _.isLength(3);
  10279. * // => true
  10280. *
  10281. * _.isLength(Number.MIN_VALUE);
  10282. * // => false
  10283. *
  10284. * _.isLength(Infinity);
  10285. * // => false
  10286. *
  10287. * _.isLength('3');
  10288. * // => false
  10289. */
  10290. function isLength(value) {
  10291. return typeof value == 'number' &&
  10292. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  10293. }
  10294. /**
  10295. * Checks if `value` is the
  10296. * [language type](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
  10297. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  10298. *
  10299. * @static
  10300. * @memberOf _
  10301. * @since 0.1.0
  10302. * @category Lang
  10303. * @param {*} value The value to check.
  10304. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  10305. * @example
  10306. *
  10307. * _.isObject({});
  10308. * // => true
  10309. *
  10310. * _.isObject([1, 2, 3]);
  10311. * // => true
  10312. *
  10313. * _.isObject(_.noop);
  10314. * // => true
  10315. *
  10316. * _.isObject(null);
  10317. * // => false
  10318. */
  10319. function isObject(value) {
  10320. var type = typeof value;
  10321. return !!value && (type == 'object' || type == 'function');
  10322. }
  10323. /**
  10324. * Checks if `value` is object-like. A value is object-like if it's not `null`
  10325. * and has a `typeof` result of "object".
  10326. *
  10327. * @static
  10328. * @memberOf _
  10329. * @since 4.0.0
  10330. * @category Lang
  10331. * @param {*} value The value to check.
  10332. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  10333. * @example
  10334. *
  10335. * _.isObjectLike({});
  10336. * // => true
  10337. *
  10338. * _.isObjectLike([1, 2, 3]);
  10339. * // => true
  10340. *
  10341. * _.isObjectLike(_.noop);
  10342. * // => false
  10343. *
  10344. * _.isObjectLike(null);
  10345. * // => false
  10346. */
  10347. function isObjectLike(value) {
  10348. return !!value && typeof value == 'object';
  10349. }
  10350. /**
  10351. * Checks if `value` is classified as a `Map` object.
  10352. *
  10353. * @static
  10354. * @memberOf _
  10355. * @since 4.3.0
  10356. * @category Lang
  10357. * @param {*} value The value to check.
  10358. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10359. * else `false`.
  10360. * @example
  10361. *
  10362. * _.isMap(new Map);
  10363. * // => true
  10364. *
  10365. * _.isMap(new WeakMap);
  10366. * // => false
  10367. */
  10368. function isMap(value) {
  10369. return isObjectLike(value) && getTag(value) == mapTag;
  10370. }
  10371. /**
  10372. * Performs a partial deep comparison between `object` and `source` to
  10373. * determine if `object` contains equivalent property values. This method is
  10374. * equivalent to a `_.matches` function when `source` is partially applied.
  10375. *
  10376. * **Note:** This method supports comparing the same values as `_.isEqual`.
  10377. *
  10378. * @static
  10379. * @memberOf _
  10380. * @since 3.0.0
  10381. * @category Lang
  10382. * @param {Object} object The object to inspect.
  10383. * @param {Object} source The object of property values to match.
  10384. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  10385. * @example
  10386. *
  10387. * var object = { 'user': 'fred', 'age': 40 };
  10388. *
  10389. * _.isMatch(object, { 'age': 40 });
  10390. * // => true
  10391. *
  10392. * _.isMatch(object, { 'age': 36 });
  10393. * // => false
  10394. */
  10395. function isMatch(object, source) {
  10396. return object === source || baseIsMatch(object, source, getMatchData(source));
  10397. }
  10398. /**
  10399. * This method is like `_.isMatch` except that it accepts `customizer` which
  10400. * is invoked to compare values. If `customizer` returns `undefined`, comparisons
  10401. * are handled by the method instead. The `customizer` is invoked with five
  10402. * arguments: (objValue, srcValue, index|key, object, source).
  10403. *
  10404. * @static
  10405. * @memberOf _
  10406. * @since 4.0.0
  10407. * @category Lang
  10408. * @param {Object} object The object to inspect.
  10409. * @param {Object} source The object of property values to match.
  10410. * @param {Function} [customizer] The function to customize comparisons.
  10411. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
  10412. * @example
  10413. *
  10414. * function isGreeting(value) {
  10415. * return /^h(?:i|ello)$/.test(value);
  10416. * }
  10417. *
  10418. * function customizer(objValue, srcValue) {
  10419. * if (isGreeting(objValue) && isGreeting(srcValue)) {
  10420. * return true;
  10421. * }
  10422. * }
  10423. *
  10424. * var object = { 'greeting': 'hello' };
  10425. * var source = { 'greeting': 'hi' };
  10426. *
  10427. * _.isMatchWith(object, source, customizer);
  10428. * // => true
  10429. */
  10430. function isMatchWith(object, source, customizer) {
  10431. customizer = typeof customizer == 'function' ? customizer : undefined;
  10432. return baseIsMatch(object, source, getMatchData(source), customizer);
  10433. }
  10434. /**
  10435. * Checks if `value` is `NaN`.
  10436. *
  10437. * **Note:** This method is based on
  10438. * [`Number.isNaN`](https://mdn.io/Number/isNaN) and is not the same as
  10439. * global [`isNaN`](https://mdn.io/isNaN) which returns `true` for
  10440. * `undefined` and other non-number values.
  10441. *
  10442. * @static
  10443. * @memberOf _
  10444. * @since 0.1.0
  10445. * @category Lang
  10446. * @param {*} value The value to check.
  10447. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  10448. * @example
  10449. *
  10450. * _.isNaN(NaN);
  10451. * // => true
  10452. *
  10453. * _.isNaN(new Number(NaN));
  10454. * // => true
  10455. *
  10456. * isNaN(undefined);
  10457. * // => true
  10458. *
  10459. * _.isNaN(undefined);
  10460. * // => false
  10461. */
  10462. function isNaN(value) {
  10463. // An `NaN` primitive is the only value that is not equal to itself.
  10464. // Perform the `toStringTag` check first to avoid errors with some
  10465. // ActiveX objects in IE.
  10466. return isNumber(value) && value != +value;
  10467. }
  10468. /**
  10469. * Checks if `value` is a pristine native function.
  10470. *
  10471. * **Note:** This method can't reliably detect native functions in the
  10472. * presence of the `core-js` package because `core-js` circumvents this kind
  10473. * of detection. Despite multiple requests, the `core-js` maintainer has made
  10474. * it clear: any attempt to fix the detection will be obstructed. As a result,
  10475. * we're left with little choice but to throw an error. Unfortunately, this
  10476. * also affects packages, like [babel-polyfill](https://www.npmjs.com/package/babel-polyfill),
  10477. * which rely on `core-js`.
  10478. *
  10479. * @static
  10480. * @memberOf _
  10481. * @since 3.0.0
  10482. * @category Lang
  10483. * @param {*} value The value to check.
  10484. * @returns {boolean} Returns `true` if `value` is a native function,
  10485. * else `false`.
  10486. * @example
  10487. *
  10488. * _.isNative(Array.prototype.push);
  10489. * // => true
  10490. *
  10491. * _.isNative(_);
  10492. * // => false
  10493. */
  10494. function isNative(value) {
  10495. if (isMaskable(value)) {
  10496. throw new Error('This method is not supported with `core-js`. Try https://github.com/es-shims.');
  10497. }
  10498. return baseIsNative(value);
  10499. }
  10500. /**
  10501. * Checks if `value` is `null`.
  10502. *
  10503. * @static
  10504. * @memberOf _
  10505. * @since 0.1.0
  10506. * @category Lang
  10507. * @param {*} value The value to check.
  10508. * @returns {boolean} Returns `true` if `value` is `null`, else `false`.
  10509. * @example
  10510. *
  10511. * _.isNull(null);
  10512. * // => true
  10513. *
  10514. * _.isNull(void 0);
  10515. * // => false
  10516. */
  10517. function isNull(value) {
  10518. return value === null;
  10519. }
  10520. /**
  10521. * Checks if `value` is `null` or `undefined`.
  10522. *
  10523. * @static
  10524. * @memberOf _
  10525. * @since 4.0.0
  10526. * @category Lang
  10527. * @param {*} value The value to check.
  10528. * @returns {boolean} Returns `true` if `value` is nullish, else `false`.
  10529. * @example
  10530. *
  10531. * _.isNil(null);
  10532. * // => true
  10533. *
  10534. * _.isNil(void 0);
  10535. * // => true
  10536. *
  10537. * _.isNil(NaN);
  10538. * // => false
  10539. */
  10540. function isNil(value) {
  10541. return value == null;
  10542. }
  10543. /**
  10544. * Checks if `value` is classified as a `Number` primitive or object.
  10545. *
  10546. * **Note:** To exclude `Infinity`, `-Infinity`, and `NaN`, which are
  10547. * classified as numbers, use the `_.isFinite` method.
  10548. *
  10549. * @static
  10550. * @memberOf _
  10551. * @since 0.1.0
  10552. * @category Lang
  10553. * @param {*} value The value to check.
  10554. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10555. * else `false`.
  10556. * @example
  10557. *
  10558. * _.isNumber(3);
  10559. * // => true
  10560. *
  10561. * _.isNumber(Number.MIN_VALUE);
  10562. * // => true
  10563. *
  10564. * _.isNumber(Infinity);
  10565. * // => true
  10566. *
  10567. * _.isNumber('3');
  10568. * // => false
  10569. */
  10570. function isNumber(value) {
  10571. return typeof value == 'number' ||
  10572. (isObjectLike(value) && objectToString.call(value) == numberTag);
  10573. }
  10574. /**
  10575. * Checks if `value` is a plain object, that is, an object created by the
  10576. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  10577. *
  10578. * @static
  10579. * @memberOf _
  10580. * @since 0.8.0
  10581. * @category Lang
  10582. * @param {*} value The value to check.
  10583. * @returns {boolean} Returns `true` if `value` is a plain object,
  10584. * else `false`.
  10585. * @example
  10586. *
  10587. * function Foo() {
  10588. * this.a = 1;
  10589. * }
  10590. *
  10591. * _.isPlainObject(new Foo);
  10592. * // => false
  10593. *
  10594. * _.isPlainObject([1, 2, 3]);
  10595. * // => false
  10596. *
  10597. * _.isPlainObject({ 'x': 0, 'y': 0 });
  10598. * // => true
  10599. *
  10600. * _.isPlainObject(Object.create(null));
  10601. * // => true
  10602. */
  10603. function isPlainObject(value) {
  10604. if (!isObjectLike(value) ||
  10605. objectToString.call(value) != objectTag || isHostObject(value)) {
  10606. return false;
  10607. }
  10608. var proto = getPrototype(value);
  10609. if (proto === null) {
  10610. return true;
  10611. }
  10612. var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
  10613. return (typeof Ctor == 'function' &&
  10614. Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString);
  10615. }
  10616. /**
  10617. * Checks if `value` is classified as a `RegExp` object.
  10618. *
  10619. * @static
  10620. * @memberOf _
  10621. * @since 0.1.0
  10622. * @category Lang
  10623. * @param {*} value The value to check.
  10624. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10625. * else `false`.
  10626. * @example
  10627. *
  10628. * _.isRegExp(/abc/);
  10629. * // => true
  10630. *
  10631. * _.isRegExp('/abc/');
  10632. * // => false
  10633. */
  10634. function isRegExp(value) {
  10635. return isObject(value) && objectToString.call(value) == regexpTag;
  10636. }
  10637. /**
  10638. * Checks if `value` is a safe integer. An integer is safe if it's an IEEE-754
  10639. * double precision number which isn't the result of a rounded unsafe integer.
  10640. *
  10641. * **Note:** This method is based on
  10642. * [`Number.isSafeInteger`](https://mdn.io/Number/isSafeInteger).
  10643. *
  10644. * @static
  10645. * @memberOf _
  10646. * @since 4.0.0
  10647. * @category Lang
  10648. * @param {*} value The value to check.
  10649. * @returns {boolean} Returns `true` if `value` is a safe integer,
  10650. * else `false`.
  10651. * @example
  10652. *
  10653. * _.isSafeInteger(3);
  10654. * // => true
  10655. *
  10656. * _.isSafeInteger(Number.MIN_VALUE);
  10657. * // => false
  10658. *
  10659. * _.isSafeInteger(Infinity);
  10660. * // => false
  10661. *
  10662. * _.isSafeInteger('3');
  10663. * // => false
  10664. */
  10665. function isSafeInteger(value) {
  10666. return isInteger(value) && value >= -MAX_SAFE_INTEGER && value <= MAX_SAFE_INTEGER;
  10667. }
  10668. /**
  10669. * Checks if `value` is classified as a `Set` object.
  10670. *
  10671. * @static
  10672. * @memberOf _
  10673. * @since 4.3.0
  10674. * @category Lang
  10675. * @param {*} value The value to check.
  10676. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10677. * else `false`.
  10678. * @example
  10679. *
  10680. * _.isSet(new Set);
  10681. * // => true
  10682. *
  10683. * _.isSet(new WeakSet);
  10684. * // => false
  10685. */
  10686. function isSet(value) {
  10687. return isObjectLike(value) && getTag(value) == setTag;
  10688. }
  10689. /**
  10690. * Checks if `value` is classified as a `String` primitive or object.
  10691. *
  10692. * @static
  10693. * @since 0.1.0
  10694. * @memberOf _
  10695. * @category Lang
  10696. * @param {*} value The value to check.
  10697. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10698. * else `false`.
  10699. * @example
  10700. *
  10701. * _.isString('abc');
  10702. * // => true
  10703. *
  10704. * _.isString(1);
  10705. * // => false
  10706. */
  10707. function isString(value) {
  10708. return typeof value == 'string' ||
  10709. (!isArray(value) && isObjectLike(value) && objectToString.call(value) == stringTag);
  10710. }
  10711. /**
  10712. * Checks if `value` is classified as a `Symbol` primitive or object.
  10713. *
  10714. * @static
  10715. * @memberOf _
  10716. * @since 4.0.0
  10717. * @category Lang
  10718. * @param {*} value The value to check.
  10719. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10720. * else `false`.
  10721. * @example
  10722. *
  10723. * _.isSymbol(Symbol.iterator);
  10724. * // => true
  10725. *
  10726. * _.isSymbol('abc');
  10727. * // => false
  10728. */
  10729. function isSymbol(value) {
  10730. return typeof value == 'symbol' ||
  10731. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  10732. }
  10733. /**
  10734. * Checks if `value` is classified as a typed array.
  10735. *
  10736. * @static
  10737. * @memberOf _
  10738. * @since 3.0.0
  10739. * @category Lang
  10740. * @param {*} value The value to check.
  10741. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10742. * else `false`.
  10743. * @example
  10744. *
  10745. * _.isTypedArray(new Uint8Array);
  10746. * // => true
  10747. *
  10748. * _.isTypedArray([]);
  10749. * // => false
  10750. */
  10751. function isTypedArray(value) {
  10752. return isObjectLike(value) &&
  10753. isLength(value.length) && !!typedArrayTags[objectToString.call(value)];
  10754. }
  10755. /**
  10756. * Checks if `value` is `undefined`.
  10757. *
  10758. * @static
  10759. * @since 0.1.0
  10760. * @memberOf _
  10761. * @category Lang
  10762. * @param {*} value The value to check.
  10763. * @returns {boolean} Returns `true` if `value` is `undefined`, else `false`.
  10764. * @example
  10765. *
  10766. * _.isUndefined(void 0);
  10767. * // => true
  10768. *
  10769. * _.isUndefined(null);
  10770. * // => false
  10771. */
  10772. function isUndefined(value) {
  10773. return value === undefined;
  10774. }
  10775. /**
  10776. * Checks if `value` is classified as a `WeakMap` object.
  10777. *
  10778. * @static
  10779. * @memberOf _
  10780. * @since 4.3.0
  10781. * @category Lang
  10782. * @param {*} value The value to check.
  10783. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10784. * else `false`.
  10785. * @example
  10786. *
  10787. * _.isWeakMap(new WeakMap);
  10788. * // => true
  10789. *
  10790. * _.isWeakMap(new Map);
  10791. * // => false
  10792. */
  10793. function isWeakMap(value) {
  10794. return isObjectLike(value) && getTag(value) == weakMapTag;
  10795. }
  10796. /**
  10797. * Checks if `value` is classified as a `WeakSet` object.
  10798. *
  10799. * @static
  10800. * @memberOf _
  10801. * @since 4.3.0
  10802. * @category Lang
  10803. * @param {*} value The value to check.
  10804. * @returns {boolean} Returns `true` if `value` is correctly classified,
  10805. * else `false`.
  10806. * @example
  10807. *
  10808. * _.isWeakSet(new WeakSet);
  10809. * // => true
  10810. *
  10811. * _.isWeakSet(new Set);
  10812. * // => false
  10813. */
  10814. function isWeakSet(value) {
  10815. return isObjectLike(value) && objectToString.call(value) == weakSetTag;
  10816. }
  10817. /**
  10818. * Checks if `value` is less than `other`.
  10819. *
  10820. * @static
  10821. * @memberOf _
  10822. * @since 3.9.0
  10823. * @category Lang
  10824. * @param {*} value The value to compare.
  10825. * @param {*} other The other value to compare.
  10826. * @returns {boolean} Returns `true` if `value` is less than `other`,
  10827. * else `false`.
  10828. * @see _.gt
  10829. * @example
  10830. *
  10831. * _.lt(1, 3);
  10832. * // => true
  10833. *
  10834. * _.lt(3, 3);
  10835. * // => false
  10836. *
  10837. * _.lt(3, 1);
  10838. * // => false
  10839. */
  10840. var lt = createRelationalOperation(baseLt);
  10841. /**
  10842. * Checks if `value` is less than or equal to `other`.
  10843. *
  10844. * @static
  10845. * @memberOf _
  10846. * @since 3.9.0
  10847. * @category Lang
  10848. * @param {*} value The value to compare.
  10849. * @param {*} other The other value to compare.
  10850. * @returns {boolean} Returns `true` if `value` is less than or equal to
  10851. * `other`, else `false`.
  10852. * @see _.gte
  10853. * @example
  10854. *
  10855. * _.lte(1, 3);
  10856. * // => true
  10857. *
  10858. * _.lte(3, 3);
  10859. * // => true
  10860. *
  10861. * _.lte(3, 1);
  10862. * // => false
  10863. */
  10864. var lte = createRelationalOperation(function(value, other) {
  10865. return value <= other;
  10866. });
  10867. /**
  10868. * Converts `value` to an array.
  10869. *
  10870. * @static
  10871. * @since 0.1.0
  10872. * @memberOf _
  10873. * @category Lang
  10874. * @param {*} value The value to convert.
  10875. * @returns {Array} Returns the converted array.
  10876. * @example
  10877. *
  10878. * _.toArray({ 'a': 1, 'b': 2 });
  10879. * // => [1, 2]
  10880. *
  10881. * _.toArray('abc');
  10882. * // => ['a', 'b', 'c']
  10883. *
  10884. * _.toArray(1);
  10885. * // => []
  10886. *
  10887. * _.toArray(null);
  10888. * // => []
  10889. */
  10890. function toArray(value) {
  10891. if (!value) {
  10892. return [];
  10893. }
  10894. if (isArrayLike(value)) {
  10895. return isString(value) ? stringToArray(value) : copyArray(value);
  10896. }
  10897. if (iteratorSymbol && value[iteratorSymbol]) {
  10898. return iteratorToArray(value[iteratorSymbol]());
  10899. }
  10900. var tag = getTag(value),
  10901. func = tag == mapTag ? mapToArray : (tag == setTag ? setToArray : values);
  10902. return func(value);
  10903. }
  10904. /**
  10905. * Converts `value` to a finite number.
  10906. *
  10907. * @static
  10908. * @memberOf _
  10909. * @since 4.12.0
  10910. * @category Lang
  10911. * @param {*} value The value to convert.
  10912. * @returns {number} Returns the converted number.
  10913. * @example
  10914. *
  10915. * _.toFinite(3.2);
  10916. * // => 3.2
  10917. *
  10918. * _.toFinite(Number.MIN_VALUE);
  10919. * // => 5e-324
  10920. *
  10921. * _.toFinite(Infinity);
  10922. * // => 1.7976931348623157e+308
  10923. *
  10924. * _.toFinite('3.2');
  10925. * // => 3.2
  10926. */
  10927. function toFinite(value) {
  10928. if (!value) {
  10929. return value === 0 ? value : 0;
  10930. }
  10931. value = toNumber(value);
  10932. if (value === INFINITY || value === -INFINITY) {
  10933. var sign = (value < 0 ? -1 : 1);
  10934. return sign * MAX_INTEGER;
  10935. }
  10936. return value === value ? value : 0;
  10937. }
  10938. /**
  10939. * Converts `value` to an integer.
  10940. *
  10941. * **Note:** This method is loosely based on
  10942. * [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
  10943. *
  10944. * @static
  10945. * @memberOf _
  10946. * @since 4.0.0
  10947. * @category Lang
  10948. * @param {*} value The value to convert.
  10949. * @returns {number} Returns the converted integer.
  10950. * @example
  10951. *
  10952. * _.toInteger(3.2);
  10953. * // => 3
  10954. *
  10955. * _.toInteger(Number.MIN_VALUE);
  10956. * // => 0
  10957. *
  10958. * _.toInteger(Infinity);
  10959. * // => 1.7976931348623157e+308
  10960. *
  10961. * _.toInteger('3.2');
  10962. * // => 3
  10963. */
  10964. function toInteger(value) {
  10965. var result = toFinite(value),
  10966. remainder = result % 1;
  10967. return result === result ? (remainder ? result - remainder : result) : 0;
  10968. }
  10969. /**
  10970. * Converts `value` to an integer suitable for use as the length of an
  10971. * array-like object.
  10972. *
  10973. * **Note:** This method is based on
  10974. * [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  10975. *
  10976. * @static
  10977. * @memberOf _
  10978. * @since 4.0.0
  10979. * @category Lang
  10980. * @param {*} value The value to convert.
  10981. * @returns {number} Returns the converted integer.
  10982. * @example
  10983. *
  10984. * _.toLength(3.2);
  10985. * // => 3
  10986. *
  10987. * _.toLength(Number.MIN_VALUE);
  10988. * // => 0
  10989. *
  10990. * _.toLength(Infinity);
  10991. * // => 4294967295
  10992. *
  10993. * _.toLength('3.2');
  10994. * // => 3
  10995. */
  10996. function toLength(value) {
  10997. return value ? baseClamp(toInteger(value), 0, MAX_ARRAY_LENGTH) : 0;
  10998. }
  10999. /**
  11000. * Converts `value` to a number.
  11001. *
  11002. * @static
  11003. * @memberOf _
  11004. * @since 4.0.0
  11005. * @category Lang
  11006. * @param {*} value The value to process.
  11007. * @returns {number} Returns the number.
  11008. * @example
  11009. *
  11010. * _.toNumber(3.2);
  11011. * // => 3.2
  11012. *
  11013. * _.toNumber(Number.MIN_VALUE);
  11014. * // => 5e-324
  11015. *
  11016. * _.toNumber(Infinity);
  11017. * // => Infinity
  11018. *
  11019. * _.toNumber('3.2');
  11020. * // => 3.2
  11021. */
  11022. function toNumber(value) {
  11023. if (typeof value == 'number') {
  11024. return value;
  11025. }
  11026. if (isSymbol(value)) {
  11027. return NAN;
  11028. }
  11029. if (isObject(value)) {
  11030. var other = isFunction(value.valueOf) ? value.valueOf() : value;
  11031. value = isObject(other) ? (other + '') : other;
  11032. }
  11033. if (typeof value != 'string') {
  11034. return value === 0 ? value : +value;
  11035. }
  11036. value = value.replace(reTrim, '');
  11037. var isBinary = reIsBinary.test(value);
  11038. return (isBinary || reIsOctal.test(value))
  11039. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  11040. : (reIsBadHex.test(value) ? NAN : +value);
  11041. }
  11042. /**
  11043. * Converts `value` to a plain object flattening inherited enumerable string
  11044. * keyed properties of `value` to own properties of the plain object.
  11045. *
  11046. * @static
  11047. * @memberOf _
  11048. * @since 3.0.0
  11049. * @category Lang
  11050. * @param {*} value The value to convert.
  11051. * @returns {Object} Returns the converted plain object.
  11052. * @example
  11053. *
  11054. * function Foo() {
  11055. * this.b = 2;
  11056. * }
  11057. *
  11058. * Foo.prototype.c = 3;
  11059. *
  11060. * _.assign({ 'a': 1 }, new Foo);
  11061. * // => { 'a': 1, 'b': 2 }
  11062. *
  11063. * _.assign({ 'a': 1 }, _.toPlainObject(new Foo));
  11064. * // => { 'a': 1, 'b': 2, 'c': 3 }
  11065. */
  11066. function toPlainObject(value) {
  11067. return copyObject(value, keysIn(value));
  11068. }
  11069. /**
  11070. * Converts `value` to a safe integer. A safe integer can be compared and
  11071. * represented correctly.
  11072. *
  11073. * @static
  11074. * @memberOf _
  11075. * @since 4.0.0
  11076. * @category Lang
  11077. * @param {*} value The value to convert.
  11078. * @returns {number} Returns the converted integer.
  11079. * @example
  11080. *
  11081. * _.toSafeInteger(3.2);
  11082. * // => 3
  11083. *
  11084. * _.toSafeInteger(Number.MIN_VALUE);
  11085. * // => 0
  11086. *
  11087. * _.toSafeInteger(Infinity);
  11088. * // => 9007199254740991
  11089. *
  11090. * _.toSafeInteger('3.2');
  11091. * // => 3
  11092. */
  11093. function toSafeInteger(value) {
  11094. return baseClamp(toInteger(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
  11095. }
  11096. /**
  11097. * Converts `value` to a string. An empty string is returned for `null`
  11098. * and `undefined` values. The sign of `-0` is preserved.
  11099. *
  11100. * @static
  11101. * @memberOf _
  11102. * @since 4.0.0
  11103. * @category Lang
  11104. * @param {*} value The value to process.
  11105. * @returns {string} Returns the string.
  11106. * @example
  11107. *
  11108. * _.toString(null);
  11109. * // => ''
  11110. *
  11111. * _.toString(-0);
  11112. * // => '-0'
  11113. *
  11114. * _.toString([1, 2, 3]);
  11115. * // => '1,2,3'
  11116. */
  11117. function toString(value) {
  11118. return value == null ? '' : baseToString(value);
  11119. }
  11120. /*------------------------------------------------------------------------*/
  11121. /**
  11122. * Assigns own enumerable string keyed properties of source objects to the
  11123. * destination object. Source objects are applied from left to right.
  11124. * Subsequent sources overwrite property assignments of previous sources.
  11125. *
  11126. * **Note:** This method mutates `object` and is loosely based on
  11127. * [`Object.assign`](https://mdn.io/Object/assign).
  11128. *
  11129. * @static
  11130. * @memberOf _
  11131. * @since 0.10.0
  11132. * @category Object
  11133. * @param {Object} object The destination object.
  11134. * @param {...Object} [sources] The source objects.
  11135. * @returns {Object} Returns `object`.
  11136. * @see _.assignIn
  11137. * @example
  11138. *
  11139. * function Foo() {
  11140. * this.c = 3;
  11141. * }
  11142. *
  11143. * function Bar() {
  11144. * this.e = 5;
  11145. * }
  11146. *
  11147. * Foo.prototype.d = 4;
  11148. * Bar.prototype.f = 6;
  11149. *
  11150. * _.assign({ 'a': 1 }, new Foo, new Bar);
  11151. * // => { 'a': 1, 'c': 3, 'e': 5 }
  11152. */
  11153. var assign = createAssigner(function(object, source) {
  11154. if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
  11155. copyObject(source, keys(source), object);
  11156. return;
  11157. }
  11158. for (var key in source) {
  11159. if (hasOwnProperty.call(source, key)) {
  11160. assignValue(object, key, source[key]);
  11161. }
  11162. }
  11163. });
  11164. /**
  11165. * This method is like `_.assign` except that it iterates over own and
  11166. * inherited source properties.
  11167. *
  11168. * **Note:** This method mutates `object`.
  11169. *
  11170. * @static
  11171. * @memberOf _
  11172. * @since 4.0.0
  11173. * @alias extend
  11174. * @category Object
  11175. * @param {Object} object The destination object.
  11176. * @param {...Object} [sources] The source objects.
  11177. * @returns {Object} Returns `object`.
  11178. * @see _.assign
  11179. * @example
  11180. *
  11181. * function Foo() {
  11182. * this.b = 2;
  11183. * }
  11184. *
  11185. * function Bar() {
  11186. * this.d = 4;
  11187. * }
  11188. *
  11189. * Foo.prototype.c = 3;
  11190. * Bar.prototype.e = 5;
  11191. *
  11192. * _.assignIn({ 'a': 1 }, new Foo, new Bar);
  11193. * // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 }
  11194. */
  11195. var assignIn = createAssigner(function(object, source) {
  11196. if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
  11197. copyObject(source, keysIn(source), object);
  11198. return;
  11199. }
  11200. for (var key in source) {
  11201. assignValue(object, key, source[key]);
  11202. }
  11203. });
  11204. /**
  11205. * This method is like `_.assignIn` except that it accepts `customizer`
  11206. * which is invoked to produce the assigned values. If `customizer` returns
  11207. * `undefined`, assignment is handled by the method instead. The `customizer`
  11208. * is invoked with five arguments: (objValue, srcValue, key, object, source).
  11209. *
  11210. * **Note:** This method mutates `object`.
  11211. *
  11212. * @static
  11213. * @memberOf _
  11214. * @since 4.0.0
  11215. * @alias extendWith
  11216. * @category Object
  11217. * @param {Object} object The destination object.
  11218. * @param {...Object} sources The source objects.
  11219. * @param {Function} [customizer] The function to customize assigned values.
  11220. * @returns {Object} Returns `object`.
  11221. * @see _.assignWith
  11222. * @example
  11223. *
  11224. * function customizer(objValue, srcValue) {
  11225. * return _.isUndefined(objValue) ? srcValue : objValue;
  11226. * }
  11227. *
  11228. * var defaults = _.partialRight(_.assignInWith, customizer);
  11229. *
  11230. * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  11231. * // => { 'a': 1, 'b': 2 }
  11232. */
  11233. var assignInWith = createAssigner(function(object, source, srcIndex, customizer) {
  11234. copyObject(source, keysIn(source), object, customizer);
  11235. });
  11236. /**
  11237. * This method is like `_.assign` except that it accepts `customizer`
  11238. * which is invoked to produce the assigned values. If `customizer` returns
  11239. * `undefined`, assignment is handled by the method instead. The `customizer`
  11240. * is invoked with five arguments: (objValue, srcValue, key, object, source).
  11241. *
  11242. * **Note:** This method mutates `object`.
  11243. *
  11244. * @static
  11245. * @memberOf _
  11246. * @since 4.0.0
  11247. * @category Object
  11248. * @param {Object} object The destination object.
  11249. * @param {...Object} sources The source objects.
  11250. * @param {Function} [customizer] The function to customize assigned values.
  11251. * @returns {Object} Returns `object`.
  11252. * @see _.assignInWith
  11253. * @example
  11254. *
  11255. * function customizer(objValue, srcValue) {
  11256. * return _.isUndefined(objValue) ? srcValue : objValue;
  11257. * }
  11258. *
  11259. * var defaults = _.partialRight(_.assignWith, customizer);
  11260. *
  11261. * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  11262. * // => { 'a': 1, 'b': 2 }
  11263. */
  11264. var assignWith = createAssigner(function(object, source, srcIndex, customizer) {
  11265. copyObject(source, keys(source), object, customizer);
  11266. });
  11267. /**
  11268. * Creates an array of values corresponding to `paths` of `object`.
  11269. *
  11270. * @static
  11271. * @memberOf _
  11272. * @since 1.0.0
  11273. * @category Object
  11274. * @param {Object} object The object to iterate over.
  11275. * @param {...(string|string[])} [paths] The property paths of elements to pick.
  11276. * @returns {Array} Returns the picked values.
  11277. * @example
  11278. *
  11279. * var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
  11280. *
  11281. * _.at(object, ['a[0].b.c', 'a[1]']);
  11282. * // => [3, 4]
  11283. */
  11284. var at = rest(function(object, paths) {
  11285. return baseAt(object, baseFlatten(paths, 1));
  11286. });
  11287. /**
  11288. * Creates an object that inherits from the `prototype` object. If a
  11289. * `properties` object is given, its own enumerable string keyed properties
  11290. * are assigned to the created object.
  11291. *
  11292. * @static
  11293. * @memberOf _
  11294. * @since 2.3.0
  11295. * @category Object
  11296. * @param {Object} prototype The object to inherit from.
  11297. * @param {Object} [properties] The properties to assign to the object.
  11298. * @returns {Object} Returns the new object.
  11299. * @example
  11300. *
  11301. * function Shape() {
  11302. * this.x = 0;
  11303. * this.y = 0;
  11304. * }
  11305. *
  11306. * function Circle() {
  11307. * Shape.call(this);
  11308. * }
  11309. *
  11310. * Circle.prototype = _.create(Shape.prototype, {
  11311. * 'constructor': Circle
  11312. * });
  11313. *
  11314. * var circle = new Circle;
  11315. * circle instanceof Circle;
  11316. * // => true
  11317. *
  11318. * circle instanceof Shape;
  11319. * // => true
  11320. */
  11321. function create(prototype, properties) {
  11322. var result = baseCreate(prototype);
  11323. return properties ? baseAssign(result, properties) : result;
  11324. }
  11325. /**
  11326. * Assigns own and inherited enumerable string keyed properties of source
  11327. * objects to the destination object for all destination properties that
  11328. * resolve to `undefined`. Source objects are applied from left to right.
  11329. * Once a property is set, additional values of the same property are ignored.
  11330. *
  11331. * **Note:** This method mutates `object`.
  11332. *
  11333. * @static
  11334. * @since 0.1.0
  11335. * @memberOf _
  11336. * @category Object
  11337. * @param {Object} object The destination object.
  11338. * @param {...Object} [sources] The source objects.
  11339. * @returns {Object} Returns `object`.
  11340. * @see _.defaultsDeep
  11341. * @example
  11342. *
  11343. * _.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  11344. * // => { 'user': 'barney', 'age': 36 }
  11345. */
  11346. var defaults = rest(function(args) {
  11347. args.push(undefined, assignInDefaults);
  11348. return apply(assignInWith, undefined, args);
  11349. });
  11350. /**
  11351. * This method is like `_.defaults` except that it recursively assigns
  11352. * default properties.
  11353. *
  11354. * **Note:** This method mutates `object`.
  11355. *
  11356. * @static
  11357. * @memberOf _
  11358. * @since 3.10.0
  11359. * @category Object
  11360. * @param {Object} object The destination object.
  11361. * @param {...Object} [sources] The source objects.
  11362. * @returns {Object} Returns `object`.
  11363. * @see _.defaults
  11364. * @example
  11365. *
  11366. * _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });
  11367. * // => { 'user': { 'name': 'barney', 'age': 36 } }
  11368. *
  11369. */
  11370. var defaultsDeep = rest(function(args) {
  11371. args.push(undefined, mergeDefaults);
  11372. return apply(mergeWith, undefined, args);
  11373. });
  11374. /**
  11375. * This method is like `_.find` except that it returns the key of the first
  11376. * element `predicate` returns truthy for instead of the element itself.
  11377. *
  11378. * @static
  11379. * @memberOf _
  11380. * @since 1.1.0
  11381. * @category Object
  11382. * @param {Object} object The object to search.
  11383. * @param {Array|Function|Object|string} [predicate=_.identity]
  11384. * The function invoked per iteration.
  11385. * @returns {string|undefined} Returns the key of the matched element,
  11386. * else `undefined`.
  11387. * @example
  11388. *
  11389. * var users = {
  11390. * 'barney': { 'age': 36, 'active': true },
  11391. * 'fred': { 'age': 40, 'active': false },
  11392. * 'pebbles': { 'age': 1, 'active': true }
  11393. * };
  11394. *
  11395. * _.findKey(users, function(o) { return o.age < 40; });
  11396. * // => 'barney' (iteration order is not guaranteed)
  11397. *
  11398. * // The `_.matches` iteratee shorthand.
  11399. * _.findKey(users, { 'age': 1, 'active': true });
  11400. * // => 'pebbles'
  11401. *
  11402. * // The `_.matchesProperty` iteratee shorthand.
  11403. * _.findKey(users, ['active', false]);
  11404. * // => 'fred'
  11405. *
  11406. * // The `_.property` iteratee shorthand.
  11407. * _.findKey(users, 'active');
  11408. * // => 'barney'
  11409. */
  11410. function findKey(object, predicate) {
  11411. return baseFindKey(object, getIteratee(predicate, 3), baseForOwn);
  11412. }
  11413. /**
  11414. * This method is like `_.findKey` except that it iterates over elements of
  11415. * a collection in the opposite order.
  11416. *
  11417. * @static
  11418. * @memberOf _
  11419. * @since 2.0.0
  11420. * @category Object
  11421. * @param {Object} object The object to search.
  11422. * @param {Array|Function|Object|string} [predicate=_.identity]
  11423. * The function invoked per iteration.
  11424. * @returns {string|undefined} Returns the key of the matched element,
  11425. * else `undefined`.
  11426. * @example
  11427. *
  11428. * var users = {
  11429. * 'barney': { 'age': 36, 'active': true },
  11430. * 'fred': { 'age': 40, 'active': false },
  11431. * 'pebbles': { 'age': 1, 'active': true }
  11432. * };
  11433. *
  11434. * _.findLastKey(users, function(o) { return o.age < 40; });
  11435. * // => returns 'pebbles' assuming `_.findKey` returns 'barney'
  11436. *
  11437. * // The `_.matches` iteratee shorthand.
  11438. * _.findLastKey(users, { 'age': 36, 'active': true });
  11439. * // => 'barney'
  11440. *
  11441. * // The `_.matchesProperty` iteratee shorthand.
  11442. * _.findLastKey(users, ['active', false]);
  11443. * // => 'fred'
  11444. *
  11445. * // The `_.property` iteratee shorthand.
  11446. * _.findLastKey(users, 'active');
  11447. * // => 'pebbles'
  11448. */
  11449. function findLastKey(object, predicate) {
  11450. return baseFindKey(object, getIteratee(predicate, 3), baseForOwnRight);
  11451. }
  11452. /**
  11453. * Iterates over own and inherited enumerable string keyed properties of an
  11454. * object and invokes `iteratee` for each property. The iteratee is invoked
  11455. * with three arguments: (value, key, object). Iteratee functions may exit
  11456. * iteration early by explicitly returning `false`.
  11457. *
  11458. * @static
  11459. * @memberOf _
  11460. * @since 0.3.0
  11461. * @category Object
  11462. * @param {Object} object The object to iterate over.
  11463. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  11464. * @returns {Object} Returns `object`.
  11465. * @see _.forInRight
  11466. * @example
  11467. *
  11468. * function Foo() {
  11469. * this.a = 1;
  11470. * this.b = 2;
  11471. * }
  11472. *
  11473. * Foo.prototype.c = 3;
  11474. *
  11475. * _.forIn(new Foo, function(value, key) {
  11476. * console.log(key);
  11477. * });
  11478. * // => Logs 'a', 'b', then 'c' (iteration order is not guaranteed).
  11479. */
  11480. function forIn(object, iteratee) {
  11481. return object == null
  11482. ? object
  11483. : baseFor(object, getIteratee(iteratee, 3), keysIn);
  11484. }
  11485. /**
  11486. * This method is like `_.forIn` except that it iterates over properties of
  11487. * `object` in the opposite order.
  11488. *
  11489. * @static
  11490. * @memberOf _
  11491. * @since 2.0.0
  11492. * @category Object
  11493. * @param {Object} object The object to iterate over.
  11494. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  11495. * @returns {Object} Returns `object`.
  11496. * @see _.forIn
  11497. * @example
  11498. *
  11499. * function Foo() {
  11500. * this.a = 1;
  11501. * this.b = 2;
  11502. * }
  11503. *
  11504. * Foo.prototype.c = 3;
  11505. *
  11506. * _.forInRight(new Foo, function(value, key) {
  11507. * console.log(key);
  11508. * });
  11509. * // => Logs 'c', 'b', then 'a' assuming `_.forIn` logs 'a', 'b', then 'c'.
  11510. */
  11511. function forInRight(object, iteratee) {
  11512. return object == null
  11513. ? object
  11514. : baseForRight(object, getIteratee(iteratee, 3), keysIn);
  11515. }
  11516. /**
  11517. * Iterates over own enumerable string keyed properties of an object and
  11518. * invokes `iteratee` for each property. The iteratee is invoked with three
  11519. * arguments: (value, key, object). Iteratee functions may exit iteration
  11520. * early by explicitly returning `false`.
  11521. *
  11522. * @static
  11523. * @memberOf _
  11524. * @since 0.3.0
  11525. * @category Object
  11526. * @param {Object} object The object to iterate over.
  11527. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  11528. * @returns {Object} Returns `object`.
  11529. * @see _.forOwnRight
  11530. * @example
  11531. *
  11532. * function Foo() {
  11533. * this.a = 1;
  11534. * this.b = 2;
  11535. * }
  11536. *
  11537. * Foo.prototype.c = 3;
  11538. *
  11539. * _.forOwn(new Foo, function(value, key) {
  11540. * console.log(key);
  11541. * });
  11542. * // => Logs 'a' then 'b' (iteration order is not guaranteed).
  11543. */
  11544. function forOwn(object, iteratee) {
  11545. return object && baseForOwn(object, getIteratee(iteratee, 3));
  11546. }
  11547. /**
  11548. * This method is like `_.forOwn` except that it iterates over properties of
  11549. * `object` in the opposite order.
  11550. *
  11551. * @static
  11552. * @memberOf _
  11553. * @since 2.0.0
  11554. * @category Object
  11555. * @param {Object} object The object to iterate over.
  11556. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  11557. * @returns {Object} Returns `object`.
  11558. * @see _.forOwn
  11559. * @example
  11560. *
  11561. * function Foo() {
  11562. * this.a = 1;
  11563. * this.b = 2;
  11564. * }
  11565. *
  11566. * Foo.prototype.c = 3;
  11567. *
  11568. * _.forOwnRight(new Foo, function(value, key) {
  11569. * console.log(key);
  11570. * });
  11571. * // => Logs 'b' then 'a' assuming `_.forOwn` logs 'a' then 'b'.
  11572. */
  11573. function forOwnRight(object, iteratee) {
  11574. return object && baseForOwnRight(object, getIteratee(iteratee, 3));
  11575. }
  11576. /**
  11577. * Creates an array of function property names from own enumerable properties
  11578. * of `object`.
  11579. *
  11580. * @static
  11581. * @since 0.1.0
  11582. * @memberOf _
  11583. * @category Object
  11584. * @param {Object} object The object to inspect.
  11585. * @returns {Array} Returns the function names.
  11586. * @see _.functionsIn
  11587. * @example
  11588. *
  11589. * function Foo() {
  11590. * this.a = _.constant('a');
  11591. * this.b = _.constant('b');
  11592. * }
  11593. *
  11594. * Foo.prototype.c = _.constant('c');
  11595. *
  11596. * _.functions(new Foo);
  11597. * // => ['a', 'b']
  11598. */
  11599. function functions(object) {
  11600. return object == null ? [] : baseFunctions(object, keys(object));
  11601. }
  11602. /**
  11603. * Creates an array of function property names from own and inherited
  11604. * enumerable properties of `object`.
  11605. *
  11606. * @static
  11607. * @memberOf _
  11608. * @since 4.0.0
  11609. * @category Object
  11610. * @param {Object} object The object to inspect.
  11611. * @returns {Array} Returns the function names.
  11612. * @see _.functions
  11613. * @example
  11614. *
  11615. * function Foo() {
  11616. * this.a = _.constant('a');
  11617. * this.b = _.constant('b');
  11618. * }
  11619. *
  11620. * Foo.prototype.c = _.constant('c');
  11621. *
  11622. * _.functionsIn(new Foo);
  11623. * // => ['a', 'b', 'c']
  11624. */
  11625. function functionsIn(object) {
  11626. return object == null ? [] : baseFunctions(object, keysIn(object));
  11627. }
  11628. /**
  11629. * Gets the value at `path` of `object`. If the resolved value is
  11630. * `undefined`, the `defaultValue` is used in its place.
  11631. *
  11632. * @static
  11633. * @memberOf _
  11634. * @since 3.7.0
  11635. * @category Object
  11636. * @param {Object} object The object to query.
  11637. * @param {Array|string} path The path of the property to get.
  11638. * @param {*} [defaultValue] The value returned for `undefined` resolved values.
  11639. * @returns {*} Returns the resolved value.
  11640. * @example
  11641. *
  11642. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  11643. *
  11644. * _.get(object, 'a[0].b.c');
  11645. * // => 3
  11646. *
  11647. * _.get(object, ['a', '0', 'b', 'c']);
  11648. * // => 3
  11649. *
  11650. * _.get(object, 'a.b.c', 'default');
  11651. * // => 'default'
  11652. */
  11653. function get(object, path, defaultValue) {
  11654. var result = object == null ? undefined : baseGet(object, path);
  11655. return result === undefined ? defaultValue : result;
  11656. }
  11657. /**
  11658. * Checks if `path` is a direct property of `object`.
  11659. *
  11660. * @static
  11661. * @since 0.1.0
  11662. * @memberOf _
  11663. * @category Object
  11664. * @param {Object} object The object to query.
  11665. * @param {Array|string} path The path to check.
  11666. * @returns {boolean} Returns `true` if `path` exists, else `false`.
  11667. * @example
  11668. *
  11669. * var object = { 'a': { 'b': 2 } };
  11670. * var other = _.create({ 'a': _.create({ 'b': 2 }) });
  11671. *
  11672. * _.has(object, 'a');
  11673. * // => true
  11674. *
  11675. * _.has(object, 'a.b');
  11676. * // => true
  11677. *
  11678. * _.has(object, ['a', 'b']);
  11679. * // => true
  11680. *
  11681. * _.has(other, 'a');
  11682. * // => false
  11683. */
  11684. function has(object, path) {
  11685. return object != null && hasPath(object, path, baseHas);
  11686. }
  11687. /**
  11688. * Checks if `path` is a direct or inherited property of `object`.
  11689. *
  11690. * @static
  11691. * @memberOf _
  11692. * @since 4.0.0
  11693. * @category Object
  11694. * @param {Object} object The object to query.
  11695. * @param {Array|string} path The path to check.
  11696. * @returns {boolean} Returns `true` if `path` exists, else `false`.
  11697. * @example
  11698. *
  11699. * var object = _.create({ 'a': _.create({ 'b': 2 }) });
  11700. *
  11701. * _.hasIn(object, 'a');
  11702. * // => true
  11703. *
  11704. * _.hasIn(object, 'a.b');
  11705. * // => true
  11706. *
  11707. * _.hasIn(object, ['a', 'b']);
  11708. * // => true
  11709. *
  11710. * _.hasIn(object, 'b');
  11711. * // => false
  11712. */
  11713. function hasIn(object, path) {
  11714. return object != null && hasPath(object, path, baseHasIn);
  11715. }
  11716. /**
  11717. * Creates an object composed of the inverted keys and values of `object`.
  11718. * If `object` contains duplicate values, subsequent values overwrite
  11719. * property assignments of previous values.
  11720. *
  11721. * @static
  11722. * @memberOf _
  11723. * @since 0.7.0
  11724. * @category Object
  11725. * @param {Object} object The object to invert.
  11726. * @returns {Object} Returns the new inverted object.
  11727. * @example
  11728. *
  11729. * var object = { 'a': 1, 'b': 2, 'c': 1 };
  11730. *
  11731. * _.invert(object);
  11732. * // => { '1': 'c', '2': 'b' }
  11733. */
  11734. var invert = createInverter(function(result, value, key) {
  11735. result[value] = key;
  11736. }, constant(identity));
  11737. /**
  11738. * This method is like `_.invert` except that the inverted object is generated
  11739. * from the results of running each element of `object` thru `iteratee`. The
  11740. * corresponding inverted value of each inverted key is an array of keys
  11741. * responsible for generating the inverted value. The iteratee is invoked
  11742. * with one argument: (value).
  11743. *
  11744. * @static
  11745. * @memberOf _
  11746. * @since 4.1.0
  11747. * @category Object
  11748. * @param {Object} object The object to invert.
  11749. * @param {Array|Function|Object|string} [iteratee=_.identity]
  11750. * The iteratee invoked per element.
  11751. * @returns {Object} Returns the new inverted object.
  11752. * @example
  11753. *
  11754. * var object = { 'a': 1, 'b': 2, 'c': 1 };
  11755. *
  11756. * _.invertBy(object);
  11757. * // => { '1': ['a', 'c'], '2': ['b'] }
  11758. *
  11759. * _.invertBy(object, function(value) {
  11760. * return 'group' + value;
  11761. * });
  11762. * // => { 'group1': ['a', 'c'], 'group2': ['b'] }
  11763. */
  11764. var invertBy = createInverter(function(result, value, key) {
  11765. if (hasOwnProperty.call(result, value)) {
  11766. result[value].push(key);
  11767. } else {
  11768. result[value] = [key];
  11769. }
  11770. }, getIteratee);
  11771. /**
  11772. * Invokes the method at `path` of `object`.
  11773. *
  11774. * @static
  11775. * @memberOf _
  11776. * @since 4.0.0
  11777. * @category Object
  11778. * @param {Object} object The object to query.
  11779. * @param {Array|string} path The path of the method to invoke.
  11780. * @param {...*} [args] The arguments to invoke the method with.
  11781. * @returns {*} Returns the result of the invoked method.
  11782. * @example
  11783. *
  11784. * var object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };
  11785. *
  11786. * _.invoke(object, 'a[0].b.c.slice', 1, 3);
  11787. * // => [2, 3]
  11788. */
  11789. var invoke = rest(baseInvoke);
  11790. /**
  11791. * Creates an array of the own enumerable property names of `object`.
  11792. *
  11793. * **Note:** Non-object values are coerced to objects. See the
  11794. * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
  11795. * for more details.
  11796. *
  11797. * @static
  11798. * @since 0.1.0
  11799. * @memberOf _
  11800. * @category Object
  11801. * @param {Object} object The object to query.
  11802. * @returns {Array} Returns the array of property names.
  11803. * @example
  11804. *
  11805. * function Foo() {
  11806. * this.a = 1;
  11807. * this.b = 2;
  11808. * }
  11809. *
  11810. * Foo.prototype.c = 3;
  11811. *
  11812. * _.keys(new Foo);
  11813. * // => ['a', 'b'] (iteration order is not guaranteed)
  11814. *
  11815. * _.keys('hi');
  11816. * // => ['0', '1']
  11817. */
  11818. function keys(object) {
  11819. var isProto = isPrototype(object);
  11820. if (!(isProto || isArrayLike(object))) {
  11821. return baseKeys(object);
  11822. }
  11823. var indexes = indexKeys(object),
  11824. skipIndexes = !!indexes,
  11825. result = indexes || [],
  11826. length = result.length;
  11827. for (var key in object) {
  11828. if (baseHas(object, key) &&
  11829. !(skipIndexes && (key == 'length' || isIndex(key, length))) &&
  11830. !(isProto && key == 'constructor')) {
  11831. result.push(key);
  11832. }
  11833. }
  11834. return result;
  11835. }
  11836. /**
  11837. * Creates an array of the own and inherited enumerable property names of `object`.
  11838. *
  11839. * **Note:** Non-object values are coerced to objects.
  11840. *
  11841. * @static
  11842. * @memberOf _
  11843. * @since 3.0.0
  11844. * @category Object
  11845. * @param {Object} object The object to query.
  11846. * @returns {Array} Returns the array of property names.
  11847. * @example
  11848. *
  11849. * function Foo() {
  11850. * this.a = 1;
  11851. * this.b = 2;
  11852. * }
  11853. *
  11854. * Foo.prototype.c = 3;
  11855. *
  11856. * _.keysIn(new Foo);
  11857. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  11858. */
  11859. function keysIn(object) {
  11860. var index = -1,
  11861. isProto = isPrototype(object),
  11862. props = baseKeysIn(object),
  11863. propsLength = props.length,
  11864. indexes = indexKeys(object),
  11865. skipIndexes = !!indexes,
  11866. result = indexes || [],
  11867. length = result.length;
  11868. while (++index < propsLength) {
  11869. var key = props[index];
  11870. if (!(skipIndexes && (key == 'length' || isIndex(key, length))) &&
  11871. !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  11872. result.push(key);
  11873. }
  11874. }
  11875. return result;
  11876. }
  11877. /**
  11878. * The opposite of `_.mapValues`; this method creates an object with the
  11879. * same values as `object` and keys generated by running each own enumerable
  11880. * string keyed property of `object` thru `iteratee`. The iteratee is invoked
  11881. * with three arguments: (value, key, object).
  11882. *
  11883. * @static
  11884. * @memberOf _
  11885. * @since 3.8.0
  11886. * @category Object
  11887. * @param {Object} object The object to iterate over.
  11888. * @param {Array|Function|Object|string} [iteratee=_.identity]
  11889. * The function invoked per iteration.
  11890. * @returns {Object} Returns the new mapped object.
  11891. * @see _.mapValues
  11892. * @example
  11893. *
  11894. * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  11895. * return key + value;
  11896. * });
  11897. * // => { 'a1': 1, 'b2': 2 }
  11898. */
  11899. function mapKeys(object, iteratee) {
  11900. var result = {};
  11901. iteratee = getIteratee(iteratee, 3);
  11902. baseForOwn(object, function(value, key, object) {
  11903. result[iteratee(value, key, object)] = value;
  11904. });
  11905. return result;
  11906. }
  11907. /**
  11908. * Creates an object with the same keys as `object` and values generated
  11909. * by running each own enumerable string keyed property of `object` thru
  11910. * `iteratee`. The iteratee is invoked with three arguments:
  11911. * (value, key, object).
  11912. *
  11913. * @static
  11914. * @memberOf _
  11915. * @since 2.4.0
  11916. * @category Object
  11917. * @param {Object} object The object to iterate over.
  11918. * @param {Array|Function|Object|string} [iteratee=_.identity]
  11919. * The function invoked per iteration.
  11920. * @returns {Object} Returns the new mapped object.
  11921. * @see _.mapKeys
  11922. * @example
  11923. *
  11924. * var users = {
  11925. * 'fred': { 'user': 'fred', 'age': 40 },
  11926. * 'pebbles': { 'user': 'pebbles', 'age': 1 }
  11927. * };
  11928. *
  11929. * _.mapValues(users, function(o) { return o.age; });
  11930. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  11931. *
  11932. * // The `_.property` iteratee shorthand.
  11933. * _.mapValues(users, 'age');
  11934. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  11935. */
  11936. function mapValues(object, iteratee) {
  11937. var result = {};
  11938. iteratee = getIteratee(iteratee, 3);
  11939. baseForOwn(object, function(value, key, object) {
  11940. result[key] = iteratee(value, key, object);
  11941. });
  11942. return result;
  11943. }
  11944. /**
  11945. * This method is like `_.assign` except that it recursively merges own and
  11946. * inherited enumerable string keyed properties of source objects into the
  11947. * destination object. Source properties that resolve to `undefined` are
  11948. * skipped if a destination value exists. Array and plain object properties
  11949. * are merged recursively. Other objects and value types are overridden by
  11950. * assignment. Source objects are applied from left to right. Subsequent
  11951. * sources overwrite property assignments of previous sources.
  11952. *
  11953. * **Note:** This method mutates `object`.
  11954. *
  11955. * @static
  11956. * @memberOf _
  11957. * @since 0.5.0
  11958. * @category Object
  11959. * @param {Object} object The destination object.
  11960. * @param {...Object} [sources] The source objects.
  11961. * @returns {Object} Returns `object`.
  11962. * @example
  11963. *
  11964. * var users = {
  11965. * 'data': [{ 'user': 'barney' }, { 'user': 'fred' }]
  11966. * };
  11967. *
  11968. * var ages = {
  11969. * 'data': [{ 'age': 36 }, { 'age': 40 }]
  11970. * };
  11971. *
  11972. * _.merge(users, ages);
  11973. * // => { 'data': [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }] }
  11974. */
  11975. var merge = createAssigner(function(object, source, srcIndex) {
  11976. baseMerge(object, source, srcIndex);
  11977. });
  11978. /**
  11979. * This method is like `_.merge` except that it accepts `customizer` which
  11980. * is invoked to produce the merged values of the destination and source
  11981. * properties. If `customizer` returns `undefined`, merging is handled by the
  11982. * method instead. The `customizer` is invoked with seven arguments:
  11983. * (objValue, srcValue, key, object, source, stack).
  11984. *
  11985. * **Note:** This method mutates `object`.
  11986. *
  11987. * @static
  11988. * @memberOf _
  11989. * @since 4.0.0
  11990. * @category Object
  11991. * @param {Object} object The destination object.
  11992. * @param {...Object} sources The source objects.
  11993. * @param {Function} customizer The function to customize assigned values.
  11994. * @returns {Object} Returns `object`.
  11995. * @example
  11996. *
  11997. * function customizer(objValue, srcValue) {
  11998. * if (_.isArray(objValue)) {
  11999. * return objValue.concat(srcValue);
  12000. * }
  12001. * }
  12002. *
  12003. * var object = {
  12004. * 'fruits': ['apple'],
  12005. * 'vegetables': ['beet']
  12006. * };
  12007. *
  12008. * var other = {
  12009. * 'fruits': ['banana'],
  12010. * 'vegetables': ['carrot']
  12011. * };
  12012. *
  12013. * _.mergeWith(object, other, customizer);
  12014. * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }
  12015. */
  12016. var mergeWith = createAssigner(function(object, source, srcIndex, customizer) {
  12017. baseMerge(object, source, srcIndex, customizer);
  12018. });
  12019. /**
  12020. * The opposite of `_.pick`; this method creates an object composed of the
  12021. * own and inherited enumerable string keyed properties of `object` that are
  12022. * not omitted.
  12023. *
  12024. * @static
  12025. * @since 0.1.0
  12026. * @memberOf _
  12027. * @category Object
  12028. * @param {Object} object The source object.
  12029. * @param {...(string|string[])} [props] The property identifiers to omit.
  12030. * @returns {Object} Returns the new object.
  12031. * @example
  12032. *
  12033. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  12034. *
  12035. * _.omit(object, ['a', 'c']);
  12036. * // => { 'b': '2' }
  12037. */
  12038. var omit = rest(function(object, props) {
  12039. if (object == null) {
  12040. return {};
  12041. }
  12042. props = arrayMap(baseFlatten(props, 1), toKey);
  12043. return basePick(object, baseDifference(getAllKeysIn(object), props));
  12044. });
  12045. /**
  12046. * The opposite of `_.pickBy`; this method creates an object composed of
  12047. * the own and inherited enumerable string keyed properties of `object` that
  12048. * `predicate` doesn't return truthy for. The predicate is invoked with two
  12049. * arguments: (value, key).
  12050. *
  12051. * @static
  12052. * @memberOf _
  12053. * @since 4.0.0
  12054. * @category Object
  12055. * @param {Object} object The source object.
  12056. * @param {Array|Function|Object|string} [predicate=_.identity]
  12057. * The function invoked per property.
  12058. * @returns {Object} Returns the new object.
  12059. * @example
  12060. *
  12061. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  12062. *
  12063. * _.omitBy(object, _.isNumber);
  12064. * // => { 'b': '2' }
  12065. */
  12066. function omitBy(object, predicate) {
  12067. predicate = getIteratee(predicate);
  12068. return basePickBy(object, function(value, key) {
  12069. return !predicate(value, key);
  12070. });
  12071. }
  12072. /**
  12073. * Creates an object composed of the picked `object` properties.
  12074. *
  12075. * @static
  12076. * @since 0.1.0
  12077. * @memberOf _
  12078. * @category Object
  12079. * @param {Object} object The source object.
  12080. * @param {...(string|string[])} [props] The property identifiers to pick.
  12081. * @returns {Object} Returns the new object.
  12082. * @example
  12083. *
  12084. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  12085. *
  12086. * _.pick(object, ['a', 'c']);
  12087. * // => { 'a': 1, 'c': 3 }
  12088. */
  12089. var pick = rest(function(object, props) {
  12090. return object == null ? {} : basePick(object, arrayMap(baseFlatten(props, 1), toKey));
  12091. });
  12092. /**
  12093. * Creates an object composed of the `object` properties `predicate` returns
  12094. * truthy for. The predicate is invoked with two arguments: (value, key).
  12095. *
  12096. * @static
  12097. * @memberOf _
  12098. * @since 4.0.0
  12099. * @category Object
  12100. * @param {Object} object The source object.
  12101. * @param {Array|Function|Object|string} [predicate=_.identity]
  12102. * The function invoked per property.
  12103. * @returns {Object} Returns the new object.
  12104. * @example
  12105. *
  12106. * var object = { 'a': 1, 'b': '2', 'c': 3 };
  12107. *
  12108. * _.pickBy(object, _.isNumber);
  12109. * // => { 'a': 1, 'c': 3 }
  12110. */
  12111. function pickBy(object, predicate) {
  12112. return object == null ? {} : basePickBy(object, getIteratee(predicate));
  12113. }
  12114. /**
  12115. * This method is like `_.get` except that if the resolved value is a
  12116. * function it's invoked with the `this` binding of its parent object and
  12117. * its result is returned.
  12118. *
  12119. * @static
  12120. * @since 0.1.0
  12121. * @memberOf _
  12122. * @category Object
  12123. * @param {Object} object The object to query.
  12124. * @param {Array|string} path The path of the property to resolve.
  12125. * @param {*} [defaultValue] The value returned for `undefined` resolved values.
  12126. * @returns {*} Returns the resolved value.
  12127. * @example
  12128. *
  12129. * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
  12130. *
  12131. * _.result(object, 'a[0].b.c1');
  12132. * // => 3
  12133. *
  12134. * _.result(object, 'a[0].b.c2');
  12135. * // => 4
  12136. *
  12137. * _.result(object, 'a[0].b.c3', 'default');
  12138. * // => 'default'
  12139. *
  12140. * _.result(object, 'a[0].b.c3', _.constant('default'));
  12141. * // => 'default'
  12142. */
  12143. function result(object, path, defaultValue) {
  12144. path = isKey(path, object) ? [path] : castPath(path);
  12145. var index = -1,
  12146. length = path.length;
  12147. // Ensure the loop is entered when path is empty.
  12148. if (!length) {
  12149. object = undefined;
  12150. length = 1;
  12151. }
  12152. while (++index < length) {
  12153. var value = object == null ? undefined : object[toKey(path[index])];
  12154. if (value === undefined) {
  12155. index = length;
  12156. value = defaultValue;
  12157. }
  12158. object = isFunction(value) ? value.call(object) : value;
  12159. }
  12160. return object;
  12161. }
  12162. /**
  12163. * Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
  12164. * it's created. Arrays are created for missing index properties while objects
  12165. * are created for all other missing properties. Use `_.setWith` to customize
  12166. * `path` creation.
  12167. *
  12168. * **Note:** This method mutates `object`.
  12169. *
  12170. * @static
  12171. * @memberOf _
  12172. * @since 3.7.0
  12173. * @category Object
  12174. * @param {Object} object The object to modify.
  12175. * @param {Array|string} path The path of the property to set.
  12176. * @param {*} value The value to set.
  12177. * @returns {Object} Returns `object`.
  12178. * @example
  12179. *
  12180. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  12181. *
  12182. * _.set(object, 'a[0].b.c', 4);
  12183. * console.log(object.a[0].b.c);
  12184. * // => 4
  12185. *
  12186. * _.set(object, ['x', '0', 'y', 'z'], 5);
  12187. * console.log(object.x[0].y.z);
  12188. * // => 5
  12189. */
  12190. function set(object, path, value) {
  12191. return object == null ? object : baseSet(object, path, value);
  12192. }
  12193. /**
  12194. * This method is like `_.set` except that it accepts `customizer` which is
  12195. * invoked to produce the objects of `path`. If `customizer` returns `undefined`
  12196. * path creation is handled by the method instead. The `customizer` is invoked
  12197. * with three arguments: (nsValue, key, nsObject).
  12198. *
  12199. * **Note:** This method mutates `object`.
  12200. *
  12201. * @static
  12202. * @memberOf _
  12203. * @since 4.0.0
  12204. * @category Object
  12205. * @param {Object} object The object to modify.
  12206. * @param {Array|string} path The path of the property to set.
  12207. * @param {*} value The value to set.
  12208. * @param {Function} [customizer] The function to customize assigned values.
  12209. * @returns {Object} Returns `object`.
  12210. * @example
  12211. *
  12212. * var object = {};
  12213. *
  12214. * _.setWith(object, '[0][1]', 'a', Object);
  12215. * // => { '0': { '1': 'a' } }
  12216. */
  12217. function setWith(object, path, value, customizer) {
  12218. customizer = typeof customizer == 'function' ? customizer : undefined;
  12219. return object == null ? object : baseSet(object, path, value, customizer);
  12220. }
  12221. /**
  12222. * Creates an array of own enumerable string keyed-value pairs for `object`
  12223. * which can be consumed by `_.fromPairs`. If `object` is a map or set, its
  12224. * entries are returned.
  12225. *
  12226. * @static
  12227. * @memberOf _
  12228. * @since 4.0.0
  12229. * @alias entries
  12230. * @category Object
  12231. * @param {Object} object The object to query.
  12232. * @returns {Array} Returns the key-value pairs.
  12233. * @example
  12234. *
  12235. * function Foo() {
  12236. * this.a = 1;
  12237. * this.b = 2;
  12238. * }
  12239. *
  12240. * Foo.prototype.c = 3;
  12241. *
  12242. * _.toPairs(new Foo);
  12243. * // => [['a', 1], ['b', 2]] (iteration order is not guaranteed)
  12244. */
  12245. var toPairs = createToPairs(keys);
  12246. /**
  12247. * Creates an array of own and inherited enumerable string keyed-value pairs
  12248. * for `object` which can be consumed by `_.fromPairs`. If `object` is a map
  12249. * or set, its entries are returned.
  12250. *
  12251. * @static
  12252. * @memberOf _
  12253. * @since 4.0.0
  12254. * @alias entriesIn
  12255. * @category Object
  12256. * @param {Object} object The object to query.
  12257. * @returns {Array} Returns the key-value pairs.
  12258. * @example
  12259. *
  12260. * function Foo() {
  12261. * this.a = 1;
  12262. * this.b = 2;
  12263. * }
  12264. *
  12265. * Foo.prototype.c = 3;
  12266. *
  12267. * _.toPairsIn(new Foo);
  12268. * // => [['a', 1], ['b', 2], ['c', 3]] (iteration order is not guaranteed)
  12269. */
  12270. var toPairsIn = createToPairs(keysIn);
  12271. /**
  12272. * An alternative to `_.reduce`; this method transforms `object` to a new
  12273. * `accumulator` object which is the result of running each of its own
  12274. * enumerable string keyed properties thru `iteratee`, with each invocation
  12275. * potentially mutating the `accumulator` object. If `accumulator` is not
  12276. * provided, a new object with the same `[[Prototype]]` will be used. The
  12277. * iteratee is invoked with four arguments: (accumulator, value, key, object).
  12278. * Iteratee functions may exit iteration early by explicitly returning `false`.
  12279. *
  12280. * @static
  12281. * @memberOf _
  12282. * @since 1.3.0
  12283. * @category Object
  12284. * @param {Object} object The object to iterate over.
  12285. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  12286. * @param {*} [accumulator] The custom accumulator value.
  12287. * @returns {*} Returns the accumulated value.
  12288. * @example
  12289. *
  12290. * _.transform([2, 3, 4], function(result, n) {
  12291. * result.push(n *= n);
  12292. * return n % 2 == 0;
  12293. * }, []);
  12294. * // => [4, 9]
  12295. *
  12296. * _.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
  12297. * (result[value] || (result[value] = [])).push(key);
  12298. * }, {});
  12299. * // => { '1': ['a', 'c'], '2': ['b'] }
  12300. */
  12301. function transform(object, iteratee, accumulator) {
  12302. var isArr = isArray(object) || isTypedArray(object);
  12303. iteratee = getIteratee(iteratee, 4);
  12304. if (accumulator == null) {
  12305. if (isArr || isObject(object)) {
  12306. var Ctor = object.constructor;
  12307. if (isArr) {
  12308. accumulator = isArray(object) ? new Ctor : [];
  12309. } else {
  12310. accumulator = isFunction(Ctor) ? baseCreate(getPrototype(object)) : {};
  12311. }
  12312. } else {
  12313. accumulator = {};
  12314. }
  12315. }
  12316. (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
  12317. return iteratee(accumulator, value, index, object);
  12318. });
  12319. return accumulator;
  12320. }
  12321. /**
  12322. * Removes the property at `path` of `object`.
  12323. *
  12324. * **Note:** This method mutates `object`.
  12325. *
  12326. * @static
  12327. * @memberOf _
  12328. * @since 4.0.0
  12329. * @category Object
  12330. * @param {Object} object The object to modify.
  12331. * @param {Array|string} path The path of the property to unset.
  12332. * @returns {boolean} Returns `true` if the property is deleted, else `false`.
  12333. * @example
  12334. *
  12335. * var object = { 'a': [{ 'b': { 'c': 7 } }] };
  12336. * _.unset(object, 'a[0].b.c');
  12337. * // => true
  12338. *
  12339. * console.log(object);
  12340. * // => { 'a': [{ 'b': {} }] };
  12341. *
  12342. * _.unset(object, ['a', '0', 'b', 'c']);
  12343. * // => true
  12344. *
  12345. * console.log(object);
  12346. * // => { 'a': [{ 'b': {} }] };
  12347. */
  12348. function unset(object, path) {
  12349. return object == null ? true : baseUnset(object, path);
  12350. }
  12351. /**
  12352. * This method is like `_.set` except that accepts `updater` to produce the
  12353. * value to set. Use `_.updateWith` to customize `path` creation. The `updater`
  12354. * is invoked with one argument: (value).
  12355. *
  12356. * **Note:** This method mutates `object`.
  12357. *
  12358. * @static
  12359. * @memberOf _
  12360. * @since 4.6.0
  12361. * @category Object
  12362. * @param {Object} object The object to modify.
  12363. * @param {Array|string} path The path of the property to set.
  12364. * @param {Function} updater The function to produce the updated value.
  12365. * @returns {Object} Returns `object`.
  12366. * @example
  12367. *
  12368. * var object = { 'a': [{ 'b': { 'c': 3 } }] };
  12369. *
  12370. * _.update(object, 'a[0].b.c', function(n) { return n * n; });
  12371. * console.log(object.a[0].b.c);
  12372. * // => 9
  12373. *
  12374. * _.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; });
  12375. * console.log(object.x[0].y.z);
  12376. * // => 0
  12377. */
  12378. function update(object, path, updater) {
  12379. return object == null ? object : baseUpdate(object, path, castFunction(updater));
  12380. }
  12381. /**
  12382. * This method is like `_.update` except that it accepts `customizer` which is
  12383. * invoked to produce the objects of `path`. If `customizer` returns `undefined`
  12384. * path creation is handled by the method instead. The `customizer` is invoked
  12385. * with three arguments: (nsValue, key, nsObject).
  12386. *
  12387. * **Note:** This method mutates `object`.
  12388. *
  12389. * @static
  12390. * @memberOf _
  12391. * @since 4.6.0
  12392. * @category Object
  12393. * @param {Object} object The object to modify.
  12394. * @param {Array|string} path The path of the property to set.
  12395. * @param {Function} updater The function to produce the updated value.
  12396. * @param {Function} [customizer] The function to customize assigned values.
  12397. * @returns {Object} Returns `object`.
  12398. * @example
  12399. *
  12400. * var object = {};
  12401. *
  12402. * _.updateWith(object, '[0][1]', _.constant('a'), Object);
  12403. * // => { '0': { '1': 'a' } }
  12404. */
  12405. function updateWith(object, path, updater, customizer) {
  12406. customizer = typeof customizer == 'function' ? customizer : undefined;
  12407. return object == null ? object : baseUpdate(object, path, castFunction(updater), customizer);
  12408. }
  12409. /**
  12410. * Creates an array of the own enumerable string keyed property values of `object`.
  12411. *
  12412. * **Note:** Non-object values are coerced to objects.
  12413. *
  12414. * @static
  12415. * @since 0.1.0
  12416. * @memberOf _
  12417. * @category Object
  12418. * @param {Object} object The object to query.
  12419. * @returns {Array} Returns the array of property values.
  12420. * @example
  12421. *
  12422. * function Foo() {
  12423. * this.a = 1;
  12424. * this.b = 2;
  12425. * }
  12426. *
  12427. * Foo.prototype.c = 3;
  12428. *
  12429. * _.values(new Foo);
  12430. * // => [1, 2] (iteration order is not guaranteed)
  12431. *
  12432. * _.values('hi');
  12433. * // => ['h', 'i']
  12434. */
  12435. function values(object) {
  12436. return object ? baseValues(object, keys(object)) : [];
  12437. }
  12438. /**
  12439. * Creates an array of the own and inherited enumerable string keyed property
  12440. * values of `object`.
  12441. *
  12442. * **Note:** Non-object values are coerced to objects.
  12443. *
  12444. * @static
  12445. * @memberOf _
  12446. * @since 3.0.0
  12447. * @category Object
  12448. * @param {Object} object The object to query.
  12449. * @returns {Array} Returns the array of property values.
  12450. * @example
  12451. *
  12452. * function Foo() {
  12453. * this.a = 1;
  12454. * this.b = 2;
  12455. * }
  12456. *
  12457. * Foo.prototype.c = 3;
  12458. *
  12459. * _.valuesIn(new Foo);
  12460. * // => [1, 2, 3] (iteration order is not guaranteed)
  12461. */
  12462. function valuesIn(object) {
  12463. return object == null ? [] : baseValues(object, keysIn(object));
  12464. }
  12465. /*------------------------------------------------------------------------*/
  12466. /**
  12467. * Clamps `number` within the inclusive `lower` and `upper` bounds.
  12468. *
  12469. * @static
  12470. * @memberOf _
  12471. * @since 4.0.0
  12472. * @category Number
  12473. * @param {number} number The number to clamp.
  12474. * @param {number} [lower] The lower bound.
  12475. * @param {number} upper The upper bound.
  12476. * @returns {number} Returns the clamped number.
  12477. * @example
  12478. *
  12479. * _.clamp(-10, -5, 5);
  12480. * // => -5
  12481. *
  12482. * _.clamp(10, -5, 5);
  12483. * // => 5
  12484. */
  12485. function clamp(number, lower, upper) {
  12486. if (upper === undefined) {
  12487. upper = lower;
  12488. lower = undefined;
  12489. }
  12490. if (upper !== undefined) {
  12491. upper = toNumber(upper);
  12492. upper = upper === upper ? upper : 0;
  12493. }
  12494. if (lower !== undefined) {
  12495. lower = toNumber(lower);
  12496. lower = lower === lower ? lower : 0;
  12497. }
  12498. return baseClamp(toNumber(number), lower, upper);
  12499. }
  12500. /**
  12501. * Checks if `n` is between `start` and up to, but not including, `end`. If
  12502. * `end` is not specified, it's set to `start` with `start` then set to `0`.
  12503. * If `start` is greater than `end` the params are swapped to support
  12504. * negative ranges.
  12505. *
  12506. * @static
  12507. * @memberOf _
  12508. * @since 3.3.0
  12509. * @category Number
  12510. * @param {number} number The number to check.
  12511. * @param {number} [start=0] The start of the range.
  12512. * @param {number} end The end of the range.
  12513. * @returns {boolean} Returns `true` if `number` is in the range, else `false`.
  12514. * @see _.range, _.rangeRight
  12515. * @example
  12516. *
  12517. * _.inRange(3, 2, 4);
  12518. * // => true
  12519. *
  12520. * _.inRange(4, 8);
  12521. * // => true
  12522. *
  12523. * _.inRange(4, 2);
  12524. * // => false
  12525. *
  12526. * _.inRange(2, 2);
  12527. * // => false
  12528. *
  12529. * _.inRange(1.2, 2);
  12530. * // => true
  12531. *
  12532. * _.inRange(5.2, 4);
  12533. * // => false
  12534. *
  12535. * _.inRange(-3, -2, -6);
  12536. * // => true
  12537. */
  12538. function inRange(number, start, end) {
  12539. start = toNumber(start) || 0;
  12540. if (end === undefined) {
  12541. end = start;
  12542. start = 0;
  12543. } else {
  12544. end = toNumber(end) || 0;
  12545. }
  12546. number = toNumber(number);
  12547. return baseInRange(number, start, end);
  12548. }
  12549. /**
  12550. * Produces a random number between the inclusive `lower` and `upper` bounds.
  12551. * If only one argument is provided a number between `0` and the given number
  12552. * is returned. If `floating` is `true`, or either `lower` or `upper` are
  12553. * floats, a floating-point number is returned instead of an integer.
  12554. *
  12555. * **Note:** JavaScript follows the IEEE-754 standard for resolving
  12556. * floating-point values which can produce unexpected results.
  12557. *
  12558. * @static
  12559. * @memberOf _
  12560. * @since 0.7.0
  12561. * @category Number
  12562. * @param {number} [lower=0] The lower bound.
  12563. * @param {number} [upper=1] The upper bound.
  12564. * @param {boolean} [floating] Specify returning a floating-point number.
  12565. * @returns {number} Returns the random number.
  12566. * @example
  12567. *
  12568. * _.random(0, 5);
  12569. * // => an integer between 0 and 5
  12570. *
  12571. * _.random(5);
  12572. * // => also an integer between 0 and 5
  12573. *
  12574. * _.random(5, true);
  12575. * // => a floating-point number between 0 and 5
  12576. *
  12577. * _.random(1.2, 5.2);
  12578. * // => a floating-point number between 1.2 and 5.2
  12579. */
  12580. function random(lower, upper, floating) {
  12581. if (floating && typeof floating != 'boolean' && isIterateeCall(lower, upper, floating)) {
  12582. upper = floating = undefined;
  12583. }
  12584. if (floating === undefined) {
  12585. if (typeof upper == 'boolean') {
  12586. floating = upper;
  12587. upper = undefined;
  12588. }
  12589. else if (typeof lower == 'boolean') {
  12590. floating = lower;
  12591. lower = undefined;
  12592. }
  12593. }
  12594. if (lower === undefined && upper === undefined) {
  12595. lower = 0;
  12596. upper = 1;
  12597. }
  12598. else {
  12599. lower = toNumber(lower) || 0;
  12600. if (upper === undefined) {
  12601. upper = lower;
  12602. lower = 0;
  12603. } else {
  12604. upper = toNumber(upper) || 0;
  12605. }
  12606. }
  12607. if (lower > upper) {
  12608. var temp = lower;
  12609. lower = upper;
  12610. upper = temp;
  12611. }
  12612. if (floating || lower % 1 || upper % 1) {
  12613. var rand = nativeRandom();
  12614. return nativeMin(lower + (rand * (upper - lower + freeParseFloat('1e-' + ((rand + '').length - 1)))), upper);
  12615. }
  12616. return baseRandom(lower, upper);
  12617. }
  12618. /*------------------------------------------------------------------------*/
  12619. /**
  12620. * Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase).
  12621. *
  12622. * @static
  12623. * @memberOf _
  12624. * @since 3.0.0
  12625. * @category String
  12626. * @param {string} [string=''] The string to convert.
  12627. * @returns {string} Returns the camel cased string.
  12628. * @example
  12629. *
  12630. * _.camelCase('Foo Bar');
  12631. * // => 'fooBar'
  12632. *
  12633. * _.camelCase('--foo-bar--');
  12634. * // => 'fooBar'
  12635. *
  12636. * _.camelCase('__FOO_BAR__');
  12637. * // => 'fooBar'
  12638. */
  12639. var camelCase = createCompounder(function(result, word, index) {
  12640. word = word.toLowerCase();
  12641. return result + (index ? capitalize(word) : word);
  12642. });
  12643. /**
  12644. * Converts the first character of `string` to upper case and the remaining
  12645. * to lower case.
  12646. *
  12647. * @static
  12648. * @memberOf _
  12649. * @since 3.0.0
  12650. * @category String
  12651. * @param {string} [string=''] The string to capitalize.
  12652. * @returns {string} Returns the capitalized string.
  12653. * @example
  12654. *
  12655. * _.capitalize('FRED');
  12656. * // => 'Fred'
  12657. */
  12658. function capitalize(string) {
  12659. return upperFirst(toString(string).toLowerCase());
  12660. }
  12661. /**
  12662. * Deburrs `string` by converting
  12663. * [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table)
  12664. * to basic latin letters and removing
  12665. * [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
  12666. *
  12667. * @static
  12668. * @memberOf _
  12669. * @since 3.0.0
  12670. * @category String
  12671. * @param {string} [string=''] The string to deburr.
  12672. * @returns {string} Returns the deburred string.
  12673. * @example
  12674. *
  12675. * _.deburr('déjà vu');
  12676. * // => 'deja vu'
  12677. */
  12678. function deburr(string) {
  12679. string = toString(string);
  12680. return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, '');
  12681. }
  12682. /**
  12683. * Checks if `string` ends with the given target string.
  12684. *
  12685. * @static
  12686. * @memberOf _
  12687. * @since 3.0.0
  12688. * @category String
  12689. * @param {string} [string=''] The string to search.
  12690. * @param {string} [target] The string to search for.
  12691. * @param {number} [position=string.length] The position to search up to.
  12692. * @returns {boolean} Returns `true` if `string` ends with `target`,
  12693. * else `false`.
  12694. * @example
  12695. *
  12696. * _.endsWith('abc', 'c');
  12697. * // => true
  12698. *
  12699. * _.endsWith('abc', 'b');
  12700. * // => false
  12701. *
  12702. * _.endsWith('abc', 'b', 2);
  12703. * // => true
  12704. */
  12705. function endsWith(string, target, position) {
  12706. string = toString(string);
  12707. target = baseToString(target);
  12708. var length = string.length;
  12709. position = position === undefined
  12710. ? length
  12711. : baseClamp(toInteger(position), 0, length);
  12712. position -= target.length;
  12713. return position >= 0 && string.indexOf(target, position) == position;
  12714. }
  12715. /**
  12716. * Converts the characters "&", "<", ">", '"', "'", and "\`" in `string` to
  12717. * their corresponding HTML entities.
  12718. *
  12719. * **Note:** No other characters are escaped. To escape additional
  12720. * characters use a third-party library like [_he_](https://mths.be/he).
  12721. *
  12722. * Though the ">" character is escaped for symmetry, characters like
  12723. * ">" and "/" don't need escaping in HTML and have no special meaning
  12724. * unless they're part of a tag or unquoted attribute value. See
  12725. * [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
  12726. * (under "semi-related fun fact") for more details.
  12727. *
  12728. * Backticks are escaped because in IE < 9, they can break out of
  12729. * attribute values or HTML comments. See [#59](https://html5sec.org/#59),
  12730. * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and
  12731. * [#133](https://html5sec.org/#133) of the
  12732. * [HTML5 Security Cheatsheet](https://html5sec.org/) for more details.
  12733. *
  12734. * When working with HTML you should always
  12735. * [quote attribute values](http://wonko.com/post/html-escaping) to reduce
  12736. * XSS vectors.
  12737. *
  12738. * @static
  12739. * @since 0.1.0
  12740. * @memberOf _
  12741. * @category String
  12742. * @param {string} [string=''] The string to escape.
  12743. * @returns {string} Returns the escaped string.
  12744. * @example
  12745. *
  12746. * _.escape('fred, barney, & pebbles');
  12747. * // => 'fred, barney, &amp; pebbles'
  12748. */
  12749. function escape(string) {
  12750. string = toString(string);
  12751. return (string && reHasUnescapedHtml.test(string))
  12752. ? string.replace(reUnescapedHtml, escapeHtmlChar)
  12753. : string;
  12754. }
  12755. /**
  12756. * Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
  12757. * "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
  12758. *
  12759. * @static
  12760. * @memberOf _
  12761. * @since 3.0.0
  12762. * @category String
  12763. * @param {string} [string=''] The string to escape.
  12764. * @returns {string} Returns the escaped string.
  12765. * @example
  12766. *
  12767. * _.escapeRegExp('[lodash](https://lodash.com/)');
  12768. * // => '\[lodash\]\(https://lodash\.com/\)'
  12769. */
  12770. function escapeRegExp(string) {
  12771. string = toString(string);
  12772. return (string && reHasRegExpChar.test(string))
  12773. ? string.replace(reRegExpChar, '\\$&')
  12774. : string;
  12775. }
  12776. /**
  12777. * Converts `string` to
  12778. * [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
  12779. *
  12780. * @static
  12781. * @memberOf _
  12782. * @since 3.0.0
  12783. * @category String
  12784. * @param {string} [string=''] The string to convert.
  12785. * @returns {string} Returns the kebab cased string.
  12786. * @example
  12787. *
  12788. * _.kebabCase('Foo Bar');
  12789. * // => 'foo-bar'
  12790. *
  12791. * _.kebabCase('fooBar');
  12792. * // => 'foo-bar'
  12793. *
  12794. * _.kebabCase('__FOO_BAR__');
  12795. * // => 'foo-bar'
  12796. */
  12797. var kebabCase = createCompounder(function(result, word, index) {
  12798. return result + (index ? '-' : '') + word.toLowerCase();
  12799. });
  12800. /**
  12801. * Converts `string`, as space separated words, to lower case.
  12802. *
  12803. * @static
  12804. * @memberOf _
  12805. * @since 4.0.0
  12806. * @category String
  12807. * @param {string} [string=''] The string to convert.
  12808. * @returns {string} Returns the lower cased string.
  12809. * @example
  12810. *
  12811. * _.lowerCase('--Foo-Bar--');
  12812. * // => 'foo bar'
  12813. *
  12814. * _.lowerCase('fooBar');
  12815. * // => 'foo bar'
  12816. *
  12817. * _.lowerCase('__FOO_BAR__');
  12818. * // => 'foo bar'
  12819. */
  12820. var lowerCase = createCompounder(function(result, word, index) {
  12821. return result + (index ? ' ' : '') + word.toLowerCase();
  12822. });
  12823. /**
  12824. * Converts the first character of `string` to lower case.
  12825. *
  12826. * @static
  12827. * @memberOf _
  12828. * @since 4.0.0
  12829. * @category String
  12830. * @param {string} [string=''] The string to convert.
  12831. * @returns {string} Returns the converted string.
  12832. * @example
  12833. *
  12834. * _.lowerFirst('Fred');
  12835. * // => 'fred'
  12836. *
  12837. * _.lowerFirst('FRED');
  12838. * // => 'fRED'
  12839. */
  12840. var lowerFirst = createCaseFirst('toLowerCase');
  12841. /**
  12842. * Pads `string` on the left and right sides if it's shorter than `length`.
  12843. * Padding characters are truncated if they can't be evenly divided by `length`.
  12844. *
  12845. * @static
  12846. * @memberOf _
  12847. * @since 3.0.0
  12848. * @category String
  12849. * @param {string} [string=''] The string to pad.
  12850. * @param {number} [length=0] The padding length.
  12851. * @param {string} [chars=' '] The string used as padding.
  12852. * @returns {string} Returns the padded string.
  12853. * @example
  12854. *
  12855. * _.pad('abc', 8);
  12856. * // => ' abc '
  12857. *
  12858. * _.pad('abc', 8, '_-');
  12859. * // => '_-abc_-_'
  12860. *
  12861. * _.pad('abc', 3);
  12862. * // => 'abc'
  12863. */
  12864. function pad(string, length, chars) {
  12865. string = toString(string);
  12866. length = toInteger(length);
  12867. var strLength = length ? stringSize(string) : 0;
  12868. if (!length || strLength >= length) {
  12869. return string;
  12870. }
  12871. var mid = (length - strLength) / 2;
  12872. return (
  12873. createPadding(nativeFloor(mid), chars) +
  12874. string +
  12875. createPadding(nativeCeil(mid), chars)
  12876. );
  12877. }
  12878. /**
  12879. * Pads `string` on the right side if it's shorter than `length`. Padding
  12880. * characters are truncated if they exceed `length`.
  12881. *
  12882. * @static
  12883. * @memberOf _
  12884. * @since 4.0.0
  12885. * @category String
  12886. * @param {string} [string=''] The string to pad.
  12887. * @param {number} [length=0] The padding length.
  12888. * @param {string} [chars=' '] The string used as padding.
  12889. * @returns {string} Returns the padded string.
  12890. * @example
  12891. *
  12892. * _.padEnd('abc', 6);
  12893. * // => 'abc '
  12894. *
  12895. * _.padEnd('abc', 6, '_-');
  12896. * // => 'abc_-_'
  12897. *
  12898. * _.padEnd('abc', 3);
  12899. * // => 'abc'
  12900. */
  12901. function padEnd(string, length, chars) {
  12902. string = toString(string);
  12903. length = toInteger(length);
  12904. var strLength = length ? stringSize(string) : 0;
  12905. return (length && strLength < length)
  12906. ? (string + createPadding(length - strLength, chars))
  12907. : string;
  12908. }
  12909. /**
  12910. * Pads `string` on the left side if it's shorter than `length`. Padding
  12911. * characters are truncated if they exceed `length`.
  12912. *
  12913. * @static
  12914. * @memberOf _
  12915. * @since 4.0.0
  12916. * @category String
  12917. * @param {string} [string=''] The string to pad.
  12918. * @param {number} [length=0] The padding length.
  12919. * @param {string} [chars=' '] The string used as padding.
  12920. * @returns {string} Returns the padded string.
  12921. * @example
  12922. *
  12923. * _.padStart('abc', 6);
  12924. * // => ' abc'
  12925. *
  12926. * _.padStart('abc', 6, '_-');
  12927. * // => '_-_abc'
  12928. *
  12929. * _.padStart('abc', 3);
  12930. * // => 'abc'
  12931. */
  12932. function padStart(string, length, chars) {
  12933. string = toString(string);
  12934. length = toInteger(length);
  12935. var strLength = length ? stringSize(string) : 0;
  12936. return (length && strLength < length)
  12937. ? (createPadding(length - strLength, chars) + string)
  12938. : string;
  12939. }
  12940. /**
  12941. * Converts `string` to an integer of the specified radix. If `radix` is
  12942. * `undefined` or `0`, a `radix` of `10` is used unless `value` is a
  12943. * hexadecimal, in which case a `radix` of `16` is used.
  12944. *
  12945. * **Note:** This method aligns with the
  12946. * [ES5 implementation](https://es5.github.io/#x15.1.2.2) of `parseInt`.
  12947. *
  12948. * @static
  12949. * @memberOf _
  12950. * @since 1.1.0
  12951. * @category String
  12952. * @param {string} string The string to convert.
  12953. * @param {number} [radix=10] The radix to interpret `value` by.
  12954. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  12955. * @returns {number} Returns the converted integer.
  12956. * @example
  12957. *
  12958. * _.parseInt('08');
  12959. * // => 8
  12960. *
  12961. * _.map(['6', '08', '10'], _.parseInt);
  12962. * // => [6, 8, 10]
  12963. */
  12964. function parseInt(string, radix, guard) {
  12965. // Chrome fails to trim leading <BOM> whitespace characters.
  12966. // See https://bugs.chromium.org/p/v8/issues/detail?id=3109 for more details.
  12967. if (guard || radix == null) {
  12968. radix = 0;
  12969. } else if (radix) {
  12970. radix = +radix;
  12971. }
  12972. string = toString(string).replace(reTrim, '');
  12973. return nativeParseInt(string, radix || (reHasHexPrefix.test(string) ? 16 : 10));
  12974. }
  12975. /**
  12976. * Repeats the given string `n` times.
  12977. *
  12978. * @static
  12979. * @memberOf _
  12980. * @since 3.0.0
  12981. * @category String
  12982. * @param {string} [string=''] The string to repeat.
  12983. * @param {number} [n=1] The number of times to repeat the string.
  12984. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  12985. * @returns {string} Returns the repeated string.
  12986. * @example
  12987. *
  12988. * _.repeat('*', 3);
  12989. * // => '***'
  12990. *
  12991. * _.repeat('abc', 2);
  12992. * // => 'abcabc'
  12993. *
  12994. * _.repeat('abc', 0);
  12995. * // => ''
  12996. */
  12997. function repeat(string, n, guard) {
  12998. if ((guard ? isIterateeCall(string, n, guard) : n === undefined)) {
  12999. n = 1;
  13000. } else {
  13001. n = toInteger(n);
  13002. }
  13003. return baseRepeat(toString(string), n);
  13004. }
  13005. /**
  13006. * Replaces matches for `pattern` in `string` with `replacement`.
  13007. *
  13008. * **Note:** This method is based on
  13009. * [`String#replace`](https://mdn.io/String/replace).
  13010. *
  13011. * @static
  13012. * @memberOf _
  13013. * @since 4.0.0
  13014. * @category String
  13015. * @param {string} [string=''] The string to modify.
  13016. * @param {RegExp|string} pattern The pattern to replace.
  13017. * @param {Function|string} replacement The match replacement.
  13018. * @returns {string} Returns the modified string.
  13019. * @example
  13020. *
  13021. * _.replace('Hi Fred', 'Fred', 'Barney');
  13022. * // => 'Hi Barney'
  13023. */
  13024. function replace() {
  13025. var args = arguments,
  13026. string = toString(args[0]);
  13027. return args.length < 3 ? string : nativeReplace.call(string, args[1], args[2]);
  13028. }
  13029. /**
  13030. * Converts `string` to
  13031. * [snake case](https://en.wikipedia.org/wiki/Snake_case).
  13032. *
  13033. * @static
  13034. * @memberOf _
  13035. * @since 3.0.0
  13036. * @category String
  13037. * @param {string} [string=''] The string to convert.
  13038. * @returns {string} Returns the snake cased string.
  13039. * @example
  13040. *
  13041. * _.snakeCase('Foo Bar');
  13042. * // => 'foo_bar'
  13043. *
  13044. * _.snakeCase('fooBar');
  13045. * // => 'foo_bar'
  13046. *
  13047. * _.snakeCase('--FOO-BAR--');
  13048. * // => 'foo_bar'
  13049. */
  13050. var snakeCase = createCompounder(function(result, word, index) {
  13051. return result + (index ? '_' : '') + word.toLowerCase();
  13052. });
  13053. /**
  13054. * Splits `string` by `separator`.
  13055. *
  13056. * **Note:** This method is based on
  13057. * [`String#split`](https://mdn.io/String/split).
  13058. *
  13059. * @static
  13060. * @memberOf _
  13061. * @since 4.0.0
  13062. * @category String
  13063. * @param {string} [string=''] The string to split.
  13064. * @param {RegExp|string} separator The separator pattern to split by.
  13065. * @param {number} [limit] The length to truncate results to.
  13066. * @returns {Array} Returns the string segments.
  13067. * @example
  13068. *
  13069. * _.split('a-b-c', '-', 2);
  13070. * // => ['a', 'b']
  13071. */
  13072. function split(string, separator, limit) {
  13073. if (limit && typeof limit != 'number' && isIterateeCall(string, separator, limit)) {
  13074. separator = limit = undefined;
  13075. }
  13076. limit = limit === undefined ? MAX_ARRAY_LENGTH : limit >>> 0;
  13077. if (!limit) {
  13078. return [];
  13079. }
  13080. string = toString(string);
  13081. if (string && (
  13082. typeof separator == 'string' ||
  13083. (separator != null && !isRegExp(separator))
  13084. )) {
  13085. separator = baseToString(separator);
  13086. if (separator == '' && reHasComplexSymbol.test(string)) {
  13087. return castSlice(stringToArray(string), 0, limit);
  13088. }
  13089. }
  13090. return nativeSplit.call(string, separator, limit);
  13091. }
  13092. /**
  13093. * Converts `string` to
  13094. * [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage).
  13095. *
  13096. * @static
  13097. * @memberOf _
  13098. * @since 3.1.0
  13099. * @category String
  13100. * @param {string} [string=''] The string to convert.
  13101. * @returns {string} Returns the start cased string.
  13102. * @example
  13103. *
  13104. * _.startCase('--foo-bar--');
  13105. * // => 'Foo Bar'
  13106. *
  13107. * _.startCase('fooBar');
  13108. * // => 'Foo Bar'
  13109. *
  13110. * _.startCase('__FOO_BAR__');
  13111. * // => 'FOO BAR'
  13112. */
  13113. var startCase = createCompounder(function(result, word, index) {
  13114. return result + (index ? ' ' : '') + upperFirst(word);
  13115. });
  13116. /**
  13117. * Checks if `string` starts with the given target string.
  13118. *
  13119. * @static
  13120. * @memberOf _
  13121. * @since 3.0.0
  13122. * @category String
  13123. * @param {string} [string=''] The string to search.
  13124. * @param {string} [target] The string to search for.
  13125. * @param {number} [position=0] The position to search from.
  13126. * @returns {boolean} Returns `true` if `string` starts with `target`,
  13127. * else `false`.
  13128. * @example
  13129. *
  13130. * _.startsWith('abc', 'a');
  13131. * // => true
  13132. *
  13133. * _.startsWith('abc', 'b');
  13134. * // => false
  13135. *
  13136. * _.startsWith('abc', 'b', 1);
  13137. * // => true
  13138. */
  13139. function startsWith(string, target, position) {
  13140. string = toString(string);
  13141. position = baseClamp(toInteger(position), 0, string.length);
  13142. return string.lastIndexOf(baseToString(target), position) == position;
  13143. }
  13144. /**
  13145. * Creates a compiled template function that can interpolate data properties
  13146. * in "interpolate" delimiters, HTML-escape interpolated data properties in
  13147. * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
  13148. * properties may be accessed as free variables in the template. If a setting
  13149. * object is given, it takes precedence over `_.templateSettings` values.
  13150. *
  13151. * **Note:** In the development build `_.template` utilizes
  13152. * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
  13153. * for easier debugging.
  13154. *
  13155. * For more information on precompiling templates see
  13156. * [lodash's custom builds documentation](https://lodash.com/custom-builds).
  13157. *
  13158. * For more information on Chrome extension sandboxes see
  13159. * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
  13160. *
  13161. * @static
  13162. * @since 0.1.0
  13163. * @memberOf _
  13164. * @category String
  13165. * @param {string} [string=''] The template string.
  13166. * @param {Object} [options={}] The options object.
  13167. * @param {RegExp} [options.escape=_.templateSettings.escape]
  13168. * The HTML "escape" delimiter.
  13169. * @param {RegExp} [options.evaluate=_.templateSettings.evaluate]
  13170. * The "evaluate" delimiter.
  13171. * @param {Object} [options.imports=_.templateSettings.imports]
  13172. * An object to import into the template as free variables.
  13173. * @param {RegExp} [options.interpolate=_.templateSettings.interpolate]
  13174. * The "interpolate" delimiter.
  13175. * @param {string} [options.sourceURL='lodash.templateSources[n]']
  13176. * The sourceURL of the compiled template.
  13177. * @param {string} [options.variable='obj']
  13178. * The data object variable name.
  13179. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13180. * @returns {Function} Returns the compiled template function.
  13181. * @example
  13182. *
  13183. * // Use the "interpolate" delimiter to create a compiled template.
  13184. * var compiled = _.template('hello <%= user %>!');
  13185. * compiled({ 'user': 'fred' });
  13186. * // => 'hello fred!'
  13187. *
  13188. * // Use the HTML "escape" delimiter to escape data property values.
  13189. * var compiled = _.template('<b><%- value %></b>');
  13190. * compiled({ 'value': '<script>' });
  13191. * // => '<b>&lt;script&gt;</b>'
  13192. *
  13193. * // Use the "evaluate" delimiter to execute JavaScript and generate HTML.
  13194. * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  13195. * compiled({ 'users': ['fred', 'barney'] });
  13196. * // => '<li>fred</li><li>barney</li>'
  13197. *
  13198. * // Use the internal `print` function in "evaluate" delimiters.
  13199. * var compiled = _.template('<% print("hello " + user); %>!');
  13200. * compiled({ 'user': 'barney' });
  13201. * // => 'hello barney!'
  13202. *
  13203. * // Use the ES delimiter as an alternative to the default "interpolate" delimiter.
  13204. * var compiled = _.template('hello ${ user }!');
  13205. * compiled({ 'user': 'pebbles' });
  13206. * // => 'hello pebbles!'
  13207. *
  13208. * // Use backslashes to treat delimiters as plain text.
  13209. * var compiled = _.template('<%= "\\<%- value %\\>" %>');
  13210. * compiled({ 'value': 'ignored' });
  13211. * // => '<%- value %>'
  13212. *
  13213. * // Use the `imports` option to import `jQuery` as `jq`.
  13214. * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  13215. * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  13216. * compiled({ 'users': ['fred', 'barney'] });
  13217. * // => '<li>fred</li><li>barney</li>'
  13218. *
  13219. * // Use the `sourceURL` option to specify a custom sourceURL for the template.
  13220. * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  13221. * compiled(data);
  13222. * // => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
  13223. *
  13224. * // Use the `variable` option to ensure a with-statement isn't used in the compiled template.
  13225. * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  13226. * compiled.source;
  13227. * // => function(data) {
  13228. * // var __t, __p = '';
  13229. * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  13230. * // return __p;
  13231. * // }
  13232. *
  13233. * // Use custom template delimiters.
  13234. * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  13235. * var compiled = _.template('hello {{ user }}!');
  13236. * compiled({ 'user': 'mustache' });
  13237. * // => 'hello mustache!'
  13238. *
  13239. * // Use the `source` property to inline compiled templates for meaningful
  13240. * // line numbers in error messages and stack traces.
  13241. * fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
  13242. * var JST = {\
  13243. * "main": ' + _.template(mainText).source + '\
  13244. * };\
  13245. * ');
  13246. */
  13247. function template(string, options, guard) {
  13248. // Based on John Resig's `tmpl` implementation
  13249. // (http://ejohn.org/blog/javascript-micro-templating/)
  13250. // and Laura Doktorova's doT.js (https://github.com/olado/doT).
  13251. var settings = lodash.templateSettings;
  13252. if (guard && isIterateeCall(string, options, guard)) {
  13253. options = undefined;
  13254. }
  13255. string = toString(string);
  13256. options = assignInWith({}, options, settings, assignInDefaults);
  13257. var imports = assignInWith({}, options.imports, settings.imports, assignInDefaults),
  13258. importsKeys = keys(imports),
  13259. importsValues = baseValues(imports, importsKeys);
  13260. var isEscaping,
  13261. isEvaluating,
  13262. index = 0,
  13263. interpolate = options.interpolate || reNoMatch,
  13264. source = "__p += '";
  13265. // Compile the regexp to match each delimiter.
  13266. var reDelimiters = RegExp(
  13267. (options.escape || reNoMatch).source + '|' +
  13268. interpolate.source + '|' +
  13269. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  13270. (options.evaluate || reNoMatch).source + '|$'
  13271. , 'g');
  13272. // Use a sourceURL for easier debugging.
  13273. var sourceURL = '//# sourceURL=' +
  13274. ('sourceURL' in options
  13275. ? options.sourceURL
  13276. : ('lodash.templateSources[' + (++templateCounter) + ']')
  13277. ) + '\n';
  13278. string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  13279. interpolateValue || (interpolateValue = esTemplateValue);
  13280. // Escape characters that can't be included in string literals.
  13281. source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  13282. // Replace delimiters with snippets.
  13283. if (escapeValue) {
  13284. isEscaping = true;
  13285. source += "' +\n__e(" + escapeValue + ") +\n'";
  13286. }
  13287. if (evaluateValue) {
  13288. isEvaluating = true;
  13289. source += "';\n" + evaluateValue + ";\n__p += '";
  13290. }
  13291. if (interpolateValue) {
  13292. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  13293. }
  13294. index = offset + match.length;
  13295. // The JS engine embedded in Adobe products needs `match` returned in
  13296. // order to produce the correct `offset` value.
  13297. return match;
  13298. });
  13299. source += "';\n";
  13300. // If `variable` is not specified wrap a with-statement around the generated
  13301. // code to add the data object to the top of the scope chain.
  13302. var variable = options.variable;
  13303. if (!variable) {
  13304. source = 'with (obj) {\n' + source + '\n}\n';
  13305. }
  13306. // Cleanup code by stripping empty strings.
  13307. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  13308. .replace(reEmptyStringMiddle, '$1')
  13309. .replace(reEmptyStringTrailing, '$1;');
  13310. // Frame code as the function body.
  13311. source = 'function(' + (variable || 'obj') + ') {\n' +
  13312. (variable
  13313. ? ''
  13314. : 'obj || (obj = {});\n'
  13315. ) +
  13316. "var __t, __p = ''" +
  13317. (isEscaping
  13318. ? ', __e = _.escape'
  13319. : ''
  13320. ) +
  13321. (isEvaluating
  13322. ? ', __j = Array.prototype.join;\n' +
  13323. "function print() { __p += __j.call(arguments, '') }\n"
  13324. : ';\n'
  13325. ) +
  13326. source +
  13327. 'return __p\n}';
  13328. var result = attempt(function() {
  13329. return Function(importsKeys, sourceURL + 'return ' + source)
  13330. .apply(undefined, importsValues);
  13331. });
  13332. // Provide the compiled function's source by its `toString` method or
  13333. // the `source` property as a convenience for inlining compiled templates.
  13334. result.source = source;
  13335. if (isError(result)) {
  13336. throw result;
  13337. }
  13338. return result;
  13339. }
  13340. /**
  13341. * Converts `string`, as a whole, to lower case just like
  13342. * [String#toLowerCase](https://mdn.io/toLowerCase).
  13343. *
  13344. * @static
  13345. * @memberOf _
  13346. * @since 4.0.0
  13347. * @category String
  13348. * @param {string} [string=''] The string to convert.
  13349. * @returns {string} Returns the lower cased string.
  13350. * @example
  13351. *
  13352. * _.toLower('--Foo-Bar--');
  13353. * // => '--foo-bar--'
  13354. *
  13355. * _.toLower('fooBar');
  13356. * // => 'foobar'
  13357. *
  13358. * _.toLower('__FOO_BAR__');
  13359. * // => '__foo_bar__'
  13360. */
  13361. function toLower(value) {
  13362. return toString(value).toLowerCase();
  13363. }
  13364. /**
  13365. * Converts `string`, as a whole, to upper case just like
  13366. * [String#toUpperCase](https://mdn.io/toUpperCase).
  13367. *
  13368. * @static
  13369. * @memberOf _
  13370. * @since 4.0.0
  13371. * @category String
  13372. * @param {string} [string=''] The string to convert.
  13373. * @returns {string} Returns the upper cased string.
  13374. * @example
  13375. *
  13376. * _.toUpper('--foo-bar--');
  13377. * // => '--FOO-BAR--'
  13378. *
  13379. * _.toUpper('fooBar');
  13380. * // => 'FOOBAR'
  13381. *
  13382. * _.toUpper('__foo_bar__');
  13383. * // => '__FOO_BAR__'
  13384. */
  13385. function toUpper(value) {
  13386. return toString(value).toUpperCase();
  13387. }
  13388. /**
  13389. * Removes leading and trailing whitespace or specified characters from `string`.
  13390. *
  13391. * @static
  13392. * @memberOf _
  13393. * @since 3.0.0
  13394. * @category String
  13395. * @param {string} [string=''] The string to trim.
  13396. * @param {string} [chars=whitespace] The characters to trim.
  13397. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13398. * @returns {string} Returns the trimmed string.
  13399. * @example
  13400. *
  13401. * _.trim(' abc ');
  13402. * // => 'abc'
  13403. *
  13404. * _.trim('-_-abc-_-', '_-');
  13405. * // => 'abc'
  13406. *
  13407. * _.map([' foo ', ' bar '], _.trim);
  13408. * // => ['foo', 'bar']
  13409. */
  13410. function trim(string, chars, guard) {
  13411. string = toString(string);
  13412. if (string && (guard || chars === undefined)) {
  13413. return string.replace(reTrim, '');
  13414. }
  13415. if (!string || !(chars = baseToString(chars))) {
  13416. return string;
  13417. }
  13418. var strSymbols = stringToArray(string),
  13419. chrSymbols = stringToArray(chars),
  13420. start = charsStartIndex(strSymbols, chrSymbols),
  13421. end = charsEndIndex(strSymbols, chrSymbols) + 1;
  13422. return castSlice(strSymbols, start, end).join('');
  13423. }
  13424. /**
  13425. * Removes trailing whitespace or specified characters from `string`.
  13426. *
  13427. * @static
  13428. * @memberOf _
  13429. * @since 4.0.0
  13430. * @category String
  13431. * @param {string} [string=''] The string to trim.
  13432. * @param {string} [chars=whitespace] The characters to trim.
  13433. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13434. * @returns {string} Returns the trimmed string.
  13435. * @example
  13436. *
  13437. * _.trimEnd(' abc ');
  13438. * // => ' abc'
  13439. *
  13440. * _.trimEnd('-_-abc-_-', '_-');
  13441. * // => '-_-abc'
  13442. */
  13443. function trimEnd(string, chars, guard) {
  13444. string = toString(string);
  13445. if (string && (guard || chars === undefined)) {
  13446. return string.replace(reTrimEnd, '');
  13447. }
  13448. if (!string || !(chars = baseToString(chars))) {
  13449. return string;
  13450. }
  13451. var strSymbols = stringToArray(string),
  13452. end = charsEndIndex(strSymbols, stringToArray(chars)) + 1;
  13453. return castSlice(strSymbols, 0, end).join('');
  13454. }
  13455. /**
  13456. * Removes leading whitespace or specified characters from `string`.
  13457. *
  13458. * @static
  13459. * @memberOf _
  13460. * @since 4.0.0
  13461. * @category String
  13462. * @param {string} [string=''] The string to trim.
  13463. * @param {string} [chars=whitespace] The characters to trim.
  13464. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13465. * @returns {string} Returns the trimmed string.
  13466. * @example
  13467. *
  13468. * _.trimStart(' abc ');
  13469. * // => 'abc '
  13470. *
  13471. * _.trimStart('-_-abc-_-', '_-');
  13472. * // => 'abc-_-'
  13473. */
  13474. function trimStart(string, chars, guard) {
  13475. string = toString(string);
  13476. if (string && (guard || chars === undefined)) {
  13477. return string.replace(reTrimStart, '');
  13478. }
  13479. if (!string || !(chars = baseToString(chars))) {
  13480. return string;
  13481. }
  13482. var strSymbols = stringToArray(string),
  13483. start = charsStartIndex(strSymbols, stringToArray(chars));
  13484. return castSlice(strSymbols, start).join('');
  13485. }
  13486. /**
  13487. * Truncates `string` if it's longer than the given maximum string length.
  13488. * The last characters of the truncated string are replaced with the omission
  13489. * string which defaults to "...".
  13490. *
  13491. * @static
  13492. * @memberOf _
  13493. * @since 4.0.0
  13494. * @category String
  13495. * @param {string} [string=''] The string to truncate.
  13496. * @param {Object} [options={}] The options object.
  13497. * @param {number} [options.length=30] The maximum string length.
  13498. * @param {string} [options.omission='...'] The string to indicate text is omitted.
  13499. * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
  13500. * @returns {string} Returns the truncated string.
  13501. * @example
  13502. *
  13503. * _.truncate('hi-diddly-ho there, neighborino');
  13504. * // => 'hi-diddly-ho there, neighbo...'
  13505. *
  13506. * _.truncate('hi-diddly-ho there, neighborino', {
  13507. * 'length': 24,
  13508. * 'separator': ' '
  13509. * });
  13510. * // => 'hi-diddly-ho there,...'
  13511. *
  13512. * _.truncate('hi-diddly-ho there, neighborino', {
  13513. * 'length': 24,
  13514. * 'separator': /,? +/
  13515. * });
  13516. * // => 'hi-diddly-ho there...'
  13517. *
  13518. * _.truncate('hi-diddly-ho there, neighborino', {
  13519. * 'omission': ' [...]'
  13520. * });
  13521. * // => 'hi-diddly-ho there, neig [...]'
  13522. */
  13523. function truncate(string, options) {
  13524. var length = DEFAULT_TRUNC_LENGTH,
  13525. omission = DEFAULT_TRUNC_OMISSION;
  13526. if (isObject(options)) {
  13527. var separator = 'separator' in options ? options.separator : separator;
  13528. length = 'length' in options ? toInteger(options.length) : length;
  13529. omission = 'omission' in options ? baseToString(options.omission) : omission;
  13530. }
  13531. string = toString(string);
  13532. var strLength = string.length;
  13533. if (reHasComplexSymbol.test(string)) {
  13534. var strSymbols = stringToArray(string);
  13535. strLength = strSymbols.length;
  13536. }
  13537. if (length >= strLength) {
  13538. return string;
  13539. }
  13540. var end = length - stringSize(omission);
  13541. if (end < 1) {
  13542. return omission;
  13543. }
  13544. var result = strSymbols
  13545. ? castSlice(strSymbols, 0, end).join('')
  13546. : string.slice(0, end);
  13547. if (separator === undefined) {
  13548. return result + omission;
  13549. }
  13550. if (strSymbols) {
  13551. end += (result.length - end);
  13552. }
  13553. if (isRegExp(separator)) {
  13554. if (string.slice(end).search(separator)) {
  13555. var match,
  13556. substring = result;
  13557. if (!separator.global) {
  13558. separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g');
  13559. }
  13560. separator.lastIndex = 0;
  13561. while ((match = separator.exec(substring))) {
  13562. var newEnd = match.index;
  13563. }
  13564. result = result.slice(0, newEnd === undefined ? end : newEnd);
  13565. }
  13566. } else if (string.indexOf(baseToString(separator), end) != end) {
  13567. var index = result.lastIndexOf(separator);
  13568. if (index > -1) {
  13569. result = result.slice(0, index);
  13570. }
  13571. }
  13572. return result + omission;
  13573. }
  13574. /**
  13575. * The inverse of `_.escape`; this method converts the HTML entities
  13576. * `&amp;`, `&lt;`, `&gt;`, `&quot;`, `&#39;`, and `&#96;` in `string` to
  13577. * their corresponding characters.
  13578. *
  13579. * **Note:** No other HTML entities are unescaped. To unescape additional
  13580. * HTML entities use a third-party library like [_he_](https://mths.be/he).
  13581. *
  13582. * @static
  13583. * @memberOf _
  13584. * @since 0.6.0
  13585. * @category String
  13586. * @param {string} [string=''] The string to unescape.
  13587. * @returns {string} Returns the unescaped string.
  13588. * @example
  13589. *
  13590. * _.unescape('fred, barney, &amp; pebbles');
  13591. * // => 'fred, barney, & pebbles'
  13592. */
  13593. function unescape(string) {
  13594. string = toString(string);
  13595. return (string && reHasEscapedHtml.test(string))
  13596. ? string.replace(reEscapedHtml, unescapeHtmlChar)
  13597. : string;
  13598. }
  13599. /**
  13600. * Converts `string`, as space separated words, to upper case.
  13601. *
  13602. * @static
  13603. * @memberOf _
  13604. * @since 4.0.0
  13605. * @category String
  13606. * @param {string} [string=''] The string to convert.
  13607. * @returns {string} Returns the upper cased string.
  13608. * @example
  13609. *
  13610. * _.upperCase('--foo-bar');
  13611. * // => 'FOO BAR'
  13612. *
  13613. * _.upperCase('fooBar');
  13614. * // => 'FOO BAR'
  13615. *
  13616. * _.upperCase('__foo_bar__');
  13617. * // => 'FOO BAR'
  13618. */
  13619. var upperCase = createCompounder(function(result, word, index) {
  13620. return result + (index ? ' ' : '') + word.toUpperCase();
  13621. });
  13622. /**
  13623. * Converts the first character of `string` to upper case.
  13624. *
  13625. * @static
  13626. * @memberOf _
  13627. * @since 4.0.0
  13628. * @category String
  13629. * @param {string} [string=''] The string to convert.
  13630. * @returns {string} Returns the converted string.
  13631. * @example
  13632. *
  13633. * _.upperFirst('fred');
  13634. * // => 'Fred'
  13635. *
  13636. * _.upperFirst('FRED');
  13637. * // => 'FRED'
  13638. */
  13639. var upperFirst = createCaseFirst('toUpperCase');
  13640. /**
  13641. * Splits `string` into an array of its words.
  13642. *
  13643. * @static
  13644. * @memberOf _
  13645. * @since 3.0.0
  13646. * @category String
  13647. * @param {string} [string=''] The string to inspect.
  13648. * @param {RegExp|string} [pattern] The pattern to match words.
  13649. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  13650. * @returns {Array} Returns the words of `string`.
  13651. * @example
  13652. *
  13653. * _.words('fred, barney, & pebbles');
  13654. * // => ['fred', 'barney', 'pebbles']
  13655. *
  13656. * _.words('fred, barney, & pebbles', /[^, ]+/g);
  13657. * // => ['fred', 'barney', '&', 'pebbles']
  13658. */
  13659. function words(string, pattern, guard) {
  13660. string = toString(string);
  13661. pattern = guard ? undefined : pattern;
  13662. if (pattern === undefined) {
  13663. pattern = reHasComplexWord.test(string) ? reComplexWord : reBasicWord;
  13664. }
  13665. return string.match(pattern) || [];
  13666. }
  13667. /*------------------------------------------------------------------------*/
  13668. /**
  13669. * Attempts to invoke `func`, returning either the result or the caught error
  13670. * object. Any additional arguments are provided to `func` when it's invoked.
  13671. *
  13672. * @static
  13673. * @memberOf _
  13674. * @since 3.0.0
  13675. * @category Util
  13676. * @param {Function} func The function to attempt.
  13677. * @param {...*} [args] The arguments to invoke `func` with.
  13678. * @returns {*} Returns the `func` result or error object.
  13679. * @example
  13680. *
  13681. * // Avoid throwing errors for invalid selectors.
  13682. * var elements = _.attempt(function(selector) {
  13683. * return document.querySelectorAll(selector);
  13684. * }, '>_>');
  13685. *
  13686. * if (_.isError(elements)) {
  13687. * elements = [];
  13688. * }
  13689. */
  13690. var attempt = rest(function(func, args) {
  13691. try {
  13692. return apply(func, undefined, args);
  13693. } catch (e) {
  13694. return isError(e) ? e : new Error(e);
  13695. }
  13696. });
  13697. /**
  13698. * Binds methods of an object to the object itself, overwriting the existing
  13699. * method.
  13700. *
  13701. * **Note:** This method doesn't set the "length" property of bound functions.
  13702. *
  13703. * @static
  13704. * @since 0.1.0
  13705. * @memberOf _
  13706. * @category Util
  13707. * @param {Object} object The object to bind and assign the bound methods to.
  13708. * @param {...(string|string[])} methodNames The object method names to bind.
  13709. * @returns {Object} Returns `object`.
  13710. * @example
  13711. *
  13712. * var view = {
  13713. * 'label': 'docs',
  13714. * 'onClick': function() {
  13715. * console.log('clicked ' + this.label);
  13716. * }
  13717. * };
  13718. *
  13719. * _.bindAll(view, ['onClick']);
  13720. * jQuery(element).on('click', view.onClick);
  13721. * // => Logs 'clicked docs' when clicked.
  13722. */
  13723. var bindAll = rest(function(object, methodNames) {
  13724. arrayEach(baseFlatten(methodNames, 1), function(key) {
  13725. key = toKey(key);
  13726. object[key] = bind(object[key], object);
  13727. });
  13728. return object;
  13729. });
  13730. /**
  13731. * Creates a function that iterates over `pairs` and invokes the corresponding
  13732. * function of the first predicate to return truthy. The predicate-function
  13733. * pairs are invoked with the `this` binding and arguments of the created
  13734. * function.
  13735. *
  13736. * @static
  13737. * @memberOf _
  13738. * @since 4.0.0
  13739. * @category Util
  13740. * @param {Array} pairs The predicate-function pairs.
  13741. * @returns {Function} Returns the new composite function.
  13742. * @example
  13743. *
  13744. * var func = _.cond([
  13745. * [_.matches({ 'a': 1 }), _.constant('matches A')],
  13746. * [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
  13747. * [_.constant(true), _.constant('no match')]
  13748. * ]);
  13749. *
  13750. * func({ 'a': 1, 'b': 2 });
  13751. * // => 'matches A'
  13752. *
  13753. * func({ 'a': 0, 'b': 1 });
  13754. * // => 'matches B'
  13755. *
  13756. * func({ 'a': '1', 'b': '2' });
  13757. * // => 'no match'
  13758. */
  13759. function cond(pairs) {
  13760. var length = pairs ? pairs.length : 0,
  13761. toIteratee = getIteratee();
  13762. pairs = !length ? [] : arrayMap(pairs, function(pair) {
  13763. if (typeof pair[1] != 'function') {
  13764. throw new TypeError(FUNC_ERROR_TEXT);
  13765. }
  13766. return [toIteratee(pair[0]), pair[1]];
  13767. });
  13768. return rest(function(args) {
  13769. var index = -1;
  13770. while (++index < length) {
  13771. var pair = pairs[index];
  13772. if (apply(pair[0], this, args)) {
  13773. return apply(pair[1], this, args);
  13774. }
  13775. }
  13776. });
  13777. }
  13778. /**
  13779. * Creates a function that invokes the predicate properties of `source` with
  13780. * the corresponding property values of a given object, returning `true` if
  13781. * all predicates return truthy, else `false`.
  13782. *
  13783. * @static
  13784. * @memberOf _
  13785. * @since 4.0.0
  13786. * @category Util
  13787. * @param {Object} source The object of property predicates to conform to.
  13788. * @returns {Function} Returns the new spec function.
  13789. * @example
  13790. *
  13791. * var users = [
  13792. * { 'user': 'barney', 'age': 36 },
  13793. * { 'user': 'fred', 'age': 40 }
  13794. * ];
  13795. *
  13796. * _.filter(users, _.conforms({ 'age': function(n) { return n > 38; } }));
  13797. * // => [{ 'user': 'fred', 'age': 40 }]
  13798. */
  13799. function conforms(source) {
  13800. return baseConforms(baseClone(source, true));
  13801. }
  13802. /**
  13803. * Creates a function that returns `value`.
  13804. *
  13805. * @static
  13806. * @memberOf _
  13807. * @since 2.4.0
  13808. * @category Util
  13809. * @param {*} value The value to return from the new function.
  13810. * @returns {Function} Returns the new constant function.
  13811. * @example
  13812. *
  13813. * var objects = _.times(2, _.constant({ 'a': 1 }));
  13814. *
  13815. * console.log(objects);
  13816. * // => [{ 'a': 1 }, { 'a': 1 }]
  13817. *
  13818. * console.log(objects[0] === objects[1]);
  13819. * // => true
  13820. */
  13821. function constant(value) {
  13822. return function() {
  13823. return value;
  13824. };
  13825. }
  13826. /**
  13827. * Creates a function that returns the result of invoking the given functions
  13828. * with the `this` binding of the created function, where each successive
  13829. * invocation is supplied the return value of the previous.
  13830. *
  13831. * @static
  13832. * @memberOf _
  13833. * @since 3.0.0
  13834. * @category Util
  13835. * @param {...(Function|Function[])} [funcs] Functions to invoke.
  13836. * @returns {Function} Returns the new composite function.
  13837. * @see _.flowRight
  13838. * @example
  13839. *
  13840. * function square(n) {
  13841. * return n * n;
  13842. * }
  13843. *
  13844. * var addSquare = _.flow([_.add, square]);
  13845. * addSquare(1, 2);
  13846. * // => 9
  13847. */
  13848. var flow = createFlow();
  13849. /**
  13850. * This method is like `_.flow` except that it creates a function that
  13851. * invokes the given functions from right to left.
  13852. *
  13853. * @static
  13854. * @since 3.0.0
  13855. * @memberOf _
  13856. * @category Util
  13857. * @param {...(Function|Function[])} [funcs] Functions to invoke.
  13858. * @returns {Function} Returns the new composite function.
  13859. * @see _.flow
  13860. * @example
  13861. *
  13862. * function square(n) {
  13863. * return n * n;
  13864. * }
  13865. *
  13866. * var addSquare = _.flowRight([square, _.add]);
  13867. * addSquare(1, 2);
  13868. * // => 9
  13869. */
  13870. var flowRight = createFlow(true);
  13871. /**
  13872. * This method returns the first argument given to it.
  13873. *
  13874. * @static
  13875. * @since 0.1.0
  13876. * @memberOf _
  13877. * @category Util
  13878. * @param {*} value Any value.
  13879. * @returns {*} Returns `value`.
  13880. * @example
  13881. *
  13882. * var object = { 'user': 'fred' };
  13883. *
  13884. * console.log(_.identity(object) === object);
  13885. * // => true
  13886. */
  13887. function identity(value) {
  13888. return value;
  13889. }
  13890. /**
  13891. * Creates a function that invokes `func` with the arguments of the created
  13892. * function. If `func` is a property name, the created function returns the
  13893. * property value for a given element. If `func` is an array or object, the
  13894. * created function returns `true` for elements that contain the equivalent
  13895. * source properties, otherwise it returns `false`.
  13896. *
  13897. * @static
  13898. * @since 4.0.0
  13899. * @memberOf _
  13900. * @category Util
  13901. * @param {*} [func=_.identity] The value to convert to a callback.
  13902. * @returns {Function} Returns the callback.
  13903. * @example
  13904. *
  13905. * var users = [
  13906. * { 'user': 'barney', 'age': 36, 'active': true },
  13907. * { 'user': 'fred', 'age': 40, 'active': false }
  13908. * ];
  13909. *
  13910. * // The `_.matches` iteratee shorthand.
  13911. * _.filter(users, _.iteratee({ 'user': 'barney', 'active': true }));
  13912. * // => [{ 'user': 'barney', 'age': 36, 'active': true }]
  13913. *
  13914. * // The `_.matchesProperty` iteratee shorthand.
  13915. * _.filter(users, _.iteratee(['user', 'fred']));
  13916. * // => [{ 'user': 'fred', 'age': 40 }]
  13917. *
  13918. * // The `_.property` iteratee shorthand.
  13919. * _.map(users, _.iteratee('user'));
  13920. * // => ['barney', 'fred']
  13921. *
  13922. * // Create custom iteratee shorthands.
  13923. * _.iteratee = _.wrap(_.iteratee, function(iteratee, func) {
  13924. * return !_.isRegExp(func) ? iteratee(func) : function(string) {
  13925. * return func.test(string);
  13926. * };
  13927. * });
  13928. *
  13929. * _.filter(['abc', 'def'], /ef/);
  13930. * // => ['def']
  13931. */
  13932. function iteratee(func) {
  13933. return baseIteratee(typeof func == 'function' ? func : baseClone(func, true));
  13934. }
  13935. /**
  13936. * Creates a function that performs a partial deep comparison between a given
  13937. * object and `source`, returning `true` if the given object has equivalent
  13938. * property values, else `false`. The created function is equivalent to
  13939. * `_.isMatch` with a `source` partially applied.
  13940. *
  13941. * **Note:** This method supports comparing the same values as `_.isEqual`.
  13942. *
  13943. * @static
  13944. * @memberOf _
  13945. * @since 3.0.0
  13946. * @category Util
  13947. * @param {Object} source The object of property values to match.
  13948. * @returns {Function} Returns the new spec function.
  13949. * @example
  13950. *
  13951. * var users = [
  13952. * { 'user': 'barney', 'age': 36, 'active': true },
  13953. * { 'user': 'fred', 'age': 40, 'active': false }
  13954. * ];
  13955. *
  13956. * _.filter(users, _.matches({ 'age': 40, 'active': false }));
  13957. * // => [{ 'user': 'fred', 'age': 40, 'active': false }]
  13958. */
  13959. function matches(source) {
  13960. return baseMatches(baseClone(source, true));
  13961. }
  13962. /**
  13963. * Creates a function that performs a partial deep comparison between the
  13964. * value at `path` of a given object to `srcValue`, returning `true` if the
  13965. * object value is equivalent, else `false`.
  13966. *
  13967. * **Note:** This method supports comparing the same values as `_.isEqual`.
  13968. *
  13969. * @static
  13970. * @memberOf _
  13971. * @since 3.2.0
  13972. * @category Util
  13973. * @param {Array|string} path The path of the property to get.
  13974. * @param {*} srcValue The value to match.
  13975. * @returns {Function} Returns the new spec function.
  13976. * @example
  13977. *
  13978. * var users = [
  13979. * { 'user': 'barney' },
  13980. * { 'user': 'fred' }
  13981. * ];
  13982. *
  13983. * _.find(users, _.matchesProperty('user', 'fred'));
  13984. * // => { 'user': 'fred' }
  13985. */
  13986. function matchesProperty(path, srcValue) {
  13987. return baseMatchesProperty(path, baseClone(srcValue, true));
  13988. }
  13989. /**
  13990. * Creates a function that invokes the method at `path` of a given object.
  13991. * Any additional arguments are provided to the invoked method.
  13992. *
  13993. * @static
  13994. * @memberOf _
  13995. * @since 3.7.0
  13996. * @category Util
  13997. * @param {Array|string} path The path of the method to invoke.
  13998. * @param {...*} [args] The arguments to invoke the method with.
  13999. * @returns {Function} Returns the new invoker function.
  14000. * @example
  14001. *
  14002. * var objects = [
  14003. * { 'a': { 'b': _.constant(2) } },
  14004. * { 'a': { 'b': _.constant(1) } }
  14005. * ];
  14006. *
  14007. * _.map(objects, _.method('a.b'));
  14008. * // => [2, 1]
  14009. *
  14010. * _.map(objects, _.method(['a', 'b']));
  14011. * // => [2, 1]
  14012. */
  14013. var method = rest(function(path, args) {
  14014. return function(object) {
  14015. return baseInvoke(object, path, args);
  14016. };
  14017. });
  14018. /**
  14019. * The opposite of `_.method`; this method creates a function that invokes
  14020. * the method at a given path of `object`. Any additional arguments are
  14021. * provided to the invoked method.
  14022. *
  14023. * @static
  14024. * @memberOf _
  14025. * @since 3.7.0
  14026. * @category Util
  14027. * @param {Object} object The object to query.
  14028. * @param {...*} [args] The arguments to invoke the method with.
  14029. * @returns {Function} Returns the new invoker function.
  14030. * @example
  14031. *
  14032. * var array = _.times(3, _.constant),
  14033. * object = { 'a': array, 'b': array, 'c': array };
  14034. *
  14035. * _.map(['a[2]', 'c[0]'], _.methodOf(object));
  14036. * // => [2, 0]
  14037. *
  14038. * _.map([['a', '2'], ['c', '0']], _.methodOf(object));
  14039. * // => [2, 0]
  14040. */
  14041. var methodOf = rest(function(object, args) {
  14042. return function(path) {
  14043. return baseInvoke(object, path, args);
  14044. };
  14045. });
  14046. /**
  14047. * Adds all own enumerable string keyed function properties of a source
  14048. * object to the destination object. If `object` is a function, then methods
  14049. * are added to its prototype as well.
  14050. *
  14051. * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
  14052. * avoid conflicts caused by modifying the original.
  14053. *
  14054. * @static
  14055. * @since 0.1.0
  14056. * @memberOf _
  14057. * @category Util
  14058. * @param {Function|Object} [object=lodash] The destination object.
  14059. * @param {Object} source The object of functions to add.
  14060. * @param {Object} [options={}] The options object.
  14061. * @param {boolean} [options.chain=true] Specify whether mixins are chainable.
  14062. * @returns {Function|Object} Returns `object`.
  14063. * @example
  14064. *
  14065. * function vowels(string) {
  14066. * return _.filter(string, function(v) {
  14067. * return /[aeiou]/i.test(v);
  14068. * });
  14069. * }
  14070. *
  14071. * _.mixin({ 'vowels': vowels });
  14072. * _.vowels('fred');
  14073. * // => ['e']
  14074. *
  14075. * _('fred').vowels().value();
  14076. * // => ['e']
  14077. *
  14078. * _.mixin({ 'vowels': vowels }, { 'chain': false });
  14079. * _('fred').vowels();
  14080. * // => ['e']
  14081. */
  14082. function mixin(object, source, options) {
  14083. var props = keys(source),
  14084. methodNames = baseFunctions(source, props);
  14085. if (options == null &&
  14086. !(isObject(source) && (methodNames.length || !props.length))) {
  14087. options = source;
  14088. source = object;
  14089. object = this;
  14090. methodNames = baseFunctions(source, keys(source));
  14091. }
  14092. var chain = !(isObject(options) && 'chain' in options) || !!options.chain,
  14093. isFunc = isFunction(object);
  14094. arrayEach(methodNames, function(methodName) {
  14095. var func = source[methodName];
  14096. object[methodName] = func;
  14097. if (isFunc) {
  14098. object.prototype[methodName] = function() {
  14099. var chainAll = this.__chain__;
  14100. if (chain || chainAll) {
  14101. var result = object(this.__wrapped__),
  14102. actions = result.__actions__ = copyArray(this.__actions__);
  14103. actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
  14104. result.__chain__ = chainAll;
  14105. return result;
  14106. }
  14107. return func.apply(object, arrayPush([this.value()], arguments));
  14108. };
  14109. }
  14110. });
  14111. return object;
  14112. }
  14113. /**
  14114. * Reverts the `_` variable to its previous value and returns a reference to
  14115. * the `lodash` function.
  14116. *
  14117. * @static
  14118. * @since 0.1.0
  14119. * @memberOf _
  14120. * @category Util
  14121. * @returns {Function} Returns the `lodash` function.
  14122. * @example
  14123. *
  14124. * var lodash = _.noConflict();
  14125. */
  14126. function noConflict() {
  14127. if (root._ === this) {
  14128. root._ = oldDash;
  14129. }
  14130. return this;
  14131. }
  14132. /**
  14133. * A method that returns `undefined`.
  14134. *
  14135. * @static
  14136. * @memberOf _
  14137. * @since 2.3.0
  14138. * @category Util
  14139. * @example
  14140. *
  14141. * _.times(2, _.noop);
  14142. * // => [undefined, undefined]
  14143. */
  14144. function noop() {
  14145. // No operation performed.
  14146. }
  14147. /**
  14148. * Creates a function that gets the argument at index `n`. If `n` is negative,
  14149. * the nth argument from the end is returned.
  14150. *
  14151. * @static
  14152. * @memberOf _
  14153. * @since 4.0.0
  14154. * @category Util
  14155. * @param {number} [n=0] The index of the argument to return.
  14156. * @returns {Function} Returns the new pass-thru function.
  14157. * @example
  14158. *
  14159. * var func = _.nthArg(1);
  14160. * func('a', 'b', 'c', 'd');
  14161. * // => 'b'
  14162. *
  14163. * var func = _.nthArg(-2);
  14164. * func('a', 'b', 'c', 'd');
  14165. * // => 'c'
  14166. */
  14167. function nthArg(n) {
  14168. n = toInteger(n);
  14169. return rest(function(args) {
  14170. return baseNth(args, n);
  14171. });
  14172. }
  14173. /**
  14174. * Creates a function that invokes `iteratees` with the arguments it receives
  14175. * and returns their results.
  14176. *
  14177. * @static
  14178. * @memberOf _
  14179. * @since 4.0.0
  14180. * @category Util
  14181. * @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
  14182. * [iteratees=[_.identity]] The iteratees to invoke.
  14183. * @returns {Function} Returns the new function.
  14184. * @example
  14185. *
  14186. * var func = _.over([Math.max, Math.min]);
  14187. *
  14188. * func(1, 2, 3, 4);
  14189. * // => [4, 1]
  14190. */
  14191. var over = createOver(arrayMap);
  14192. /**
  14193. * Creates a function that checks if **all** of the `predicates` return
  14194. * truthy when invoked with the arguments it receives.
  14195. *
  14196. * @static
  14197. * @memberOf _
  14198. * @since 4.0.0
  14199. * @category Util
  14200. * @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
  14201. * [predicates=[_.identity]] The predicates to check.
  14202. * @returns {Function} Returns the new function.
  14203. * @example
  14204. *
  14205. * var func = _.overEvery([Boolean, isFinite]);
  14206. *
  14207. * func('1');
  14208. * // => true
  14209. *
  14210. * func(null);
  14211. * // => false
  14212. *
  14213. * func(NaN);
  14214. * // => false
  14215. */
  14216. var overEvery = createOver(arrayEvery);
  14217. /**
  14218. * Creates a function that checks if **any** of the `predicates` return
  14219. * truthy when invoked with the arguments it receives.
  14220. *
  14221. * @static
  14222. * @memberOf _
  14223. * @since 4.0.0
  14224. * @category Util
  14225. * @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
  14226. * [predicates=[_.identity]] The predicates to check.
  14227. * @returns {Function} Returns the new function.
  14228. * @example
  14229. *
  14230. * var func = _.overSome([Boolean, isFinite]);
  14231. *
  14232. * func('1');
  14233. * // => true
  14234. *
  14235. * func(null);
  14236. * // => true
  14237. *
  14238. * func(NaN);
  14239. * // => false
  14240. */
  14241. var overSome = createOver(arraySome);
  14242. /**
  14243. * Creates a function that returns the value at `path` of a given object.
  14244. *
  14245. * @static
  14246. * @memberOf _
  14247. * @since 2.4.0
  14248. * @category Util
  14249. * @param {Array|string} path The path of the property to get.
  14250. * @returns {Function} Returns the new accessor function.
  14251. * @example
  14252. *
  14253. * var objects = [
  14254. * { 'a': { 'b': 2 } },
  14255. * { 'a': { 'b': 1 } }
  14256. * ];
  14257. *
  14258. * _.map(objects, _.property('a.b'));
  14259. * // => [2, 1]
  14260. *
  14261. * _.map(_.sortBy(objects, _.property(['a', 'b'])), 'a.b');
  14262. * // => [1, 2]
  14263. */
  14264. function property(path) {
  14265. return isKey(path) ? baseProperty(toKey(path)) : basePropertyDeep(path);
  14266. }
  14267. /**
  14268. * The opposite of `_.property`; this method creates a function that returns
  14269. * the value at a given path of `object`.
  14270. *
  14271. * @static
  14272. * @memberOf _
  14273. * @since 3.0.0
  14274. * @category Util
  14275. * @param {Object} object The object to query.
  14276. * @returns {Function} Returns the new accessor function.
  14277. * @example
  14278. *
  14279. * var array = [0, 1, 2],
  14280. * object = { 'a': array, 'b': array, 'c': array };
  14281. *
  14282. * _.map(['a[2]', 'c[0]'], _.propertyOf(object));
  14283. * // => [2, 0]
  14284. *
  14285. * _.map([['a', '2'], ['c', '0']], _.propertyOf(object));
  14286. * // => [2, 0]
  14287. */
  14288. function propertyOf(object) {
  14289. return function(path) {
  14290. return object == null ? undefined : baseGet(object, path);
  14291. };
  14292. }
  14293. /**
  14294. * Creates an array of numbers (positive and/or negative) progressing from
  14295. * `start` up to, but not including, `end`. A step of `-1` is used if a negative
  14296. * `start` is specified without an `end` or `step`. If `end` is not specified,
  14297. * it's set to `start` with `start` then set to `0`.
  14298. *
  14299. * **Note:** JavaScript follows the IEEE-754 standard for resolving
  14300. * floating-point values which can produce unexpected results.
  14301. *
  14302. * @static
  14303. * @since 0.1.0
  14304. * @memberOf _
  14305. * @category Util
  14306. * @param {number} [start=0] The start of the range.
  14307. * @param {number} end The end of the range.
  14308. * @param {number} [step=1] The value to increment or decrement by.
  14309. * @returns {Array} Returns the range of numbers.
  14310. * @see _.inRange, _.rangeRight
  14311. * @example
  14312. *
  14313. * _.range(4);
  14314. * // => [0, 1, 2, 3]
  14315. *
  14316. * _.range(-4);
  14317. * // => [0, -1, -2, -3]
  14318. *
  14319. * _.range(1, 5);
  14320. * // => [1, 2, 3, 4]
  14321. *
  14322. * _.range(0, 20, 5);
  14323. * // => [0, 5, 10, 15]
  14324. *
  14325. * _.range(0, -4, -1);
  14326. * // => [0, -1, -2, -3]
  14327. *
  14328. * _.range(1, 4, 0);
  14329. * // => [1, 1, 1]
  14330. *
  14331. * _.range(0);
  14332. * // => []
  14333. */
  14334. var range = createRange();
  14335. /**
  14336. * This method is like `_.range` except that it populates values in
  14337. * descending order.
  14338. *
  14339. * @static
  14340. * @memberOf _
  14341. * @since 4.0.0
  14342. * @category Util
  14343. * @param {number} [start=0] The start of the range.
  14344. * @param {number} end The end of the range.
  14345. * @param {number} [step=1] The value to increment or decrement by.
  14346. * @returns {Array} Returns the range of numbers.
  14347. * @see _.inRange, _.range
  14348. * @example
  14349. *
  14350. * _.rangeRight(4);
  14351. * // => [3, 2, 1, 0]
  14352. *
  14353. * _.rangeRight(-4);
  14354. * // => [-3, -2, -1, 0]
  14355. *
  14356. * _.rangeRight(1, 5);
  14357. * // => [4, 3, 2, 1]
  14358. *
  14359. * _.rangeRight(0, 20, 5);
  14360. * // => [15, 10, 5, 0]
  14361. *
  14362. * _.rangeRight(0, -4, -1);
  14363. * // => [-3, -2, -1, 0]
  14364. *
  14365. * _.rangeRight(1, 4, 0);
  14366. * // => [1, 1, 1]
  14367. *
  14368. * _.rangeRight(0);
  14369. * // => []
  14370. */
  14371. var rangeRight = createRange(true);
  14372. /**
  14373. * A method that returns a new empty array.
  14374. *
  14375. * @static
  14376. * @memberOf _
  14377. * @since 4.13.0
  14378. * @category Util
  14379. * @returns {Array} Returns the new empty array.
  14380. * @example
  14381. *
  14382. * var arrays = _.times(2, _.stubArray);
  14383. *
  14384. * console.log(arrays);
  14385. * // => [[], []]
  14386. *
  14387. * console.log(arrays[0] === arrays[1]);
  14388. * // => false
  14389. */
  14390. function stubArray() {
  14391. return [];
  14392. }
  14393. /**
  14394. * A method that returns `false`.
  14395. *
  14396. * @static
  14397. * @memberOf _
  14398. * @since 4.13.0
  14399. * @category Util
  14400. * @returns {boolean} Returns `false`.
  14401. * @example
  14402. *
  14403. * _.times(2, _.stubFalse);
  14404. * // => [false, false]
  14405. */
  14406. function stubFalse() {
  14407. return false;
  14408. }
  14409. /**
  14410. * A method that returns a new empty object.
  14411. *
  14412. * @static
  14413. * @memberOf _
  14414. * @since 4.13.0
  14415. * @category Util
  14416. * @returns {Object} Returns the new empty object.
  14417. * @example
  14418. *
  14419. * var objects = _.times(2, _.stubObject);
  14420. *
  14421. * console.log(objects);
  14422. * // => [{}, {}]
  14423. *
  14424. * console.log(objects[0] === objects[1]);
  14425. * // => false
  14426. */
  14427. function stubObject() {
  14428. return {};
  14429. }
  14430. /**
  14431. * A method that returns an empty string.
  14432. *
  14433. * @static
  14434. * @memberOf _
  14435. * @since 4.13.0
  14436. * @category Util
  14437. * @returns {string} Returns the empty string.
  14438. * @example
  14439. *
  14440. * _.times(2, _.stubString);
  14441. * // => ['', '']
  14442. */
  14443. function stubString() {
  14444. return '';
  14445. }
  14446. /**
  14447. * A method that returns `true`.
  14448. *
  14449. * @static
  14450. * @memberOf _
  14451. * @since 4.13.0
  14452. * @category Util
  14453. * @returns {boolean} Returns `true`.
  14454. * @example
  14455. *
  14456. * _.times(2, _.stubTrue);
  14457. * // => [true, true]
  14458. */
  14459. function stubTrue() {
  14460. return true;
  14461. }
  14462. /**
  14463. * Invokes the iteratee `n` times, returning an array of the results of
  14464. * each invocation. The iteratee is invoked with one argument; (index).
  14465. *
  14466. * @static
  14467. * @since 0.1.0
  14468. * @memberOf _
  14469. * @category Util
  14470. * @param {number} n The number of times to invoke `iteratee`.
  14471. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  14472. * @returns {Array} Returns the array of results.
  14473. * @example
  14474. *
  14475. * _.times(3, String);
  14476. * // => ['0', '1', '2']
  14477. *
  14478. * _.times(4, _.constant(0));
  14479. * // => [0, 0, 0, 0]
  14480. */
  14481. function times(n, iteratee) {
  14482. n = toInteger(n);
  14483. if (n < 1 || n > MAX_SAFE_INTEGER) {
  14484. return [];
  14485. }
  14486. var index = MAX_ARRAY_LENGTH,
  14487. length = nativeMin(n, MAX_ARRAY_LENGTH);
  14488. iteratee = getIteratee(iteratee);
  14489. n -= MAX_ARRAY_LENGTH;
  14490. var result = baseTimes(length, iteratee);
  14491. while (++index < n) {
  14492. iteratee(index);
  14493. }
  14494. return result;
  14495. }
  14496. /**
  14497. * Converts `value` to a property path array.
  14498. *
  14499. * @static
  14500. * @memberOf _
  14501. * @since 4.0.0
  14502. * @category Util
  14503. * @param {*} value The value to convert.
  14504. * @returns {Array} Returns the new property path array.
  14505. * @example
  14506. *
  14507. * _.toPath('a.b.c');
  14508. * // => ['a', 'b', 'c']
  14509. *
  14510. * _.toPath('a[0].b.c');
  14511. * // => ['a', '0', 'b', 'c']
  14512. */
  14513. function toPath(value) {
  14514. if (isArray(value)) {
  14515. return arrayMap(value, toKey);
  14516. }
  14517. return isSymbol(value) ? [value] : copyArray(stringToPath(value));
  14518. }
  14519. /**
  14520. * Generates a unique ID. If `prefix` is given, the ID is appended to it.
  14521. *
  14522. * @static
  14523. * @since 0.1.0
  14524. * @memberOf _
  14525. * @category Util
  14526. * @param {string} [prefix=''] The value to prefix the ID with.
  14527. * @returns {string} Returns the unique ID.
  14528. * @example
  14529. *
  14530. * _.uniqueId('contact_');
  14531. * // => 'contact_104'
  14532. *
  14533. * _.uniqueId();
  14534. * // => '105'
  14535. */
  14536. function uniqueId(prefix) {
  14537. var id = ++idCounter;
  14538. return toString(prefix) + id;
  14539. }
  14540. /*------------------------------------------------------------------------*/
  14541. /**
  14542. * Adds two numbers.
  14543. *
  14544. * @static
  14545. * @memberOf _
  14546. * @since 3.4.0
  14547. * @category Math
  14548. * @param {number} augend The first number in an addition.
  14549. * @param {number} addend The second number in an addition.
  14550. * @returns {number} Returns the total.
  14551. * @example
  14552. *
  14553. * _.add(6, 4);
  14554. * // => 10
  14555. */
  14556. var add = createMathOperation(function(augend, addend) {
  14557. return augend + addend;
  14558. });
  14559. /**
  14560. * Computes `number` rounded up to `precision`.
  14561. *
  14562. * @static
  14563. * @memberOf _
  14564. * @since 3.10.0
  14565. * @category Math
  14566. * @param {number} number The number to round up.
  14567. * @param {number} [precision=0] The precision to round up to.
  14568. * @returns {number} Returns the rounded up number.
  14569. * @example
  14570. *
  14571. * _.ceil(4.006);
  14572. * // => 5
  14573. *
  14574. * _.ceil(6.004, 2);
  14575. * // => 6.01
  14576. *
  14577. * _.ceil(6040, -2);
  14578. * // => 6100
  14579. */
  14580. var ceil = createRound('ceil');
  14581. /**
  14582. * Divide two numbers.
  14583. *
  14584. * @static
  14585. * @memberOf _
  14586. * @since 4.7.0
  14587. * @category Math
  14588. * @param {number} dividend The first number in a division.
  14589. * @param {number} divisor The second number in a division.
  14590. * @returns {number} Returns the quotient.
  14591. * @example
  14592. *
  14593. * _.divide(6, 4);
  14594. * // => 1.5
  14595. */
  14596. var divide = createMathOperation(function(dividend, divisor) {
  14597. return dividend / divisor;
  14598. });
  14599. /**
  14600. * Computes `number` rounded down to `precision`.
  14601. *
  14602. * @static
  14603. * @memberOf _
  14604. * @since 3.10.0
  14605. * @category Math
  14606. * @param {number} number The number to round down.
  14607. * @param {number} [precision=0] The precision to round down to.
  14608. * @returns {number} Returns the rounded down number.
  14609. * @example
  14610. *
  14611. * _.floor(4.006);
  14612. * // => 4
  14613. *
  14614. * _.floor(0.046, 2);
  14615. * // => 0.04
  14616. *
  14617. * _.floor(4060, -2);
  14618. * // => 4000
  14619. */
  14620. var floor = createRound('floor');
  14621. /**
  14622. * Computes the maximum value of `array`. If `array` is empty or falsey,
  14623. * `undefined` is returned.
  14624. *
  14625. * @static
  14626. * @since 0.1.0
  14627. * @memberOf _
  14628. * @category Math
  14629. * @param {Array} array The array to iterate over.
  14630. * @returns {*} Returns the maximum value.
  14631. * @example
  14632. *
  14633. * _.max([4, 2, 8, 6]);
  14634. * // => 8
  14635. *
  14636. * _.max([]);
  14637. * // => undefined
  14638. */
  14639. function max(array) {
  14640. return (array && array.length)
  14641. ? baseExtremum(array, identity, baseGt)
  14642. : undefined;
  14643. }
  14644. /**
  14645. * This method is like `_.max` except that it accepts `iteratee` which is
  14646. * invoked for each element in `array` to generate the criterion by which
  14647. * the value is ranked. The iteratee is invoked with one argument: (value).
  14648. *
  14649. * @static
  14650. * @memberOf _
  14651. * @since 4.0.0
  14652. * @category Math
  14653. * @param {Array} array The array to iterate over.
  14654. * @param {Array|Function|Object|string} [iteratee=_.identity]
  14655. * The iteratee invoked per element.
  14656. * @returns {*} Returns the maximum value.
  14657. * @example
  14658. *
  14659. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  14660. *
  14661. * _.maxBy(objects, function(o) { return o.n; });
  14662. * // => { 'n': 2 }
  14663. *
  14664. * // The `_.property` iteratee shorthand.
  14665. * _.maxBy(objects, 'n');
  14666. * // => { 'n': 2 }
  14667. */
  14668. function maxBy(array, iteratee) {
  14669. return (array && array.length)
  14670. ? baseExtremum(array, getIteratee(iteratee), baseGt)
  14671. : undefined;
  14672. }
  14673. /**
  14674. * Computes the mean of the values in `array`.
  14675. *
  14676. * @static
  14677. * @memberOf _
  14678. * @since 4.0.0
  14679. * @category Math
  14680. * @param {Array} array The array to iterate over.
  14681. * @returns {number} Returns the mean.
  14682. * @example
  14683. *
  14684. * _.mean([4, 2, 8, 6]);
  14685. * // => 5
  14686. */
  14687. function mean(array) {
  14688. return baseMean(array, identity);
  14689. }
  14690. /**
  14691. * This method is like `_.mean` except that it accepts `iteratee` which is
  14692. * invoked for each element in `array` to generate the value to be averaged.
  14693. * The iteratee is invoked with one argument: (value).
  14694. *
  14695. * @static
  14696. * @memberOf _
  14697. * @since 4.7.0
  14698. * @category Math
  14699. * @param {Array} array The array to iterate over.
  14700. * @param {Array|Function|Object|string} [iteratee=_.identity]
  14701. * The iteratee invoked per element.
  14702. * @returns {number} Returns the mean.
  14703. * @example
  14704. *
  14705. * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
  14706. *
  14707. * _.meanBy(objects, function(o) { return o.n; });
  14708. * // => 5
  14709. *
  14710. * // The `_.property` iteratee shorthand.
  14711. * _.meanBy(objects, 'n');
  14712. * // => 5
  14713. */
  14714. function meanBy(array, iteratee) {
  14715. return baseMean(array, getIteratee(iteratee));
  14716. }
  14717. /**
  14718. * Computes the minimum value of `array`. If `array` is empty or falsey,
  14719. * `undefined` is returned.
  14720. *
  14721. * @static
  14722. * @since 0.1.0
  14723. * @memberOf _
  14724. * @category Math
  14725. * @param {Array} array The array to iterate over.
  14726. * @returns {*} Returns the minimum value.
  14727. * @example
  14728. *
  14729. * _.min([4, 2, 8, 6]);
  14730. * // => 2
  14731. *
  14732. * _.min([]);
  14733. * // => undefined
  14734. */
  14735. function min(array) {
  14736. return (array && array.length)
  14737. ? baseExtremum(array, identity, baseLt)
  14738. : undefined;
  14739. }
  14740. /**
  14741. * This method is like `_.min` except that it accepts `iteratee` which is
  14742. * invoked for each element in `array` to generate the criterion by which
  14743. * the value is ranked. The iteratee is invoked with one argument: (value).
  14744. *
  14745. * @static
  14746. * @memberOf _
  14747. * @since 4.0.0
  14748. * @category Math
  14749. * @param {Array} array The array to iterate over.
  14750. * @param {Array|Function|Object|string} [iteratee=_.identity]
  14751. * The iteratee invoked per element.
  14752. * @returns {*} Returns the minimum value.
  14753. * @example
  14754. *
  14755. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  14756. *
  14757. * _.minBy(objects, function(o) { return o.n; });
  14758. * // => { 'n': 1 }
  14759. *
  14760. * // The `_.property` iteratee shorthand.
  14761. * _.minBy(objects, 'n');
  14762. * // => { 'n': 1 }
  14763. */
  14764. function minBy(array, iteratee) {
  14765. return (array && array.length)
  14766. ? baseExtremum(array, getIteratee(iteratee), baseLt)
  14767. : undefined;
  14768. }
  14769. /**
  14770. * Multiply two numbers.
  14771. *
  14772. * @static
  14773. * @memberOf _
  14774. * @since 4.7.0
  14775. * @category Math
  14776. * @param {number} multiplier The first number in a multiplication.
  14777. * @param {number} multiplicand The second number in a multiplication.
  14778. * @returns {number} Returns the product.
  14779. * @example
  14780. *
  14781. * _.multiply(6, 4);
  14782. * // => 24
  14783. */
  14784. var multiply = createMathOperation(function(multiplier, multiplicand) {
  14785. return multiplier * multiplicand;
  14786. });
  14787. /**
  14788. * Computes `number` rounded to `precision`.
  14789. *
  14790. * @static
  14791. * @memberOf _
  14792. * @since 3.10.0
  14793. * @category Math
  14794. * @param {number} number The number to round.
  14795. * @param {number} [precision=0] The precision to round to.
  14796. * @returns {number} Returns the rounded number.
  14797. * @example
  14798. *
  14799. * _.round(4.006);
  14800. * // => 4
  14801. *
  14802. * _.round(4.006, 2);
  14803. * // => 4.01
  14804. *
  14805. * _.round(4060, -2);
  14806. * // => 4100
  14807. */
  14808. var round = createRound('round');
  14809. /**
  14810. * Subtract two numbers.
  14811. *
  14812. * @static
  14813. * @memberOf _
  14814. * @since 4.0.0
  14815. * @category Math
  14816. * @param {number} minuend The first number in a subtraction.
  14817. * @param {number} subtrahend The second number in a subtraction.
  14818. * @returns {number} Returns the difference.
  14819. * @example
  14820. *
  14821. * _.subtract(6, 4);
  14822. * // => 2
  14823. */
  14824. var subtract = createMathOperation(function(minuend, subtrahend) {
  14825. return minuend - subtrahend;
  14826. });
  14827. /**
  14828. * Computes the sum of the values in `array`.
  14829. *
  14830. * @static
  14831. * @memberOf _
  14832. * @since 3.4.0
  14833. * @category Math
  14834. * @param {Array} array The array to iterate over.
  14835. * @returns {number} Returns the sum.
  14836. * @example
  14837. *
  14838. * _.sum([4, 2, 8, 6]);
  14839. * // => 20
  14840. */
  14841. function sum(array) {
  14842. return (array && array.length)
  14843. ? baseSum(array, identity)
  14844. : 0;
  14845. }
  14846. /**
  14847. * This method is like `_.sum` except that it accepts `iteratee` which is
  14848. * invoked for each element in `array` to generate the value to be summed.
  14849. * The iteratee is invoked with one argument: (value).
  14850. *
  14851. * @static
  14852. * @memberOf _
  14853. * @since 4.0.0
  14854. * @category Math
  14855. * @param {Array} array The array to iterate over.
  14856. * @param {Array|Function|Object|string} [iteratee=_.identity]
  14857. * The iteratee invoked per element.
  14858. * @returns {number} Returns the sum.
  14859. * @example
  14860. *
  14861. * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
  14862. *
  14863. * _.sumBy(objects, function(o) { return o.n; });
  14864. * // => 20
  14865. *
  14866. * // The `_.property` iteratee shorthand.
  14867. * _.sumBy(objects, 'n');
  14868. * // => 20
  14869. */
  14870. function sumBy(array, iteratee) {
  14871. return (array && array.length)
  14872. ? baseSum(array, getIteratee(iteratee))
  14873. : 0;
  14874. }
  14875. /*------------------------------------------------------------------------*/
  14876. // Add methods that return wrapped values in chain sequences.
  14877. lodash.after = after;
  14878. lodash.ary = ary;
  14879. lodash.assign = assign;
  14880. lodash.assignIn = assignIn;
  14881. lodash.assignInWith = assignInWith;
  14882. lodash.assignWith = assignWith;
  14883. lodash.at = at;
  14884. lodash.before = before;
  14885. lodash.bind = bind;
  14886. lodash.bindAll = bindAll;
  14887. lodash.bindKey = bindKey;
  14888. lodash.castArray = castArray;
  14889. lodash.chain = chain;
  14890. lodash.chunk = chunk;
  14891. lodash.compact = compact;
  14892. lodash.concat = concat;
  14893. lodash.cond = cond;
  14894. lodash.conforms = conforms;
  14895. lodash.constant = constant;
  14896. lodash.countBy = countBy;
  14897. lodash.create = create;
  14898. lodash.curry = curry;
  14899. lodash.curryRight = curryRight;
  14900. lodash.debounce = debounce;
  14901. lodash.defaults = defaults;
  14902. lodash.defaultsDeep = defaultsDeep;
  14903. lodash.defer = defer;
  14904. lodash.delay = delay;
  14905. lodash.difference = difference;
  14906. lodash.differenceBy = differenceBy;
  14907. lodash.differenceWith = differenceWith;
  14908. lodash.drop = drop;
  14909. lodash.dropRight = dropRight;
  14910. lodash.dropRightWhile = dropRightWhile;
  14911. lodash.dropWhile = dropWhile;
  14912. lodash.fill = fill;
  14913. lodash.filter = filter;
  14914. lodash.flatMap = flatMap;
  14915. lodash.flatMapDeep = flatMapDeep;
  14916. lodash.flatMapDepth = flatMapDepth;
  14917. lodash.flatten = flatten;
  14918. lodash.flattenDeep = flattenDeep;
  14919. lodash.flattenDepth = flattenDepth;
  14920. lodash.flip = flip;
  14921. lodash.flow = flow;
  14922. lodash.flowRight = flowRight;
  14923. lodash.fromPairs = fromPairs;
  14924. lodash.functions = functions;
  14925. lodash.functionsIn = functionsIn;
  14926. lodash.groupBy = groupBy;
  14927. lodash.initial = initial;
  14928. lodash.intersection = intersection;
  14929. lodash.intersectionBy = intersectionBy;
  14930. lodash.intersectionWith = intersectionWith;
  14931. lodash.invert = invert;
  14932. lodash.invertBy = invertBy;
  14933. lodash.invokeMap = invokeMap;
  14934. lodash.iteratee = iteratee;
  14935. lodash.keyBy = keyBy;
  14936. lodash.keys = keys;
  14937. lodash.keysIn = keysIn;
  14938. lodash.map = map;
  14939. lodash.mapKeys = mapKeys;
  14940. lodash.mapValues = mapValues;
  14941. lodash.matches = matches;
  14942. lodash.matchesProperty = matchesProperty;
  14943. lodash.memoize = memoize;
  14944. lodash.merge = merge;
  14945. lodash.mergeWith = mergeWith;
  14946. lodash.method = method;
  14947. lodash.methodOf = methodOf;
  14948. lodash.mixin = mixin;
  14949. lodash.negate = negate;
  14950. lodash.nthArg = nthArg;
  14951. lodash.omit = omit;
  14952. lodash.omitBy = omitBy;
  14953. lodash.once = once;
  14954. lodash.orderBy = orderBy;
  14955. lodash.over = over;
  14956. lodash.overArgs = overArgs;
  14957. lodash.overEvery = overEvery;
  14958. lodash.overSome = overSome;
  14959. lodash.partial = partial;
  14960. lodash.partialRight = partialRight;
  14961. lodash.partition = partition;
  14962. lodash.pick = pick;
  14963. lodash.pickBy = pickBy;
  14964. lodash.property = property;
  14965. lodash.propertyOf = propertyOf;
  14966. lodash.pull = pull;
  14967. lodash.pullAll = pullAll;
  14968. lodash.pullAllBy = pullAllBy;
  14969. lodash.pullAllWith = pullAllWith;
  14970. lodash.pullAt = pullAt;
  14971. lodash.range = range;
  14972. lodash.rangeRight = rangeRight;
  14973. lodash.rearg = rearg;
  14974. lodash.reject = reject;
  14975. lodash.remove = remove;
  14976. lodash.rest = rest;
  14977. lodash.reverse = reverse;
  14978. lodash.sampleSize = sampleSize;
  14979. lodash.set = set;
  14980. lodash.setWith = setWith;
  14981. lodash.shuffle = shuffle;
  14982. lodash.slice = slice;
  14983. lodash.sortBy = sortBy;
  14984. lodash.sortedUniq = sortedUniq;
  14985. lodash.sortedUniqBy = sortedUniqBy;
  14986. lodash.split = split;
  14987. lodash.spread = spread;
  14988. lodash.tail = tail;
  14989. lodash.take = take;
  14990. lodash.takeRight = takeRight;
  14991. lodash.takeRightWhile = takeRightWhile;
  14992. lodash.takeWhile = takeWhile;
  14993. lodash.tap = tap;
  14994. lodash.throttle = throttle;
  14995. lodash.thru = thru;
  14996. lodash.toArray = toArray;
  14997. lodash.toPairs = toPairs;
  14998. lodash.toPairsIn = toPairsIn;
  14999. lodash.toPath = toPath;
  15000. lodash.toPlainObject = toPlainObject;
  15001. lodash.transform = transform;
  15002. lodash.unary = unary;
  15003. lodash.union = union;
  15004. lodash.unionBy = unionBy;
  15005. lodash.unionWith = unionWith;
  15006. lodash.uniq = uniq;
  15007. lodash.uniqBy = uniqBy;
  15008. lodash.uniqWith = uniqWith;
  15009. lodash.unset = unset;
  15010. lodash.unzip = unzip;
  15011. lodash.unzipWith = unzipWith;
  15012. lodash.update = update;
  15013. lodash.updateWith = updateWith;
  15014. lodash.values = values;
  15015. lodash.valuesIn = valuesIn;
  15016. lodash.without = without;
  15017. lodash.words = words;
  15018. lodash.wrap = wrap;
  15019. lodash.xor = xor;
  15020. lodash.xorBy = xorBy;
  15021. lodash.xorWith = xorWith;
  15022. lodash.zip = zip;
  15023. lodash.zipObject = zipObject;
  15024. lodash.zipObjectDeep = zipObjectDeep;
  15025. lodash.zipWith = zipWith;
  15026. // Add aliases.
  15027. lodash.entries = toPairs;
  15028. lodash.entriesIn = toPairsIn;
  15029. lodash.extend = assignIn;
  15030. lodash.extendWith = assignInWith;
  15031. // Add methods to `lodash.prototype`.
  15032. mixin(lodash, lodash);
  15033. /*------------------------------------------------------------------------*/
  15034. // Add methods that return unwrapped values in chain sequences.
  15035. lodash.add = add;
  15036. lodash.attempt = attempt;
  15037. lodash.camelCase = camelCase;
  15038. lodash.capitalize = capitalize;
  15039. lodash.ceil = ceil;
  15040. lodash.clamp = clamp;
  15041. lodash.clone = clone;
  15042. lodash.cloneDeep = cloneDeep;
  15043. lodash.cloneDeepWith = cloneDeepWith;
  15044. lodash.cloneWith = cloneWith;
  15045. lodash.deburr = deburr;
  15046. lodash.divide = divide;
  15047. lodash.endsWith = endsWith;
  15048. lodash.eq = eq;
  15049. lodash.escape = escape;
  15050. lodash.escapeRegExp = escapeRegExp;
  15051. lodash.every = every;
  15052. lodash.find = find;
  15053. lodash.findIndex = findIndex;
  15054. lodash.findKey = findKey;
  15055. lodash.findLast = findLast;
  15056. lodash.findLastIndex = findLastIndex;
  15057. lodash.findLastKey = findLastKey;
  15058. lodash.floor = floor;
  15059. lodash.forEach = forEach;
  15060. lodash.forEachRight = forEachRight;
  15061. lodash.forIn = forIn;
  15062. lodash.forInRight = forInRight;
  15063. lodash.forOwn = forOwn;
  15064. lodash.forOwnRight = forOwnRight;
  15065. lodash.get = get;
  15066. lodash.gt = gt;
  15067. lodash.gte = gte;
  15068. lodash.has = has;
  15069. lodash.hasIn = hasIn;
  15070. lodash.head = head;
  15071. lodash.identity = identity;
  15072. lodash.includes = includes;
  15073. lodash.indexOf = indexOf;
  15074. lodash.inRange = inRange;
  15075. lodash.invoke = invoke;
  15076. lodash.isArguments = isArguments;
  15077. lodash.isArray = isArray;
  15078. lodash.isArrayBuffer = isArrayBuffer;
  15079. lodash.isArrayLike = isArrayLike;
  15080. lodash.isArrayLikeObject = isArrayLikeObject;
  15081. lodash.isBoolean = isBoolean;
  15082. lodash.isBuffer = isBuffer;
  15083. lodash.isDate = isDate;
  15084. lodash.isElement = isElement;
  15085. lodash.isEmpty = isEmpty;
  15086. lodash.isEqual = isEqual;
  15087. lodash.isEqualWith = isEqualWith;
  15088. lodash.isError = isError;
  15089. lodash.isFinite = isFinite;
  15090. lodash.isFunction = isFunction;
  15091. lodash.isInteger = isInteger;
  15092. lodash.isLength = isLength;
  15093. lodash.isMap = isMap;
  15094. lodash.isMatch = isMatch;
  15095. lodash.isMatchWith = isMatchWith;
  15096. lodash.isNaN = isNaN;
  15097. lodash.isNative = isNative;
  15098. lodash.isNil = isNil;
  15099. lodash.isNull = isNull;
  15100. lodash.isNumber = isNumber;
  15101. lodash.isObject = isObject;
  15102. lodash.isObjectLike = isObjectLike;
  15103. lodash.isPlainObject = isPlainObject;
  15104. lodash.isRegExp = isRegExp;
  15105. lodash.isSafeInteger = isSafeInteger;
  15106. lodash.isSet = isSet;
  15107. lodash.isString = isString;
  15108. lodash.isSymbol = isSymbol;
  15109. lodash.isTypedArray = isTypedArray;
  15110. lodash.isUndefined = isUndefined;
  15111. lodash.isWeakMap = isWeakMap;
  15112. lodash.isWeakSet = isWeakSet;
  15113. lodash.join = join;
  15114. lodash.kebabCase = kebabCase;
  15115. lodash.last = last;
  15116. lodash.lastIndexOf = lastIndexOf;
  15117. lodash.lowerCase = lowerCase;
  15118. lodash.lowerFirst = lowerFirst;
  15119. lodash.lt = lt;
  15120. lodash.lte = lte;
  15121. lodash.max = max;
  15122. lodash.maxBy = maxBy;
  15123. lodash.mean = mean;
  15124. lodash.meanBy = meanBy;
  15125. lodash.min = min;
  15126. lodash.minBy = minBy;
  15127. lodash.stubArray = stubArray;
  15128. lodash.stubFalse = stubFalse;
  15129. lodash.stubObject = stubObject;
  15130. lodash.stubString = stubString;
  15131. lodash.stubTrue = stubTrue;
  15132. lodash.multiply = multiply;
  15133. lodash.nth = nth;
  15134. lodash.noConflict = noConflict;
  15135. lodash.noop = noop;
  15136. lodash.now = now;
  15137. lodash.pad = pad;
  15138. lodash.padEnd = padEnd;
  15139. lodash.padStart = padStart;
  15140. lodash.parseInt = parseInt;
  15141. lodash.random = random;
  15142. lodash.reduce = reduce;
  15143. lodash.reduceRight = reduceRight;
  15144. lodash.repeat = repeat;
  15145. lodash.replace = replace;
  15146. lodash.result = result;
  15147. lodash.round = round;
  15148. lodash.runInContext = runInContext;
  15149. lodash.sample = sample;
  15150. lodash.size = size;
  15151. lodash.snakeCase = snakeCase;
  15152. lodash.some = some;
  15153. lodash.sortedIndex = sortedIndex;
  15154. lodash.sortedIndexBy = sortedIndexBy;
  15155. lodash.sortedIndexOf = sortedIndexOf;
  15156. lodash.sortedLastIndex = sortedLastIndex;
  15157. lodash.sortedLastIndexBy = sortedLastIndexBy;
  15158. lodash.sortedLastIndexOf = sortedLastIndexOf;
  15159. lodash.startCase = startCase;
  15160. lodash.startsWith = startsWith;
  15161. lodash.subtract = subtract;
  15162. lodash.sum = sum;
  15163. lodash.sumBy = sumBy;
  15164. lodash.template = template;
  15165. lodash.times = times;
  15166. lodash.toFinite = toFinite;
  15167. lodash.toInteger = toInteger;
  15168. lodash.toLength = toLength;
  15169. lodash.toLower = toLower;
  15170. lodash.toNumber = toNumber;
  15171. lodash.toSafeInteger = toSafeInteger;
  15172. lodash.toString = toString;
  15173. lodash.toUpper = toUpper;
  15174. lodash.trim = trim;
  15175. lodash.trimEnd = trimEnd;
  15176. lodash.trimStart = trimStart;
  15177. lodash.truncate = truncate;
  15178. lodash.unescape = unescape;
  15179. lodash.uniqueId = uniqueId;
  15180. lodash.upperCase = upperCase;
  15181. lodash.upperFirst = upperFirst;
  15182. // Add aliases.
  15183. lodash.each = forEach;
  15184. lodash.eachRight = forEachRight;
  15185. lodash.first = head;
  15186. mixin(lodash, (function() {
  15187. var source = {};
  15188. baseForOwn(lodash, function(func, methodName) {
  15189. if (!hasOwnProperty.call(lodash.prototype, methodName)) {
  15190. source[methodName] = func;
  15191. }
  15192. });
  15193. return source;
  15194. }()), { 'chain': false });
  15195. /*------------------------------------------------------------------------*/
  15196. /**
  15197. * The semantic version number.
  15198. *
  15199. * @static
  15200. * @memberOf _
  15201. * @type {string}
  15202. */
  15203. lodash.VERSION = VERSION;
  15204. // Assign default placeholders.
  15205. arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) {
  15206. lodash[methodName].placeholder = lodash;
  15207. });
  15208. // Add `LazyWrapper` methods for `_.drop` and `_.take` variants.
  15209. arrayEach(['drop', 'take'], function(methodName, index) {
  15210. LazyWrapper.prototype[methodName] = function(n) {
  15211. var filtered = this.__filtered__;
  15212. if (filtered && !index) {
  15213. return new LazyWrapper(this);
  15214. }
  15215. n = n === undefined ? 1 : nativeMax(toInteger(n), 0);
  15216. var result = this.clone();
  15217. if (filtered) {
  15218. result.__takeCount__ = nativeMin(n, result.__takeCount__);
  15219. } else {
  15220. result.__views__.push({
  15221. 'size': nativeMin(n, MAX_ARRAY_LENGTH),
  15222. 'type': methodName + (result.__dir__ < 0 ? 'Right' : '')
  15223. });
  15224. }
  15225. return result;
  15226. };
  15227. LazyWrapper.prototype[methodName + 'Right'] = function(n) {
  15228. return this.reverse()[methodName](n).reverse();
  15229. };
  15230. });
  15231. // Add `LazyWrapper` methods that accept an `iteratee` value.
  15232. arrayEach(['filter', 'map', 'takeWhile'], function(methodName, index) {
  15233. var type = index + 1,
  15234. isFilter = type == LAZY_FILTER_FLAG || type == LAZY_WHILE_FLAG;
  15235. LazyWrapper.prototype[methodName] = function(iteratee) {
  15236. var result = this.clone();
  15237. result.__iteratees__.push({
  15238. 'iteratee': getIteratee(iteratee, 3),
  15239. 'type': type
  15240. });
  15241. result.__filtered__ = result.__filtered__ || isFilter;
  15242. return result;
  15243. };
  15244. });
  15245. // Add `LazyWrapper` methods for `_.head` and `_.last`.
  15246. arrayEach(['head', 'last'], function(methodName, index) {
  15247. var takeName = 'take' + (index ? 'Right' : '');
  15248. LazyWrapper.prototype[methodName] = function() {
  15249. return this[takeName](1).value()[0];
  15250. };
  15251. });
  15252. // Add `LazyWrapper` methods for `_.initial` and `_.tail`.
  15253. arrayEach(['initial', 'tail'], function(methodName, index) {
  15254. var dropName = 'drop' + (index ? '' : 'Right');
  15255. LazyWrapper.prototype[methodName] = function() {
  15256. return this.__filtered__ ? new LazyWrapper(this) : this[dropName](1);
  15257. };
  15258. });
  15259. LazyWrapper.prototype.compact = function() {
  15260. return this.filter(identity);
  15261. };
  15262. LazyWrapper.prototype.find = function(predicate) {
  15263. return this.filter(predicate).head();
  15264. };
  15265. LazyWrapper.prototype.findLast = function(predicate) {
  15266. return this.reverse().find(predicate);
  15267. };
  15268. LazyWrapper.prototype.invokeMap = rest(function(path, args) {
  15269. if (typeof path == 'function') {
  15270. return new LazyWrapper(this);
  15271. }
  15272. return this.map(function(value) {
  15273. return baseInvoke(value, path, args);
  15274. });
  15275. });
  15276. LazyWrapper.prototype.reject = function(predicate) {
  15277. predicate = getIteratee(predicate, 3);
  15278. return this.filter(function(value) {
  15279. return !predicate(value);
  15280. });
  15281. };
  15282. LazyWrapper.prototype.slice = function(start, end) {
  15283. start = toInteger(start);
  15284. var result = this;
  15285. if (result.__filtered__ && (start > 0 || end < 0)) {
  15286. return new LazyWrapper(result);
  15287. }
  15288. if (start < 0) {
  15289. result = result.takeRight(-start);
  15290. } else if (start) {
  15291. result = result.drop(start);
  15292. }
  15293. if (end !== undefined) {
  15294. end = toInteger(end);
  15295. result = end < 0 ? result.dropRight(-end) : result.take(end - start);
  15296. }
  15297. return result;
  15298. };
  15299. LazyWrapper.prototype.takeRightWhile = function(predicate) {
  15300. return this.reverse().takeWhile(predicate).reverse();
  15301. };
  15302. LazyWrapper.prototype.toArray = function() {
  15303. return this.take(MAX_ARRAY_LENGTH);
  15304. };
  15305. // Add `LazyWrapper` methods to `lodash.prototype`.
  15306. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  15307. var checkIteratee = /^(?:filter|find|map|reject)|While$/.test(methodName),
  15308. isTaker = /^(?:head|last)$/.test(methodName),
  15309. lodashFunc = lodash[isTaker ? ('take' + (methodName == 'last' ? 'Right' : '')) : methodName],
  15310. retUnwrapped = isTaker || /^find/.test(methodName);
  15311. if (!lodashFunc) {
  15312. return;
  15313. }
  15314. lodash.prototype[methodName] = function() {
  15315. var value = this.__wrapped__,
  15316. args = isTaker ? [1] : arguments,
  15317. isLazy = value instanceof LazyWrapper,
  15318. iteratee = args[0],
  15319. useLazy = isLazy || isArray(value);
  15320. var interceptor = function(value) {
  15321. var result = lodashFunc.apply(lodash, arrayPush([value], args));
  15322. return (isTaker && chainAll) ? result[0] : result;
  15323. };
  15324. if (useLazy && checkIteratee && typeof iteratee == 'function' && iteratee.length != 1) {
  15325. // Avoid lazy use if the iteratee has a "length" value other than `1`.
  15326. isLazy = useLazy = false;
  15327. }
  15328. var chainAll = this.__chain__,
  15329. isHybrid = !!this.__actions__.length,
  15330. isUnwrapped = retUnwrapped && !chainAll,
  15331. onlyLazy = isLazy && !isHybrid;
  15332. if (!retUnwrapped && useLazy) {
  15333. value = onlyLazy ? value : new LazyWrapper(this);
  15334. var result = func.apply(value, args);
  15335. result.__actions__.push({ 'func': thru, 'args': [interceptor], 'thisArg': undefined });
  15336. return new LodashWrapper(result, chainAll);
  15337. }
  15338. if (isUnwrapped && onlyLazy) {
  15339. return func.apply(this, args);
  15340. }
  15341. result = this.thru(interceptor);
  15342. return isUnwrapped ? (isTaker ? result.value()[0] : result.value()) : result;
  15343. };
  15344. });
  15345. // Add `Array` methods to `lodash.prototype`.
  15346. arrayEach(['pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
  15347. var func = arrayProto[methodName],
  15348. chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
  15349. retUnwrapped = /^(?:pop|shift)$/.test(methodName);
  15350. lodash.prototype[methodName] = function() {
  15351. var args = arguments;
  15352. if (retUnwrapped && !this.__chain__) {
  15353. var value = this.value();
  15354. return func.apply(isArray(value) ? value : [], args);
  15355. }
  15356. return this[chainName](function(value) {
  15357. return func.apply(isArray(value) ? value : [], args);
  15358. });
  15359. };
  15360. });
  15361. // Map minified method names to their real names.
  15362. baseForOwn(LazyWrapper.prototype, function(func, methodName) {
  15363. var lodashFunc = lodash[methodName];
  15364. if (lodashFunc) {
  15365. var key = (lodashFunc.name + ''),
  15366. names = realNames[key] || (realNames[key] = []);
  15367. names.push({ 'name': methodName, 'func': lodashFunc });
  15368. }
  15369. });
  15370. realNames[createHybridWrapper(undefined, BIND_KEY_FLAG).name] = [{
  15371. 'name': 'wrapper',
  15372. 'func': undefined
  15373. }];
  15374. // Add methods to `LazyWrapper`.
  15375. LazyWrapper.prototype.clone = lazyClone;
  15376. LazyWrapper.prototype.reverse = lazyReverse;
  15377. LazyWrapper.prototype.value = lazyValue;
  15378. // Add chain sequence methods to the `lodash` wrapper.
  15379. lodash.prototype.at = wrapperAt;
  15380. lodash.prototype.chain = wrapperChain;
  15381. lodash.prototype.commit = wrapperCommit;
  15382. lodash.prototype.next = wrapperNext;
  15383. lodash.prototype.plant = wrapperPlant;
  15384. lodash.prototype.reverse = wrapperReverse;
  15385. lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue;
  15386. if (iteratorSymbol) {
  15387. lodash.prototype[iteratorSymbol] = wrapperToIterator;
  15388. }
  15389. return lodash;
  15390. }
  15391. /*--------------------------------------------------------------------------*/
  15392. // Export lodash.
  15393. var _ = runInContext();
  15394. // Expose Lodash on the free variable `window` or `self` when available so it's
  15395. // globally accessible, even when bundled with Browserify, Webpack, etc. This
  15396. // also prevents errors in cases where Lodash is loaded by a script tag in the
  15397. // presence of an AMD loader. See http://requirejs.org/docs/errors.html#mismatch
  15398. // for more details. Use `_.noConflict` to remove Lodash from the global object.
  15399. (freeSelf || {})._ = _;
  15400. // Some AMD build optimizers like r.js check for condition patterns like the following:
  15401. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  15402. // Define as an anonymous module so, through path mapping, it can be
  15403. // referenced as the "underscore" module.
  15404. define(function() {
  15405. return _;
  15406. });
  15407. }
  15408. // Check for `exports` after `define` in case a build optimizer adds an `exports` object.
  15409. else if (freeModule) {
  15410. // Export for Node.js.
  15411. (freeModule.exports = _)._ = _;
  15412. // Export for CommonJS support.
  15413. freeExports._ = _;
  15414. }
  15415. else {
  15416. // Export to the global object.
  15417. root._ = _;
  15418. }
  15419. }.call(this));