PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules_bad/grunt-img/node_modules/lodash/lodash.underscore.js

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

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