PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/lodash.js/1.0.0-rc.1/lodash.underscore.js

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