PageRenderTime 66ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/bower/node_modules/inquirer/node_modules/lodash/dist/lodash.backbone.js

https://bitbucket.org/biojazzard/gantry-eboracast
JavaScript | 3162 lines | 1339 code | 157 blank | 1666 comment | 268 complexity | f3d964e2eb3b7693778c8ad5acb5bd4d MD5 | raw file
Possible License(s): MIT, JSON, BSD-2-Clause, Unlicense, GPL-2.0, WTFPL, LGPL-3.0, Apache-2.0, 0BSD, BSD-3-Clause, CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file