PageRenderTime 48ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/lodash.js/1.3.0/lodash.underscore.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 1651 lines | 894 code | 65 blank | 692 comment | 128 complexity | 401e26dd96e26da2751f2f2780239521 MD5 | raw file
  1. /**
  2. * @license
  3. * Lo-Dash 1.3.0 (Custom Build) <http://lodash.com/>
  4. * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js`
  5. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  6. * Based on Underscore.js 1.4.4 <http://underscorejs.org/>
  7. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
  8. * Available under MIT license <http://lodash.com/license>
  9. */
  10. ;(function(window) {
  11. /** Used as a safe reference for `undefined` in pre ES5 environments */
  12. var undefined;
  13. /** Used to generate unique IDs */
  14. var idCounter = 0;
  15. /** Used internally to indicate various things */
  16. var indicatorObject = {};
  17. /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
  18. var keyPrefix = +new Date + '';
  19. /** Used as the size when optimizations are enabled for large arrays */
  20. var largeArraySize = 75;
  21. /** Used to match empty string literals in compiled template source */
  22. var reEmptyStringLeading = /\b__p \+= '';/g,
  23. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  24. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  25. /** Used to match HTML entities */
  26. var reEscapedHtml = /&(?:amp|lt|gt|quot|#39);/g;
  27. /**
  28. * Used to match ES6 template delimiters
  29. * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-7.8.6
  30. */
  31. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  32. /** Used to match regexp flags from their coerced string values */
  33. var reFlags = /\w*$/;
  34. /** Used to match "interpolate" template delimiters */
  35. var reInterpolate = /<%=([\s\S]+?)%>/g;
  36. /** Used to ensure capturing order of template delimiters */
  37. var reNoMatch = /($^)/;
  38. /** Used to match HTML characters */
  39. var reUnescapedHtml = /[&<>"']/g;
  40. /** Used to match unescaped characters in compiled string literals */
  41. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  42. /** Used to make template sourceURLs easier to identify */
  43. var templateCounter = 0;
  44. /** `Object#toString` result shortcuts */
  45. var argsClass = '[object Arguments]',
  46. arrayClass = '[object Array]',
  47. boolClass = '[object Boolean]',
  48. dateClass = '[object Date]',
  49. errorClass = '[object Error]',
  50. funcClass = '[object Function]',
  51. numberClass = '[object Number]',
  52. objectClass = '[object Object]',
  53. regexpClass = '[object RegExp]',
  54. stringClass = '[object String]';
  55. /** Used to determine if values are of the language type Object */
  56. var objectTypes = {
  57. 'boolean': false,
  58. 'function': true,
  59. 'object': true,
  60. 'number': false,
  61. 'string': false,
  62. 'undefined': false
  63. };
  64. /** Used to escape characters for inclusion in compiled string literals */
  65. var stringEscapes = {
  66. '\\': '\\',
  67. "'": "'",
  68. '\n': 'n',
  69. '\r': 'r',
  70. '\t': 't',
  71. '\u2028': 'u2028',
  72. '\u2029': 'u2029'
  73. };
  74. /** Detect free variable `exports` */
  75. var freeExports = objectTypes[typeof exports] && exports;
  76. /** Detect free variable `module` */
  77. var freeModule = objectTypes[typeof module] && module && module.exports == freeExports && module;
  78. /** Detect free variable `global`, from Node.js or Browserified code, and use it as `window` */
  79. var freeGlobal = objectTypes[typeof global] && global;
  80. if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
  81. window = freeGlobal;
  82. }
  83. /*--------------------------------------------------------------------------*/
  84. /**
  85. * A basic implementation of `_.indexOf` without support for binary searches
  86. * or `fromIndex` constraints.
  87. *
  88. * @private
  89. * @param {Array} array The array to search.
  90. * @param {Mixed} value The value to search for.
  91. * @param {Number} [fromIndex=0] The index to search from.
  92. * @returns {Number} Returns the index of the matched value or `-1`.
  93. */
  94. function basicIndexOf(array, value, fromIndex) {
  95. var index = (fromIndex || 0) - 1,
  96. length = array.length;
  97. while (++index < length) {
  98. if (array[index] === value) {
  99. return index;
  100. }
  101. }
  102. return -1;
  103. }
  104. /**
  105. * Used by `sortBy` to compare transformed `collection` values, stable sorting
  106. * them in ascending order.
  107. *
  108. * @private
  109. * @param {Object} a The object to compare to `b`.
  110. * @param {Object} b The object to compare to `a`.
  111. * @returns {Number} Returns the sort order indicator of `1` or `-1`.
  112. */
  113. function compareAscending(a, b) {
  114. var ai = a.index,
  115. bi = b.index;
  116. a = a.criteria;
  117. b = b.criteria;
  118. // ensure a stable sort in V8 and other engines
  119. // http://code.google.com/p/v8/issues/detail?id=90
  120. if (a !== b) {
  121. if (a > b || typeof a == 'undefined') {
  122. return 1;
  123. }
  124. if (a < b || typeof b == 'undefined') {
  125. return -1;
  126. }
  127. }
  128. return ai < bi ? -1 : 1;
  129. }
  130. /**
  131. * Used by `template` to escape characters for inclusion in compiled
  132. * string literals.
  133. *
  134. * @private
  135. * @param {String} match The matched character to escape.
  136. * @returns {String} Returns the escaped character.
  137. */
  138. function escapeStringChar(match) {
  139. return '\\' + stringEscapes[match];
  140. }
  141. /**
  142. * A no-operation function.
  143. *
  144. * @private
  145. */
  146. function noop() {
  147. // no operation performed
  148. }
  149. /*--------------------------------------------------------------------------*/
  150. /**
  151. * Used for `Array` method references.
  152. *
  153. * Normally `Array.prototype` would suffice, however, using an array literal
  154. * avoids issues in Narwhal.
  155. */
  156. var arrayRef = [];
  157. /** Used for native method references */
  158. var objectProto = Object.prototype,
  159. stringProto = String.prototype;
  160. /** Used to restore the original `_` reference in `noConflict` */
  161. var oldDash = window._;
  162. /** Used to detect if a method is native */
  163. var reNative = RegExp('^' +
  164. String(objectProto.valueOf)
  165. .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  166. .replace(/valueOf|for [^\]]+/g, '.+?') + '$'
  167. );
  168. /** Native method shortcuts */
  169. var ceil = Math.ceil,
  170. clearTimeout = window.clearTimeout,
  171. concat = arrayRef.concat,
  172. floor = Math.floor,
  173. hasOwnProperty = objectProto.hasOwnProperty,
  174. push = arrayRef.push,
  175. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  176. setTimeout = window.setTimeout,
  177. toString = objectProto.toString;
  178. /* Native method shortcuts for methods with the same name as other `lodash` methods */
  179. var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind,
  180. nativeCreate = reNative.test(nativeCreate = Object.create) && nativeCreate,
  181. nativeIsArray = reNative.test(nativeIsArray = Array.isArray) && nativeIsArray,
  182. nativeIsFinite = window.isFinite,
  183. nativeIsNaN = window.isNaN,
  184. nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
  185. nativeMax = Math.max,
  186. nativeMin = Math.min,
  187. nativeRandom = Math.random,
  188. nativeSlice = arrayRef.slice;
  189. /** Detect various environments */
  190. var isIeOpera = reNative.test(window.attachEvent),
  191. isV8 = nativeBind && !/\n|true/.test(nativeBind + isIeOpera);
  192. /*--------------------------------------------------------------------------*/
  193. /**
  194. * Creates a `lodash` object, which wraps the given `value`, to enable method
  195. * chaining.
  196. *
  197. * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
  198. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
  199. * and `unshift`
  200. *
  201. * Chaining is supported in custom builds as long as the `value` method is
  202. * implicitly or explicitly included in the build.
  203. *
  204. * The chainable wrapper functions are:
  205. * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
  206. * `compose`, `concat`, `countBy`, `createCallback`, `debounce`, `defaults`,
  207. * `defer`, `delay`, `difference`, `filter`, `flatten`, `forEach`, `forIn`,
  208. * `forOwn`, `functions`, `groupBy`, `initial`, `intersection`, `invert`,
  209. * `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
  210. * `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
  211. * `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
  212. * `tap`, `throttle`, `times`, `toArray`, `transform`, `union`, `uniq`, `unshift`,
  213. * `unzip`, `values`, `where`, `without`, `wrap`, and `zip`
  214. *
  215. * The non-chainable wrapper functions are:
  216. * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
  217. * `identity`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`,
  218. * `isElement`, `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`,
  219. * `isNull`, `isNumber`, `isObject`, `isPlainObject`, `isRegExp`, `isString`,
  220. * `isUndefined`, `join`, `lastIndexOf`, `mixin`, `noConflict`, `parseInt`,
  221. * `pop`, `random`, `reduce`, `reduceRight`, `result`, `shift`, `size`, `some`,
  222. * `sortedIndex`, `runInContext`, `template`, `unescape`, `uniqueId`, and `value`
  223. *
  224. * The wrapper functions `first` and `last` return wrapped values when `n` is
  225. * passed, otherwise they return unwrapped values.
  226. *
  227. * @name _
  228. * @constructor
  229. * @alias chain
  230. * @category Chaining
  231. * @param {Mixed} value The value to wrap in a `lodash` instance.
  232. * @returns {Object} Returns a `lodash` instance.
  233. * @example
  234. *
  235. * var wrapped = _([1, 2, 3]);
  236. *
  237. * // returns an unwrapped value
  238. * wrapped.reduce(function(sum, num) {
  239. * return sum + num;
  240. * });
  241. * // => 6
  242. *
  243. * // returns a wrapped value
  244. * var squares = wrapped.map(function(num) {
  245. * return num * num;
  246. * });
  247. *
  248. * _.isArray(squares);
  249. * // => false
  250. *
  251. * _.isArray(squares.value());
  252. * // => true
  253. */
  254. function lodash(value) {
  255. return (value instanceof lodash)
  256. ? value
  257. : new lodashWrapper(value);
  258. }
  259. /**
  260. * A fast path for creating `lodash` wrapper objects.
  261. *
  262. * @private
  263. * @param {Mixed} value The value to wrap in a `lodash` instance.
  264. * @returns {Object} Returns a `lodash` instance.
  265. */
  266. function lodashWrapper(value) {
  267. this.__wrapped__ = value;
  268. }
  269. // ensure `new lodashWrapper` is an instance of `lodash`
  270. lodashWrapper.prototype = lodash.prototype;
  271. /**
  272. * An object used to flag environments features.
  273. *
  274. * @static
  275. * @memberOf _
  276. * @type Object
  277. */
  278. var support = {};
  279. (function() {
  280. var object = { '0': 1, 'length': 1 };
  281. /**
  282. * Detect if `Function#bind` exists and is inferred to be fast (all but V8).
  283. *
  284. * @memberOf _.support
  285. * @type Boolean
  286. */
  287. support.fastBind = nativeBind && !isV8;
  288. /**
  289. * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly.
  290. *
  291. * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
  292. * and `splice()` functions that fail to remove the last element, `value[0]`,
  293. * of array-like objects even though the `length` property is set to `0`.
  294. * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
  295. * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
  296. *
  297. * @memberOf _.support
  298. * @type Boolean
  299. */
  300. support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]);
  301. }(1));
  302. /**
  303. * By default, the template delimiters used by Lo-Dash are similar to those in
  304. * embedded Ruby (ERB). Change the following template settings to use alternative
  305. * delimiters.
  306. *
  307. * @static
  308. * @memberOf _
  309. * @type Object
  310. */
  311. lodash.templateSettings = {
  312. /**
  313. * Used to detect `data` property values to be HTML-escaped.
  314. *
  315. * @memberOf _.templateSettings
  316. * @type RegExp
  317. */
  318. 'escape': /<%-([\s\S]+?)%>/g,
  319. /**
  320. * Used to detect code to be evaluated.
  321. *
  322. * @memberOf _.templateSettings
  323. * @type RegExp
  324. */
  325. 'evaluate': /<%([\s\S]+?)%>/g,
  326. /**
  327. * Used to detect `data` property values to inject.
  328. *
  329. * @memberOf _.templateSettings
  330. * @type RegExp
  331. */
  332. 'interpolate': reInterpolate,
  333. /**
  334. * Used to reference the data object in the template text.
  335. *
  336. * @memberOf _.templateSettings
  337. * @type String
  338. */
  339. 'variable': ''
  340. };
  341. /*--------------------------------------------------------------------------*/
  342. /**
  343. * Creates a function that, when called, invokes `func` with the `this` binding
  344. * of `thisArg` and prepends any `partialArgs` to the arguments passed to the
  345. * bound function.
  346. *
  347. * @private
  348. * @param {Function|String} func The function to bind or the method name.
  349. * @param {Mixed} [thisArg] The `this` binding of `func`.
  350. * @param {Array} partialArgs An array of arguments to be partially applied.
  351. * @param {Object} [idicator] Used to indicate binding by key or partially
  352. * applying arguments from the right.
  353. * @returns {Function} Returns the new bound function.
  354. */
  355. function createBound(func, thisArg, partialArgs, indicator) {
  356. var isFunc = isFunction(func),
  357. isPartial = !partialArgs,
  358. key = thisArg;
  359. // juggle arguments
  360. if (isPartial) {
  361. var rightIndicator = indicator;
  362. partialArgs = thisArg;
  363. }
  364. else if (!isFunc) {
  365. if (!indicator) {
  366. throw new TypeError;
  367. }
  368. thisArg = func;
  369. }
  370. function bound() {
  371. // `Function#bind` spec
  372. // http://es5.github.com/#x15.3.4.5
  373. var args = arguments,
  374. thisBinding = isPartial ? this : thisArg;
  375. if (!isFunc) {
  376. func = thisArg[key];
  377. }
  378. if (partialArgs.length) {
  379. args = args.length
  380. ? (args = nativeSlice.call(args), rightIndicator ? args.concat(partialArgs) : partialArgs.concat(args))
  381. : partialArgs;
  382. }
  383. if (this instanceof bound) {
  384. // ensure `new bound` is an instance of `func`
  385. thisBinding = createObject(func.prototype);
  386. // mimic the constructor's `return` behavior
  387. // http://es5.github.com/#x13.2.2
  388. var result = func.apply(thisBinding, args);
  389. return isObject(result) ? result : thisBinding;
  390. }
  391. return func.apply(thisBinding, args);
  392. }
  393. return bound;
  394. }
  395. /**
  396. * Creates a new object with the specified `prototype`.
  397. *
  398. * @private
  399. * @param {Object} prototype The prototype object.
  400. * @returns {Object} Returns the new object.
  401. */
  402. function createObject(prototype) {
  403. return isObject(prototype) ? nativeCreate(prototype) : {};
  404. }
  405. // fallback for browsers without `Object.create`
  406. if (!nativeCreate) {
  407. var createObject = function(prototype) {
  408. if (isObject(prototype)) {
  409. noop.prototype = prototype;
  410. var result = new noop;
  411. noop.prototype = null;
  412. }
  413. return result || {};
  414. };
  415. }
  416. /**
  417. * Used by `escape` to convert characters to HTML entities.
  418. *
  419. * @private
  420. * @param {String} match The matched character to escape.
  421. * @returns {String} Returns the escaped character.
  422. */
  423. function escapeHtmlChar(match) {
  424. return htmlEscapes[match];
  425. }
  426. /**
  427. * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
  428. * customized, this method returns the custom method, otherwise it returns
  429. * the `basicIndexOf` function.
  430. *
  431. * @private
  432. * @returns {Function} Returns the "indexOf" function.
  433. */
  434. function getIndexOf(array, value, fromIndex) {
  435. var result = (result = lodash.indexOf) === indexOf ? basicIndexOf : result;
  436. return result;
  437. }
  438. /**
  439. * Used by `unescape` to convert HTML entities to characters.
  440. *
  441. * @private
  442. * @param {String} match The matched character to unescape.
  443. * @returns {String} Returns the unescaped character.
  444. */
  445. function unescapeHtmlChar(match) {
  446. return htmlUnescapes[match];
  447. }
  448. /*--------------------------------------------------------------------------*/
  449. /**
  450. * Checks if `value` is an `arguments` object.
  451. *
  452. * @static
  453. * @memberOf _
  454. * @category Objects
  455. * @param {Mixed} value The value to check.
  456. * @returns {Boolean} Returns `true`, if the `value` is an `arguments` object, else `false`.
  457. * @example
  458. *
  459. * (function() { return _.isArguments(arguments); })(1, 2, 3);
  460. * // => true
  461. *
  462. * _.isArguments([1, 2, 3]);
  463. * // => false
  464. */
  465. function isArguments(value) {
  466. return toString.call(value) == argsClass;
  467. }
  468. // fallback for browsers that can't detect `arguments` objects by [[Class]]
  469. if (!isArguments(arguments)) {
  470. isArguments = function(value) {
  471. return value ? hasOwnProperty.call(value, 'callee') : false;
  472. };
  473. }
  474. /**
  475. * Checks if `value` is an array.
  476. *
  477. * @static
  478. * @memberOf _
  479. * @category Objects
  480. * @param {Mixed} value The value to check.
  481. * @returns {Boolean} Returns `true`, if the `value` is an array, else `false`.
  482. * @example
  483. *
  484. * (function() { return _.isArray(arguments); })();
  485. * // => false
  486. *
  487. * _.isArray([1, 2, 3]);
  488. * // => true
  489. */
  490. var isArray = nativeIsArray || function(value) {
  491. return value ? (typeof value == 'object' && toString.call(value) == arrayClass) : false;
  492. };
  493. /**
  494. * A fallback implementation of `Object.keys` which produces an array of the
  495. * given object's own enumerable property names.
  496. *
  497. * @private
  498. * @type Function
  499. * @param {Object} object The object to inspect.
  500. * @returns {Array} Returns a new array of property names.
  501. */
  502. var shimKeys = function (object) {
  503. var index, iterable = object, result = [];
  504. if (!iterable) return result;
  505. if (!(objectTypes[typeof object])) return result;
  506. for (index in iterable) {
  507. if (hasOwnProperty.call(iterable, index)) {
  508. result.push(index);
  509. }
  510. }
  511. return result
  512. };
  513. /**
  514. * Creates an array composed of the own enumerable property names of `object`.
  515. *
  516. * @static
  517. * @memberOf _
  518. * @category Objects
  519. * @param {Object} object The object to inspect.
  520. * @returns {Array} Returns a new array of property names.
  521. * @example
  522. *
  523. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  524. * // => ['one', 'two', 'three'] (order is not guaranteed)
  525. */
  526. var keys = !nativeKeys ? shimKeys : function(object) {
  527. if (!isObject(object)) {
  528. return [];
  529. }
  530. return nativeKeys(object);
  531. };
  532. /**
  533. * Used to convert characters to HTML entities:
  534. *
  535. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  536. * don't require escaping in HTML and have no special meaning unless they're part
  537. * of a tag or an unquoted attribute value.
  538. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  539. */
  540. var htmlEscapes = {
  541. '&': '&amp;',
  542. '<': '&lt;',
  543. '>': '&gt;',
  544. '"': '&quot;',
  545. "'": '&#39;'
  546. };
  547. /** Used to convert HTML entities to characters */
  548. var htmlUnescapes = invert(htmlEscapes);
  549. /*--------------------------------------------------------------------------*/
  550. /**
  551. * Assigns own enumerable properties of source object(s) to the destination
  552. * object. Subsequent sources will overwrite property assignments of previous
  553. * sources. If a `callback` function is passed, it will be executed to produce
  554. * the assigned values. The `callback` is bound to `thisArg` and invoked with
  555. * two arguments; (objectValue, sourceValue).
  556. *
  557. * @static
  558. * @memberOf _
  559. * @type Function
  560. * @alias extend
  561. * @category Objects
  562. * @param {Object} object The destination object.
  563. * @param {Object} [source1, source2, ...] The source objects.
  564. * @param {Function} [callback] The function to customize assigning values.
  565. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  566. * @returns {Object} Returns the destination object.
  567. * @example
  568. *
  569. * _.assign({ 'name': 'moe' }, { 'age': 40 });
  570. * // => { 'name': 'moe', 'age': 40 }
  571. *
  572. * var defaults = _.partialRight(_.assign, function(a, b) {
  573. * return typeof a == 'undefined' ? b : a;
  574. * });
  575. *
  576. * var food = { 'name': 'apple' };
  577. * defaults(food, { 'name': 'banana', 'type': 'fruit' });
  578. * // => { 'name': 'apple', 'type': 'fruit' }
  579. */
  580. function assign(object) {
  581. if (!object) {
  582. return object;
  583. }
  584. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  585. var iterable = arguments[argsIndex];
  586. if (iterable) {
  587. for (var key in iterable) {
  588. object[key] = iterable[key];
  589. }
  590. }
  591. }
  592. return object;
  593. }
  594. /**
  595. * Creates a clone of `value`. If `deep` is `true`, nested objects will also
  596. * be cloned, otherwise they will be assigned by reference. If a `callback`
  597. * function is passed, it will be executed to produce the cloned values. If
  598. * `callback` returns `undefined`, cloning will be handled by the method instead.
  599. * The `callback` is bound to `thisArg` and invoked with one argument; (value).
  600. *
  601. * @static
  602. * @memberOf _
  603. * @category Objects
  604. * @param {Mixed} value The value to clone.
  605. * @param {Boolean} [deep=false] A flag to indicate a deep clone.
  606. * @param {Function} [callback] The function to customize cloning values.
  607. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  608. * @param- {Array} [stackA=[]] Tracks traversed source objects.
  609. * @param- {Array} [stackB=[]] Associates clones with source counterparts.
  610. * @returns {Mixed} Returns the cloned `value`.
  611. * @example
  612. *
  613. * var stooges = [
  614. * { 'name': 'moe', 'age': 40 },
  615. * { 'name': 'larry', 'age': 50 }
  616. * ];
  617. *
  618. * var shallow = _.clone(stooges);
  619. * shallow[0] === stooges[0];
  620. * // => true
  621. *
  622. * var deep = _.clone(stooges, true);
  623. * deep[0] === stooges[0];
  624. * // => false
  625. *
  626. * _.mixin({
  627. * 'clone': _.partialRight(_.clone, function(value) {
  628. * return _.isElement(value) ? value.cloneNode(false) : undefined;
  629. * })
  630. * });
  631. *
  632. * var clone = _.clone(document.body);
  633. * clone.childNodes.length;
  634. * // => 0
  635. */
  636. function clone(value) {
  637. return isObject(value)
  638. ? (isArray(value) ? nativeSlice.call(value) : assign({}, value))
  639. : value;
  640. }
  641. /**
  642. * Assigns own enumerable properties of source object(s) to the destination
  643. * object for all destination properties that resolve to `undefined`. Once a
  644. * property is set, additional defaults of the same property will be ignored.
  645. *
  646. * @static
  647. * @memberOf _
  648. * @type Function
  649. * @category Objects
  650. * @param {Object} object The destination object.
  651. * @param {Object} [source1, source2, ...] The source objects.
  652. * @param- {Object} [guard] Allows working with `_.reduce` without using its
  653. * callback's `key` and `object` arguments as sources.
  654. * @returns {Object} Returns the destination object.
  655. * @example
  656. *
  657. * var food = { 'name': 'apple' };
  658. * _.defaults(food, { 'name': 'banana', 'type': 'fruit' });
  659. * // => { 'name': 'apple', 'type': 'fruit' }
  660. */
  661. function defaults(object) {
  662. if (!object) {
  663. return object;
  664. }
  665. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  666. var iterable = arguments[argsIndex];
  667. if (iterable) {
  668. for (var key in iterable) {
  669. if (object[key] == null) {
  670. object[key] = iterable[key];
  671. }
  672. }
  673. }
  674. }
  675. return object;
  676. }
  677. /**
  678. * Iterates over `object`'s own and inherited enumerable properties, executing
  679. * the `callback` for each property. The `callback` is bound to `thisArg` and
  680. * invoked with three arguments; (value, key, object). Callbacks may exit iteration
  681. * early by explicitly returning `false`.
  682. *
  683. * @static
  684. * @memberOf _
  685. * @type Function
  686. * @category Objects
  687. * @param {Object} object The object to iterate over.
  688. * @param {Function} [callback=identity] The function called per iteration.
  689. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  690. * @returns {Object} Returns `object`.
  691. * @example
  692. *
  693. * function Dog(name) {
  694. * this.name = name;
  695. * }
  696. *
  697. * Dog.prototype.bark = function() {
  698. * alert('Woof, woof!');
  699. * };
  700. *
  701. * _.forIn(new Dog('Dagny'), function(value, key) {
  702. * alert(key);
  703. * });
  704. * // => alerts 'name' and 'bark' (order is not guaranteed)
  705. */
  706. var forIn = function (collection, callback) {
  707. var index, iterable = collection, result = iterable;
  708. if (!iterable) return result;
  709. if (!objectTypes[typeof iterable]) return result;
  710. for (index in iterable) {
  711. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  712. }
  713. return result
  714. };
  715. /**
  716. * Iterates over an object's own enumerable properties, executing the `callback`
  717. * for each property. The `callback` is bound to `thisArg` and invoked with three
  718. * arguments; (value, key, object). Callbacks may exit iteration early by explicitly
  719. * returning `false`.
  720. *
  721. * @static
  722. * @memberOf _
  723. * @type Function
  724. * @category Objects
  725. * @param {Object} object The object to iterate over.
  726. * @param {Function} [callback=identity] The function called per iteration.
  727. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  728. * @returns {Object} Returns `object`.
  729. * @example
  730. *
  731. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  732. * alert(key);
  733. * });
  734. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  735. */
  736. var forOwn = function (collection, callback) {
  737. var index, iterable = collection, result = iterable;
  738. if (!iterable) return result;
  739. if (!objectTypes[typeof iterable]) return result;
  740. for (index in iterable) {
  741. if (hasOwnProperty.call(iterable, index)) {
  742. if (callback(iterable[index], index, collection) === indicatorObject) return result;
  743. }
  744. }
  745. return result
  746. };
  747. /**
  748. * Creates a sorted array of all enumerable properties, own and inherited,
  749. * of `object` that have function values.
  750. *
  751. * @static
  752. * @memberOf _
  753. * @alias methods
  754. * @category Objects
  755. * @param {Object} object The object to inspect.
  756. * @returns {Array} Returns a new array of property names that have function values.
  757. * @example
  758. *
  759. * _.functions(_);
  760. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  761. */
  762. function functions(object) {
  763. var result = [];
  764. forIn(object, function(value, key) {
  765. if (isFunction(value)) {
  766. result.push(key);
  767. }
  768. });
  769. return result.sort();
  770. }
  771. /**
  772. * Checks if the specified object `property` exists and is a direct property,
  773. * instead of an inherited property.
  774. *
  775. * @static
  776. * @memberOf _
  777. * @category Objects
  778. * @param {Object} object The object to check.
  779. * @param {String} property The property to check for.
  780. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  781. * @example
  782. *
  783. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  784. * // => true
  785. */
  786. function has(object, property) {
  787. return object ? hasOwnProperty.call(object, property) : false;
  788. }
  789. /**
  790. * Creates an object composed of the inverted keys and values of the given `object`.
  791. *
  792. * @static
  793. * @memberOf _
  794. * @category Objects
  795. * @param {Object} object The object to invert.
  796. * @returns {Object} Returns the created inverted object.
  797. * @example
  798. *
  799. * _.invert({ 'first': 'moe', 'second': 'larry' });
  800. * // => { 'moe': 'first', 'larry': 'second' }
  801. */
  802. function invert(object) {
  803. var index = -1,
  804. props = keys(object),
  805. length = props.length,
  806. result = {};
  807. while (++index < length) {
  808. var key = props[index];
  809. result[object[key]] = key;
  810. }
  811. return result;
  812. }
  813. /**
  814. * Checks if `value` is a boolean value.
  815. *
  816. * @static
  817. * @memberOf _
  818. * @category Objects
  819. * @param {Mixed} value The value to check.
  820. * @returns {Boolean} Returns `true`, if the `value` is a boolean value, else `false`.
  821. * @example
  822. *
  823. * _.isBoolean(null);
  824. * // => false
  825. */
  826. function isBoolean(value) {
  827. return value === true || value === false || toString.call(value) == boolClass;
  828. }
  829. /**
  830. * Checks if `value` is a date.
  831. *
  832. * @static
  833. * @memberOf _
  834. * @category Objects
  835. * @param {Mixed} value The value to check.
  836. * @returns {Boolean} Returns `true`, if the `value` is a date, else `false`.
  837. * @example
  838. *
  839. * _.isDate(new Date);
  840. * // => true
  841. */
  842. function isDate(value) {
  843. return value ? (typeof value == 'object' && toString.call(value) == dateClass) : false;
  844. }
  845. /**
  846. * Checks if `value` is a DOM element.
  847. *
  848. * @static
  849. * @memberOf _
  850. * @category Objects
  851. * @param {Mixed} value The value to check.
  852. * @returns {Boolean} Returns `true`, if the `value` is a DOM element, else `false`.
  853. * @example
  854. *
  855. * _.isElement(document.body);
  856. * // => true
  857. */
  858. function isElement(value) {
  859. return value ? value.nodeType === 1 : false;
  860. }
  861. /**
  862. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  863. * length of `0` and objects with no own enumerable properties are considered
  864. * "empty".
  865. *
  866. * @static
  867. * @memberOf _
  868. * @category Objects
  869. * @param {Array|Object|String} value The value to inspect.
  870. * @returns {Boolean} Returns `true`, if the `value` is empty, else `false`.
  871. * @example
  872. *
  873. * _.isEmpty([1, 2, 3]);
  874. * // => false
  875. *
  876. * _.isEmpty({});
  877. * // => true
  878. *
  879. * _.isEmpty('');
  880. * // => true
  881. */
  882. function isEmpty(value) {
  883. if (!value) {
  884. return true;
  885. }
  886. if (isArray(value) || isString(value)) {
  887. return !value.length;
  888. }
  889. for (var key in value) {
  890. if (hasOwnProperty.call(value, key)) {
  891. return false;
  892. }
  893. }
  894. return true;
  895. }
  896. /**
  897. * Performs a deep comparison between two values to determine if they are
  898. * equivalent to each other. If `callback` is passed, it will be executed to
  899. * compare values. If `callback` returns `undefined`, comparisons will be handled
  900. * by the method instead. The `callback` is bound to `thisArg` and invoked with
  901. * two arguments; (a, b).
  902. *
  903. * @static
  904. * @memberOf _
  905. * @category Objects
  906. * @param {Mixed} a The value to compare.
  907. * @param {Mixed} b The other value to compare.
  908. * @param {Function} [callback] The function to customize comparing values.
  909. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  910. * @param- {Array} [stackA=[]] Tracks traversed `a` objects.
  911. * @param- {Array} [stackB=[]] Tracks traversed `b` objects.
  912. * @returns {Boolean} Returns `true`, if the values are equivalent, else `false`.
  913. * @example
  914. *
  915. * var moe = { 'name': 'moe', 'age': 40 };
  916. * var copy = { 'name': 'moe', 'age': 40 };
  917. *
  918. * moe == copy;
  919. * // => false
  920. *
  921. * _.isEqual(moe, copy);
  922. * // => true
  923. *
  924. * var words = ['hello', 'goodbye'];
  925. * var otherWords = ['hi', 'goodbye'];
  926. *
  927. * _.isEqual(words, otherWords, function(a, b) {
  928. * var reGreet = /^(?:hello|hi)$/i,
  929. * aGreet = _.isString(a) && reGreet.test(a),
  930. * bGreet = _.isString(b) && reGreet.test(b);
  931. *
  932. * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
  933. * });
  934. * // => true
  935. */
  936. function isEqual(a, b, stackA, stackB) {
  937. if (a === b) {
  938. return a !== 0 || (1 / a == 1 / b);
  939. }
  940. var type = typeof a,
  941. otherType = typeof b;
  942. if (a === a &&
  943. (!a || (type != 'function' && type != 'object')) &&
  944. (!b || (otherType != 'function' && otherType != 'object'))) {
  945. return false;
  946. }
  947. if (a == null || b == null) {
  948. return a === b;
  949. }
  950. var className = toString.call(a),
  951. otherClass = toString.call(b);
  952. if (className != otherClass) {
  953. return false;
  954. }
  955. switch (className) {
  956. case boolClass:
  957. case dateClass:
  958. return +a == +b;
  959. case numberClass:
  960. return a != +a
  961. ? b != +b
  962. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  963. case regexpClass:
  964. case stringClass:
  965. return a == String(b);
  966. }
  967. var isArr = className == arrayClass;
  968. if (!isArr) {
  969. if (a instanceof lodash || b instanceof lodash) {
  970. return isEqual(a.__wrapped__ || a, b.__wrapped__ || b, stackA, stackB);
  971. }
  972. if (className != objectClass) {
  973. return false;
  974. }
  975. var ctorA = a.constructor,
  976. ctorB = b.constructor;
  977. if (ctorA != ctorB && !(
  978. isFunction(ctorA) && ctorA instanceof ctorA &&
  979. isFunction(ctorB) && ctorB instanceof ctorB
  980. )) {
  981. return false;
  982. }
  983. }
  984. stackA || (stackA = []);
  985. stackB || (stackB = []);
  986. var length = stackA.length;
  987. while (length--) {
  988. if (stackA[length] == a) {
  989. return stackB[length] == b;
  990. }
  991. }
  992. var result = true,
  993. size = 0;
  994. stackA.push(a);
  995. stackB.push(b);
  996. if (isArr) {
  997. size = b.length;
  998. result = size == a.length;
  999. if (result) {
  1000. while (size--) {
  1001. if (!(result = isEqual(a[size], b[size], stackA, stackB))) {
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. return result;
  1007. }
  1008. forIn(b, function(value, key, b) {
  1009. if (hasOwnProperty.call(b, key)) {
  1010. size++;
  1011. return !(result = hasOwnProperty.call(a, key) && isEqual(a[key], value, stackA, stackB)) && indicatorObject;
  1012. }
  1013. });
  1014. if (result) {
  1015. forIn(a, function(value, key, a) {
  1016. if (hasOwnProperty.call(a, key)) {
  1017. return !(result = --size > -1) && indicatorObject;
  1018. }
  1019. });
  1020. }
  1021. return result;
  1022. }
  1023. /**
  1024. * Checks if `value` is, or can be coerced to, a finite number.
  1025. *
  1026. * Note: This is not the same as native `isFinite`, which will return true for
  1027. * booleans and empty strings. See http://es5.github.com/#x15.1.2.5.
  1028. *
  1029. * @static
  1030. * @memberOf _
  1031. * @category Objects
  1032. * @param {Mixed} value The value to check.
  1033. * @returns {Boolean} Returns `true`, if the `value` is finite, else `false`.
  1034. * @example
  1035. *
  1036. * _.isFinite(-101);
  1037. * // => true
  1038. *
  1039. * _.isFinite('10');
  1040. * // => true
  1041. *
  1042. * _.isFinite(true);
  1043. * // => false
  1044. *
  1045. * _.isFinite('');
  1046. * // => false
  1047. *
  1048. * _.isFinite(Infinity);
  1049. * // => false
  1050. */
  1051. function isFinite(value) {
  1052. return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
  1053. }
  1054. /**
  1055. * Checks if `value` is a function.
  1056. *
  1057. * @static
  1058. * @memberOf _
  1059. * @category Objects
  1060. * @param {Mixed} value The value to check.
  1061. * @returns {Boolean} Returns `true`, if the `value` is a function, else `false`.
  1062. * @example
  1063. *
  1064. * _.isFunction(_);
  1065. * // => true
  1066. */
  1067. function isFunction(value) {
  1068. return typeof value == 'function';
  1069. }
  1070. // fallback for older versions of Chrome and Safari
  1071. if (isFunction(/x/)) {
  1072. isFunction = function(value) {
  1073. return typeof value == 'function' && toString.call(value) == funcClass;
  1074. };
  1075. }
  1076. /**
  1077. * Checks if `value` is the language type of Object.
  1078. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1079. *
  1080. * @static
  1081. * @memberOf _
  1082. * @category Objects
  1083. * @param {Mixed} value The value to check.
  1084. * @returns {Boolean} Returns `true`, if the `value` is an object, else `false`.
  1085. * @example
  1086. *
  1087. * _.isObject({});
  1088. * // => true
  1089. *
  1090. * _.isObject([1, 2, 3]);
  1091. * // => true
  1092. *
  1093. * _.isObject(1);
  1094. * // => false
  1095. */
  1096. function isObject(value) {
  1097. // check if the value is the ECMAScript language type of Object
  1098. // http://es5.github.com/#x8
  1099. // and avoid a V8 bug
  1100. // http://code.google.com/p/v8/issues/detail?id=2291
  1101. return !!(value && objectTypes[typeof value]);
  1102. }
  1103. /**
  1104. * Checks if `value` is `NaN`.
  1105. *
  1106. * Note: This is not the same as native `isNaN`, which will return `true` for
  1107. * `undefined` and other values. See http://es5.github.com/#x15.1.2.4.
  1108. *
  1109. * @static
  1110. * @memberOf _
  1111. * @category Objects
  1112. * @param {Mixed} value The value to check.
  1113. * @returns {Boolean} Returns `true`, if the `value` is `NaN`, else `false`.
  1114. * @example
  1115. *
  1116. * _.isNaN(NaN);
  1117. * // => true
  1118. *
  1119. * _.isNaN(new Number(NaN));
  1120. * // => true
  1121. *
  1122. * isNaN(undefined);
  1123. * // => true
  1124. *
  1125. * _.isNaN(undefined);
  1126. * // => false
  1127. */
  1128. function isNaN(value) {
  1129. // `NaN` as a primitive is the only value that is not equal to itself
  1130. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  1131. return isNumber(value) && value != +value
  1132. }
  1133. /**
  1134. * Checks if `value` is `null`.
  1135. *
  1136. * @static
  1137. * @memberOf _
  1138. * @category Objects
  1139. * @param {Mixed} value The value to check.
  1140. * @returns {Boolean} Returns `true`, if the `value` is `null`, else `false`.
  1141. * @example
  1142. *
  1143. * _.isNull(null);
  1144. * // => true
  1145. *
  1146. * _.isNull(undefined);
  1147. * // => false
  1148. */
  1149. function isNull(value) {
  1150. return value === null;
  1151. }
  1152. /**
  1153. * Checks if `value` is a number.
  1154. *
  1155. * @static
  1156. * @memberOf _
  1157. * @category Objects
  1158. * @param {Mixed} value The value to check.
  1159. * @returns {Boolean} Returns `true`, if the `value` is a number, else `false`.
  1160. * @example
  1161. *
  1162. * _.isNumber(8.4 * 5);
  1163. * // => true
  1164. */
  1165. function isNumber(value) {
  1166. return typeof value == 'number' || toString.call(value) == numberClass;
  1167. }
  1168. /**
  1169. * Checks if `value` is a regular expression.
  1170. *
  1171. * @static
  1172. * @memberOf _
  1173. * @category Objects
  1174. * @param {Mixed} value The value to check.
  1175. * @returns {Boolean} Returns `true`, if the `value` is a regular expression, else `false`.
  1176. * @example
  1177. *
  1178. * _.isRegExp(/moe/);
  1179. * // => true
  1180. */
  1181. function isRegExp(value) {
  1182. return !!(value && objectTypes[typeof value]) && toString.call(value) == regexpClass;
  1183. }
  1184. /**
  1185. * Checks if `value` is a string.
  1186. *
  1187. * @static
  1188. * @memberOf _
  1189. * @category Objects
  1190. * @param {Mixed} value The value to check.
  1191. * @returns {Boolean} Returns `true`, if the `value` is a string, else `false`.
  1192. * @example
  1193. *
  1194. * _.isString('moe');
  1195. * // => true
  1196. */
  1197. function isString(value) {
  1198. return typeof value == 'string' || toString.call(value) == stringClass;
  1199. }
  1200. /**
  1201. * Checks if `value` is `undefined`.
  1202. *
  1203. * @static
  1204. * @memberOf _
  1205. * @category Objects
  1206. * @param {Mixed} value The value to check.
  1207. * @returns {Boolean} Returns `true`, if the `value` is `undefined`, else `false`.
  1208. * @example
  1209. *
  1210. * _.isUndefined(void 0);
  1211. * // => true
  1212. */
  1213. function isUndefined(value) {
  1214. return typeof value == 'undefined';
  1215. }
  1216. /**
  1217. * Creates a shallow clone of `object` excluding the specified properties.
  1218. * Property names may be specified as individual arguments or as arrays of
  1219. * property names. If a `callback` function is passed, it will be executed
  1220. * for each property in the `object`, omitting the properties `callback`
  1221. * returns truthy for. The `callback` is bound to `thisArg` and invoked
  1222. * with three arguments; (value, key, object).
  1223. *
  1224. * @static
  1225. * @memberOf _
  1226. * @category Objects
  1227. * @param {Object} object The source object.
  1228. * @param {Function|String} callback|[prop1, prop2, ...] The properties to omit
  1229. * or the function called per iteration.
  1230. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1231. * @returns {Object} Returns an object without the omitted properties.
  1232. * @example
  1233. *
  1234. * _.omit({ 'name': 'moe', 'age': 40 }, 'age');
  1235. * // => { 'name': 'moe' }
  1236. *
  1237. * _.omit({ 'name': 'moe', 'age': 40 }, function(value) {
  1238. * return typeof value == 'number';
  1239. * });
  1240. * // => { 'name': 'moe' }
  1241. */
  1242. function omit(object) {
  1243. var indexOf = getIndexOf(),
  1244. props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
  1245. result = {};
  1246. forIn(object, function(value, key) {
  1247. if (indexOf(props, key) < 0) {
  1248. result[key] = value;
  1249. }
  1250. });
  1251. return result;
  1252. }
  1253. /**
  1254. * Creates a two dimensional array of the given object's key-value pairs,
  1255. * i.e. `[[key1, value1], [key2, value2]]`.
  1256. *
  1257. * @static
  1258. * @memberOf _
  1259. * @category Objects
  1260. * @param {Object} object The object to inspect.
  1261. * @returns {Array} Returns new array of key-value pairs.
  1262. * @example
  1263. *
  1264. * _.pairs({ 'moe': 30, 'larry': 40 });
  1265. * // => [['moe', 30], ['larry', 40]] (order is not guaranteed)
  1266. */
  1267. function pairs(object) {
  1268. var index = -1,
  1269. props = keys(object),
  1270. length = props.length,
  1271. result = Array(length);
  1272. while (++index < length) {
  1273. var key = props[index];
  1274. result[index] = [key, object[key]];
  1275. }
  1276. return result;
  1277. }
  1278. /**
  1279. * Creates a shallow clone of `object` composed of the specified properties.
  1280. * Property names may be specified as individual arguments or as arrays of property
  1281. * names. If `callback` is passed, it will be executed for each property in the
  1282. * `object`, picking the properties `callback` returns truthy for. The `callback`
  1283. * is bound to `thisArg` and invoked with three arguments; (value, key, object).
  1284. *
  1285. * @static
  1286. * @memberOf _
  1287. * @category Objects
  1288. * @param {Object} object The source object.
  1289. * @param {Array|Function|String} callback|[prop1, prop2, ...] The function called
  1290. * per iteration or properties to pick, either as individual arguments or arrays.
  1291. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1292. * @returns {Object} Returns an object composed of the picked properties.
  1293. * @example
  1294. *
  1295. * _.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name');
  1296. * // => { 'name': 'moe' }
  1297. *
  1298. * _.pick({ 'name': 'moe', '_userid': 'moe1' }, function(value, key) {
  1299. * return key.charAt(0) != '_';
  1300. * });
  1301. * // => { 'name': 'moe' }
  1302. */
  1303. function pick(object) {
  1304. var index = -1,
  1305. props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
  1306. length = props.length,
  1307. result = {};
  1308. while (++index < length) {
  1309. var prop = props[index];
  1310. if (prop in object) {
  1311. result[prop] = object[prop];
  1312. }
  1313. }
  1314. return result;
  1315. }
  1316. /**
  1317. * Creates an array composed of the own enumerable property values of `object`.
  1318. *
  1319. * @static
  1320. * @memberOf _
  1321. * @category Objects
  1322. * @param {Object} object The object to inspect.
  1323. * @returns {Array} Returns a new array of property values.
  1324. * @example
  1325. *
  1326. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  1327. * // => [1, 2, 3] (order is not guaranteed)
  1328. */
  1329. function values(object) {
  1330. var index = -1,
  1331. props = keys(object),
  1332. length = props.length,
  1333. result = Array(length);
  1334. while (++index < length) {
  1335. result[index] = object[props[index]];
  1336. }
  1337. return result;
  1338. }
  1339. /*--------------------------------------------------------------------------*/
  1340. /**
  1341. * Checks if a given `target` element is present in a `collection` using strict
  1342. * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
  1343. * as the offset from the end of the collection.
  1344. *
  1345. * @static
  1346. * @memberOf _
  1347. * @alias include
  1348. * @category Collections
  1349. * @param {Array|Object|String} collection The collection to iterate over.
  1350. * @param {Mixed} target The value to check for.
  1351. * @param {Number} [fromIndex=0] The index to search from.
  1352. * @returns {Boolean} Returns `true` if the `target` element is found, else `false`.
  1353. * @example
  1354. *
  1355. * _.contains([1, 2, 3], 1);
  1356. * // => true
  1357. *
  1358. * _.contains([1, 2, 3], 1, 2);
  1359. * // => false
  1360. *
  1361. * _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
  1362. * // => true
  1363. *
  1364. * _.contains('curly', 'ur');
  1365. * // => true
  1366. */
  1367. function contains(collection, target) {
  1368. var indexOf = getIndexOf(),
  1369. length = collection ? collection.length : 0,
  1370. result = false;
  1371. if (length && typeof length == 'number') {
  1372. result = indexOf(collection, target) > -1;
  1373. } else {
  1374. forOwn(collection, function(value) {
  1375. return (result = value === target) && indicatorObject;
  1376. });
  1377. }
  1378. return result;
  1379. }
  1380. /**
  1381. * Creates an object composed of keys returned from running each element of the
  1382. * `collection` through the given `callback`. The corresponding value of each key
  1383. * is the number of times the key was returned by the `callback`. The `callback`
  1384. * is bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
  1385. *
  1386. * If a property name is passed for `callback`, the created "_.pluck" style
  1387. * callback will return the property value of the given element.
  1388. *
  1389. * If an object is passed for `callback`, the created "_.where" style callback
  1390. * will return `true` for elements that have the properties of the given object,
  1391. * else `false`.
  1392. *
  1393. * @static
  1394. * @memberOf _
  1395. * @category Collections
  1396. * @param {Array|Object|String} collection The collection to iterate over.
  1397. * @param {Function|Object|String} [callback=identity] The function called per
  1398. * iteration. If a property name or object is passed, it will be used to create
  1399. * a "_.pluck" or "_.where" style callback, respectively.
  1400. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1401. * @returns {Object} Returns the composed aggregate object.
  1402. * @example
  1403. *
  1404. * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
  1405. * // => { '4': 1, '6': 2 }
  1406. *
  1407. * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  1408. * // => { '4': 1, '6': 2 }
  1409. *
  1410. * _.countBy(['one', 'two', 'three'], 'length');
  1411. * // => { '3': 2, '5': 1 }
  1412. */
  1413. function countBy(collection, callback, thisArg) {
  1414. var result = {};
  1415. callback = createCallback(callback, thisArg);
  1416. forEach(collection, function(value, key, collection) {
  1417. key = String(callback(value, key, collection));
  1418. (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
  1419. });
  1420. return result;
  1421. }
  1422. /**
  1423. * Checks if the `callback` returns a truthy value for **all** elements of a
  1424. * `collection`. The `callback` is bound to `thisArg` and invoked with three
  1425. * arguments; (value, index|key, collection).
  1426. *
  1427. * If a property name is passed for `callback`, the created "_.pluck" style
  1428. * callback will return the property value of the given element.
  1429. *
  1430. * If an object is passed for `callback`, the created "_.where" style callback
  1431. * will return `true` for elements that have the properties of the given object,
  1432. * else `false`.
  1433. *
  1434. * @static
  1435. * @memberOf _
  1436. * @alias all
  1437. * @category Collections
  1438. * @param {Array|Object|String} collection The collection to iterate over.
  1439. * @param {Function|Object|String} [callback=identity] The function called per
  1440. * iteration. If a property name or object is passed, it will be used to create
  1441. * a "_.pluck" or "_.where" style callback, respectively.
  1442. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1443. * @returns {Boolean} Returns `true` if all elements pass the callback check,
  1444. * else `false`.
  1445. * @example
  1446. *
  1447. * _.every([true, 1, null, 'yes'], Boolean);
  1448. * // => false
  1449. *
  1450. * var stooges = [
  1451. * { 'name': 'moe', 'age': 40 },
  1452. * { 'name': 'larry', 'age': 50 }
  1453. * ];
  1454. *
  1455. * // using "_.pluck" callback shorthand
  1456. * _.every(stooges, 'age');
  1457. * // => true
  1458. *
  1459. * // using "_.where" callback shorthand
  1460. * _.every(stooges, { 'age': 50 });
  1461. * // => false
  1462. */
  1463. function every(collection, callback, thisArg) {
  1464. var result = true;
  1465. callback = createCallback(callback, thisArg);
  1466. var index = -1,
  1467. length = collection ? collection.length : 0;
  1468. if (typeof length == 'number') {
  1469. while (++index < length) {
  1470. if (!(result = !!callback(collection[index], index, collection))) {
  1471. break;
  1472. }
  1473. }
  1474. } else {
  1475. forOwn(collection, function(value, index, collection) {
  1476. return !(result = !!callback(value, index, collection)) && indicatorObject;
  1477. });
  1478. }
  1479. return result;
  1480. }
  1481. /**
  1482. * Examines each element in a `collection`, returning an array of all elements
  1483. * the `callback` returns truthy for. The `callback` is bound to `thisArg` and
  1484. * invoked with three arguments; (value, index|key, collection).
  1485. *
  1486. * If a property name is passed for `callback`, the created "_.pluck" style
  1487. * callback will return the property value of the given element.
  1488. *
  1489. * If an object is passed for `callback`, the created "_.where" style callback
  1490. * will return `true` for elements that have the properties of the given object,
  1491. * else `false`.
  1492. *
  1493. * @static
  1494. * @memberOf _
  1495. * @alias select
  1496. * @category Collections
  1497. * @param {Array|Object|String} collection The collection to iterate over.
  1498. * @param {Function|Object|String} [callback=identity] The function called per
  1499. * iteration. If a property name or object is passed, it will be used to create
  1500. * a "_.pluck" or "_.where" style callback, respectively.
  1501. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1502. * @returns {Array} Returns a new array of elements that passed the callback check.
  1503. * @example
  1504. *
  1505. * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1506. * // => [2, 4, 6]
  1507. *
  1508. * var food = [
  1509. * { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  1510. * { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  1511. * ];
  1512. *
  1513. * // using "_.pluck" callback shorthand
  1514. * _.filter(food, 'organic');
  1515. * // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
  1516. *
  1517. * // using "_.where" callback shorthand
  1518. * _.filter(food, { 'type': 'fruit' });
  1519. * // => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]
  1520. */
  1521. function filter(collection, callback, thisArg) {
  1522. var result = [];
  1523. callback = createCallback(callback, thisArg);
  1524. var index = -1,
  1525. length = collection ? collection.length : 0;
  1526. if (typeof length == 'number') {
  1527. while (++index < length) {
  1528. var value = collection[index];
  1529. if (callback(value, index, collection)) {
  1530. result.push(value);
  1531. }
  1532. }
  1533. } else {
  1534. forOwn(collection, function(value, index, collec