PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/project-grunt/node_modules/grunt-bower-task/node_modules/lodash/lodash.underscore.js

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