PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/lodash.js/1.0.0/lodash.underscore.js

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