PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Mirros/cdnjs
JavaScript | 1626 lines | 1222 code | 63 blank | 341 comment | 64 complexity | 2a42cc28ab6be7d3d355f462261106dc MD5 | raw file
  1. /*!
  2. * Lo-Dash 1.0.0-rc.2 (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.3 <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. return result
  555. };
  556. /**
  557. * Iterates over an object's own enumerable properties, executing the `callback`
  558. * for each property. The `callback` is bound to `thisArg` and invoked with three
  559. * arguments; (value, key, object). Callbacks may exit iteration early by explicitly
  560. * returning `false`.
  561. *
  562. * @static
  563. * @memberOf _
  564. * @category Objects
  565. * @param {Object} object The object to iterate over.
  566. * @param {Function} [callback=identity] The function called per iteration.
  567. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  568. * @returns {Object} Returns `object`.
  569. * @example
  570. *
  571. * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
  572. * alert(key);
  573. * });
  574. * // => alerts '0', '1', and 'length' (order is not guaranteed)
  575. */
  576. var forOwn = function (collection, callback) {
  577. var index, iteratee = collection, result = collection;
  578. if (!collection) return result;
  579. callback || (callback = identity);
  580. for (index in iteratee) {
  581. if (hasOwnProperty.call(iteratee, index)) {
  582. if (callback(iteratee[index], index, collection) === indicatorObject) return result;
  583. }
  584. }
  585. return result
  586. };
  587. /**
  588. * A fallback implementation of `isPlainObject` that checks if a given `value`
  589. * is an object created by the `Object` constructor, assuming objects created
  590. * by the `Object` constructor have no inherited enumerable properties and that
  591. * there are no `Object.prototype` extensions.
  592. *
  593. * @private
  594. * @param {Mixed} value The value to check.
  595. * @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
  596. */
  597. function shimIsPlainObject(value) {
  598. // avoid non-objects and false positives for `arguments` objects
  599. var result = false;
  600. if (!(value && typeof value == 'object') || isArguments(value)) {
  601. return result;
  602. }
  603. // check that the constructor is `Object` (i.e. `Object instanceof Object`)
  604. var ctor = value.constructor;
  605. if ((!isFunction(ctor)) || ctor instanceof ctor) {
  606. // In most environments an object's own properties are iterated before
  607. // its inherited properties. If the last iterated property is an object's
  608. // own property then there are no inherited enumerable properties.
  609. forIn(value, function(value, key) {
  610. result = key;
  611. });
  612. return result === false || hasOwnProperty.call(value, result);
  613. }
  614. return result;
  615. }
  616. /**
  617. * A fallback implementation of `Object.keys` that produces an array of the
  618. * given object's own enumerable property names.
  619. *
  620. * @private
  621. * @param {Object} object The object to inspect.
  622. * @returns {Array} Returns a new array of property names.
  623. */
  624. function shimKeys(object) {
  625. var result = [];
  626. forOwn(object, function(value, key) {
  627. result.push(key);
  628. });
  629. return result;
  630. }
  631. /**
  632. * Used to convert characters to HTML entities:
  633. *
  634. * Though the `>` character is escaped for symmetry, characters like `>` and `/`
  635. * don't require escaping in HTML and have no special meaning unless they're part
  636. * of a tag or an unquoted attribute value.
  637. * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
  638. */
  639. var htmlEscapes = {
  640. '&': '&amp;',
  641. '<': '&lt;',
  642. '>': '&gt;',
  643. '"': '&quot;',
  644. "'": '&#x27;'
  645. };
  646. /** Used to convert HTML entities to characters */
  647. var htmlUnescapes = invert(htmlEscapes);
  648. /*--------------------------------------------------------------------------*/
  649. /**
  650. * Creates a clone of `value`. If `deep` is `true`, all nested objects will
  651. * also be cloned, otherwise they will be assigned by reference. Functions and
  652. * DOM nodes are **not** cloned. The enumerable properties of `arguments` objects
  653. * and objects created by constructors other than `Object` are cloned to plain
  654. * `Object` objects.
  655. *
  656. * Note: Lo-Dash's deep clone functionality is loosely based on the structured clone algorithm.
  657. * See http://www.w3.org/TR/html5/common-dom-interfaces.html#internal-structured-cloning-algorithm.
  658. *
  659. * @static
  660. * @memberOf _
  661. * @category Objects
  662. * @param {Mixed} value The value to clone.
  663. * @param {Boolean} deep A flag to indicate a deep clone.
  664. * @param- {Object} [guard] Internally used to allow this method to work with
  665. * others like `_.map` without using their callback `index` argument for `deep`.
  666. * @param- {Array} [stackA=[]] Internally used to track traversed source objects.
  667. * @param- {Array} [stackB=[]] Internally used to associate clones with their
  668. * source counterparts.
  669. * @returns {Mixed} Returns the cloned `value`.
  670. * @example
  671. *
  672. * var stooges = [
  673. * { 'name': 'moe', 'age': 40 },
  674. * { 'name': 'larry', 'age': 50 },
  675. * { 'name': 'curly', 'age': 60 }
  676. * ];
  677. *
  678. * _.clone({ 'name': 'moe' });
  679. * // => { 'name': 'moe' }
  680. *
  681. * var shallow = _.clone(stooges);
  682. * shallow[0] === stooges[0];
  683. * // => true
  684. *
  685. * var deep = _.clone(stooges, true);
  686. * deep[0] === stooges[0];
  687. * // => false
  688. */
  689. function clone(value) {
  690. return value && objectTypes[typeof value]
  691. ? (isArray(value) ? slice(value) : assign({}, value))
  692. : value
  693. }
  694. /**
  695. * Assigns own enumerable properties of source object(s) to the `destination`
  696. * object for all `destination` properties that resolve to `null`/`undefined`.
  697. * Once a property is set, additional defaults of the same property will be
  698. * ignored.
  699. *
  700. * @static
  701. * @memberOf _
  702. * @category Objects
  703. * @param {Object} object The destination object.
  704. * @param {Object} [default1, default2, ...] The default objects.
  705. * @returns {Object} Returns the destination object.
  706. * @example
  707. *
  708. * var iceCream = { 'flavor': 'chocolate' };
  709. * _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
  710. * // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
  711. */
  712. function defaults(object) {
  713. if (!object) {
  714. return object;
  715. }
  716. for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
  717. var iteratee = arguments[argsIndex];
  718. if (iteratee) {
  719. for (var key in iteratee) {
  720. if (object[key] == null) {
  721. object[key] = iteratee[key];
  722. }
  723. }
  724. }
  725. }
  726. return object;
  727. }
  728. /**
  729. * Creates a sorted array of all enumerable properties, own and inherited,
  730. * of `object` that have function values.
  731. *
  732. * @static
  733. * @memberOf _
  734. * @alias methods
  735. * @category Objects
  736. * @param {Object} object The object to inspect.
  737. * @returns {Array} Returns a new array of property names that have function values.
  738. * @example
  739. *
  740. * _.functions(_);
  741. * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
  742. */
  743. function functions(object) {
  744. var result = [];
  745. forIn(object, function(value, key) {
  746. if (isFunction(value)) {
  747. result.push(key);
  748. }
  749. });
  750. return result.sort();
  751. }
  752. /**
  753. * Checks if the specified object `property` exists and is a direct property,
  754. * instead of an inherited property.
  755. *
  756. * @static
  757. * @memberOf _
  758. * @category Objects
  759. * @param {Object} object The object to check.
  760. * @param {String} property The property to check for.
  761. * @returns {Boolean} Returns `true` if key is a direct property, else `false`.
  762. * @example
  763. *
  764. * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
  765. * // => true
  766. */
  767. function has(object, property) {
  768. return object ? hasOwnProperty.call(object, property) : false;
  769. }
  770. /**
  771. * Creates an object composed of the inverted keys and values of the given `object`.
  772. *
  773. * @static
  774. * @memberOf _
  775. * @category Objects
  776. * @param {Object} object The object to invert.
  777. * @returns {Object} Returns the created inverted object.
  778. * @example
  779. *
  780. * _.invert({ 'first': 'Moe', 'second': 'Larry', 'third': 'Curly' });
  781. * // => { 'Moe': 'first', 'Larry': 'second', 'Curly': 'third' } (order is not guaranteed)
  782. */
  783. function invert(object) {
  784. var result = {};
  785. forOwn(object, function(value, key) {
  786. result[value] = key;
  787. });
  788. return result;
  789. }
  790. /**
  791. * Checks if `value` is an array.
  792. *
  793. * @static
  794. * @memberOf _
  795. * @category Objects
  796. * @param {Mixed} value The value to check.
  797. * @returns {Boolean} Returns `true` if the `value` is an array, else `false`.
  798. * @example
  799. *
  800. * (function() { return _.isArray(arguments); })();
  801. * // => false
  802. *
  803. * _.isArray([1, 2, 3]);
  804. * // => true
  805. */
  806. var isArray = nativeIsArray || function(value) {
  807. return toString.call(value) == arrayClass;
  808. };
  809. /**
  810. * Checks if `value` is a boolean (`true` or `false`) value.
  811. *
  812. * @static
  813. * @memberOf _
  814. * @category Objects
  815. * @param {Mixed} value The value to check.
  816. * @returns {Boolean} Returns `true` if the `value` is a boolean value, else `false`.
  817. * @example
  818. *
  819. * _.isBoolean(null);
  820. * // => false
  821. */
  822. function isBoolean(value) {
  823. return value === true || value === false || toString.call(value) == boolClass;
  824. }
  825. /**
  826. * Checks if `value` is a date.
  827. *
  828. * @static
  829. * @memberOf _
  830. * @category Objects
  831. * @param {Mixed} value The value to check.
  832. * @returns {Boolean} Returns `true` if the `value` is a date, else `false`.
  833. * @example
  834. *
  835. * _.isDate(new Date);
  836. * // => true
  837. */
  838. function isDate(value) {
  839. return toString.call(value) == dateClass;
  840. }
  841. /**
  842. * Checks if `value` is a DOM element.
  843. *
  844. * @static
  845. * @memberOf _
  846. * @category Objects
  847. * @param {Mixed} value The value to check.
  848. * @returns {Boolean} Returns `true` if the `value` is a DOM element, else `false`.
  849. * @example
  850. *
  851. * _.isElement(document.body);
  852. * // => true
  853. */
  854. function isElement(value) {
  855. return value ? value.nodeType === 1 : false;
  856. }
  857. /**
  858. * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
  859. * length of `0` and objects with no own enumerable properties are considered
  860. * "empty".
  861. *
  862. * @static
  863. * @memberOf _
  864. * @category Objects
  865. * @param {Array|Object|String} value The value to inspect.
  866. * @returns {Boolean} Returns `true` if the `value` is empty, else `false`.
  867. * @example
  868. *
  869. * _.isEmpty([1, 2, 3]);
  870. * // => false
  871. *
  872. * _.isEmpty({});
  873. * // => true
  874. *
  875. * _.isEmpty('');
  876. * // => true
  877. */
  878. function isEmpty(value) {
  879. if (!value) {
  880. return true;
  881. }
  882. if (isArray(value) || isString(value)) {
  883. return !value.length;
  884. }
  885. for (var key in value) {
  886. if (hasOwnProperty.call(value, key)) {
  887. return false;
  888. }
  889. }
  890. return true;
  891. }
  892. /**
  893. * Performs a deep comparison between two values to determine if they are
  894. * equivalent to each other.
  895. *
  896. * @static
  897. * @memberOf _
  898. * @category Objects
  899. * @param {Mixed} a The value to compare.
  900. * @param {Mixed} b The other value to compare.
  901. * @param- {Object} [stackA=[]] Internally used track traversed `a` objects.
  902. * @param- {Object} [stackB=[]] Internally used track traversed `b` objects.
  903. * @returns {Boolean} Returns `true` if the values are equvalent, else `false`.
  904. * @example
  905. *
  906. * var moe = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  907. * var clone = { 'name': 'moe', 'luckyNumbers': [13, 27, 34] };
  908. *
  909. * moe == clone;
  910. * // => false
  911. *
  912. * _.isEqual(moe, clone);
  913. * // => true
  914. */
  915. function isEqual(a, b, stackA, stackB) {
  916. // exit early for identical values
  917. if (a === b) {
  918. // treat `+0` vs. `-0` as not equal
  919. return a !== 0 || (1 / a == 1 / b);
  920. }
  921. // a strict comparison is necessary because `null == undefined`
  922. if (a == null || b == null) {
  923. return a === b;
  924. }
  925. // compare [[Class]] names
  926. var className = toString.call(a),
  927. otherName = toString.call(b);
  928. if (className != otherName) {
  929. return false;
  930. }
  931. switch (className) {
  932. case boolClass:
  933. case dateClass:
  934. // coerce dates and booleans to numbers, dates to milliseconds and booleans
  935. // to `1` or `0`, treating invalid dates coerced to `NaN` as not equal
  936. return +a == +b;
  937. case numberClass:
  938. // treat `NaN` vs. `NaN` as equal
  939. return a != +a
  940. ? b != +b
  941. // but treat `+0` vs. `-0` as not equal
  942. : (a == 0 ? (1 / a == 1 / b) : a == +b);
  943. case regexpClass:
  944. case stringClass:
  945. // coerce regexes to strings (http://es5.github.com/#x15.10.6.4)
  946. // treat string primitives and their corresponding object instances as equal
  947. return a == b + '';
  948. }
  949. var isArr = className == arrayClass;
  950. if (!isArr) {
  951. // unwrap any `lodash` wrapped values
  952. if (a.__wrapped__ || b.__wrapped__) {
  953. return isEqual(a.__wrapped__ || a, b.__wrapped__ || b);
  954. }
  955. // exit for functions and DOM nodes
  956. if (className != objectClass) {
  957. return false;
  958. }
  959. // in older versions of Opera, `arguments` objects have `Array` constructors
  960. var ctorA = a.constructor,
  961. ctorB = b.constructor;
  962. // non `Object` object instances with different constructors are not equal
  963. if (ctorA != ctorB && !(
  964. isFunction(ctorA) && ctorA instanceof ctorA &&
  965. isFunction(ctorB) && ctorB instanceof ctorB
  966. )) {
  967. return false;
  968. }
  969. }
  970. // assume cyclic structures are equal
  971. // the algorithm for detecting cyclic structures is adapted from ES 5.1
  972. // section 15.12.3, abstract operation `JO` (http://es5.github.com/#x15.12.3)
  973. stackA || (stackA = []);
  974. stackB || (stackB = []);
  975. var length = stackA.length;
  976. while (length--) {
  977. if (stackA[length] == a) {
  978. return stackB[length] == b;
  979. }
  980. }
  981. var index = -1,
  982. result = true,
  983. size = 0;
  984. // add `a` and `b` to the stack of traversed objects
  985. stackA.push(a);
  986. stackB.push(b);
  987. // recursively compare objects and arrays (susceptible to call stack limits)
  988. if (isArr) {
  989. // compare lengths to determine if a deep comparison is necessary
  990. size = a.length;
  991. result = size == b.length;
  992. if (result) {
  993. // deep compare the contents, ignoring non-numeric properties
  994. while (size--) {
  995. if (!(result = isEqual(a[size], b[size], stackA, stackB))) {
  996. break;
  997. }
  998. }
  999. }
  1000. return result;
  1001. }
  1002. // deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
  1003. // which, in this case, is more costly
  1004. forIn(a, function(value, key, a) {
  1005. if (hasOwnProperty.call(a, key)) {
  1006. // count the number of properties.
  1007. size++;
  1008. // deep compare each property value.
  1009. return !(result = hasOwnProperty.call(b, key) && isEqual(value, b[key], stackA, stackB)) && indicatorObject;
  1010. }
  1011. });
  1012. if (result) {
  1013. // ensure both objects have the same number of properties
  1014. forIn(b, function(value, key, b) {
  1015. if (hasOwnProperty.call(b, key)) {
  1016. // `size` will be `-1` if `b` has more properties than `a`
  1017. return !(result = --size > -1) && indicatorObject;
  1018. }
  1019. });
  1020. }
  1021. return result;
  1022. }
  1023. /**
  1024. * Checks if `value` is, or can be coerced to, a finite number.
  1025. *
  1026. * Note: This is not the same as native `isFinite`, which will return true for
  1027. * booleans and empty strings. See http://es5.github.com/#x15.1.2.5.
  1028. *
  1029. * @static
  1030. * @memberOf _
  1031. * @category Objects
  1032. * @param {Mixed} value The value to check.
  1033. * @returns {Boolean} Returns `true` if the `value` is a finite number, else `false`.
  1034. * @example
  1035. *
  1036. * _.isFinite(-101);
  1037. * // => true
  1038. *
  1039. * _.isFinite('10');
  1040. * // => true
  1041. *
  1042. * _.isFinite(true);
  1043. * // => false
  1044. *
  1045. * _.isFinite('');
  1046. * // => false
  1047. *
  1048. * _.isFinite(Infinity);
  1049. * // => false
  1050. */
  1051. function isFinite(value) {
  1052. return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
  1053. }
  1054. /**
  1055. * Checks if `value` is a function.
  1056. *
  1057. * @static
  1058. * @memberOf _
  1059. * @category Objects
  1060. * @param {Mixed} value The value to check.
  1061. * @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
  1062. * @example
  1063. *
  1064. * _.isFunction(_);
  1065. * // => true
  1066. */
  1067. function isFunction(value) {
  1068. return typeof value == 'function';
  1069. }
  1070. // fallback for older versions of Chrome and Safari
  1071. if (isFunction(/x/)) {
  1072. isFunction = function(value) {
  1073. return toString.call(value) == funcClass;
  1074. };
  1075. }
  1076. /**
  1077. * Checks if `value` is the language type of Object.
  1078. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1079. *
  1080. * @static
  1081. * @memberOf _
  1082. * @category Objects
  1083. * @param {Mixed} value The value to check.
  1084. * @returns {Boolean} Returns `true` if the `value` is an object, else `false`.
  1085. * @example
  1086. *
  1087. * _.isObject({});
  1088. * // => true
  1089. *
  1090. * _.isObject([1, 2, 3]);
  1091. * // => true
  1092. *
  1093. * _.isObject(1);
  1094. * // => false
  1095. */
  1096. function isObject(value) {
  1097. // check if the value is the ECMAScript language type of Object
  1098. // http://es5.github.com/#x8
  1099. // and avoid a V8 bug
  1100. // http://code.google.com/p/v8/issues/detail?id=2291
  1101. return value ? objectTypes[typeof value] : false;
  1102. }
  1103. /**
  1104. * Checks if `value` is `NaN`.
  1105. *
  1106. * Note: This is not the same as native `isNaN`, which will return `true` for
  1107. * `undefined` and other values. See http://es5.github.com/#x15.1.2.4.
  1108. *
  1109. * @static
  1110. * @memberOf _
  1111. * @category Objects
  1112. * @param {Mixed} value The value to check.
  1113. * @returns {Boolean} Returns `true` if the `value` is `NaN`, else `false`.
  1114. * @example
  1115. *
  1116. * _.isNaN(NaN);
  1117. * // => true
  1118. *
  1119. * _.isNaN(new Number(NaN));
  1120. * // => true
  1121. *
  1122. * isNaN(undefined);
  1123. * // => true
  1124. *
  1125. * _.isNaN(undefined);
  1126. * // => false
  1127. */
  1128. function isNaN(value) {
  1129. // `NaN` as a primitive is the only value that is not equal to itself
  1130. // (perform the [[Class]] check first to avoid errors with some host objects in IE)
  1131. return isNumber(value) && value != +value
  1132. }
  1133. /**
  1134. * Checks if `value` is `null`.
  1135. *
  1136. * @static
  1137. * @memberOf _
  1138. * @category Objects
  1139. * @param {Mixed} value The value to check.
  1140. * @returns {Boolean} Returns `true` if the `value` is `null`, else `false`.
  1141. * @example
  1142. *
  1143. * _.isNull(null);
  1144. * // => true
  1145. *
  1146. * _.isNull(undefined);
  1147. * // => false
  1148. */
  1149. function isNull(value) {
  1150. return value === null;
  1151. }
  1152. /**
  1153. * Checks if `value` is a number.
  1154. *
  1155. * @static
  1156. * @memberOf _
  1157. * @category Objects
  1158. * @param {Mixed} value The value to check.
  1159. * @returns {Boolean} Returns `true` if the `value` is a number, else `false`.
  1160. * @example
  1161. *
  1162. * _.isNumber(8.4 * 5);
  1163. * // => true
  1164. */
  1165. function isNumber(value) {
  1166. return typeof value == 'number' || toString.call(value) == numberClass;
  1167. }
  1168. /**
  1169. * Checks if `value` is a regular expression.
  1170. *
  1171. * @static
  1172. * @memberOf _
  1173. * @category Objects
  1174. * @param {Mixed} value The value to check.
  1175. * @returns {Boolean} Returns `true` if the `value` is a regular expression, else `false`.
  1176. * @example
  1177. *
  1178. * _.isRegExp(/moe/);
  1179. * // => true
  1180. */
  1181. function isRegExp(value) {
  1182. return toString.call(value) == regexpClass;
  1183. }
  1184. /**
  1185. * Checks if `value` is a string.
  1186. *
  1187. * @static
  1188. * @memberOf _
  1189. * @category Objects
  1190. * @param {Mixed} value The value to check.
  1191. * @returns {Boolean} Returns `true` if the `value` is a string, else `false`.
  1192. * @example
  1193. *
  1194. * _.isString('moe');
  1195. * // => true
  1196. */
  1197. function isString(value) {
  1198. return typeof value == 'string' || toString.call(value) == stringClass;
  1199. }
  1200. /**
  1201. * Checks if `value` is `undefined`.
  1202. *
  1203. * @static
  1204. * @memberOf _
  1205. * @category Objects
  1206. * @param {Mixed} value The value to check.
  1207. * @returns {Boolean} Returns `true` if the `value` is `undefined`, else `false`.
  1208. * @example
  1209. *
  1210. * _.isUndefined(void 0);
  1211. * // => true
  1212. */
  1213. function isUndefined(value) {
  1214. return typeof value == 'undefined';
  1215. }
  1216. /**
  1217. * Creates an array composed of the own enumerable property names of `object`.
  1218. *
  1219. * @static
  1220. * @memberOf _
  1221. * @category Objects
  1222. * @param {Object} object The object to inspect.
  1223. * @returns {Array} Returns a new array of property names.
  1224. * @example
  1225. *
  1226. * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
  1227. * // => ['one', 'two', 'three'] (order is not guaranteed)
  1228. */
  1229. var keys = !nativeKeys ? shimKeys : function(object) {
  1230. return (isObject(object) ? nativeKeys(object) : []);
  1231. };
  1232. /**
  1233. * Creates a shallow clone of `object` excluding the specified properties.
  1234. * Property names may be specified as individual arguments or as arrays of
  1235. * property names. If `callback` is passed, it will be executed for each property
  1236. * in the `object`, omitting the properties `callback` returns truthy for. The
  1237. * `callback` is bound to `thisArg` and invoked with three arguments; (value, key, object).
  1238. *
  1239. * @static
  1240. * @memberOf _
  1241. * @category Objects
  1242. * @param {Object} object The source object.
  1243. * @param {Function|String} callback|[prop1, prop2, ...] The properties to omit
  1244. * or the function called per iteration.
  1245. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1246. * @returns {Object} Returns an object without the omitted properties.
  1247. * @example
  1248. *
  1249. * _.omit({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
  1250. * // => { 'name': 'moe', 'age': 40 }
  1251. *
  1252. * _.omit({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
  1253. * return key.charAt(0) == '_';
  1254. * });
  1255. * // => { 'name': 'moe' }
  1256. */
  1257. function omit(object) {
  1258. var props = concat.apply(arrayRef, arguments),
  1259. result = {};
  1260. forIn(object, function(value, key) {
  1261. if (indexOf(props, key, 1) < 0) {
  1262. result[key] = value;
  1263. }
  1264. });
  1265. return result;
  1266. }
  1267. /**
  1268. * Creates a two dimensional array of the given object's key-value pairs,
  1269. * i.e. `[[key1, value1], [key2, value2]]`.
  1270. *
  1271. * @static
  1272. * @memberOf _
  1273. * @category Objects
  1274. * @param {Object} object The object to inspect.
  1275. * @returns {Array} Returns new array of key-value pairs.
  1276. * @example
  1277. *
  1278. * _.pairs({ 'moe': 30, 'larry': 40, 'curly': 50 });
  1279. * // => [['moe', 30], ['larry', 40], ['curly', 50]] (order is not guaranteed)
  1280. */
  1281. function pairs(object) {
  1282. var result = [];
  1283. forOwn(object, function(value, key) {
  1284. result.push([key, value]);
  1285. });
  1286. return result;
  1287. }
  1288. /**
  1289. * Creates a shallow clone of `object` composed of the specified properties.
  1290. * Property names may be specified as individual arguments or as arrays of
  1291. * property names. If `callback` is passed, it will be executed for each property
  1292. * in the `object`, picking the properties `callback` returns truthy for. The
  1293. * `callback` is bound to `thisArg` and invoked with three arguments; (value, key, object).
  1294. *
  1295. * @static
  1296. * @memberOf _
  1297. * @category Objects
  1298. * @param {Object} object The source object.
  1299. * @param {Function|String} callback|[prop1, prop2, ...] The properties to pick
  1300. * or the function called per iteration.
  1301. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1302. * @returns {Object} Returns an object composed of the picked properties.
  1303. * @example
  1304. *
  1305. * _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age');
  1306. * // => { 'name': 'moe', 'age': 40 }
  1307. *
  1308. * _.pick({ 'name': 'moe', '_hint': 'knucklehead', '_seed': '96c4eb' }, function(value, key) {
  1309. * return key.charAt(0) != '_';
  1310. * });
  1311. * // => { 'name': 'moe' }
  1312. */
  1313. function pick(object) {
  1314. var index = 0,
  1315. props = concat.apply(arrayRef, arguments),
  1316. length = props.length,
  1317. result = {};
  1318. while (++index < length) {
  1319. var prop = props[index];
  1320. if (prop in object) {
  1321. result[prop] = object[prop];
  1322. }
  1323. }
  1324. return result;
  1325. }
  1326. /**
  1327. * Creates an array composed of the own enumerable property values of `object`.
  1328. *
  1329. * @static
  1330. * @memberOf _
  1331. * @category Objects
  1332. * @param {Object} object The object to inspect.
  1333. * @returns {Array} Returns a new array of property values.
  1334. * @example
  1335. *
  1336. * _.values({ 'one': 1, 'two': 2, 'three': 3 });
  1337. * // => [1, 2, 3]
  1338. */
  1339. function values(object) {
  1340. var result = [];
  1341. forOwn(object, function(value) {
  1342. result.push(value);
  1343. });
  1344. return result;
  1345. }
  1346. /*--------------------------------------------------------------------------*/
  1347. /**
  1348. * Checks if a given `target` element is present in a `collection` using strict
  1349. * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
  1350. * as the offset from the end of the collection.
  1351. *
  1352. * @static
  1353. * @memberOf _
  1354. * @alias include
  1355. * @category Collections
  1356. * @param {Array|Object|String} collection The collection to iterate over.
  1357. * @param {Mixed} target The value to check for.
  1358. * @param {Number} [fromIndex=0] The index to search from.
  1359. * @returns {Boolean} Returns `true` if the `target` element is found, else `false`.
  1360. * @example
  1361. *
  1362. * _.contains([1, 2, 3], 1);
  1363. * // => true
  1364. *
  1365. * _.contains([1, 2, 3], 1, 2);
  1366. * // => false
  1367. *
  1368. * _.contains({ 'name': 'moe', 'age': 40 }, 'moe');
  1369. * // => true
  1370. *
  1371. * _.contains('curly', 'ur');
  1372. * // => true
  1373. */
  1374. function contains(collection, target) {
  1375. var length = collection ? collection.length : 0,
  1376. result = false;
  1377. if (typeof length == 'number') {
  1378. result = indexOf(collection, target) > -1;
  1379. } else {
  1380. forEach(collection, function(value) {
  1381. return (result = value === target) && indicatorObject;
  1382. });
  1383. }
  1384. return result;
  1385. }
  1386. /**
  1387. * Creates an object composed of keys returned from running each element of
  1388. * `collection` through a `callback`. The corresponding value of each key is
  1389. * the number of times the key was returned by `callback`. The `callback` is
  1390. * bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
  1391. * The `callback` argument may also be the name of a property to count by (e.g. 'length').
  1392. *
  1393. * @static
  1394. * @memberOf _
  1395. * @category Collections
  1396. * @param {Array|Object|String} collection The collection to iterate over.
  1397. * @param {Function|String} callback|property The function called per iteration
  1398. * or property name to count by.
  1399. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1400. * @returns {Object} Returns the composed aggregate object.
  1401. * @example
  1402. *
  1403. * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
  1404. * // => { '4': 1, '6': 2 }
  1405. *
  1406. * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
  1407. * // => { '4': 1, '6': 2 }
  1408. *
  1409. * _.countBy(['one', 'two', 'three'], 'length');
  1410. * // => { '3': 2, '5': 1 }
  1411. */
  1412. function countBy(collection, callback, thisArg) {
  1413. var result = {};
  1414. callback = createCallback(callback, thisArg);
  1415. forEach(collection, function(value, key, collection) {
  1416. key = callback(value, key, collection);
  1417. (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
  1418. });
  1419. return result;
  1420. }
  1421. /**
  1422. * Checks if the `callback` returns a truthy value for **all** elements of a
  1423. * `collection`. The `callback` is bound to `thisArg` and invoked with three
  1424. * arguments; (value, index|key, collection).
  1425. *
  1426. * @static
  1427. * @memberOf _
  1428. * @alias all
  1429. * @category Collections
  1430. * @param {Array|Object|String} collection The collection to iterate over.
  1431. * @param {Function} [callback=identity] The function called per iteration.
  1432. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1433. * @returns {Boolean} Returns `true` if all elements pass the callback check,
  1434. * else `false`.
  1435. * @example
  1436. *
  1437. * _.every([true, 1, null, 'yes'], Boolean);
  1438. * // => false
  1439. */
  1440. function every(collection, callback, thisArg) {
  1441. var result = true;
  1442. callback = createCallback(callback, thisArg);
  1443. if (isArray(collection)) {
  1444. var index = -1,
  1445. length = collection.length;
  1446. while (++index < length) {
  1447. if (!(result = !!callback(collection[index], index, collection))) {
  1448. break;
  1449. }
  1450. }
  1451. } else {
  1452. forEach(collection, function(value, index, collection) {
  1453. return !(result = !!callback(value, index, collection)) && indicatorObject;
  1454. });
  1455. }
  1456. return result;
  1457. }
  1458. /**
  1459. * Examines each element in a `collection`, returning an array of all elements
  1460. * the `callback` returns truthy for. The `callback` is bound to `thisArg` and
  1461. * invoked with three arguments; (value, index|key, collection).
  1462. *
  1463. * @static
  1464. * @memberOf _
  1465. * @alias select
  1466. * @category Collections
  1467. * @param {Array|Object|String} collection The collection to iterate over.
  1468. * @param {Function} [callback=identity] The function called per iteration.
  1469. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1470. * @returns {Array} Returns a new array of elements that passed the callback check.
  1471. * @example
  1472. *
  1473. * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
  1474. * // => [2, 4, 6]
  1475. */
  1476. function filter(collection, callback, thisArg) {
  1477. var result = [];
  1478. callback = createCallback(callback, thisArg);
  1479. if (isArray(collection)) {
  1480. var index = -1,
  1481. length = collection.length;
  1482. while (++index < length) {
  1483. var value = collection[index];
  1484. if (callback(value, index, collection)) {
  1485. result.push(value);
  1486. }
  1487. }
  1488. } else {
  1489. forEach(collection, function(value, index, collection) {
  1490. if (callback(value, index, collection)) {
  1491. result.push(value);
  1492. }
  1493. });
  1494. }
  1495. return result;
  1496. }
  1497. /**
  1498. * Examines each element in a `collection`, returning the first one the `callback`
  1499. * returns truthy for. The function returns as soon as it finds an acceptable
  1500. * element, and does not iterate over the entire `collection`. The `callback` is
  1501. * bound to `thisArg` and invoked with three arguments; (value, index|key, collection).
  1502. *
  1503. * @static
  1504. * @memberOf _
  1505. * @alias detect
  1506. * @category Collections
  1507. * @param {Array|Object|String} collection The collection to iterate over.
  1508. * @param {Function} [callback=identity] The function called per iteration.
  1509. * @param {Mixed} [thisArg] The `this` binding of `callback`.
  1510. * @returns {Mixed} Returns the element