PageRenderTime 77ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  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;
  1451. }
  1452. /**
  1453. * Creates an array of values by running each element in the `collection`
  1454. * through the `callback`. The `callback` is bound to `thisArg` and invoked with
  1455. * three arguments; (value, index|key, collection).
  1456. *
  1457. * If a property name is passed for `callback`, the created "_.pluck" style
  1458. * callback will return the property value of the given element.
  1459. *
  1460. * If an object is passed for `callback`, the created "_.where" style callback
  1461. * will return `true` for elements that have the properties of the given object,
  1462. * else `false`.
  1463. *
  1464. * @static
  1465. * @memberOf _
  1466. * @alias collect
  1467. * @category Collections
  1468. * @param {Array|Object|String} collection The collection to iterate over.
  1469. * @param {Function|Object|String} [callback=identity] The function called per
  1470. * iteration. If a property name or object is passed, it will be used to create
  1471. * a "_.pluck" or "_.where" style callback, respectively.
  1472. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1473. * @returns {Array} Returns a new array of the results of each `callback` execution.
  1474. * @example
  1475. *
  1476. * _.map([1, 2, 3], function(num) { return num * 3; });
  1477. * // => [3, 6, 9]
  1478. *
  1479. * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
  1480. * // => [3, 6, 9] (order is not guaranteed)
  1481. *
  1482. * var stooges = [
  1483. * { 'name': 'moe', 'age': 40 },
  1484. * { 'name': 'larry', 'age': 50 }
  1485. * ];
  1486. *
  1487. * // using "_.pluck" callback shorthand
  1488. * _.map(stooges, 'name');
  1489. * // => ['moe', 'larry']
  1490. */
  1491. function map(collection, callback, thisArg) {
  1492. var index = -1,
  1493. length = collection ? collection.length : 0;
  1494. callback = createCallback(callback, thisArg);
  1495. if (typeof length == 'number') {
  1496. var result = Array(length);
  1497. while (++index < length) {
  1498. result[index] = callback(collection[index], index, collection);
  1499. }
  1500. } else {
  1501. result = [];
  1502. forOwn(collection, function(value, key, collection) {
  1503. result[++index] = callback(value, key, collection);
  1504. });
  1505. }
  1506. return result;
  1507. }
  1508. /**
  1509. * Retrieves the maximum value of an `array`. If `callback` is passed,
  1510. * it will be executed for each value in the `array` to generate the
  1511. * criterion by which the value is ranked. The `callback` is bound to
  1512. * `thisArg` and invoked with three arguments; (value, index, collection).
  1513. *
  1514. * If a property name is passed for `callback`, the created "_.pluck" style
  1515. * callback will return the property value of the given element.
  1516. *
  1517. * If an object is passed for `callback`, the created "_.where" style callback
  1518. * will return `true` for elements that have the properties of the given object,
  1519. * else `false`.
  1520. *
  1521. * @static
  1522. * @memberOf _
  1523. * @category Collections
  1524. * @param {Array|Object|String} collection The collection to iterate over.
  1525. * @param {Function|Object|String} [callback=identity] The function called per
  1526. * iteration. If a property name or object is passed, it will be used to create
  1527. * a "_.pluck" or "_.where" style callback, respectively.
  1528. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1529. * @returns {Mixed} Returns the maximum value.
  1530. * @example
  1531. *
  1532. * _.max([4, 2, 8, 6]);
  1533. * // => 8
  1534. *
  1535. * var stooges = [
  1536. * { 'name': 'moe', 'age': 40 },
  1537. * { 'name': 'larry', 'age': 50 }
  1538. * ];
  1539. *
  1540. * _.max(stooges, function(stooge) { return stooge.age; });
  1541. * // => { 'name': 'larry', 'age': 50 };
  1542. *
  1543. * // using "_.pluck" callback shorthand
  1544. * _.max(stooges, 'age');
  1545. * // => { 'name': 'larry', 'age': 50 };
  1546. */
  1547. function max(collection, callback, thisArg) {
  1548. var computed = -Infinity,
  1549. result = computed;
  1550. var index = -1,
  1551. length = collection ? collection.length : 0;
  1552. if (!callback && typeof length == 'number') {
  1553. while (++index < length) {
  1554. var value = collection[index];
  1555. if (value > result) {
  1556. result = value;
  1557. }
  1558. }
  1559. } else {
  1560. callback = createCallback(callback, thisArg);
  1561. forEach(collection, function(value, index, collection) {
  1562. var current = callback(value, index, collection);
  1563. if (current > computed) {
  1564. computed = current;
  1565. result = value;
  1566. }
  1567. });
  1568. }
  1569. return result;
  1570. }
  1571. /**
  1572. * Retrieves the minimum value of an `array`. If `callback` is passed,
  1573. * it will be executed for each value in the `array` to generate the
  1574. * criterion by which the value is ranked. The `callback` is bound to `thisArg`
  1575. * and invoked with three arguments; (value, index, collection).
  1576. *
  1577. * If a property name is passed for `callback`, the created "_.pluck" style
  1578. * callback will return the property value of the given element.
  1579. *
  1580. * If an object is passed for `callback`, the created "_.where" style callback
  1581. * will return `true` for elements that have the properties of the given object,
  1582. * else `false`.
  1583. *
  1584. * @static
  1585. * @memberOf _
  1586. * @category Collections
  1587. * @param {Array|Object|String} collection The collection to iterate over.
  1588. * @param {Function|Object|String} [callback=identity] The function called per
  1589. * iteration. If a property name or object is passed, it will be used to create
  1590. * a "_.pluck" or "_.where" style callback, respectively.
  1591. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1592. * @returns {Mixed} Returns the minimum value.
  1593. * @example
  1594. *
  1595. * _.min([4, 2, 8, 6]);
  1596. * // => 2
  1597. *
  1598. * var stooges = [
  1599. * { 'name': 'moe', 'age': 40 },
  1600. * { 'name': 'larry', 'age': 50 }
  1601. * ];
  1602. *
  1603. * _.min(stooges, function(stooge) { return stooge.age; });
  1604. * // => { 'name': 'moe', 'age': 40 };
  1605. *
  1606. * // using "_.pluck" callback shorthand
  1607. * _.min(stooges, 'age');
  1608. * // => { 'name': 'moe', 'age': 40 };
  1609. */
  1610. function min(collection, callback, thisArg) {
  1611. var computed = Infinity,
  1612. result = computed;
  1613. var index = -1,
  1614. length = collection ? collection.length : 0;
  1615. if (!callback && typeof length == 'number') {
  1616. while (++index < length) {
  1617. var value = collection[index];
  1618. if (value < result) {
  1619. result = value;
  1620. }
  1621. }
  1622. } else {
  1623. callback = createCallback(callback, thisArg);
  1624. forEach(collection, function(value, index, collection) {
  1625. var current = callback(value, index, collection);
  1626. if (current < computed) {
  1627. computed = current;
  1628. result = value;
  1629. }
  1630. });
  1631. }
  1632. return result;
  1633. }
  1634. /**
  1635. * Reduces a `collection` to a value which is the accumulated result of running
  1636. * each element in the `collection` through the `callback`, where each successive
  1637. * `callback` execution consumes the return value of the previous execution.
  1638. * If `accumulator` is not passed, the first element of the `collection` will be
  1639. * used as the initial `accumulator` value. The `callback` is bound to `thisArg`
  1640. * and invoked with four arguments; (accumulator, value, index|key, collection).
  1641. *
  1642. * @static
  1643. * @memberOf _
  1644. * @alias foldl, inject
  1645. * @category Collections
  1646. * @param {Array|Object|String} collection The collection to iterate over.
  1647. * @param {Function} [callback=identity] The function called per iteration.
  1648. * @param {Mixed} [accumulator] Initial value of the accumulator.
  1649. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1650. * @returns {Mixed} Returns the accumulated value.
  1651. * @example
  1652. *
  1653. * var sum = _.reduce([1, 2, 3], function(sum, num) {
  1654. * return sum + num;
  1655. * });
  1656. * // => 6
  1657. *
  1658. * var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
  1659. * result[key] = num * 3;
  1660. * return result;
  1661. * }, {});
  1662. * // => { 'a': 3, 'b': 6, 'c': 9 }
  1663. */
  1664. function reduce(collection, callback, accumulator, thisArg) {
  1665. if (!collection) return accumulator;
  1666. var noaccum = arguments.length < 3;
  1667. callback = createCallback(callback, thisArg, 4);
  1668. var index = -1,
  1669. length = collection.length;
  1670. if (typeof length == 'number') {
  1671. if (noaccum) {
  1672. accumulator = collection[++index];
  1673. }
  1674. while (++index < length) {
  1675. accumulator = callback(accumulator, collection[index], index, collection);
  1676. }
  1677. } else {
  1678. forOwn(collection, function(value, index, collection) {
  1679. accumulator = noaccum
  1680. ? (noaccum = false, value)
  1681. : callback(accumulator, value, index, collection)
  1682. });
  1683. }
  1684. return accumulator;
  1685. }
  1686. /**
  1687. * This method is similar to `_.reduce`, except that it iterates over a
  1688. * `collection` from right to left.
  1689. *
  1690. * @static
  1691. * @memberOf _
  1692. * @alias foldr
  1693. * @category Collections
  1694. * @param {Array|Object|String} collection The collection to iterate over.
  1695. * @param {Function} [callback=identity] The function called per iteration.
  1696. * @param {Mixed} [accumulator] Initial value of the accumulator.
  1697. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1698. * @returns {Mixed} Returns the accumulated value.
  1699. * @example
  1700. *
  1701. * var list = [[0, 1], [2, 3], [4, 5]];
  1702. * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
  1703. * // => [4, 5, 2, 3, 0, 1]
  1704. */
  1705. function reduceRight(collection, callback, accumulator, thisArg) {
  1706. var iterable = collection,
  1707. length = collection ? collection.length : 0,
  1708. noaccum = arguments.length < 3;
  1709. if (typeof length != 'number') {
  1710. var props = keys(collection);
  1711. length = props.length;
  1712. }
  1713. callback = createCallback(callback, thisArg, 4);
  1714. forEach(collection, function(value, index, collection) {
  1715. index = props ? props[--length] : --length;
  1716. accumulator = noaccum
  1717. ? (noaccum = false, iterable[index])
  1718. : callback(accumulator, iterable[index], index, collection);
  1719. });
  1720. return accumulator;
  1721. }
  1722. /**
  1723. * The opposite of `_.filter`, this method returns the elements of a
  1724. * `collection` that `callback` does **not** return truthy for.
  1725. *
  1726. * If a property name is passed for `callback`, the created "_.pluck" style
  1727. * callback will return the property value of the given element.
  1728. *
  1729. * If an object is passed for `callback`, the created "_.where" style callback
  1730. * will return `true` for elements that have the properties of the given object,
  1731. * else `false`.
  1732. *
  1733. * @static
  1734. * @memberOf _
  1735. * @category Collections
  1736. * @param {Array|Object|String} collection The collection to iterate over.
  1737. * @param {Function|Object|String} [callback=identity] The function called per
  1738. * iteration. If a property name or object is passed, it will be used to create
  1739. * a "_.pluck" or "_.where" style callback, respectively.
  1740. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1741. * @returns {Array} Returns a new array of elements that did **not** pass the
  1742. * callback check.
  1743. * @example
  1744. *
  1745. * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1746. * // => [1, 3, 5]
  1747. *
  1748. * var food = [
  1749. * { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  1750. * { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  1751. * ];
  1752. *
  1753. * // using "_.pluck" callback shorthand
  1754. * _.reject(food, 'organic');
  1755. * // => [{ 'name': 'apple', 'organic': false, 'type': 'fruit' }]
  1756. *
  1757. * // using "_.where" callback shorthand
  1758. * _.reject(food, { 'type': 'fruit' });
  1759. * // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
  1760. */
  1761. function reject(collection, callback, thisArg) {
  1762. callback = createCallback(callback, thisArg);
  1763. return filter(collection, function(value, index, collection) {
  1764. return !callback(value, index, collection);
  1765. });
  1766. }
  1767. /**
  1768. * Creates an array of shuffled `array` values, using a version of the
  1769. * Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
  1770. *
  1771. * @static
  1772. * @memberOf _
  1773. * @category Collections
  1774. * @param {Array|Object|String} collection The collection to shuffle.
  1775. * @returns {Array} Returns a new shuffled collection.
  1776. * @example
  1777. *
  1778. * _.shuffle([1, 2, 3, 4, 5, 6]);
  1779. * // => [4, 1, 6, 3, 5, 2]
  1780. */
  1781. function shuffle(collection) {
  1782. var index = -1,
  1783. length = collection ? collection.length : 0,
  1784. result = Array(typeof length == 'number' ? length : 0);
  1785. forEach(collection, function(value) {
  1786. var rand = floor(nativeRandom() * (++index + 1));
  1787. result[index] = result[rand];
  1788. result[rand] = value;
  1789. });
  1790. return result;
  1791. }
  1792. /**
  1793. * Gets the size of the `collection` by returning `collection.length` for arrays
  1794. * and array-like objects or the number of own enumerable properties for objects.
  1795. *
  1796. * @static
  1797. * @memberOf _
  1798. * @category Collections
  1799. * @param {Array|Object|String} collection The collection to inspect.
  1800. * @returns {Number} Returns `collection.length` or number of own enumerable properties.
  1801. * @example
  1802. *
  1803. * _.size([1, 2]);
  1804. * // => 2
  1805. *
  1806. * _.size({ 'one': 1, 'two': 2, 'three': 3 });
  1807. * // => 3
  1808. *
  1809. * _.size('curly');
  1810. * // => 5
  1811. */
  1812. function size(collection) {
  1813. var length = collection ? collection.length : 0;
  1814. return typeof length == 'number' ? length : keys(collection).length;
  1815. }
  1816. /**
  1817. * Checks if the `callback` returns a truthy value for **any** element of a
  1818. * `collection`. The function returns as soon as it finds passing value, and
  1819. * does not iterate over the entire `collection`. The `callback` is bound to
  1820. * `thisArg` and invoked with three arguments; (value, index|key, collection).
  1821. *
  1822. * If a property name is passed for `callback`, the created "_.pluck" style
  1823. * callback will return the property value of the given element.
  1824. *
  1825. * If an object is passed for `callback`, the created "_.where" style callback
  1826. * will return `true` for elements that have the properties of the given object,
  1827. * else `false`.
  1828. *
  1829. * @static
  1830. * @memberOf _
  1831. * @alias any
  1832. * @category Collections
  1833. * @param {Array|Object|String} collection The collection to iterate over.
  1834. * @param {Function|Object|String} [callback=identity] The function called per
  1835. * iteration. If a property name or object is passed, it will be used to create
  1836. * a "_.pluck" or "_.where" style callback, respectively.
  1837. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1838. * @returns {Boolean} Returns `true` if any element passes the callback check,
  1839. * else `false`.
  1840. * @example
  1841. *
  1842. * _.some([null, 0, 'yes', false], Boolean);
  1843. * // => true
  1844. *
  1845. * var food = [
  1846. * { 'name': 'apple', 'organic': false, 'type': 'fruit' },
  1847. * { 'name': 'carrot', 'organic': true, 'type': 'vegetable' }
  1848. * ];
  1849. *
  1850. * // using "_.pluck" callback shorthand
  1851. * _.some(food, 'organic');
  1852. * // => true
  1853. *
  1854. * // using "_.where" callback shorthand
  1855. * _.some(food, { 'type': 'meat' });
  1856. * // => false
  1857. */
  1858. function some(collection, callback, thisArg) {
  1859. var result;
  1860. callback = createCallback(callback, thisArg);
  1861. var index = -1,
  1862. length = collection ? collection.length : 0;
  1863. if (typeof length == 'number') {
  1864. while (++index < length) {
  1865. if ((result = callback(collection[index], index, collection))) {
  1866. break;
  1867. }
  1868. }
  1869. } else {
  1870. forOwn(collection, function(value, index, collection) {
  1871. return (result = callback(value, index, collection)) && indicatorObject;
  1872. });
  1873. }
  1874. return !!result;
  1875. }
  1876. /**
  1877. * Creates an array of elements, sorted in ascending order by the results of
  1878. * running each element in the `collection` through the `callback`. This method
  1879. * performs a stable sort, that is, it will preserve the original sort order of
  1880. * equal elements. The `callback` is bound to `thisArg` and invoked with three
  1881. * arguments; (value, index|key, collection).
  1882. *
  1883. * If a property name is passed for `callback`, the created "_.pluck" style
  1884. * callback will return the property value of the given element.
  1885. *
  1886. * If an object is passed for `callback`, the created "_.where" style callback
  1887. * will return `true` for elements that have the properties of the given object,
  1888. * else `false`.
  1889. *
  1890. * @static
  1891. * @memberOf _
  1892. * @category Collections
  1893. * @param {Array|Object|String} collection The collection to iterate over.
  1894. * @param {Function|Object|String} [callback=identity] The function called per
  1895. * iteration. If a property name or object is passed, it will be used to create
  1896. * a "_.pluck" or "_.where" style callback, respectively.
  1897. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1898. * @returns {Array} Returns a new array of sorted elements.
  1899. * @example
  1900. *
  1901. * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
  1902. * // => [3, 1, 2]
  1903. *
  1904. * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
  1905. * // => [3, 1, 2]
  1906. *
  1907. * // using "_.pluck" callback shorthand
  1908. * _.sortBy(['banana', 'strawberry', 'apple'], 'length');
  1909. * // => ['apple', 'banana', 'strawberry']
  1910. */
  1911. function sortBy(collection, callback, thisArg) {
  1912. var index = -1,
  1913. length = collection ? collection.length : 0,
  1914. result = Array(typeof length == 'number' ? length : 0);
  1915. callback = createCallback(callback, thisArg);
  1916. forEach(collection, function(value, key, collection) {
  1917. result[++index] = {
  1918. 'criteria': callback(value, key, collection),
  1919. 'index': index,
  1920. 'value': value
  1921. };
  1922. });
  1923. length = result.length;
  1924. result.sort(compareAscending);
  1925. while (length--) {
  1926. result[length] = result[length].value;
  1927. }
  1928. return result;
  1929. }
  1930. /**
  1931. * Converts the `collection` to an array.
  1932. *
  1933. * @static
  1934. * @memberOf _
  1935. * @category Collections
  1936. * @param {Array|Object|String} collection The collection to convert.
  1937. * @returns {Array} Returns the new converted array.
  1938. * @example
  1939. *
  1940. * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
  1941. * // => [2, 3, 4]
  1942. */
  1943. function toArray(collection) {
  1944. if (isArray(collection)) {
  1945. return nativeSlice.call(collection);
  1946. }
  1947. if (collection && typeof collection.length == 'number') {
  1948. return map(collection);
  1949. }
  1950. return values(collection);
  1951. }
  1952. /*--------------------------------------------------------------------------*/
  1953. /**
  1954. * Creates an array of `array` elements not present in the other arrays
  1955. * using strict equality for comparisons, i.e. `===`.
  1956. *
  1957. * @static
  1958. * @memberOf _
  1959. * @category Arrays
  1960. * @param {Array} array The array to process.
  1961. * @param {Array} [array1, array2, ...] Arrays to check.
  1962. * @returns {Array} Returns a new array of `array` elements not present in the
  1963. * other arrays.
  1964. * @example
  1965. *
  1966. * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
  1967. * // => [1, 3, 4]
  1968. */
  1969. function difference(array) {
  1970. var index = -1,
  1971. length = array.length,
  1972. flattened = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
  1973. result = [];
  1974. while (++index < length) {
  1975. var value = array[index];
  1976. if (indexOf(flattened, value) < 0) {
  1977. result.push(value);
  1978. }
  1979. }
  1980. return result;
  1981. }
  1982. /**
  1983. * Gets the first element of the `array`. If a number `n` is passed, the first
  1984. * `n` elements of the `array` are returned. If a `callback` function is passed,
  1985. * elements at the beginning of the array are returned as long as the `callback`
  1986. * returns truthy. The `callback` is bound to `thisArg` and invoked with three
  1987. * arguments; (value, index, array).
  1988. *
  1989. * If a property name is passed for `callback`, the created "_.pluck" style
  1990. * callback will return the property value of the given element.
  1991. *
  1992. * If an object is passed for `callback`, the created "_.where" style callback
  1993. * will return `true` for elements that have the properties of the given object,
  1994. * else `false`.
  1995. *
  1996. * @static
  1997. * @memberOf _
  1998. * @alias head, take
  1999. * @category Arrays
  2000. * @param {Array} array The array to query.
  2001. * @param {Function|Object|Number|String} [callback|n] The function called
  2002. * per element or the number of elements to return. If a property name or
  2003. * object is passed, it will be used to create a "_.pluck" or "_.where"
  2004. * style callback, respectively.
  2005. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  2006. * @returns {Mixed} Returns the first element(s) of `array`.
  2007. * @example
  2008. *
  2009. * _.first([1, 2, 3]);
  2010. * // => 1
  2011. *
  2012. * _.first([1, 2, 3], 2);
  2013. * // => [1, 2]
  2014. *
  2015. * _.first([1, 2, 3], function(num) {
  2016. * return num < 3;
  2017. * });
  2018. * // => [1, 2]
  2019. *
  2020. * var food = [
  2021. * { 'name': 'banana', 'organic': true },
  2022. * { 'name': 'beet', 'organic': false },
  2023. * ];
  2024. *
  2025. * // using "_.pluck" callback shorthand
  2026. * _.first(food, 'organic');
  2027. * // => [{ 'name': 'banana', 'organic': true }]
  2028. *
  2029. * var food = [
  2030. * { 'name': 'apple', 'type': 'fruit' },
  2031. * { 'name': 'banana', 'type': 'fruit' },
  2032. * { 'name': 'beet', 'type': 'vegetable' }
  2033. * ];
  2034. *
  2035. * // using "_.where" callback shorthand
  2036. * _.first(food, { 'type': 'fruit' });
  2037. * // => [{ 'name': 'apple', 'type': 'fruit' }, { 'name': 'banana', 'type': 'fruit' }]
  2038. */
  2039. function first(array, callback, thisArg) {
  2040. if (array) {
  2041. var n = 0,
  2042. length = array.length;
  2043. if (typeof callback != 'number' && callback != null) {
  2044. var index = -1;
  2045. callback = createCallback(callback, thisArg);
  2046. while (++index < length && callback(array[index], index, array)) {
  2047. n++;
  2048. }
  2049. } else {
  2050. n = callback;
  2051. if (n == null || thisArg) {
  2052. return array[0];
  2053. }
  2054. }
  2055. return nativeSlice.call(array, 0, nativeMin(nativeMax(0, n), length));
  2056. }
  2057. }
  2058. /**
  2059. * Gets the index at which the first occurrence of `value` is found using
  2060. * strict equality for comparisons, i.e. `===`. If the `array` is already
  2061. * sorted, passing `true` for `fromIndex` will run a faster binary search.
  2062. *
  2063. * @static
  2064. * @memberOf _
  2065. * @category Arrays
  2066. * @param {Array} array The array to search.
  2067. * @param {Mixed} value The value to search for.
  2068. * @param {Boolean|Number} [fromIndex=0] The index to search from or `true` to
  2069. * perform a binary search on a sorted `array`.
  2070. * @returns {Number} Returns the index of the matched value or `-1`.
  2071. * @example
  2072. *
  2073. * _.indexOf([1, 2, 3, 1, 2, 3], 2);
  2074. * // => 1
  2075. *
  2076. * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
  2077. * // => 4
  2078. *
  2079. * _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
  2080. * // => 2
  2081. */
  2082. function indexOf(array, value, fromIndex) {
  2083. var index = -1,
  2084. length = array ? array.length : 0;
  2085. if (typeof fromIndex == 'number') {
  2086. index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0) - 1;
  2087. } else if (fromIndex) {
  2088. index = sortedIndex(array, value);
  2089. return array[index] === value ? index : -1;
  2090. }
  2091. while (++index < length) {
  2092. if (array[index] === value) {
  2093. return index;
  2094. }
  2095. }
  2096. return -1;
  2097. }
  2098. /**
  2099. * Gets all but the last element of `array`. If a number `n` is passed, the
  2100. * last `n` elements are excluded from the result. If a `callback` function
  2101. * is passed, elements at the end of the array are excluded from the result
  2102. * as long as the `callback` returns truthy. The `callback` is bound to
  2103. * `thisArg` and invoked with three arguments; (value, index, array).
  2104. *
  2105. * If a property name is passed for `callback`, the created "_.pluck" style
  2106. * callback will return the property value of the given element.
  2107. *
  2108. * If an object is passed for `callback`, the created "_.where" style callback
  2109. * will return `true` for elements that have the properties of the given object,
  2110. * else `false`.
  2111. *
  2112. * @static
  2113. * @memberOf _
  2114. * @category Arrays
  2115. * @param {Array} array The array to query.
  2116. * @param {Function|Object|Number|String} [callback|n=1] The function called
  2117. * per element or the number of elements to exclude. If a property name or
  2118. * object is passed, it will be used to create a "_.pluck" or "_.where"
  2119. * style callback, respectively.
  2120. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  2121. * @returns {Array} Returns a slice of `array`.
  2122. * @example
  2123. *
  2124. * _.initial([1, 2, 3]);
  2125. * // => [1, 2]
  2126. *
  2127. * _.initial([1, 2, 3], 2);
  2128. * // => [1]
  2129. *
  2130. * _.initial([1, 2, 3], function(num) {
  2131. * return num > 1;
  2132. * });
  2133. * // => [1]
  2134. *
  2135. * var food = [
  2136. * { 'name': 'beet', 'organic': false },
  2137. * { 'name': 'carrot', 'organic': true }
  2138. * ];
  2139. *
  2140. * // using "_.pluck" callback shorthand
  2141. * _.initial(food, 'organic');
  2142. * // => [{ 'name': 'beet', 'organic': false }]
  2143. *
  2144. * var food = [
  2145. * { 'name': 'banana', 'type': 'fruit' },
  2146. * { 'name': 'beet', 'type': 'vegetable' },
  2147. * { 'name': 'carrot', 'type': 'vegetable' }
  2148. * ];
  2149. *
  2150. * // using "_.where" callback shorthand
  2151. * _.initial(food, { 'type': 'vegetable' });
  2152. * // => [{ 'name': 'banana', 'type': 'fruit' }]
  2153. */
  2154. function initial(array, callback, thisArg) {
  2155. if (!array) {
  2156. return [];
  2157. }
  2158. var n = 0,
  2159. length = array.length;
  2160. if (typeof callback != 'number' && callback != null) {
  2161. var index = length;
  2162. callback = createCallback(callback, thisArg);
  2163. while (index-- && callback(array[index], index, array)) {
  2164. n++;
  2165. }
  2166. } else {
  2167. n = (callback == null || thisArg) ? 1 : callback || n;
  2168. }
  2169. return nativeSlice.call(array, 0, nativeMin(nativeMax(0, length - n), length));
  2170. }
  2171. /**
  2172. * Gets the last element of the `array`. If a number `n` is passed, the
  2173. * last `n` elements of the `array` are returned. If a `callback` function
  2174. * is passed, elements at the end of the array are returned as long as the
  2175. * `callback` returns truthy. The `callback` is bound to `thisArg` and
  2176. * invoked with three arguments;(value, index, array).
  2177. *
  2178. *
  2179. * If a property name is passed for `callback`, the created "_.pluck" style
  2180. * callback will return the property value of the given element.
  2181. *
  2182. * If an object is passed for `callback`, the created "_.where" style callback
  2183. * will return `true` for elements that have the properties of the given object,
  2184. * else `false`.
  2185. *
  2186. * @static
  2187. * @memberOf _
  2188. * @category Arrays
  2189. * @param {Array} array The array to query.
  2190. * @param {Function|Object|Number|String} [callback|n] The function called
  2191. * per element or the number of elements to return. If a property name or
  2192. * object is passed, it will be used to create a "_.pluck" or "_.where"
  2193. * style callback, respectively.
  2194. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  2195. * @returns {Mixed} Returns the last element(s) of `array`.
  2196. * @example
  2197. *
  2198. * _.last([1, 2, 3]);
  2199. * // => 3
  2200. *
  2201. * _.last([1, 2, 3], 2);
  2202. * // => [2, 3]
  2203. *
  2204. * _.last([1, 2, 3], function(num) {
  2205. * return num > 1;
  2206. * });
  2207. * // => [2, 3]
  2208. *
  2209. * var food = [
  2210. * { 'name': 'beet', 'organic': false },
  2211. * { 'name': 'carrot', 'organic': true }
  2212. * ];
  2213. *
  2214. * // using "_.pluck" callback shorthand
  2215. * _.last(food, 'organic');
  2216. * // => [{ 'name': 'carrot', 'organic': true }]
  2217. *
  2218. * var food = [
  2219. * { 'name': 'banana', 'type': 'fruit' },
  2220. * { 'name': 'beet', 'type': 'vegetable' },
  2221. * { 'name': 'carrot', 'type': 'vegetable' }
  2222. * ];
  2223. *
  2224. * // using "_.where" callback shorthand
  2225. * _.last(food, { 'type': 'vegetable' });
  2226. * // => [{ 'name': 'beet', 'type': 'vegetable' }, { 'name': 'carrot', 'type': 'vegetable' }]
  2227. */
  2228. function last(array, callback, thisArg) {
  2229. if (array) {
  2230. var n = 0,
  2231. length = array.length;
  2232. if (typeof callback != 'number' && callback != null) {
  2233. var index = length;
  2234. callback = createCallback(callback, thisArg);
  2235. while (index-- && callback(array[index], index, array)) {
  2236. n++;
  2237. }
  2238. } else {
  2239. n = callback;
  2240. if (n == null || thisArg) {
  2241. return array[length - 1];
  2242. }
  2243. }
  2244. return nativeSlice.call(array, nativeMax(0, length - n));
  2245. }
  2246. }
  2247. /**
  2248. * Gets the index at which the last occurrence of `value` is found using strict
  2249. * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
  2250. * as the offset from the end of the collection.
  2251. *
  2252. * @static
  2253. * @memberOf _
  2254. * @category Arrays
  2255. * @param {Array} array The array to search.
  2256. * @param {Mixed} value The value to search for.
  2257. * @param {Number} [fromIndex=array.length-1] The index to search from.
  2258. * @returns {Number} Returns the index of the matched value or `-1`.
  2259. * @example
  2260. *
  2261. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
  2262. * // => 4
  2263. *
  2264. * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
  2265. * // => 1
  2266. */
  2267. function lastIndexOf(array, value, fromIndex) {
  2268. var index = array ? array.length : 0;
  2269. if (typeof fromIndex == 'number') {
  2270. index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
  2271. }
  2272. while (index--) {
  2273. if (array[index] === value) {
  2274. return index;
  2275. }
  2276. }
  2277. return -1;
  2278. }
  2279. /**
  2280. * The opposite of `_.initial`, this method gets all but the first value of
  2281. * `array`. If a number `n` is passed, the first `n` values are excluded from
  2282. * the result. If a `callback` function is passed, elements at the beginning
  2283. * of the array are excluded from the result as long as the `callback` returns
  2284. * truthy. The `callback` is bound to `thisArg` and invoked with three
  2285. * arguments; (value, index, array).
  2286. *
  2287. * If a property name is passed for `callback`, the created "_.pluck" style
  2288. * callback will return the property value of the given element.
  2289. *
  2290. * If an object is passed for `callback`, the created "_.where" style callback
  2291. * will return `true` for elements that have the properties of the given object,
  2292. * else `false`.
  2293. *
  2294. * @static
  2295. * @memberOf _
  2296. * @alias drop, tail
  2297. * @category Arrays
  2298. * @param {Array} array The array to query.
  2299. * @param {Function|Object|Number|String} [callback|n=1] The function called
  2300. * per element or the number of elements to exclude. If a property name or
  2301. * object is passed, it will be used to create a "_.pluck" or "_.where"
  2302. * style callback, respectively.
  2303. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  2304. * @returns {Array} Returns a slice of `array`.
  2305. * @example
  2306. *
  2307. * _.rest([1, 2, 3]);
  2308. * // => [2, 3]
  2309. *
  2310. * _.rest([1, 2, 3], 2);
  2311. * // => [3]
  2312. *
  2313. * _.rest([1, 2, 3], function(num) {
  2314. * return num < 3;
  2315. * });
  2316. * // => [3]
  2317. *
  2318. * var food = [
  2319. * { 'name': 'banana', 'organic': true },
  2320. * { 'name': 'beet', 'organic': false },
  2321. * ];
  2322. *
  2323. * // using "_.pluck" callback shorthand
  2324. * _.rest(food, 'organic');
  2325. * // => [{ 'name': 'beet', 'organic': false }]
  2326. *
  2327. * var food = [
  2328. * { 'name': 'apple', 'type': 'fruit' },
  2329. * { 'name': 'banana', 'type': 'fruit' },
  2330. * { 'name': 'beet', 'type': 'vegetable' }
  2331. * ];
  2332. *
  2333. * // using "_.where" callback shorthand
  2334. * _.rest(food, { 'type': 'fruit' });
  2335. * // => [{ 'name': 'beet', 'type': 'vegetable' }]
  2336. */
  2337. function rest(array, callback, thisArg) {
  2338. if (typeof callback != 'number' && callback != null) {
  2339. var n = 0,
  2340. index = -1,
  2341. length = array ? array.length : 0;
  2342. callback = createCallback(callback, thisArg);
  2343. while (++index < length && callback(array[index], index, array)) {
  2344. n++;
  2345. }
  2346. } else {
  2347. n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
  2348. }
  2349. return nativeSlice.call(array, n);
  2350. }
  2351. /**
  2352. * Uses a binary search to determine the smallest index at which the `value`
  2353. * should be inserted into `array` in order to maintain the sort order of the
  2354. * sorted `array`. If `callback` is passed, it will be executed for `value` and
  2355. * each element in `array` to compute their sort ranking. The `callback` is
  2356. * bound to `thisArg` and invoked with one argument; (value).
  2357. *
  2358. * If a property name is passed for `callback`, the created "_.pluck" style
  2359. * callback will return the property value of the given element.
  2360. *
  2361. * If an object is passed for `callback`, the created "_.where" style callback
  2362. * will return `true` for elements that have the properties of the given object,
  2363. * else `false`.
  2364. *
  2365. * @static
  2366. * @memberOf _
  2367. * @category Arrays
  2368. * @param {Array} array The array to inspect.
  2369. * @param {Mixed} value The value to evaluate.
  2370. * @param {Function|Object|String} [callback=identity] The function called per
  2371. * iteration. If a property name or object is passed, it will be used to create
  2372. * a "_.pluck" or "_.where" style callback, respectively.
  2373. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  2374. * @returns {Number} Returns the index at which the value should be inserted
  2375. * into `array`.
  2376. * @example
  2377. *
  2378. * _.sortedIndex([20, 30, 50], 40);
  2379. * // => 2
  2380. *
  2381. * // using "_.pluck" callback shorthand
  2382. * _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
  2383. * // => 2
  2384. *
  2385. * var dict = {
  2386. * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
  2387. * };
  2388. *
  2389. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  2390. * return dict.wordToNumber[word];
  2391. * });
  2392. * // => 2
  2393. *
  2394. * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
  2395. * return this.wordToNumber[word];
  2396. * }, dict);
  2397. * // => 2
  2398. */
  2399. function sortedIndex(array, value, callback, thisArg) {
  2400. var low = 0,
  2401. high = array ? array.length : low;
  2402. // explicitly reference `identity` for better inlining in Firefox
  2403. callback = callback ? createCallback(callback, thisArg, 1) : identity;
  2404. value = callback(value);
  2405. while (low < high) {
  2406. var mid = (low + high) >>> 1;
  2407. (callback(array[mid]) < value)
  2408. ? low = mid + 1
  2409. : high = mid;
  2410. }
  2411. return low;
  2412. }
  2413. /**
  2414. * Creates an array with all occurrences of the passed values removed using
  2415. * strict equality for comparisons, i.e. `===`.
  2416. *
  2417. * @static
  2418. * @memberOf _
  2419. * @category Arrays
  2420. * @param {Array} array The array to filter.
  2421. * @param {Mixed} [value1, value2, ...] Values to remove.
  2422. * @returns {Array} Returns a new filtered array.
  2423. * @example
  2424. *
  2425. * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
  2426. * // => [2, 3, 4]
  2427. */
  2428. function without(array) {
  2429. return difference(array, nativeSlice.call(arguments, 1));
  2430. }
  2431. /*--------------------------------------------------------------------------*/
  2432. /**
  2433. * Creates a function that, when called, invokes `func` with the `this`
  2434. * binding of `thisArg` and prepends any additional `bind` arguments to those
  2435. * passed to the bound function.
  2436. *
  2437. * @static
  2438. * @memberOf _
  2439. * @category Functions
  2440. * @param {Function} func The function to bind.
  2441. * @param {Mixed} [thisArg] The `this` binding of `func`.
  2442. * @param {Mixed} [arg1, arg2, ...] Arguments to be partially applied.
  2443. * @returns {Function} Returns the new bound function.
  2444. * @example
  2445. *
  2446. * var func = function(greeting) {
  2447. * return greeting + ' ' + this.name;
  2448. * };
  2449. *
  2450. * func = _.bind(func, { 'name': 'moe' }, 'hi');
  2451. * func();
  2452. * // => 'hi moe'
  2453. */
  2454. function bind(func, thisArg) {
  2455. // use `Function#bind` if it exists and is fast
  2456. // (in V8 `Function#bind` is slower except when partially applied)
  2457. return support.fastBind || (nativeBind && arguments.length > 2)
  2458. ? nativeBind.call.apply(nativeBind, arguments)
  2459. : createBound(func, thisArg, nativeSlice.call(arguments, 2));
  2460. }
  2461. /**
  2462. * Binds methods on `object` to `object`, overwriting the existing method.
  2463. * Method names may be specified as individual arguments or as arrays of method
  2464. * names. If no method names are provided, all the function properties of `object`
  2465. * will be bound.
  2466. *
  2467. * @static
  2468. * @memberOf _
  2469. * @category Functions
  2470. * @param {Object} object The object to bind and assign the bound methods to.
  2471. * @param {String} [methodName1, methodName2, ...] Method names on the object to bind.
  2472. * @returns {Object} Returns `object`.
  2473. * @example
  2474. *
  2475. * var view = {
  2476. * 'label': 'docs',
  2477. * 'onClick': function() { alert('clicked ' + this.label); }
  2478. * };
  2479. *
  2480. * _.bindAll(view);
  2481. * jQuery('#docs').on('click', view.onClick);
  2482. * // => alerts 'clicked docs', when the button is clicked
  2483. */
  2484. function bindAll(object) {
  2485. var funcs = arguments.length > 1 ? concat.apply(arrayRef, nativeSlice.call(arguments, 1)) : functions(object),
  2486. index = -1,
  2487. length = funcs.length;
  2488. while (++index < length) {
  2489. var key = funcs[index];
  2490. object[key] = bind(object[key], object);
  2491. }
  2492. return object;
  2493. }
  2494. /**
  2495. * Produces a callback bound to an optional `thisArg`. If `func` is a property
  2496. * name, the created callback will return the property value for a given element.
  2497. * If `func` is an object, the created callback will return `true` for elements
  2498. * that contain the equivalent object properties, otherwise it will return `false`.
  2499. *
  2500. * Note: All Lo-Dash methods, that accept a `callback` argument, use `_.createCallback`.
  2501. *
  2502. * @static
  2503. * @memberOf _
  2504. * @category Functions
  2505. * @param {Mixed} [func=identity] The value to convert to a callback.
  2506. * @param {Mixed} [thisArg] The `this` binding of the created callback.
  2507. * @param {Number} [argCount=3] The number of arguments the callback accepts.
  2508. * @returns {Function} Returns a callback function.
  2509. * @example
  2510. *
  2511. * var stooges = [
  2512. * { 'name': 'moe', 'age': 40 },
  2513. * { 'name': 'larry', 'age': 50 }
  2514. * ];
  2515. *
  2516. * // wrap to create custom callback shorthands
  2517. * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
  2518. * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
  2519. * return !match ? func(callback, thisArg) : function(object) {
  2520. * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
  2521. * };
  2522. * });
  2523. *
  2524. * _.filter(stooges, 'age__gt45');
  2525. * // => [{ 'name': 'larry', 'age': 50 }]
  2526. *
  2527. * // create mixins with support for "_.pluck" and "_.where" callback shorthands
  2528. * _.mixin({
  2529. * 'toLookup': function(collection, callback, thisArg) {
  2530. * callback = _.createCallback(callback, thisArg);
  2531. * return _.reduce(collection, function(result, value, index, collection) {
  2532. * return (result[callback(value, index, collection)] = value, result);
  2533. * }, {});
  2534. * }
  2535. * });
  2536. *
  2537. * _.toLookup(stooges, 'name');
  2538. * // => { 'moe': { 'name': 'moe', 'age': 40 }, 'larry': { 'name': 'larry', 'age': 50 } }
  2539. */
  2540. function createCallback(func, thisArg, argCount) {
  2541. if (func == null) {
  2542. return identity;
  2543. }
  2544. var type = typeof func;
  2545. if (type != 'function') {
  2546. if (type != 'object') {
  2547. return function(object) {
  2548. return object[func];
  2549. };
  2550. }
  2551. var props = keys(func);
  2552. return function(object) {
  2553. var length = props.length,
  2554. result = false;
  2555. while (length--) {
  2556. if (!(result = object[props[length]] === func[props[length]])) {
  2557. break;
  2558. }
  2559. }
  2560. return result;
  2561. };
  2562. }
  2563. if (typeof thisArg != 'undefined') {
  2564. if (argCount === 1) {
  2565. return function(value) {
  2566. return func.call(thisArg, value);
  2567. };
  2568. }
  2569. if (argCount === 2) {
  2570. return function(a, b) {
  2571. return func.call(thisArg, a, b);
  2572. };
  2573. }
  2574. if (argCount === 4) {
  2575. return function(accumulator, value, index, collection) {
  2576. return func.call(thisArg, accumulator, value, index, collection);
  2577. };
  2578. }
  2579. return function(value, index, collection) {
  2580. return func.call(thisArg, value, index, collection);
  2581. };
  2582. }
  2583. return func;
  2584. }
  2585. /**
  2586. * Creates a function that is restricted to execute `func` once. Repeat calls to
  2587. * the function will return the value of the first call. The `func` is executed
  2588. * with the `this` binding of the created function.
  2589. *
  2590. * @static
  2591. * @memberOf _
  2592. * @category Functions
  2593. * @param {Function} func The function to restrict.
  2594. * @returns {Function} Returns the new restricted function.
  2595. * @example
  2596. *
  2597. * var initialize = _.once(createApplication);
  2598. * initialize();
  2599. * initialize();
  2600. * // `initialize` executes `createApplication` once
  2601. */
  2602. function once(func) {
  2603. var ran,
  2604. result;
  2605. return function() {
  2606. if (ran) {
  2607. return result;
  2608. }
  2609. ran = true;
  2610. result = func.apply(this, arguments);
  2611. // clear the `func` variable so the function may be garbage collected
  2612. func = null;
  2613. return result;
  2614. };
  2615. }
  2616. /*--------------------------------------------------------------------------*/
  2617. /**
  2618. * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
  2619. * corresponding HTML entities.
  2620. *
  2621. * @static
  2622. * @memberOf _
  2623. * @category Utilities
  2624. * @param {String} string The string to escape.
  2625. * @returns {String} Returns the escaped string.
  2626. * @example
  2627. *
  2628. * _.escape('Moe, Larry & Curly');
  2629. * // => 'Moe, Larry &amp; Curly'
  2630. */
  2631. function escape(string) {
  2632. return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar);
  2633. }
  2634. /**
  2635. * This function returns the first argument passed to it.
  2636. *
  2637. * @static
  2638. * @memberOf _
  2639. * @category Utilities
  2640. * @param {Mixed} value Any value.
  2641. * @returns {Mixed} Returns `value`.
  2642. * @example
  2643. *
  2644. * var moe = { 'name': 'moe' };
  2645. * moe === _.identity(moe);
  2646. * // => true
  2647. */
  2648. function identity(value) {
  2649. return value;
  2650. }
  2651. /**
  2652. * Adds functions properties of `object` to the `lodash` function and chainable
  2653. * wrapper.
  2654. *
  2655. * @static
  2656. * @memberOf _
  2657. * @category Utilities
  2658. * @param {Object} object The object of function properties to add to `lodash`.
  2659. * @example
  2660. *
  2661. * _.mixin({
  2662. * 'capitalize': function(string) {
  2663. * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  2664. * }
  2665. * });
  2666. *
  2667. * _.capitalize('moe');
  2668. * // => 'Moe'
  2669. *
  2670. * _('moe').capitalize();
  2671. * // => 'Moe'
  2672. */
  2673. function mixin(object) {
  2674. forEach(functions(object), function(methodName) {
  2675. var func = lodash[methodName] = object[methodName];
  2676. lodash.prototype[methodName] = function() {
  2677. var args = [this.__wrapped__];
  2678. push.apply(args, arguments);
  2679. var result = func.apply(lodash, args);
  2680. if (this.__chain__) {
  2681. result = new lodashWrapper(result);
  2682. result.__chain__ = true;
  2683. }
  2684. return result;
  2685. };
  2686. });
  2687. }
  2688. /**
  2689. * Resolves the value of `property` on `object`. If `property` is a function,
  2690. * it will be invoked with the `this` binding of `object` and its result returned,
  2691. * else the property value is returned. If `object` is falsey, then `undefined`
  2692. * is returned.
  2693. *
  2694. * @static
  2695. * @memberOf _
  2696. * @category Utilities
  2697. * @param {Object} object The object to inspect.
  2698. * @param {String} property The property to get the value of.
  2699. * @returns {Mixed} Returns the resolved value.
  2700. * @example
  2701. *
  2702. * var object = {
  2703. * 'cheese': 'crumpets',
  2704. * 'stuff': function() {
  2705. * return 'nonsense';
  2706. * }
  2707. * };
  2708. *
  2709. * _.result(object, 'cheese');
  2710. * // => 'crumpets'
  2711. *
  2712. * _.result(object, 'stuff');
  2713. * // => 'nonsense'
  2714. */
  2715. function result(object, property) {
  2716. var value = object ? object[property] : null;
  2717. return isFunction(value) ? object[property]() : value;
  2718. }
  2719. /**
  2720. * Generates a unique ID. If `prefix` is passed, the ID will be appended to it.
  2721. *
  2722. * @static
  2723. * @memberOf _
  2724. * @category Utilities
  2725. * @param {String} [prefix] The value to prefix the ID with.
  2726. * @returns {String} Returns the unique ID.
  2727. * @example
  2728. *
  2729. * _.uniqueId('contact_');
  2730. * // => 'contact_104'
  2731. *
  2732. * _.uniqueId();
  2733. * // => '105'
  2734. */
  2735. function uniqueId(prefix) {
  2736. var id = ++idCounter + '';
  2737. return prefix ? prefix + id : id;
  2738. }
  2739. /*--------------------------------------------------------------------------*/
  2740. /**
  2741. * Creates a `lodash` object that wraps the given `value`.
  2742. *
  2743. * @static
  2744. * @memberOf _
  2745. * @category Chaining
  2746. * @param {Mixed} value The value to wrap.
  2747. * @returns {Object} Returns the wrapper object.
  2748. * @example
  2749. *
  2750. * var stooges = [
  2751. * { 'name': 'moe', 'age': 40 },
  2752. * { 'name': 'larry', 'age': 50 },
  2753. * { 'name': 'curly', 'age': 60 }
  2754. * ];
  2755. *
  2756. * var youngest = _.chain(stooges)
  2757. * .sortBy(function(stooge) { return stooge.age; })
  2758. * .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
  2759. * .first();
  2760. * // => 'moe is 40'
  2761. */
  2762. function chain(value) {
  2763. value = new lodashWrapper(value);
  2764. value.__chain__ = true;
  2765. return value;
  2766. }
  2767. /**
  2768. * Enables method chaining on the wrapper object.
  2769. *
  2770. * @name chain
  2771. * @memberOf _
  2772. * @category Chaining
  2773. * @returns {Mixed} Returns the wrapper object.
  2774. * @example
  2775. *
  2776. * var sum = _([1, 2, 3])
  2777. * .chain()
  2778. * .reduce(function(sum, num) { return sum + num; })
  2779. * .value()
  2780. * // => 6`
  2781. */
  2782. function wrapperChain() {
  2783. this.__chain__ = true;
  2784. return this;
  2785. }
  2786. /**
  2787. * Produces the `toString` result of the wrapped value.
  2788. *
  2789. * @name toString
  2790. * @memberOf _
  2791. * @category Chaining
  2792. * @returns {String} Returns the string result.
  2793. * @example
  2794. *
  2795. * _([1, 2, 3]).toString();
  2796. * // => '1,2,3'
  2797. */
  2798. function wrapperToString() {
  2799. return String(this.__wrapped__);
  2800. }
  2801. /**
  2802. * Extracts the wrapped value.
  2803. *
  2804. * @name valueOf
  2805. * @memberOf _
  2806. * @alias value
  2807. * @category Chaining
  2808. * @returns {Mixed} Returns the wrapped value.
  2809. * @example
  2810. *
  2811. * _([1, 2, 3]).valueOf();
  2812. * // => [1, 2, 3]
  2813. */
  2814. function wrapperValueOf() {
  2815. return this.__wrapped__;
  2816. }
  2817. /*--------------------------------------------------------------------------*/
  2818. lodash.bind = bind;
  2819. lodash.bindAll = bindAll;
  2820. lodash.countBy = countBy;
  2821. lodash.defaults = defaults;
  2822. lodash.difference = difference;
  2823. lodash.filter = filter;
  2824. lodash.forEach = forEach;
  2825. lodash.functions = functions;
  2826. lodash.groupBy = groupBy;
  2827. lodash.initial = initial;
  2828. lodash.invert = invert;
  2829. lodash.invoke = invoke;
  2830. lodash.keys = keys;
  2831. lodash.map = map;
  2832. lodash.max = max;
  2833. lodash.min = min;
  2834. lodash.omit = omit;
  2835. lodash.once = once;
  2836. lodash.pairs = pairs;
  2837. lodash.pick = pick;
  2838. lodash.reject = reject;
  2839. lodash.rest = rest;
  2840. lodash.shuffle = shuffle;
  2841. lodash.sortBy = sortBy;
  2842. lodash.toArray = toArray;
  2843. lodash.values = values;
  2844. lodash.without = without;
  2845. // add aliases
  2846. lodash.collect = map;
  2847. lodash.drop = rest;
  2848. lodash.each = forEach;
  2849. lodash.extend = assign;
  2850. lodash.methods = functions;
  2851. lodash.select = filter;
  2852. lodash.tail = rest;
  2853. /*--------------------------------------------------------------------------*/
  2854. // add functions that return unwrapped values when chaining
  2855. lodash.clone = clone;
  2856. lodash.contains = contains;
  2857. lodash.escape = escape;
  2858. lodash.every = every;
  2859. lodash.find = find;
  2860. lodash.has = has;
  2861. lodash.identity = identity;
  2862. lodash.indexOf = indexOf;
  2863. lodash.isArguments = isArguments;
  2864. lodash.isArray = isArray;
  2865. lodash.isEmpty = isEmpty;
  2866. lodash.isEqual = isEqual;
  2867. lodash.isFunction = isFunction;
  2868. lodash.isObject = isObject;
  2869. lodash.isRegExp = isRegExp;
  2870. lodash.isString = isString;
  2871. lodash.lastIndexOf = lastIndexOf;
  2872. lodash.mixin = mixin;
  2873. lodash.reduce = reduce;
  2874. lodash.reduceRight = reduceRight;
  2875. lodash.result = result;
  2876. lodash.size = size;
  2877. lodash.some = some;
  2878. lodash.sortedIndex = sortedIndex;
  2879. lodash.uniqueId = uniqueId;
  2880. // add aliases
  2881. lodash.all = every;
  2882. lodash.any = some;
  2883. lodash.detect = find;
  2884. lodash.foldl = reduce;
  2885. lodash.foldr = reduceRight;
  2886. lodash.include = contains;
  2887. lodash.inject = reduce;
  2888. /*--------------------------------------------------------------------------*/
  2889. // add functions capable of returning wrapped and unwrapped values when chaining
  2890. lodash.first = first;
  2891. lodash.last = last;
  2892. // add aliases
  2893. lodash.take = first;
  2894. lodash.head = first;
  2895. /*--------------------------------------------------------------------------*/
  2896. lodash.chain = chain;
  2897. /**
  2898. * The semantic version number.
  2899. *
  2900. * @static
  2901. * @memberOf _
  2902. * @type String
  2903. */
  2904. lodash.VERSION = '1.2.1';
  2905. // add functions to `lodash.prototype`
  2906. mixin(lodash);
  2907. // add "Chaining" functions to the wrapper
  2908. lodash.prototype.chain = wrapperChain;
  2909. lodash.prototype.value = wrapperValueOf;
  2910. // add `Array` mutator functions to the wrapper
  2911. forEach(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
  2912. var func = arrayRef[methodName];
  2913. lodash.prototype[methodName] = function() {
  2914. var value = this.__wrapped__;
  2915. func.apply(value, arguments);
  2916. // avoid array-like object bugs with `Array#shift` and `Array#splice`
  2917. // in Firefox < 10 and IE < 9
  2918. if (!support.spliceObjects && value.length === 0) {
  2919. delete value[0];
  2920. }
  2921. return this;
  2922. };
  2923. });
  2924. // add `Array` accessor functions to the wrapper
  2925. forEach(['concat', 'join', 'slice'], function(methodName) {
  2926. var func = arrayRef[methodName];
  2927. lodash.prototype[methodName] = function() {
  2928. var value = this.__wrapped__,
  2929. result = func.apply(value, arguments);
  2930. if (this.__chain__) {
  2931. result = new lodashWrapper(result);
  2932. result.__chain__ = true;
  2933. }
  2934. return result;
  2935. };
  2936. });
  2937. /*--------------------------------------------------------------------------*/
  2938. // expose Lo-Dash
  2939. // some AMD build optimizers, like r.js, check for specific condition patterns like the following:
  2940. if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  2941. // Expose Lo-Dash to the global object even when an AMD loader is present in
  2942. // case Lo-Dash was injected by a third-party script and not intended to be
  2943. // loaded as a module. The global assignment can be reverted in the Lo-Dash
  2944. // module via its `noConflict()` method.
  2945. window._ = lodash;
  2946. // define as an anonymous module so, through path mapping, it can be
  2947. // referenced as the "underscore" module
  2948. define(function() {
  2949. return lodash;
  2950. });
  2951. }
  2952. // check for `exports` after `define` in case a build optimizer adds an `exports` object
  2953. else if (freeExports && !freeExports.nodeType) {
  2954. // in Node.js or RingoJS v0.8.0+
  2955. if (freeModule) {
  2956. (freeModule.exports = lodash)._ = lodash;
  2957. }
  2958. // in Narwhal or RingoJS v0.7.0-
  2959. else {
  2960. freeExports._ = lodash;
  2961. }
  2962. }
  2963. else {
  2964. // in a browser or Rhino
  2965. window._ = lodash;
  2966. }
  2967. }(this));