PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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