PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/lodash.js/1.1.1/lodash.underscore.js

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