3 * Lo-Dash 2.4.2 (Custom Build) <https://lodash.com/>
4 * Build: `lodash -o ./dist/lodash.compat.js`
5 * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
6 * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
7 * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
8 * Available under MIT license <https://lodash.com/license>
12 /** Used as a safe reference for `undefined` in pre ES5 environments */
15 /** Used to pool arrays and objects used internally */
19 /** Used to generate unique IDs */
22 /** Used internally to indicate various things */
23 var indicatorObject = {};
25 /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
26 var keyPrefix = +new Date + '';
28 /** Used as the size when optimizations are enabled for large arrays */
29 var largeArraySize = 75;
31 /** Used as the max size of the `arrayPool` and `objectPool` */
34 /** Used to detect and test whitespace */
37 ' \t\x0B\f\xA0\ufeff' +
42 // unicode category "Zs" space separators
43 '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000'
46 /** Used to match empty string literals in compiled template source */
47 var reEmptyStringLeading = /\b__p \+= '';/g,
48 reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
49 reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
52 * Used to match ES6 template delimiters
53 * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals
55 var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
57 /** Used to match regexp flags from their coerced string values */
60 /** Used to detected named functions */
61 var reFuncName = /^\s*function[ \n\r\t]+\w/;
63 /** Used to match "interpolate" template delimiters */
64 var reInterpolate = /<%=([\s\S]+?)%>/g;
66 /** Used to match leading whitespace and zeros to be removed */
67 var reLeadingSpacesAndZeros = RegExp('^[' + whitespace + ']*0+(?=.$)');
69 /** Used to ensure capturing order of template delimiters */
70 var reNoMatch = /($^)/;
72 /** Used to detect functions containing a `this` reference */
73 var reThis = /\bthis\b/;
75 /** Used to match unescaped characters in compiled string literals */
76 var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
78 /** Used to assign default `context` object properties */
80 'Array', 'Boolean', 'Date', 'Error', 'Function', 'Math', 'Number', 'Object',
81 'RegExp', 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN',
82 'parseInt', 'setTimeout'
85 /** Used to fix the JScript [[DontEnum]] bug */
87 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable',
88 'toLocaleString', 'toString', 'valueOf'
91 /** Used to make template sourceURLs easier to identify */
92 var templateCounter = 0;
94 /** `Object#toString` result shortcuts */
95 var argsClass = '[object Arguments]',
96 arrayClass = '[object Array]',
97 boolClass = '[object Boolean]',
98 dateClass = '[object Date]',
99 errorClass = '[object Error]',
100 funcClass = '[object Function]',
101 numberClass = '[object Number]',
102 objectClass = '[object Object]',
103 regexpClass = '[object RegExp]',
104 stringClass = '[object String]';
106 /** Used to identify object classifications that `_.clone` supports */
107 var cloneableClasses = {};
108 cloneableClasses[funcClass] = false;
109 cloneableClasses[argsClass] = cloneableClasses[arrayClass] =
110 cloneableClasses[boolClass] = cloneableClasses[dateClass] =
111 cloneableClasses[numberClass] = cloneableClasses[objectClass] =
112 cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true;
114 /** Used as an internal `_.debounce` options object */
115 var debounceOptions = {
121 /** Used as the property descriptor for `__bindData__` */
123 'configurable': false,
129 /** Used as the data object for `iteratorTemplate` */
138 'shadowedProps': null,
144 /** Used to determine if values are of the language type Object */
154 /** Used to escape characters for inclusion in compiled string literals */
155 var stringEscapes = {
165 /** Used as a reference to the global object */
166 var root = (objectTypes[typeof window] && window) || this;
168 /** Detect free variable `exports` */
169 var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;
171 /** Detect free variable `module` */
172 var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;
174 /** Detect the popular CommonJS extension `module.exports` */
175 var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;
177 /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */
178 var freeGlobal = objectTypes[typeof global] && global;
179 if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
183 /*--------------------------------------------------------------------------*/
186 * The base implementation of `_.indexOf` without support for binary searches
187 * or `fromIndex` constraints.
190 * @param {Array} array The array to search.
191 * @param {*} value The value to search for.
192 * @param {number} [fromIndex=0] The index to search from.
193 * @returns {number} Returns the index of the matched value or `-1`.
195 function baseIndexOf(array, value, fromIndex) {
196 var index = (fromIndex || 0) - 1,
197 length = array ? array.length : 0;
199 while (++index < length) {
200 if (array[index] === value) {
208 * An implementation of `_.contains` for cache objects that mimics the return
209 * signature of `_.indexOf` by returning `0` if the value is found, else `-1`.
212 * @param {Object} cache The cache object to inspect.
213 * @param {*} value The value to search for.
214 * @returns {number} Returns `0` if `value` is found, else `-1`.
216 function cacheIndexOf(cache, value) {
217 var type = typeof value;
220 if (type == 'boolean' || value == null) {
221 return cache[value] ? 0 : -1;
223 if (type != 'number' && type != 'string') {
226 var key = type == 'number' ? value : keyPrefix + value;
227 cache = (cache = cache[type]) && cache[key];
229 return type == 'object'
230 ? (cache && baseIndexOf(cache, value) > -1 ? 0 : -1)
235 * Adds a given value to the corresponding cache object.
238 * @param {*} value The value to add to the cache.
240 function cachePush(value) {
241 var cache = this.cache,
244 if (type == 'boolean' || value == null) {
247 if (type != 'number' && type != 'string') {
250 var key = type == 'number' ? value : keyPrefix + value,
251 typeCache = cache[type] || (cache[type] = {});
253 if (type == 'object') {
254 (typeCache[key] || (typeCache[key] = [])).push(value);
256 typeCache[key] = true;
262 * Used by `_.max` and `_.min` as the default callback when a given
263 * collection is a string value.
266 * @param {string} value The character to inspect.
267 * @returns {number} Returns the code unit of given character.
269 function charAtCallback(value) {
270 return value.charCodeAt(0);
274 * Used by `sortBy` to compare transformed `collection` elements, stable sorting
275 * them in ascending order.
278 * @param {Object} a The object to compare to `b`.
279 * @param {Object} b The object to compare to `a`.
280 * @returns {number} Returns the sort order indicator of `1` or `-1`.
282 function compareAscending(a, b) {
288 while (++index < length) {
289 var value = ac[index],
292 if (value !== other) {
293 if (value > other || typeof value == 'undefined') {
296 if (value < other || typeof other == 'undefined') {
301 // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
302 // that causes it, under certain circumstances, to return the same value for
303 // `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247
305 // This also ensures a stable sort in V8 and other engines.
306 // See http://code.google.com/p/v8/issues/detail?id=90
307 return a.index - b.index;
311 * Creates a cache object to optimize linear searches of large arrays.
314 * @param {Array} [array=[]] The array to search.
315 * @returns {null|Object} Returns the cache object or `null` if caching should not be used.
317 function createCache(array) {
319 length = array.length,
321 mid = array[(length / 2) | 0],
322 last = array[length - 1];
324 if (first && typeof first == 'object' &&
325 mid && typeof mid == 'object' && last && typeof last == 'object') {
328 var cache = getObject();
329 cache['false'] = cache['null'] = cache['true'] = cache['undefined'] = false;
331 var result = getObject();
332 result.array = array;
333 result.cache = cache;
334 result.push = cachePush;
336 while (++index < length) {
337 result.push(array[index]);
343 * Used by `template` to escape characters for inclusion in compiled
347 * @param {string} match The matched character to escape.
348 * @returns {string} Returns the escaped character.
350 function escapeStringChar(match) {
351 return '\\' + stringEscapes[match];
355 * Gets an array from the array pool or creates a new one if the pool is empty.
358 * @returns {Array} The array from the pool.
360 function getArray() {
361 return arrayPool.pop() || [];
365 * Gets an object from the object pool or creates a new one if the pool is empty.
368 * @returns {Object} The object from the pool.
370 function getObject() {
371 return objectPool.pop() || {
389 * Checks if `value` is a DOM node in IE < 9.
392 * @param {*} value The value to check.
393 * @returns {boolean} Returns `true` if the `value` is a DOM node, else `false`.
395 function isNode(value) {
396 // IE < 9 presents DOM nodes as `Object` objects except they have `toString`
397 // methods that are `typeof` "string" and still can coerce nodes to strings
398 return typeof value.toString != 'function' && typeof (value + '') == 'string';
402 * Releases the given array back to the array pool.
405 * @param {Array} [array] The array to release.
407 function releaseArray(array) {
409 if (arrayPool.length < maxPoolSize) {
410 arrayPool.push(array);
415 * Releases the given object back to the object pool.
418 * @param {Object} [object] The object to release.
420 function releaseObject(object) {
421 var cache = object.cache;
423 releaseObject(cache);
425 object.array = object.cache = object.criteria = object.object = object.number = object.string = object.value = null;
426 if (objectPool.length < maxPoolSize) {
427 objectPool.push(object);
432 * Slices the `collection` from the `start` index up to, but not including,
435 * Note: This function is used instead of `Array#slice` to support node lists
436 * in IE < 9 and to ensure dense arrays are returned.
439 * @param {Array|Object|string} collection The collection to slice.
440 * @param {number} start The start index.
441 * @param {number} end The end index.
442 * @returns {Array} Returns the new array.
444 function slice(array, start, end) {
445 start || (start = 0);
446 if (typeof end == 'undefined') {
447 end = array ? array.length : 0;
450 length = end - start || 0,
451 result = Array(length < 0 ? 0 : length);
453 while (++index < length) {
454 result[index] = array[start + index];
459 /*--------------------------------------------------------------------------*/
462 * Create a new `lodash` function using the given context object.
466 * @category Utilities
467 * @param {Object} [context=root] The context object.
468 * @returns {Function} Returns the `lodash` function.
470 function runInContext(context) {
471 // Avoid issues with some ES3 environments that attempt to use values, named
472 // after built-in constructors like `Object`, for the creation of literals.
473 // ES5 clears this up by stating that literals must use built-in constructors.
474 // See http://es5.github.io/#x11.1.5.
475 context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root;
477 /** Native constructor references */
478 var Array = context.Array,
479 Boolean = context.Boolean,
481 Error = context.Error,
482 Function = context.Function,
484 Number = context.Number,
485 Object = context.Object,
486 RegExp = context.RegExp,
487 String = context.String,
488 TypeError = context.TypeError;
491 * Used for `Array` method references.
493 * Normally `Array.prototype` would suffice, however, using an array literal
494 * avoids issues in Narwhal.
498 /** Used for native method references */
499 var errorProto = Error.prototype,
500 objectProto = Object.prototype,
501 stringProto = String.prototype;
503 /** Used to restore the original `_` reference in `noConflict` */
504 var oldDash = context._;
506 /** Used to resolve the internal [[Class]] of values */
507 var toString = objectProto.toString;
509 /** Used to detect if a method is native */
510 var reNative = RegExp('^' +
512 .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
513 .replace(/toString| for [^\]]+/g, '.*?') + '$'
516 /** Native method shortcuts */
517 var ceil = Math.ceil,
518 clearTimeout = context.clearTimeout,
520 fnToString = Function.prototype.toString,
521 getPrototypeOf = isNative(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
522 hasOwnProperty = objectProto.hasOwnProperty,
523 push = arrayRef.push,
524 propertyIsEnumerable = objectProto.propertyIsEnumerable,
525 setTimeout = context.setTimeout,
526 splice = arrayRef.splice,
527 unshift = arrayRef.unshift;
529 /** Used to set meta data on functions */
530 var defineProperty = (function() {
531 // IE 8 only accepts DOM elements
534 func = isNative(func = Object.defineProperty) && func,
535 result = func(o, o, o) && func;
540 /* Native method shortcuts for methods with the same name as other `lodash` methods */
541 var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
542 nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
543 nativeIsFinite = context.isFinite,
544 nativeIsNaN = context.isNaN,
545 nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys,
546 nativeMax = Math.max,
547 nativeMin = Math.min,
548 nativeParseInt = context.parseInt,
549 nativeRandom = Math.random;
551 /** Used to lookup a built-in constructor by [[Class]] */
552 var ctorByClass = {};
553 ctorByClass[arrayClass] = Array;
554 ctorByClass[boolClass] = Boolean;
555 ctorByClass[dateClass] = Date;
556 ctorByClass[funcClass] = Function;
557 ctorByClass[objectClass] = Object;
558 ctorByClass[numberClass] = Number;
559 ctorByClass[regexpClass] = RegExp;
560 ctorByClass[stringClass] = String;
562 /** Used to avoid iterating non-enumerable properties in IE < 9 */
563 var nonEnumProps = {};
564 nonEnumProps[arrayClass] = nonEnumProps[dateClass] = nonEnumProps[numberClass] = { 'constructor': true, 'toLocaleString': true, 'toString': true, 'valueOf': true };
565 nonEnumProps[boolClass] = nonEnumProps[stringClass] = { 'constructor': true, 'toString': true, 'valueOf': true };
566 nonEnumProps[errorClass] = nonEnumProps[funcClass] = nonEnumProps[regexpClass] = { 'constructor': true, 'toString': true };
567 nonEnumProps[objectClass] = { 'constructor': true };
570 var length = shadowedProps.length;
572 var key = shadowedProps[length];
573 for (var className in nonEnumProps) {
574 if (hasOwnProperty.call(nonEnumProps, className) && !hasOwnProperty.call(nonEnumProps[className], key)) {
575 nonEnumProps[className][key] = false;
581 /*--------------------------------------------------------------------------*/
584 * Creates a `lodash` object which wraps the given value to enable intuitive
587 * In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
588 * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
591 * Chaining is supported in custom builds as long as the `value` method is
592 * implicitly or explicitly included in the build.
594 * The chainable wrapper functions are:
595 * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
596 * `compose`, `concat`, `countBy`, `create`, `createCallback`, `curry`,
597 * `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`,
598 * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
599 * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
600 * `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
601 * `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`,
602 * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
603 * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
604 * `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`,
607 * The non-chainable wrapper functions are:
608 * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`,
609 * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`,
610 * `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
611 * `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`,
612 * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`,
613 * `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`,
614 * `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`,
615 * `template`, `unescape`, `uniqueId`, and `value`
617 * The wrapper functions `first` and `last` return wrapped values when `n` is
618 * provided, otherwise they return unwrapped values.
620 * Explicit chaining can be enabled by using the `_.chain` method.
625 * @param {*} value The value to wrap in a `lodash` instance.
626 * @returns {Object} Returns a `lodash` instance.
629 * var wrapped = _([1, 2, 3]);
631 * // returns an unwrapped value
632 * wrapped.reduce(function(sum, num) {
637 * // returns a wrapped value
638 * var squares = wrapped.map(function(num) {
642 * _.isArray(squares);
645 * _.isArray(squares.value());
648 function lodash(value) {
649 // don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
650 return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
652 : new lodashWrapper(value);
656 * A fast path for creating `lodash` wrapper objects.
659 * @param {*} value The value to wrap in a `lodash` instance.
660 * @param {boolean} chainAll A flag to enable chaining for all methods
661 * @returns {Object} Returns a `lodash` instance.
663 function lodashWrapper(value, chainAll) {
664 this.__chain__ = !!chainAll;
665 this.__wrapped__ = value;
667 // ensure `new lodashWrapper` is an instance of `lodash`
668 lodashWrapper.prototype = lodash.prototype;
671 * An object used to flag environments features.
677 var support = lodash.support = {};
680 var ctor = function() { this.x = 1; },
681 object = { '0': 1, 'length': 1 },
684 ctor.prototype = { 'valueOf': 1, 'y': 1 };
685 for (var key in new ctor) { props.push(key); }
686 for (key in arguments) { }
689 * Detect if an `arguments` object's [[Class]] is resolvable (all but Firefox < 4, IE < 9).
691 * @memberOf _.support
694 support.argsClass = toString.call(arguments) == argsClass;
697 * Detect if `arguments` objects are `Object` objects (all but Narwhal and Opera < 10.5).
699 * @memberOf _.support
702 support.argsObject = arguments.constructor == Object && !(arguments instanceof Array);
705 * Detect if `name` or `message` properties of `Error.prototype` are
706 * enumerable by default. (IE < 9, Safari < 5.1)
708 * @memberOf _.support
711 support.enumErrorProps = propertyIsEnumerable.call(errorProto, 'message') || propertyIsEnumerable.call(errorProto, 'name');
714 * Detect if `prototype` properties are enumerable by default.
716 * Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1
717 * (if the prototype or a property on the prototype has been set)
718 * incorrectly sets a function's `prototype` property [[Enumerable]]
721 * @memberOf _.support
724 support.enumPrototypes = propertyIsEnumerable.call(ctor, 'prototype');
727 * Detect if functions can be decompiled by `Function#toString`
728 * (all but PS3 and older Opera mobile browsers & avoided in Windows 8 apps).
730 * @memberOf _.support
733 support.funcDecomp = !isNative(context.WinRTError) && reThis.test(runInContext);
736 * Detect if `Function#name` is supported (all but IE).
738 * @memberOf _.support
741 support.funcNames = typeof Function.name == 'string';
744 * Detect if `arguments` object indexes are non-enumerable
745 * (Firefox < 4, IE < 9, PhantomJS, Safari < 5.1).
747 * @memberOf _.support
750 support.nonEnumArgs = key != 0;
753 * Detect if properties shadowing those on `Object.prototype` are non-enumerable.
755 * In IE < 9 an objects own properties, shadowing non-enumerable ones, are
756 * made non-enumerable as well (a.k.a the JScript [[DontEnum]] bug).
758 * @memberOf _.support
761 support.nonEnumShadows = !/valueOf/.test(props);
764 * Detect if own properties are iterated after inherited properties (all but IE < 9).
766 * @memberOf _.support
769 support.ownLast = props[0] != 'x';
772 * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly.
774 * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()`
775 * and `splice()` functions that fail to remove the last element, `value[0]`,
776 * of array-like objects even though the `length` property is set to `0`.
777 * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()`
778 * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9.
780 * @memberOf _.support
783 support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]);
786 * Detect lack of support for accessing string characters by index.
788 * IE < 8 can't access characters by index and IE 8 can only access
789 * characters by index on string literals.
791 * @memberOf _.support
794 support.unindexedChars = ('x'[0] + Object('x')[0]) != 'xx';
797 * Detect if a DOM node's [[Class]] is resolvable (all but IE < 9)
798 * and that the JS engine errors when attempting to coerce an object to
799 * a string without a `toString` function.
801 * @memberOf _.support
805 support.nodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + ''));
807 support.nodeClass = true;
812 * By default, the template delimiters used by Lo-Dash are similar to those in
813 * embedded Ruby (ERB). Change the following template settings to use alternative
820 lodash.templateSettings = {
823 * Used to detect `data` property values to be HTML-escaped.
825 * @memberOf _.templateSettings
828 'escape': /<%-([\s\S]+?)%>/g,
831 * Used to detect code to be evaluated.
833 * @memberOf _.templateSettings
836 'evaluate': /<%([\s\S]+?)%>/g,
839 * Used to detect `data` property values to inject.
841 * @memberOf _.templateSettings
844 'interpolate': reInterpolate,
847 * Used to reference the data object in the template text.
849 * @memberOf _.templateSettings
855 * Used to import variables into the compiled template.
857 * @memberOf _.templateSettings
863 * A reference to the `lodash` function.
865 * @memberOf _.templateSettings.imports
872 /*--------------------------------------------------------------------------*/
875 * The template used to create iterator functions.
878 * @param {Object} data The data object used to populate the text.
879 * @returns {string} Returns the interpolated text.
881 var iteratorTemplate = function(obj) {
883 var __p = 'var index, iterable = ' +
887 ';\nif (!iterable) return result;\n' +
891 __p += '\nvar length = iterable.length; index = -1;\nif (' +
894 if (support.unindexedChars) {
895 __p += '\n if (isString(iterable)) {\n iterable = iterable.split(\'\')\n } ';
897 __p += '\n while (++index < length) {\n ' +
900 } else if (support.nonEnumArgs) {
901 __p += '\n var length = iterable.length; index = -1;\n if (length && isArguments(iterable)) {\n while (++index < length) {\n index += \'\';\n ' +
906 if (support.enumPrototypes) {
907 __p += '\n var skipProto = typeof iterable == \'function\';\n ';
910 if (support.enumErrorProps) {
911 __p += '\n var skipErrorProps = iterable === errorProto || iterable instanceof Error;\n ';
914 var conditions = []; if (support.enumPrototypes) { conditions.push('!(skipProto && index == "prototype")'); } if (support.enumErrorProps) { conditions.push('!(skipErrorProps && (index == "message" || index == "name"))'); }
916 if (obj.useHas && obj.keys) {
917 __p += '\n var ownIndex = -1,\n ownProps = objectTypes[typeof iterable] && keys(iterable),\n length = ownProps ? ownProps.length : 0;\n\n while (++ownIndex < length) {\n index = ownProps[ownIndex];\n';
918 if (conditions.length) {
920 (conditions.join(' && ')) +
926 if (conditions.length) {
931 __p += '\n for (index in iterable) {\n';
932 if (obj.useHas) { conditions.push("hasOwnProperty.call(iterable, index)"); } if (conditions.length) {
934 (conditions.join(' && ')) +
940 if (conditions.length) {
944 if (support.nonEnumShadows) {
945 __p += '\n\n if (iterable !== objectProto) {\n var ctor = iterable.constructor,\n isProto = iterable === (ctor && ctor.prototype),\n className = iterable === stringProto ? stringClass : iterable === errorProto ? errorClass : toString.call(iterable),\n nonEnum = nonEnumProps[className];\n ';
946 for (k = 0; k < 7; k++) {
947 __p += '\n index = \'' +
948 (obj.shadowedProps[k]) +
949 '\';\n if ((!(isProto && nonEnum[index]) && hasOwnProperty.call(iterable, index))';
951 __p += ' || (!nonEnum[index] && iterable[index] !== objectProto[index])';
962 if (obj.array || support.nonEnumArgs) {
972 /*--------------------------------------------------------------------------*/
975 * The base implementation of `_.bind` that creates the bound function and
976 * sets its meta data.
979 * @param {Array} bindData The bind data array.
980 * @returns {Function} Returns the new bound function.
982 function baseBind(bindData) {
983 var func = bindData[0],
984 partialArgs = bindData[2],
985 thisArg = bindData[4];
988 // `Function#bind` spec
989 // http://es5.github.io/#x15.3.4.5
991 // avoid `arguments` object deoptimizations by using `slice` instead
992 // of `Array.prototype.slice.call` and not assigning `arguments` to a
993 // variable as a ternary expression
994 var args = slice(partialArgs);
995 push.apply(args, arguments);
997 // mimic the constructor's `return` behavior
998 // http://es5.github.io/#x13.2.2
999 if (this instanceof bound) {
1000 // ensure `new bound` is an instance of `func`
1001 var thisBinding = baseCreate(func.prototype),
1002 result = func.apply(thisBinding, args || arguments);
1003 return isObject(result) ? result : thisBinding;
1005 return func.apply(thisArg, args || arguments);
1007 setBindData(bound, bindData);
1012 * The base implementation of `_.clone` without argument juggling or support
1013 * for `thisArg` binding.
1016 * @param {*} value The value to clone.
1017 * @param {boolean} [isDeep=false] Specify a deep clone.
1018 * @param {Function} [callback] The function to customize cloning values.
1019 * @param {Array} [stackA=[]] Tracks traversed source objects.
1020 * @param {Array} [stackB=[]] Associates clones with source counterparts.
1021 * @returns {*} Returns the cloned value.
1023 function baseClone(value, isDeep, callback, stackA, stackB) {
1025 var result = callback(value);
1026 if (typeof result != 'undefined') {
1030 // inspect [[Class]]
1031 var isObj = isObject(value);
1033 var className = toString.call(value);
1034 if (!cloneableClasses[className] || (!support.nodeClass && isNode(value))) {
1037 var ctor = ctorByClass[className];
1038 switch (className) {
1041 return new ctor(+value);
1045 return new ctor(value);
1048 result = ctor(value.source, reFlags.exec(value));
1049 result.lastIndex = value.lastIndex;
1055 var isArr = isArray(value);
1057 // check for circular references and return corresponding clone
1058 var initedStack = !stackA;
1059 stackA || (stackA = getArray());
1060 stackB || (stackB = getArray());
1062 var length = stackA.length;
1064 if (stackA[length] == value) {
1065 return stackB[length];
1068 result = isArr ? ctor(value.length) : {};
1071 result = isArr ? slice(value) : assign({}, value);
1073 // add array properties assigned by `RegExp#exec`
1075 if (hasOwnProperty.call(value, 'index')) {
1076 result.index = value.index;
1078 if (hasOwnProperty.call(value, 'input')) {
1079 result.input = value.input;
1082 // exit for shallow clone
1086 // add the source value to the stack of traversed objects
1087 // and associate it with its clone
1089 stackB.push(result);
1091 // recursively populate clone (susceptible to call stack limits)
1092 (isArr ? baseEach : forOwn)(value, function(objValue, key) {
1093 result[key] = baseClone(objValue, isDeep, callback, stackA, stackB);
1097 releaseArray(stackA);
1098 releaseArray(stackB);
1104 * The base implementation of `_.create` without support for assigning
1105 * properties to the created object.
1108 * @param {Object} prototype The object to inherit from.
1109 * @returns {Object} Returns the new object.
1111 function baseCreate(prototype, properties) {
1112 return isObject(prototype) ? nativeCreate(prototype) : {};
1114 // fallback for browsers without `Object.create`
1115 if (!nativeCreate) {
1116 baseCreate = (function() {
1117 function Object() {}
1118 return function(prototype) {
1119 if (isObject(prototype)) {
1120 Object.prototype = prototype;
1121 var result = new Object;
1122 Object.prototype = null;
1124 return result || context.Object();
1130 * The base implementation of `_.createCallback` without support for creating
1131 * "_.pluck" or "_.where" style callbacks.
1134 * @param {*} [func=identity] The value to convert to a callback.
1135 * @param {*} [thisArg] The `this` binding of the created callback.
1136 * @param {number} [argCount] The number of arguments the callback accepts.
1137 * @returns {Function} Returns a callback function.
1139 function baseCreateCallback(func, thisArg, argCount) {
1140 if (typeof func != 'function') {
1143 // exit early for no `thisArg` or already bound by `Function#bind`
1144 if (typeof thisArg == 'undefined' || !('prototype' in func)) {
1147 var bindData = func.__bindData__;
1148 if (typeof bindData == 'undefined') {
1149 if (support.funcNames) {
1150 bindData = !func.name;
1152 bindData = bindData || !support.funcDecomp;
1154 var source = fnToString.call(func);
1155 if (!support.funcNames) {
1156 bindData = !reFuncName.test(source);
1159 // checks if `func` references the `this` keyword and stores the result
1160 bindData = reThis.test(source);
1161 setBindData(func, bindData);
1165 // exit early if there are no `this` references or `func` is bound
1166 if (bindData === false || (bindData !== true && bindData[1] & 1)) {
1170 case 1: return function(value) {
1171 return func.call(thisArg, value);
1173 case 2: return function(a, b) {
1174 return func.call(thisArg, a, b);
1176 case 3: return function(value, index, collection) {
1177 return func.call(thisArg, value, index, collection);
1179 case 4: return function(accumulator, value, index, collection) {
1180 return func.call(thisArg, accumulator, value, index, collection);
1183 return bind(func, thisArg);
1187 * The base implementation of `createWrapper` that creates the wrapper and
1188 * sets its meta data.
1191 * @param {Array} bindData The bind data array.
1192 * @returns {Function} Returns the new function.
1194 function baseCreateWrapper(bindData) {
1195 var func = bindData[0],
1196 bitmask = bindData[1],
1197 partialArgs = bindData[2],
1198 partialRightArgs = bindData[3],
1199 thisArg = bindData[4],
1200 arity = bindData[5];
1202 var isBind = bitmask & 1,
1203 isBindKey = bitmask & 2,
1204 isCurry = bitmask & 4,
1205 isCurryBound = bitmask & 8,
1209 var thisBinding = isBind ? thisArg : this;
1211 var args = slice(partialArgs);
1212 push.apply(args, arguments);
1214 if (partialRightArgs || isCurry) {
1215 args || (args = slice(arguments));
1216 if (partialRightArgs) {
1217 push.apply(args, partialRightArgs);
1219 if (isCurry && args.length < arity) {
1220 bitmask |= 16 & ~32;
1221 return baseCreateWrapper([func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity]);
1224 args || (args = arguments);
1226 func = thisBinding[key];
1228 if (this instanceof bound) {
1229 thisBinding = baseCreate(func.prototype);
1230 var result = func.apply(thisBinding, args);
1231 return isObject(result) ? result : thisBinding;
1233 return func.apply(thisBinding, args);
1235 setBindData(bound, bindData);
1240 * The base implementation of `_.difference` that accepts a single array
1241 * of values to exclude.
1244 * @param {Array} array The array to process.
1245 * @param {Array} [values] The array of values to exclude.
1246 * @returns {Array} Returns a new array of filtered values.
1248 function baseDifference(array, values) {
1250 indexOf = getIndexOf(),
1251 length = array ? array.length : 0,
1252 isLarge = length >= largeArraySize && indexOf === baseIndexOf,
1256 var cache = createCache(values);
1258 indexOf = cacheIndexOf;
1264 while (++index < length) {
1265 var value = array[index];
1266 if (indexOf(values, value) < 0) {
1271 releaseObject(values);
1277 * The base implementation of `_.flatten` without support for callback
1278 * shorthands or `thisArg` binding.
1281 * @param {Array} array The array to flatten.
1282 * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
1283 * @param {boolean} [isStrict=false] A flag to restrict flattening to arrays and `arguments` objects.
1284 * @param {number} [fromIndex=0] The index to start from.
1285 * @returns {Array} Returns a new flattened array.
1287 function baseFlatten(array, isShallow, isStrict, fromIndex) {
1288 var index = (fromIndex || 0) - 1,
1289 length = array ? array.length : 0,
1292 while (++index < length) {
1293 var value = array[index];
1295 if (value && typeof value == 'object' && typeof value.length == 'number'
1296 && (isArray(value) || isArguments(value))) {
1297 // recursively flatten arrays (susceptible to call stack limits)
1299 value = baseFlatten(value, isShallow, isStrict);
1302 valLength = value.length,
1303 resIndex = result.length;
1305 result.length += valLength;
1306 while (++valIndex < valLength) {
1307 result[resIndex++] = value[valIndex];
1309 } else if (!isStrict) {
1317 * The base implementation of `_.isEqual`, without support for `thisArg` binding,
1318 * that allows partial "_.where" style comparisons.
1321 * @param {*} a The value to compare.
1322 * @param {*} b The other value to compare.
1323 * @param {Function} [callback] The function to customize comparing values.
1324 * @param {Function} [isWhere=false] A flag to indicate performing partial comparisons.
1325 * @param {Array} [stackA=[]] Tracks traversed `a` objects.
1326 * @param {Array} [stackB=[]] Tracks traversed `b` objects.
1327 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
1329 function baseIsEqual(a, b, callback, isWhere, stackA, stackB) {
1330 // used to indicate that when comparing objects, `a` has at least the properties of `b`
1332 var result = callback(a, b);
1333 if (typeof result != 'undefined') {
1337 // exit early for identical values
1339 // treat `+0` vs. `-0` as not equal
1340 return a !== 0 || (1 / a == 1 / b);
1342 var type = typeof a,
1343 otherType = typeof b;
1345 // exit early for unlike primitive values
1347 !(a && objectTypes[type]) &&
1348 !(b && objectTypes[otherType])) {
1351 // exit early for `null` and `undefined` avoiding ES3's Function#call behavior
1352 // http://es5.github.io/#x15.3.4.4
1353 if (a == null || b == null) {
1356 // compare [[Class]] names
1357 var className = toString.call(a),
1358 otherClass = toString.call(b);
1360 if (className == argsClass) {
1361 className = objectClass;
1363 if (otherClass == argsClass) {
1364 otherClass = objectClass;
1366 if (className != otherClass) {
1369 switch (className) {
1372 // coerce dates and booleans to numbers, dates to milliseconds and booleans
1373 // to `1` or `0` treating invalid dates coerced to `NaN` as not equal
1377 // treat `NaN` vs. `NaN` as equal
1380 // but treat `+0` vs. `-0` as not equal
1381 : (a == 0 ? (1 / a == 1 / b) : a == +b);
1385 // coerce regexes to strings (http://es5.github.io/#x15.10.6.4)
1386 // treat string primitives and their corresponding object instances as equal
1387 return a == String(b);
1389 var isArr = className == arrayClass;
1391 // unwrap any `lodash` wrapped values
1392 var aWrapped = hasOwnProperty.call(a, '__wrapped__'),
1393 bWrapped = hasOwnProperty.call(b, '__wrapped__');
1395 if (aWrapped || bWrapped) {
1396 return baseIsEqual(aWrapped ? a.__wrapped__ : a, bWrapped ? b.__wrapped__ : b, callback, isWhere, stackA, stackB);
1398 // exit for functions and DOM nodes
1399 if (className != objectClass || (!support.nodeClass && (isNode(a) || isNode(b)))) {
1402 // in older versions of Opera, `arguments` objects have `Array` constructors
1403 var ctorA = !support.argsObject && isArguments(a) ? Object : a.constructor,
1404 ctorB = !support.argsObject && isArguments(b) ? Object : b.constructor;
1406 // non `Object` object instances with different constructors are not equal
1407 if (ctorA != ctorB &&
1408 !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
1409 ('constructor' in a && 'constructor' in b)
1414 // assume cyclic structures are equal
1415 // the algorithm for detecting cyclic structures is adapted from ES 5.1
1416 // section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3)
1417 var initedStack = !stackA;
1418 stackA || (stackA = getArray());
1419 stackB || (stackB = getArray());
1421 var length = stackA.length;
1423 if (stackA[length] == a) {
1424 return stackB[length] == b;
1430 // add `a` and `b` to the stack of traversed objects
1434 // recursively compare objects and arrays (susceptible to call stack limits)
1436 // compare lengths to determine if a deep comparison is necessary
1439 result = size == length;
1441 if (result || isWhere) {
1442 // deep compare the contents, ignoring non-numeric properties
1449 if ((result = baseIsEqual(a[index], value, callback, isWhere, stackA, stackB))) {
1453 } else if (!(result = baseIsEqual(a[size], value, callback, isWhere, stackA, stackB))) {
1460 // deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
1461 // which, in this case, is more costly
1462 forIn(b, function(value, key, b) {
1463 if (hasOwnProperty.call(b, key)) {
1464 // count the number of properties.
1466 // deep compare each property value.
1467 return (result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, callback, isWhere, stackA, stackB));
1471 if (result && !isWhere) {
1472 // ensure both objects have the same number of properties
1473 forIn(a, function(value, key, a) {
1474 if (hasOwnProperty.call(a, key)) {
1475 // `size` will be `-1` if `a` has more properties than `b`
1476 return (result = --size > -1);
1485 releaseArray(stackA);
1486 releaseArray(stackB);
1492 * The base implementation of `_.merge` without argument juggling or support
1493 * for `thisArg` binding.
1496 * @param {Object} object The destination object.
1497 * @param {Object} source The source object.
1498 * @param {Function} [callback] The function to customize merging properties.
1499 * @param {Array} [stackA=[]] Tracks traversed source objects.
1500 * @param {Array} [stackB=[]] Associates values with source counterparts.
1502 function baseMerge(object, source, callback, stackA, stackB) {
1503 (isArray(source) ? forEach : forOwn)(source, function(source, key) {
1507 value = object[key];
1509 if (source && ((isArr = isArray(source)) || isPlainObject(source))) {
1510 // avoid merging previously merged cyclic sources
1511 var stackLength = stackA.length;
1512 while (stackLength--) {
1513 if ((found = stackA[stackLength] == source)) {
1514 value = stackB[stackLength];
1521 result = callback(value, source);
1522 if ((isShallow = typeof result != 'undefined')) {
1528 ? (isArray(value) ? value : [])
1529 : (isPlainObject(value) ? value : {});
1531 // add `source` and associated `value` to the stack of traversed objects
1532 stackA.push(source);
1535 // recursively merge objects and arrays (susceptible to call stack limits)
1537 baseMerge(value, source, callback, stackA, stackB);
1543 result = callback(value, source);
1544 if (typeof result == 'undefined') {
1548 if (typeof result != 'undefined') {
1552 object[key] = value;
1557 * The base implementation of `_.random` without argument juggling or support
1558 * for returning floating-point numbers.
1561 * @param {number} min The minimum possible value.
1562 * @param {number} max The maximum possible value.
1563 * @returns {number} Returns a random number.
1565 function baseRandom(min, max) {
1566 return min + floor(nativeRandom() * (max - min + 1));
1570 * The base implementation of `_.uniq` without support for callback shorthands
1571 * or `thisArg` binding.
1574 * @param {Array} array The array to process.
1575 * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
1576 * @param {Function} [callback] The function called per iteration.
1577 * @returns {Array} Returns a duplicate-value-free array.
1579 function baseUniq(array, isSorted, callback) {
1581 indexOf = getIndexOf(),
1582 length = array ? array.length : 0,
1585 var isLarge = !isSorted && length >= largeArraySize && indexOf === baseIndexOf,
1586 seen = (callback || isLarge) ? getArray() : result;
1589 var cache = createCache(seen);
1590 indexOf = cacheIndexOf;
1593 while (++index < length) {
1594 var value = array[index],
1595 computed = callback ? callback(value, index, array) : value;
1598 ? !index || seen[seen.length - 1] !== computed
1599 : indexOf(seen, computed) < 0
1601 if (callback || isLarge) {
1602 seen.push(computed);
1608 releaseArray(seen.array);
1609 releaseObject(seen);
1610 } else if (callback) {
1617 * Creates a function that aggregates a collection, creating an object composed
1618 * of keys generated from the results of running each element of the collection
1619 * through a callback. The given `setter` function sets the keys and values
1620 * of the composed object.
1623 * @param {Function} setter The setter function.
1624 * @returns {Function} Returns the new aggregator function.
1626 function createAggregator(setter) {
1627 return function(collection, callback, thisArg) {
1629 callback = lodash.createCallback(callback, thisArg, 3);
1631 if (isArray(collection)) {
1633 length = collection.length;
1635 while (++index < length) {
1636 var value = collection[index];
1637 setter(result, value, callback(value, index, collection), collection);
1640 baseEach(collection, function(value, key, collection) {
1641 setter(result, value, callback(value, key, collection), collection);
1649 * Creates a function that, when called, either curries or invokes `func`
1650 * with an optional `this` binding and partially applied arguments.
1653 * @param {Function|string} func The function or method name to reference.
1654 * @param {number} bitmask The bitmask of method flags to compose.
1655 * The bitmask may be composed of the following flags:
1659 * 8 - `_.curry` (bound)
1661 * 32 - `_.partialRight`
1662 * @param {Array} [partialArgs] An array of arguments to prepend to those
1663 * provided to the new function.
1664 * @param {Array} [partialRightArgs] An array of arguments to append to those
1665 * provided to the new function.
1666 * @param {*} [thisArg] The `this` binding of `func`.
1667 * @param {number} [arity] The arity of `func`.
1668 * @returns {Function} Returns the new function.
1670 function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) {
1671 var isBind = bitmask & 1,
1672 isBindKey = bitmask & 2,
1673 isCurry = bitmask & 4,
1674 isCurryBound = bitmask & 8,
1675 isPartial = bitmask & 16,
1676 isPartialRight = bitmask & 32;
1678 if (!isBindKey && !isFunction(func)) {
1679 throw new TypeError;
1681 if (isPartial && !partialArgs.length) {
1683 isPartial = partialArgs = false;
1685 if (isPartialRight && !partialRightArgs.length) {
1687 isPartialRight = partialRightArgs = false;
1689 var bindData = func && func.__bindData__;
1690 if (bindData && bindData !== true) {
1692 bindData = slice(bindData);
1694 bindData[2] = slice(bindData[2]);
1697 bindData[3] = slice(bindData[3]);
1699 // set `thisBinding` is not previously bound
1700 if (isBind && !(bindData[1] & 1)) {
1701 bindData[4] = thisArg;
1703 // set if previously bound but not currently (subsequent curried functions)
1704 if (!isBind && bindData[1] & 1) {
1707 // set curried arity if not yet set
1708 if (isCurry && !(bindData[1] & 4)) {
1709 bindData[5] = arity;
1711 // append partial left arguments
1713 push.apply(bindData[2] || (bindData[2] = []), partialArgs);
1715 // append partial right arguments
1716 if (isPartialRight) {
1717 unshift.apply(bindData[3] || (bindData[3] = []), partialRightArgs);
1720 bindData[1] |= bitmask;
1721 return createWrapper.apply(null, bindData);
1723 // fast path for `_.bind`
1724 var creater = (bitmask == 1 || bitmask === 17) ? baseBind : baseCreateWrapper;
1725 return creater([func, bitmask, partialArgs, partialRightArgs, thisArg, arity]);
1729 * Creates compiled iteration functions.
1732 * @param {...Object} [options] The compile options object(s).
1733 * @param {string} [options.array] Code to determine if the iterable is an array or array-like.
1734 * @param {boolean} [options.useHas] Specify using `hasOwnProperty` checks in the object loop.
1735 * @param {Function} [options.keys] A reference to `_.keys` for use in own property iteration.
1736 * @param {string} [options.args] A comma separated string of iteration function arguments.
1737 * @param {string} [options.top] Code to execute before the iteration branches.
1738 * @param {string} [options.loop] Code to execute in the object loop.
1739 * @param {string} [options.bottom] Code to execute after the iteration branches.
1740 * @returns {Function} Returns the compiled function.
1742 function createIterator() {
1744 iteratorData.shadowedProps = shadowedProps;
1747 iteratorData.array = iteratorData.bottom = iteratorData.loop = iteratorData.top = '';
1748 iteratorData.init = 'iterable';
1749 iteratorData.useHas = true;
1751 // merge options into a template data object
1752 for (var object, index = 0; object = arguments[index]; index++) {
1753 for (var key in object) {
1754 iteratorData[key] = object[key];
1757 var args = iteratorData.args;
1758 iteratorData.firstArg = /^[^,]+/.exec(args)[0];
1760 // create the function factory
1761 var factory = Function(
1762 'baseCreateCallback, errorClass, errorProto, hasOwnProperty, ' +
1763 'indicatorObject, isArguments, isArray, isString, keys, objectProto, ' +
1764 'objectTypes, nonEnumProps, stringClass, stringProto, toString',
1765 'return function(' + args + ') {\n' + iteratorTemplate(iteratorData) + '\n}'
1768 // return the compiled function
1770 baseCreateCallback, errorClass, errorProto, hasOwnProperty,
1771 indicatorObject, isArguments, isArray, isString, iteratorData.keys, objectProto,
1772 objectTypes, nonEnumProps, stringClass, stringProto, toString
1777 * Used by `escape` to convert characters to HTML entities.
1780 * @param {string} match The matched character to escape.
1781 * @returns {string} Returns the escaped character.
1783 function escapeHtmlChar(match) {
1784 return htmlEscapes[match];
1788 * Gets the appropriate "indexOf" function. If the `_.indexOf` method is
1789 * customized, this method returns the custom method, otherwise it returns
1790 * the `baseIndexOf` function.
1793 * @returns {Function} Returns the "indexOf" function.
1795 function getIndexOf() {
1796 var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result;
1801 * Checks if `value` is a native function.
1804 * @param {*} value The value to check.
1805 * @returns {boolean} Returns `true` if the `value` is a native function, else `false`.
1807 function isNative(value) {
1808 return typeof value == 'function' && reNative.test(value);
1812 * Sets `this` binding data on a given function.
1815 * @param {Function} func The function to set data on.
1816 * @param {Array} value The data array to set.
1818 var setBindData = !defineProperty ? noop : function(func, value) {
1819 descriptor.value = value;
1820 defineProperty(func, '__bindData__', descriptor);
1821 descriptor.value = null;
1825 * A fallback implementation of `isPlainObject` which checks if a given value
1826 * is an object created by the `Object` constructor, assuming objects created
1827 * by the `Object` constructor have no inherited enumerable properties and that
1828 * there are no `Object.prototype` extensions.
1831 * @param {*} value The value to check.
1832 * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
1834 function shimIsPlainObject(value) {
1838 // avoid non Object objects, `arguments` objects, and DOM elements
1839 if (!(value && toString.call(value) == objectClass) ||
1840 (ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor)) ||
1841 (!support.argsClass && isArguments(value)) ||
1842 (!support.nodeClass && isNode(value))) {
1845 // IE < 9 iterates inherited properties before own properties. If the first
1846 // iterated property is an object's own property then there are no inherited
1847 // enumerable properties.
1848 if (support.ownLast) {
1849 forIn(value, function(value, key, object) {
1850 result = hasOwnProperty.call(object, key);
1853 return result !== false;
1855 // In most environments an object's own properties are iterated before
1856 // its inherited properties. If the last iterated property is an object's
1857 // own property then there are no inherited enumerable properties.
1858 forIn(value, function(value, key) {
1861 return typeof result == 'undefined' || hasOwnProperty.call(value, result);
1865 * Used by `unescape` to convert HTML entities to characters.
1868 * @param {string} match The matched character to unescape.
1869 * @returns {string} Returns the unescaped character.
1871 function unescapeHtmlChar(match) {
1872 return htmlUnescapes[match];
1875 /*--------------------------------------------------------------------------*/
1878 * Checks if `value` is an `arguments` object.
1883 * @param {*} value The value to check.
1884 * @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`.
1887 * (function() { return _.isArguments(arguments); })(1, 2, 3);
1890 * _.isArguments([1, 2, 3]);
1893 function isArguments(value) {
1894 return value && typeof value == 'object' && typeof value.length == 'number' &&
1895 toString.call(value) == argsClass || false;
1897 // fallback for browsers that can't detect `arguments` objects by [[Class]]
1898 if (!support.argsClass) {
1899 isArguments = function(value) {
1900 return value && typeof value == 'object' && typeof value.length == 'number' &&
1901 hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee') || false;
1906 * Checks if `value` is an array.
1912 * @param {*} value The value to check.
1913 * @returns {boolean} Returns `true` if the `value` is an array, else `false`.
1916 * (function() { return _.isArray(arguments); })();
1919 * _.isArray([1, 2, 3]);
1922 var isArray = nativeIsArray || function(value) {
1923 return value && typeof value == 'object' && typeof value.length == 'number' &&
1924 toString.call(value) == arrayClass || false;
1928 * A fallback implementation of `Object.keys` which produces an array of the
1929 * given object's own enumerable property names.
1933 * @param {Object} object The object to inspect.
1934 * @returns {Array} Returns an array of property names.
1936 var shimKeys = createIterator({
1939 'top': 'if (!(objectTypes[typeof object])) return result',
1940 'loop': 'result.push(index)'
1944 * Creates an array composed of the own enumerable property names of an object.
1949 * @param {Object} object The object to inspect.
1950 * @returns {Array} Returns an array of property names.
1953 * _.keys({ 'one': 1, 'two': 2, 'three': 3 });
1954 * // => ['one', 'two', 'three'] (property order is not guaranteed across environments)
1956 var keys = !nativeKeys ? shimKeys : function(object) {
1957 if (!isObject(object)) {
1960 if ((support.enumPrototypes && typeof object == 'function') ||
1961 (support.nonEnumArgs && object.length && isArguments(object))) {
1962 return shimKeys(object);
1964 return nativeKeys(object);
1967 /** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */
1968 var eachIteratorOptions = {
1969 'args': 'collection, callback, thisArg',
1970 'top': "callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3)",
1971 'array': "typeof length == 'number'",
1973 'loop': 'if (callback(iterable[index], index, collection) === false) return result'
1976 /** Reusable iterator options for `assign` and `defaults` */
1977 var defaultsIteratorOptions = {
1978 'args': 'object, source, guard',
1980 'var args = arguments,\n' +
1981 ' argsIndex = 0,\n' +
1982 " argsLength = typeof guard == 'number' ? 2 : args.length;\n" +
1983 'while (++argsIndex < argsLength) {\n' +
1984 ' iterable = args[argsIndex];\n' +
1985 ' if (iterable && objectTypes[typeof iterable]) {',
1987 'loop': "if (typeof result[index] == 'undefined') result[index] = iterable[index]",
1991 /** Reusable iterator options for `forIn` and `forOwn` */
1992 var forOwnIteratorOptions = {
1993 'top': 'if (!objectTypes[typeof iterable]) return result;\n' + eachIteratorOptions.top,
1998 * Used to convert characters to HTML entities:
2000 * Though the `>` character is escaped for symmetry, characters like `>` and `/`
2001 * don't require escaping in HTML and have no special meaning unless they're part
2002 * of a tag or an unquoted attribute value.
2003 * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact")
2013 /** Used to convert HTML entities to characters */
2014 var htmlUnescapes = invert(htmlEscapes);
2016 /** Used to match HTML entities and HTML characters */
2017 var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'),
2018 reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g');
2021 * A function compiled to iterate `arguments` objects, arrays, objects, and
2022 * strings consistenly across environments, executing the callback for each
2023 * element in the collection. The callback is bound to `thisArg` and invoked
2024 * with three arguments; (value, index|key, collection). Callbacks may exit
2025 * iteration early by explicitly returning `false`.
2029 * @param {Array|Object|string} collection The collection to iterate over.
2030 * @param {Function} [callback=identity] The function called per iteration.
2031 * @param {*} [thisArg] The `this` binding of `callback`.
2032 * @returns {Array|Object|string} Returns `collection`.
2034 var baseEach = createIterator(eachIteratorOptions);
2036 /*--------------------------------------------------------------------------*/
2039 * Assigns own enumerable properties of source object(s) to the destination
2040 * object. Subsequent sources will overwrite property assignments of previous
2041 * sources. If a callback is provided it will be executed to produce the
2042 * assigned values. The callback is bound to `thisArg` and invoked with two
2043 * arguments; (objectValue, sourceValue).
2050 * @param {Object} object The destination object.
2051 * @param {...Object} [source] The source objects.
2052 * @param {Function} [callback] The function to customize assigning values.
2053 * @param {*} [thisArg] The `this` binding of `callback`.
2054 * @returns {Object} Returns the destination object.
2057 * _.assign({ 'name': 'fred' }, { 'employer': 'slate' });
2058 * // => { 'name': 'fred', 'employer': 'slate' }
2060 * var defaults = _.partialRight(_.assign, function(a, b) {
2061 * return typeof a == 'undefined' ? b : a;
2064 * var object = { 'name': 'barney' };
2065 * defaults(object, { 'name': 'fred', 'employer': 'slate' });
2066 * // => { 'name': 'barney', 'employer': 'slate' }
2068 var assign = createIterator(defaultsIteratorOptions, {
2070 defaultsIteratorOptions.top.replace(';',
2072 "if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {\n" +
2073 ' var callback = baseCreateCallback(args[--argsLength - 1], args[argsLength--], 2);\n' +
2074 "} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {\n" +
2075 ' callback = args[--argsLength];\n' +
2078 'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]'
2082 * Creates a clone of `value`. If `isDeep` is `true` nested objects will also
2083 * be cloned, otherwise they will be assigned by reference. If a callback
2084 * is provided it will be executed to produce the cloned values. If the
2085 * callback returns `undefined` cloning will be handled by the method instead.
2086 * The callback is bound to `thisArg` and invoked with one argument; (value).
2091 * @param {*} value The value to clone.
2092 * @param {boolean} [isDeep=false] Specify a deep clone.
2093 * @param {Function} [callback] The function to customize cloning values.
2094 * @param {*} [thisArg] The `this` binding of `callback`.
2095 * @returns {*} Returns the cloned value.
2098 * var characters = [
2099 * { 'name': 'barney', 'age': 36 },
2100 * { 'name': 'fred', 'age': 40 }
2103 * var shallow = _.clone(characters);
2104 * shallow[0] === characters[0];
2107 * var deep = _.clone(characters, true);
2108 * deep[0] === characters[0];
2112 * 'clone': _.partialRight(_.clone, function(value) {
2113 * return _.isElement(value) ? value.cloneNode(false) : undefined;
2117 * var clone = _.clone(document.body);
2118 * clone.childNodes.length;
2121 function clone(value, isDeep, callback, thisArg) {
2122 // allows working with "Collections" methods without using their `index`
2123 // and `collection` arguments for `isDeep` and `callback`
2124 if (typeof isDeep != 'boolean' && isDeep != null) {
2129 return baseClone(value, isDeep, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
2133 * Creates a deep clone of `value`. If a callback is provided it will be
2134 * executed to produce the cloned values. If the callback returns `undefined`
2135 * cloning will be handled by the method instead. The callback is bound to
2136 * `thisArg` and invoked with one argument; (value).
2138 * Note: This method is loosely based on the structured clone algorithm. Functions
2139 * and DOM nodes are **not** cloned. The enumerable properties of `arguments` objects and
2140 * objects created by constructors other than `Object` are cloned to plain `Object` objects.
2141 * See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm.
2146 * @param {*} value The value to deep clone.
2147 * @param {Function} [callback] The function to customize cloning values.
2148 * @param {*} [thisArg] The `this` binding of `callback`.
2149 * @returns {*} Returns the deep cloned value.
2152 * var characters = [
2153 * { 'name': 'barney', 'age': 36 },
2154 * { 'name': 'fred', 'age': 40 }
2157 * var deep = _.cloneDeep(characters);
2158 * deep[0] === characters[0];
2166 * var clone = _.cloneDeep(view, function(value) {
2167 * return _.isElement(value) ? value.cloneNode(true) : undefined;
2170 * clone.node == view.node;
2173 function cloneDeep(value, callback, thisArg) {
2174 return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
2178 * Creates an object that inherits from the given `prototype` object. If a
2179 * `properties` object is provided its own enumerable properties are assigned
2180 * to the created object.
2185 * @param {Object} prototype The object to inherit from.
2186 * @param {Object} [properties] The properties to assign to the object.
2187 * @returns {Object} Returns the new object.
2190 * function Shape() {
2195 * function Circle() {
2199 * Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle });
2201 * var circle = new Circle;
2202 * circle instanceof Circle;
2205 * circle instanceof Shape;
2208 function create(prototype, properties) {
2209 var result = baseCreate(prototype);
2210 return properties ? assign(result, properties) : result;
2214 * Assigns own enumerable properties of source object(s) to the destination
2215 * object for all destination properties that resolve to `undefined`. Once a
2216 * property is set, additional defaults of the same property will be ignored.
2222 * @param {Object} object The destination object.
2223 * @param {...Object} [source] The source objects.
2224 * @param- {Object} [guard] Allows working with `_.reduce` without using its
2225 * `key` and `object` arguments as sources.
2226 * @returns {Object} Returns the destination object.
2229 * var object = { 'name': 'barney' };
2230 * _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
2231 * // => { 'name': 'barney', 'employer': 'slate' }
2233 var defaults = createIterator(defaultsIteratorOptions);
2236 * This method is like `_.findIndex` except that it returns the key of the
2237 * first element that passes the callback check, instead of the element itself.
2239 * If a property name is provided for `callback` the created "_.pluck" style
2240 * callback will return the property value of the given element.
2242 * If an object is provided for `callback` the created "_.where" style callback
2243 * will return `true` for elements that have the properties of the given object,
2249 * @param {Object} object The object to search.
2250 * @param {Function|Object|string} [callback=identity] The function called per
2251 * iteration. If a property name or object is provided it will be used to
2252 * create a "_.pluck" or "_.where" style callback, respectively.
2253 * @param {*} [thisArg] The `this` binding of `callback`.
2254 * @returns {string|undefined} Returns the key of the found element, else `undefined`.
2257 * var characters = {
2258 * 'barney': { 'age': 36, 'blocked': false },
2259 * 'fred': { 'age': 40, 'blocked': true },
2260 * 'pebbles': { 'age': 1, 'blocked': false }
2263 * _.findKey(characters, function(chr) {
2264 * return chr.age < 40;
2266 * // => 'barney' (property order is not guaranteed across environments)
2268 * // using "_.where" callback shorthand
2269 * _.findKey(characters, { 'age': 1 });
2272 * // using "_.pluck" callback shorthand
2273 * _.findKey(characters, 'blocked');
2276 function findKey(object, callback, thisArg) {
2278 callback = lodash.createCallback(callback, thisArg, 3);
2279 forOwn(object, function(value, key, object) {
2280 if (callback(value, key, object)) {
2289 * This method is like `_.findKey` except that it iterates over elements
2290 * of a `collection` in the opposite order.
2292 * If a property name is provided for `callback` the created "_.pluck" style
2293 * callback will return the property value of the given element.
2295 * If an object is provided for `callback` the created "_.where" style callback
2296 * will return `true` for elements that have the properties of the given object,
2302 * @param {Object} object The object to search.
2303 * @param {Function|Object|string} [callback=identity] The function called per
2304 * iteration. If a property name or object is provided it will be used to
2305 * create a "_.pluck" or "_.where" style callback, respectively.
2306 * @param {*} [thisArg] The `this` binding of `callback`.
2307 * @returns {string|undefined} Returns the key of the found element, else `undefined`.
2310 * var characters = {
2311 * 'barney': { 'age': 36, 'blocked': true },
2312 * 'fred': { 'age': 40, 'blocked': false },
2313 * 'pebbles': { 'age': 1, 'blocked': true }
2316 * _.findLastKey(characters, function(chr) {
2317 * return chr.age < 40;
2319 * // => returns `pebbles`, assuming `_.findKey` returns `barney`
2321 * // using "_.where" callback shorthand
2322 * _.findLastKey(characters, { 'age': 40 });
2325 * // using "_.pluck" callback shorthand
2326 * _.findLastKey(characters, 'blocked');
2329 function findLastKey(object, callback, thisArg) {
2331 callback = lodash.createCallback(callback, thisArg, 3);
2332 forOwnRight(object, function(value, key, object) {
2333 if (callback(value, key, object)) {
2342 * Iterates over own and inherited enumerable properties of an object,
2343 * executing the callback for each property. The callback is bound to `thisArg`
2344 * and invoked with three arguments; (value, key, object). Callbacks may exit
2345 * iteration early by explicitly returning `false`.
2351 * @param {Object} object The object to iterate over.
2352 * @param {Function} [callback=identity] The function called per iteration.
2353 * @param {*} [thisArg] The `this` binding of `callback`.
2354 * @returns {Object} Returns `object`.
2357 * function Shape() {
2362 * Shape.prototype.move = function(x, y) {
2367 * _.forIn(new Shape, function(value, key) {
2370 * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments)
2372 var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, {
2377 * This method is like `_.forIn` except that it iterates over elements
2378 * of a `collection` in the opposite order.
2383 * @param {Object} object The object to iterate over.
2384 * @param {Function} [callback=identity] The function called per iteration.
2385 * @param {*} [thisArg] The `this` binding of `callback`.
2386 * @returns {Object} Returns `object`.
2389 * function Shape() {
2394 * Shape.prototype.move = function(x, y) {
2399 * _.forInRight(new Shape, function(value, key) {
2402 * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move'
2404 function forInRight(object, callback, thisArg) {
2407 forIn(object, function(value, key) {
2408 pairs.push(key, value);
2411 var length = pairs.length;
2412 callback = baseCreateCallback(callback, thisArg, 3);
2414 if (callback(pairs[length--], pairs[length], object) === false) {
2422 * Iterates over own enumerable properties of an object, executing the callback
2423 * for each property. The callback is bound to `thisArg` and invoked with three
2424 * arguments; (value, key, object). Callbacks may exit iteration early by
2425 * explicitly returning `false`.
2431 * @param {Object} object The object to iterate over.
2432 * @param {Function} [callback=identity] The function called per iteration.
2433 * @param {*} [thisArg] The `this` binding of `callback`.
2434 * @returns {Object} Returns `object`.
2437 * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
2440 * // => logs '0', '1', and 'length' (property order is not guaranteed across environments)
2442 var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions);
2445 * This method is like `_.forOwn` except that it iterates over elements
2446 * of a `collection` in the opposite order.
2451 * @param {Object} object The object to iterate over.
2452 * @param {Function} [callback=identity] The function called per iteration.
2453 * @param {*} [thisArg] The `this` binding of `callback`.
2454 * @returns {Object} Returns `object`.
2457 * _.forOwnRight({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) {
2460 * // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length'
2462 function forOwnRight(object, callback, thisArg) {
2463 var props = keys(object),
2464 length = props.length;
2466 callback = baseCreateCallback(callback, thisArg, 3);
2468 var key = props[length];
2469 if (callback(object[key], key, object) === false) {
2477 * Creates a sorted array of property names of all enumerable properties,
2478 * own and inherited, of `object` that have function values.
2484 * @param {Object} object The object to inspect.
2485 * @returns {Array} Returns an array of property names that have function values.
2489 * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...]
2491 function functions(object) {
2493 forIn(object, function(value, key) {
2494 if (isFunction(value)) {
2498 return result.sort();
2502 * Checks if the specified property name exists as a direct property of `object`,
2503 * instead of an inherited property.
2508 * @param {Object} object The object to inspect.
2509 * @param {string} key The name of the property to check.
2510 * @returns {boolean} Returns `true` if key is a direct property, else `false`.
2513 * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
2516 function has(object, key) {
2517 return object ? hasOwnProperty.call(object, key) : false;
2521 * Creates an object composed of the inverted keys and values of the given object.
2526 * @param {Object} object The object to invert.
2527 * @returns {Object} Returns the created inverted object.
2530 * _.invert({ 'first': 'fred', 'second': 'barney' });
2531 * // => { 'fred': 'first', 'barney': 'second' }
2533 function invert(object) {
2535 props = keys(object),
2536 length = props.length,
2539 while (++index < length) {
2540 var key = props[index];
2541 result[object[key]] = key;
2547 * Checks if `value` is a boolean value.
2552 * @param {*} value The value to check.
2553 * @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`.
2556 * _.isBoolean(null);
2559 function isBoolean(value) {
2560 return value === true || value === false ||
2561 value && typeof value == 'object' && toString.call(value) == boolClass || false;
2565 * Checks if `value` is a date.
2570 * @param {*} value The value to check.
2571 * @returns {boolean} Returns `true` if the `value` is a date, else `false`.
2574 * _.isDate(new Date);
2577 function isDate(value) {
2578 return value && typeof value == 'object' && toString.call(value) == dateClass || false;
2582 * Checks if `value` is a DOM element.
2587 * @param {*} value The value to check.
2588 * @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`.
2591 * _.isElement(document.body);
2594 function isElement(value) {
2595 return value && value.nodeType === 1 || false;
2599 * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
2600 * length of `0` and objects with no own enumerable properties are considered
2606 * @param {Array|Object|string} value The value to inspect.
2607 * @returns {boolean} Returns `true` if the `value` is empty, else `false`.
2610 * _.isEmpty([1, 2, 3]);
2619 function isEmpty(value) {
2624 var className = toString.call(value),
2625 length = value.length;
2627 if ((className == arrayClass || className == stringClass ||
2628 (support.argsClass ? className == argsClass : isArguments(value))) ||
2629 (className == objectClass && typeof length == 'number' && isFunction(value.splice))) {
2632 forOwn(value, function() {
2633 return (result = false);
2639 * Performs a deep comparison between two values to determine if they are
2640 * equivalent to each other. If a callback is provided it will be executed
2641 * to compare values. If the callback returns `undefined` comparisons will
2642 * be handled by the method instead. The callback is bound to `thisArg` and
2643 * invoked with two arguments; (a, b).
2648 * @param {*} a The value to compare.
2649 * @param {*} b The other value to compare.
2650 * @param {Function} [callback] The function to customize comparing values.
2651 * @param {*} [thisArg] The `this` binding of `callback`.
2652 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
2655 * var object = { 'name': 'fred' };
2656 * var copy = { 'name': 'fred' };
2661 * _.isEqual(object, copy);
2664 * var words = ['hello', 'goodbye'];
2665 * var otherWords = ['hi', 'goodbye'];
2667 * _.isEqual(words, otherWords, function(a, b) {
2668 * var reGreet = /^(?:hello|hi)$/i,
2669 * aGreet = _.isString(a) && reGreet.test(a),
2670 * bGreet = _.isString(b) && reGreet.test(b);
2672 * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined;
2676 function isEqual(a, b, callback, thisArg) {
2677 return baseIsEqual(a, b, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2));
2681 * Checks if `value` is, or can be coerced to, a finite number.
2683 * Note: This is not the same as native `isFinite` which will return true for
2684 * booleans and empty strings. See http://es5.github.io/#x15.1.2.5.
2689 * @param {*} value The value to check.
2690 * @returns {boolean} Returns `true` if the `value` is finite, else `false`.
2705 * _.isFinite(Infinity);
2708 function isFinite(value) {
2709 return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value));
2713 * Checks if `value` is a function.
2718 * @param {*} value The value to check.
2719 * @returns {boolean} Returns `true` if the `value` is a function, else `false`.
2725 function isFunction(value) {
2726 return typeof value == 'function';
2728 // fallback for older versions of Chrome and Safari
2729 if (isFunction(/x/)) {
2730 isFunction = function(value) {
2731 return typeof value == 'function' && toString.call(value) == funcClass;
2736 * Checks if `value` is the language type of Object.
2737 * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
2742 * @param {*} value The value to check.
2743 * @returns {boolean} Returns `true` if the `value` is an object, else `false`.
2749 * _.isObject([1, 2, 3]);
2755 function isObject(value) {
2756 // check if the value is the ECMAScript language type of Object
2757 // http://es5.github.io/#x8
2758 // and avoid a V8 bug
2759 // http://code.google.com/p/v8/issues/detail?id=2291
2760 return !!(value && objectTypes[typeof value]);
2764 * Checks if `value` is `NaN`.
2766 * Note: This is not the same as native `isNaN` which will return `true` for
2767 * `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4.
2772 * @param {*} value The value to check.
2773 * @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`.
2779 * _.isNaN(new Number(NaN));
2785 * _.isNaN(undefined);
2788 function isNaN(value) {
2789 // `NaN` as a primitive is the only value that is not equal to itself
2790 // (perform the [[Class]] check first to avoid errors with some host objects in IE)
2791 return isNumber(value) && value != +value;
2795 * Checks if `value` is `null`.
2800 * @param {*} value The value to check.
2801 * @returns {boolean} Returns `true` if the `value` is `null`, else `false`.
2807 * _.isNull(undefined);
2810 function isNull(value) {
2811 return value === null;
2815 * Checks if `value` is a number.
2817 * Note: `NaN` is considered a number. See http://es5.github.io/#x8.5.
2822 * @param {*} value The value to check.
2823 * @returns {boolean} Returns `true` if the `value` is a number, else `false`.
2826 * _.isNumber(8.4 * 5);
2829 function isNumber(value) {
2830 return typeof value == 'number' ||
2831 value && typeof value == 'object' && toString.call(value) == numberClass || false;
2835 * Checks if `value` is an object created by the `Object` constructor.
2840 * @param {*} value The value to check.
2841 * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
2844 * function Shape() {
2849 * _.isPlainObject(new Shape);
2852 * _.isPlainObject([1, 2, 3]);
2855 * _.isPlainObject({ 'x': 0, 'y': 0 });
2858 var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) {
2859 if (!(value && toString.call(value) == objectClass) || (!support.argsClass && isArguments(value))) {
2862 var valueOf = value.valueOf,
2863 objProto = isNative(valueOf) && (objProto = getPrototypeOf(valueOf)) && getPrototypeOf(objProto);
2866 ? (value == objProto || getPrototypeOf(value) == objProto)
2867 : shimIsPlainObject(value);
2871 * Checks if `value` is a regular expression.
2876 * @param {*} value The value to check.
2877 * @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`.
2880 * _.isRegExp(/fred/);
2883 function isRegExp(value) {
2884 return value && objectTypes[typeof value] && toString.call(value) == regexpClass || false;
2888 * Checks if `value` is a string.
2893 * @param {*} value The value to check.
2894 * @returns {boolean} Returns `true` if the `value` is a string, else `false`.
2897 * _.isString('fred');
2900 function isString(value) {
2901 return typeof value == 'string' ||
2902 value && typeof value == 'object' && toString.call(value) == stringClass || false;
2906 * Checks if `value` is `undefined`.
2911 * @param {*} value The value to check.
2912 * @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`.
2915 * _.isUndefined(void 0);
2918 function isUndefined(value) {
2919 return typeof value == 'undefined';
2923 * Creates an object with the same keys as `object` and values generated by
2924 * running each own enumerable property of `object` through the callback.
2925 * The callback is bound to `thisArg` and invoked with three arguments;
2926 * (value, key, object).
2928 * If a property name is provided for `callback` the created "_.pluck" style
2929 * callback will return the property value of the given element.
2931 * If an object is provided for `callback` the created "_.where" style callback
2932 * will return `true` for elements that have the properties of the given object,
2938 * @param {Object} object The object to iterate over.
2939 * @param {Function|Object|string} [callback=identity] The function called
2940 * per iteration. If a property name or object is provided it will be used
2941 * to create a "_.pluck" or "_.where" style callback, respectively.
2942 * @param {*} [thisArg] The `this` binding of `callback`.
2943 * @returns {Array} Returns a new object with values of the results of each `callback` execution.
2946 * _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; });
2947 * // => { 'a': 3, 'b': 6, 'c': 9 }
2949 * var characters = {
2950 * 'fred': { 'name': 'fred', 'age': 40 },
2951 * 'pebbles': { 'name': 'pebbles', 'age': 1 }
2954 * // using "_.pluck" callback shorthand
2955 * _.mapValues(characters, 'age');
2956 * // => { 'fred': 40, 'pebbles': 1 }
2958 function mapValues(object, callback, thisArg) {
2960 callback = lodash.createCallback(callback, thisArg, 3);
2962 forOwn(object, function(value, key, object) {
2963 result[key] = callback(value, key, object);
2969 * Recursively merges own enumerable properties of the source object(s), that
2970 * don't resolve to `undefined` into the destination object. Subsequent sources
2971 * will overwrite property assignments of previous sources. If a callback is
2972 * provided it will be executed to produce the merged values of the destination
2973 * and source properties. If the callback returns `undefined` merging will
2974 * be handled by the method instead. The callback is bound to `thisArg` and
2975 * invoked with two arguments; (objectValue, sourceValue).
2980 * @param {Object} object The destination object.
2981 * @param {...Object} [source] The source objects.
2982 * @param {Function} [callback] The function to customize merging properties.
2983 * @param {*} [thisArg] The `this` binding of `callback`.
2984 * @returns {Object} Returns the destination object.
2989 * { 'name': 'barney' },
2990 * { 'name': 'fred' }
3001 * _.merge(names, ages);
3002 * // => { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] }
3005 * 'fruits': ['apple'],
3006 * 'vegetables': ['beet']
3010 * 'fruits': ['banana'],
3011 * 'vegetables': ['carrot']
3014 * _.merge(food, otherFood, function(a, b) {
3015 * return _.isArray(a) ? a.concat(b) : undefined;
3017 * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }
3019 function merge(object) {
3020 var args = arguments,
3023 if (!isObject(object)) {
3026 // allows working with `_.reduce` and `_.reduceRight` without using
3027 // their `index` and `collection` arguments
3028 if (typeof args[2] != 'number') {
3029 length = args.length;
3031 if (length > 3 && typeof args[length - 2] == 'function') {
3032 var callback = baseCreateCallback(args[--length - 1], args[length--], 2);
3033 } else if (length > 2 && typeof args[length - 1] == 'function') {
3034 callback = args[--length];
3036 var sources = slice(arguments, 1, length),
3038 stackA = getArray(),
3039 stackB = getArray();
3041 while (++index < length) {
3042 baseMerge(object, sources[index], callback, stackA, stackB);
3044 releaseArray(stackA);
3045 releaseArray(stackB);
3050 * Creates a shallow clone of `object` excluding the specified properties.
3051 * Property names may be specified as individual arguments or as arrays of
3052 * property names. If a callback is provided it will be executed for each
3053 * property of `object` omitting the properties the callback returns truey
3054 * for. The callback is bound to `thisArg` and invoked with three arguments;
3055 * (value, key, object).
3060 * @param {Object} object The source object.
3061 * @param {Function|...string|string[]} [callback] The properties to omit or the
3062 * function called per iteration.
3063 * @param {*} [thisArg] The `this` binding of `callback`.
3064 * @returns {Object} Returns an object without the omitted properties.
3067 * _.omit({ 'name': 'fred', 'age': 40 }, 'age');
3068 * // => { 'name': 'fred' }
3070 * _.omit({ 'name': 'fred', 'age': 40 }, function(value) {
3071 * return typeof value == 'number';
3073 * // => { 'name': 'fred' }
3075 function omit(object, callback, thisArg) {
3077 if (typeof callback != 'function') {
3079 forIn(object, function(value, key) {
3082 props = baseDifference(props, baseFlatten(arguments, true, false, 1));
3085 length = props.length;
3087 while (++index < length) {
3088 var key = props[index];
3089 result[key] = object[key];
3092 callback = lodash.createCallback(callback, thisArg, 3);
3093 forIn(object, function(value, key, object) {
3094 if (!callback(value, key, object)) {
3095 result[key] = value;
3103 * Creates a two dimensional array of an object's key-value pairs,
3104 * i.e. `[[key1, value1], [key2, value2]]`.
3109 * @param {Object} object The object to inspect.
3110 * @returns {Array} Returns new array of key-value pairs.
3113 * _.pairs({ 'barney': 36, 'fred': 40 });
3114 * // => [['barney', 36], ['fred', 40]] (property order is not guaranteed across environments)
3116 function pairs(object) {
3118 props = keys(object),
3119 length = props.length,
3120 result = Array(length);
3122 while (++index < length) {
3123 var key = props[index];
3124 result[index] = [key, object[key]];
3130 * Creates a shallow clone of `object` composed of the specified properties.
3131 * Property names may be specified as individual arguments or as arrays of
3132 * property names. If a callback is provided it will be executed for each
3133 * property of `object` picking the properties the callback returns truey
3134 * for. The callback is bound to `thisArg` and invoked with three arguments;
3135 * (value, key, object).
3140 * @param {Object} object The source object.
3141 * @param {Function|...string|string[]} [callback] The function called per
3142 * iteration or property names to pick, specified as individual property
3143 * names or arrays of property names.
3144 * @param {*} [thisArg] The `this` binding of `callback`.
3145 * @returns {Object} Returns an object composed of the picked properties.
3148 * _.pick({ 'name': 'fred', '_userid': 'fred1' }, 'name');
3149 * // => { 'name': 'fred' }
3151 * _.pick({ 'name': 'fred', '_userid': 'fred1' }, function(value, key) {
3152 * return key.charAt(0) != '_';
3154 * // => { 'name': 'fred' }
3156 function pick(object, callback, thisArg) {
3158 if (typeof callback != 'function') {
3160 props = baseFlatten(arguments, true, false, 1),
3161 length = isObject(object) ? props.length : 0;
3163 while (++index < length) {
3164 var key = props[index];
3165 if (key in object) {
3166 result[key] = object[key];
3170 callback = lodash.createCallback(callback, thisArg, 3);
3171 forIn(object, function(value, key, object) {
3172 if (callback(value, key, object)) {
3173 result[key] = value;
3181 * An alternative to `_.reduce` this method transforms `object` to a new
3182 * `accumulator` object which is the result of running each of its own
3183 * enumerable properties through a callback, with each callback execution
3184 * potentially mutating the `accumulator` object. The callback is bound to
3185 * `thisArg` and invoked with four arguments; (accumulator, value, key, object).
3186 * Callbacks may exit iteration early by explicitly returning `false`.
3191 * @param {Array|Object} object The object to iterate over.
3192 * @param {Function} [callback=identity] The function called per iteration.
3193 * @param {*} [accumulator] The custom accumulator value.
3194 * @param {*} [thisArg] The `this` binding of `callback`.
3195 * @returns {*} Returns the accumulated value.
3198 * var squares = _.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) {
3201 * return result.push(num) < 3;
3206 * var mapped = _.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
3207 * result[key] = num * 3;
3209 * // => { 'a': 3, 'b': 6, 'c': 9 }
3211 function transform(object, callback, accumulator, thisArg) {
3212 var isArr = isArray(object);
3213 if (accumulator == null) {
3217 var ctor = object && object.constructor,
3218 proto = ctor && ctor.prototype;
3220 accumulator = baseCreate(proto);
3224 callback = lodash.createCallback(callback, thisArg, 4);
3225 (isArr ? baseEach : forOwn)(object, function(value, index, object) {
3226 return callback(accumulator, value, index, object);
3233 * Creates an array composed of the own enumerable property values of `object`.
3238 * @param {Object} object The object to inspect.
3239 * @returns {Array} Returns an array of property values.
3242 * _.values({ 'one': 1, 'two': 2, 'three': 3 });
3243 * // => [1, 2, 3] (property order is not guaranteed across environments)
3245 function values(object) {
3247 props = keys(object),
3248 length = props.length,
3249 result = Array(length);
3251 while (++index < length) {
3252 result[index] = object[props[index]];
3257 /*--------------------------------------------------------------------------*/
3260 * Creates an array of elements from the specified indexes, or keys, of the
3261 * `collection`. Indexes may be specified as individual arguments or as arrays
3266 * @category Collections
3267 * @param {Array|Object|string} collection The collection to iterate over.
3268 * @param {...(number|number[]|string|string[])} [index] The indexes of `collection`
3269 * to retrieve, specified as individual indexes or arrays of indexes.
3270 * @returns {Array} Returns a new array of elements corresponding to the
3274 * _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
3275 * // => ['a', 'c', 'e']
3277 * _.at(['fred', 'barney', 'pebbles'], 0, 2);
3278 * // => ['fred', 'pebbles']
3280 function at(collection) {
3281 var args = arguments,
3283 props = baseFlatten(args, true, false, 1),
3284 length = (args[2] && args[2][args[1]] === collection) ? 1 : props.length,
3285 result = Array(length);
3287 if (support.unindexedChars && isString(collection)) {
3288 collection = collection.split('');
3290 while(++index < length) {
3291 result[index] = collection[props[index]];
3297 * Checks if a given value is present in a collection using strict equality
3298 * for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the
3299 * offset from the end of the collection.
3304 * @category Collections
3305 * @param {Array|Object|string} collection The collection to iterate over.
3306 * @param {*} target The value to check for.
3307 * @param {number} [fromIndex=0] The index to search from.
3308 * @returns {boolean} Returns `true` if the `target` element is found, else `false`.
3311 * _.contains([1, 2, 3], 1);
3314 * _.contains([1, 2, 3], 1, 2);
3317 * _.contains({ 'name': 'fred', 'age': 40 }, 'fred');
3320 * _.contains('pebbles', 'eb');
3323 function contains(collection, target, fromIndex) {
3325 indexOf = getIndexOf(),
3326 length = collection ? collection.length : 0,
3329 fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
3330 if (isArray(collection)) {
3331 result = indexOf(collection, target, fromIndex) > -1;
3332 } else if (typeof length == 'number') {
3333 result = (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex)) > -1;
3335 baseEach(collection, function(value) {
3336 if (++index >= fromIndex) {
3337 return !(result = value === target);
3345 * Creates an object composed of keys generated from the results of running
3346 * each element of `collection` through the callback. The corresponding value
3347 * of each key is the number of times the key was returned by the callback.
3348 * The callback is bound to `thisArg` and invoked with three arguments;
3349 * (value, index|key, collection).
3351 * If a property name is provided for `callback` the created "_.pluck" style
3352 * callback will return the property value of the given element.
3354 * If an object is provided for `callback` the created "_.where" style callback
3355 * will return `true` for elements that have the properties of the given object,
3360 * @category Collections
3361 * @param {Array|Object|string} collection The collection to iterate over.
3362 * @param {Function|Object|string} [callback=identity] The function called
3363 * per iteration. If a property name or object is provided it will be used
3364 * to create a "_.pluck" or "_.where" style callback, respectively.
3365 * @param {*} [thisArg] The `this` binding of `callback`.
3366 * @returns {Object} Returns the composed aggregate object.
3369 * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); });
3370 * // => { '4': 1, '6': 2 }
3372 * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
3373 * // => { '4': 1, '6': 2 }
3375 * _.countBy(['one', 'two', 'three'], 'length');
3376 * // => { '3': 2, '5': 1 }
3378 var countBy = createAggregator(function(result, value, key) {
3379 (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1);
3383 * Checks if the given callback returns truey value for **all** elements of
3384 * a collection. The callback is bound to `thisArg` and invoked with three
3385 * arguments; (value, index|key, collection).
3387 * If a property name is provided for `callback` the created "_.pluck" style
3388 * callback will return the property value of the given element.
3390 * If an object is provided for `callback` the created "_.where" style callback
3391 * will return `true` for elements that have the properties of the given object,
3397 * @category Collections
3398 * @param {Array|Object|string} collection The collection to iterate over.
3399 * @param {Function|Object|string} [callback=identity] The function called
3400 * per iteration. If a property name or object is provided it will be used
3401 * to create a "_.pluck" or "_.where" style callback, respectively.
3402 * @param {*} [thisArg] The `this` binding of `callback`.
3403 * @returns {boolean} Returns `true` if all elements passed the callback check,
3407 * _.every([true, 1, null, 'yes']);
3410 * var characters = [
3411 * { 'name': 'barney', 'age': 36 },
3412 * { 'name': 'fred', 'age': 40 }
3415 * // using "_.pluck" callback shorthand
3416 * _.every(characters, 'age');
3419 * // using "_.where" callback shorthand
3420 * _.every(characters, { 'age': 36 });
3423 function every(collection, callback, thisArg) {
3425 callback = lodash.createCallback(callback, thisArg, 3);
3427 if (isArray(collection)) {
3429 length = collection.length;
3431 while (++index < length) {
3432 if (!(result = !!callback(collection[index], index, collection))) {
3437 baseEach(collection, function(value, index, collection) {
3438 return (result = !!callback(value, index, collection));
3445 * Iterates over elements of a collection, returning an array of all elements
3446 * the callback returns truey for. The callback is bound to `thisArg` and
3447 * invoked with three arguments; (value, index|key, collection).
3449 * If a property name is provided for `callback` the created "_.pluck" style
3450 * callback will return the property value of the given element.
3452 * If an object is provided for `callback` the created "_.where" style callback
3453 * will return `true` for elements that have the properties of the given object,
3459 * @category Collections
3460 * @param {Array|Object|string} collection The collection to iterate over.
3461 * @param {Function|Object|string} [callback=identity] The function called
3462 * per iteration. If a property name or object is provided it will be used
3463 * to create a "_.pluck" or "_.where" style callback, respectively.
3464 * @param {*} [thisArg] The `this` binding of `callback`.
3465 * @returns {Array} Returns a new array of elements that passed the callback check.
3468 * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
3471 * var characters = [
3472 * { 'name': 'barney', 'age': 36, 'blocked': false },
3473 * { 'name': 'fred', 'age': 40, 'blocked': true }
3476 * // using "_.pluck" callback shorthand
3477 * _.filter(characters, 'blocked');
3478 * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
3480 * // using "_.where" callback shorthand
3481 * _.filter(characters, { 'age': 36 });
3482 * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
3484 function filter(collection, callback, thisArg) {
3486 callback = lodash.createCallback(callback, thisArg, 3);
3488 if (isArray(collection)) {
3490 length = collection.length;
3492 while (++index < length) {
3493 var value = collection[index];
3494 if (callback(value, index, collection)) {
3499 baseEach(collection, function(value, index, collection) {
3500 if (callback(value, index, collection)) {
3509 * Iterates over elements of a collection, returning the first element that
3510 * the callback returns truey for. The callback is bound to `thisArg` and
3511 * invoked with three arguments; (value, index|key, collection).
3513 * If a property name is provided for `callback` the created "_.pluck" style
3514 * callback will return the property value of the given element.
3516 * If an object is provided for `callback` the created "_.where" style callback
3517 * will return `true` for elements that have the properties of the given object,
3522 * @alias detect, findWhere
3523 * @category Collections
3524 * @param {Array|Object|string} collection The collection to iterate over.
3525 * @param {Function|Object|string} [callback=identity] The function called
3526 * per iteration. If a property name or object is provided it will be used
3527 * to create a "_.pluck" or "_.where" style callback, respectively.
3528 * @param {*} [thisArg] The `this` binding of `callback`.
3529 * @returns {*} Returns the found element, else `undefined`.
3532 * var characters = [
3533 * { 'name': 'barney', 'age': 36, 'blocked': false },
3534 * { 'name': 'fred', 'age': 40, 'blocked': true },
3535 * { 'name': 'pebbles', 'age': 1, 'blocked': false }
3538 * _.find(characters, function(chr) {
3539 * return chr.age < 40;
3541 * // => { 'name': 'barney', 'age': 36, 'blocked': false }
3543 * // using "_.where" callback shorthand
3544 * _.find(characters, { 'age': 1 });
3545 * // => { 'name': 'pebbles', 'age': 1, 'blocked': false }
3547 * // using "_.pluck" callback shorthand
3548 * _.find(characters, 'blocked');
3549 * // => { 'name': 'fred', 'age': 40, 'blocked': true }
3551 function find(collection, callback, thisArg) {
3552 callback = lodash.createCallback(callback, thisArg, 3);
3554 if (isArray(collection)) {
3556 length = collection.length;
3558 while (++index < length) {
3559 var value = collection[index];
3560 if (callback(value, index, collection)) {
3566 baseEach(collection, function(value, index, collection) {
3567 if (callback(value, index, collection)) {
3577 * This method is like `_.find` except that it iterates over elements
3578 * of a `collection` from right to left.
3582 * @category Collections
3583 * @param {Array|Object|string} collection The collection to iterate over.
3584 * @param {Function|Object|string} [callback=identity] The function called
3585 * per iteration. If a property name or object is provided it will be used
3586 * to create a "_.pluck" or "_.where" style callback, respectively.
3587 * @param {*} [thisArg] The `this` binding of `callback`.
3588 * @returns {*} Returns the found element, else `undefined`.
3591 * _.findLast([1, 2, 3, 4], function(num) {
3592 * return num % 2 == 1;
3596 function findLast(collection, callback, thisArg) {
3598 callback = lodash.createCallback(callback, thisArg, 3);
3599 forEachRight(collection, function(value, index, collection) {
3600 if (callback(value, index, collection)) {
3609 * Iterates over elements of a collection, executing the callback for each
3610 * element. The callback is bound to `thisArg` and invoked with three arguments;
3611 * (value, index|key, collection). Callbacks may exit iteration early by
3612 * explicitly returning `false`.
3614 * Note: As with other "Collections" methods, objects with a `length` property
3615 * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn`
3616 * may be used for object iteration.
3621 * @category Collections
3622 * @param {Array|Object|string} collection The collection to iterate over.
3623 * @param {Function} [callback=identity] The function called per iteration.
3624 * @param {*} [thisArg] The `this` binding of `callback`.
3625 * @returns {Array|Object|string} Returns `collection`.
3628 * _([1, 2, 3]).forEach(function(num) { console.log(num); }).join(',');
3629 * // => logs each number and returns '1,2,3'
3631 * _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); });
3632 * // => logs each number and returns the object (property order is not guaranteed across environments)
3634 function forEach(collection, callback, thisArg) {
3635 if (callback && typeof thisArg == 'undefined' && isArray(collection)) {
3637 length = collection.length;
3639 while (++index < length) {
3640 if (callback(collection[index], index, collection) === false) {
3645 baseEach(collection, callback, thisArg);
3651 * This method is like `_.forEach` except that it iterates over elements
3652 * of a `collection` from right to left.
3657 * @category Collections
3658 * @param {Array|Object|string} collection The collection to iterate over.
3659 * @param {Function} [callback=identity] The function called per iteration.
3660 * @param {*} [thisArg] The `this` binding of `callback`.
3661 * @returns {Array|Object|string} Returns `collection`.
3664 * _([1, 2, 3]).forEachRight(function(num) { console.log(num); }).join(',');
3665 * // => logs each number from right to left and returns '3,2,1'
3667 function forEachRight(collection, callback, thisArg) {
3668 var iterable = collection,
3669 length = collection ? collection.length : 0;
3671 callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
3672 if (isArray(collection)) {
3674 if (callback(collection[length], length, collection) === false) {
3679 if (typeof length != 'number') {
3680 var props = keys(collection);
3681 length = props.length;
3682 } else if (support.unindexedChars && isString(collection)) {
3683 iterable = collection.split('');
3685 baseEach(collection, function(value, key, collection) {
3686 key = props ? props[--length] : --length;
3687 return callback(iterable[key], key, collection);
3694 * Creates an object composed of keys generated from the results of running
3695 * each element of a collection through the callback. The corresponding value
3696 * of each key is an array of the elements responsible for generating the key.
3697 * The callback is bound to `thisArg` and invoked with three arguments;
3698 * (value, index|key, collection).
3700 * If a property name is provided for `callback` the created "_.pluck" style
3701 * callback will return the property value of the given element.
3703 * If an object is provided for `callback` the created "_.where" style callback
3704 * will return `true` for elements that have the properties of the given object,
3709 * @category Collections
3710 * @param {Array|Object|string} collection The collection to iterate over.
3711 * @param {Function|Object|string} [callback=identity] The function called
3712 * per iteration. If a property name or object is provided it will be used
3713 * to create a "_.pluck" or "_.where" style callback, respectively.
3714 * @param {*} [thisArg] The `this` binding of `callback`.
3715 * @returns {Object} Returns the composed aggregate object.
3718 * _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); });
3719 * // => { '4': [4.2], '6': [6.1, 6.4] }
3721 * _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math);
3722 * // => { '4': [4.2], '6': [6.1, 6.4] }
3724 * // using "_.pluck" callback shorthand
3725 * _.groupBy(['one', 'two', 'three'], 'length');
3726 * // => { '3': ['one', 'two'], '5': ['three'] }
3728 var groupBy = createAggregator(function(result, value, key) {
3729 (hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value);
3733 * Creates an object composed of keys generated from the results of running
3734 * each element of the collection through the given callback. The corresponding
3735 * value of each key is the last element responsible for generating the key.
3736 * The callback is bound to `thisArg` and invoked with three arguments;
3737 * (value, index|key, collection).
3739 * If a property name is provided for `callback` the created "_.pluck" style
3740 * callback will return the property value of the given element.
3742 * If an object is provided for `callback` the created "_.where" style callback
3743 * will return `true` for elements that have the properties of the given object,
3748 * @category Collections
3749 * @param {Array|Object|string} collection The collection to iterate over.
3750 * @param {Function|Object|string} [callback=identity] The function called
3751 * per iteration. If a property name or object is provided it will be used
3752 * to create a "_.pluck" or "_.where" style callback, respectively.
3753 * @param {*} [thisArg] The `this` binding of `callback`.
3754 * @returns {Object} Returns the composed aggregate object.
3758 * { 'dir': 'left', 'code': 97 },
3759 * { 'dir': 'right', 'code': 100 }
3762 * _.indexBy(keys, 'dir');
3763 * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
3765 * _.indexBy(keys, function(key) { return String.fromCharCode(key.code); });
3766 * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
3768 * _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String);
3769 * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
3771 var indexBy = createAggregator(function(result, value, key) {
3772 result[key] = value;
3776 * Invokes the method named by `methodName` on each element in the `collection`
3777 * returning an array of the results of each invoked method. Additional arguments
3778 * will be provided to each invoked method. If `methodName` is a function it
3779 * will be invoked for, and `this` bound to, each element in the `collection`.
3783 * @category Collections
3784 * @param {Array|Object|string} collection The collection to iterate over.
3785 * @param {Function|string} methodName The name of the method to invoke or
3786 * the function invoked per iteration.
3787 * @param {...*} [arg] Arguments to invoke the method with.
3788 * @returns {Array} Returns a new array of the results of each invoked method.
3791 * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
3792 * // => [[1, 5, 7], [1, 2, 3]]
3794 * _.invoke([123, 456], String.prototype.split, '');
3795 * // => [['1', '2', '3'], ['4', '5', '6']]
3797 function invoke(collection, methodName) {
3798 var args = slice(arguments, 2),
3800 isFunc = typeof methodName == 'function',
3801 length = collection ? collection.length : 0,
3802 result = Array(typeof length == 'number' ? length : 0);
3804 forEach(collection, function(value) {
3805 result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
3811 * Creates an array of values by running each element in the collection
3812 * through the callback. The callback is bound to `thisArg` and invoked with
3813 * three arguments; (value, index|key, collection).
3815 * If a property name is provided for `callback` the created "_.pluck" style
3816 * callback will return the property value of the given element.
3818 * If an object is provided for `callback` the created "_.where" style callback
3819 * will return `true` for elements that have the properties of the given object,
3825 * @category Collections
3826 * @param {Array|Object|string} collection The collection to iterate over.
3827 * @param {Function|Object|string} [callback=identity] The function called
3828 * per iteration. If a property name or object is provided it will be used
3829 * to create a "_.pluck" or "_.where" style callback, respectively.
3830 * @param {*} [thisArg] The `this` binding of `callback`.
3831 * @returns {Array} Returns a new array of the results of each `callback` execution.
3834 * _.map([1, 2, 3], function(num) { return num * 3; });
3837 * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; });
3838 * // => [3, 6, 9] (property order is not guaranteed across environments)
3840 * var characters = [
3841 * { 'name': 'barney', 'age': 36 },
3842 * { 'name': 'fred', 'age': 40 }
3845 * // using "_.pluck" callback shorthand
3846 * _.map(characters, 'name');
3847 * // => ['barney', 'fred']
3849 function map(collection, callback, thisArg) {
3851 length = collection ? collection.length : 0,
3852 result = Array(typeof length == 'number' ? length : 0);
3854 callback = lodash.createCallback(callback, thisArg, 3);
3855 if (isArray(collection)) {
3856 while (++index < length) {
3857 result[index] = callback(collection[index], index, collection);
3860 baseEach(collection, function(value, key, collection) {
3861 result[++index] = callback(value, key, collection);
3868 * Retrieves the maximum value of a collection. If the collection is empty or
3869 * falsey `-Infinity` is returned. If a callback is provided it will be executed
3870 * for each value in the collection to generate the criterion by which the value
3871 * is ranked. The callback is bound to `thisArg` and invoked with three
3872 * arguments; (value, index, collection).
3874 * If a property name is provided for `callback` the created "_.pluck" style
3875 * callback will return the property value of the given element.
3877 * If an object is provided for `callback` the created "_.where" style callback
3878 * will return `true` for elements that have the properties of the given object,
3883 * @category Collections
3884 * @param {Array|Object|string} collection The collection to iterate over.
3885 * @param {Function|Object|string} [callback=identity] The function called
3886 * per iteration. If a property name or object is provided it will be used
3887 * to create a "_.pluck" or "_.where" style callback, respectively.
3888 * @param {*} [thisArg] The `this` binding of `callback`.
3889 * @returns {*} Returns the maximum value.
3892 * _.max([4, 2, 8, 6]);
3895 * var characters = [
3896 * { 'name': 'barney', 'age': 36 },
3897 * { 'name': 'fred', 'age': 40 }
3900 * _.max(characters, function(chr) { return chr.age; });
3901 * // => { 'name': 'fred', 'age': 40 };
3903 * // using "_.pluck" callback shorthand
3904 * _.max(characters, 'age');
3905 * // => { 'name': 'fred', 'age': 40 };
3907 function max(collection, callback, thisArg) {
3908 var computed = -Infinity,
3911 // allows working with functions like `_.map` without using
3912 // their `index` argument as a callback
3913 if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
3916 if (callback == null && isArray(collection)) {
3918 length = collection.length;
3920 while (++index < length) {
3921 var value = collection[index];
3922 if (value > result) {
3927 callback = (callback == null && isString(collection))
3929 : lodash.createCallback(callback, thisArg, 3);
3931 baseEach(collection, function(value, index, collection) {
3932 var current = callback(value, index, collection);
3933 if (current > computed) {
3943 * Retrieves the minimum value of a collection. If the collection is empty or
3944 * falsey `Infinity` is returned. If a callback is provided it will be executed
3945 * for each value in the collection to generate the criterion by which the value
3946 * is ranked. The callback is bound to `thisArg` and invoked with three
3947 * arguments; (value, index, collection).
3949 * If a property name is provided for `callback` the created "_.pluck" style
3950 * callback will return the property value of the given element.
3952 * If an object is provided for `callback` the created "_.where" style callback
3953 * will return `true` for elements that have the properties of the given object,
3958 * @category Collections
3959 * @param {Array|Object|string} collection The collection to iterate over.
3960 * @param {Function|Object|string} [callback=identity] The function called
3961 * per iteration. If a property name or object is provided it will be used
3962 * to create a "_.pluck" or "_.where" style callback, respectively.
3963 * @param {*} [thisArg] The `this` binding of `callback`.
3964 * @returns {*} Returns the minimum value.
3967 * _.min([4, 2, 8, 6]);
3970 * var characters = [
3971 * { 'name': 'barney', 'age': 36 },
3972 * { 'name': 'fred', 'age': 40 }
3975 * _.min(characters, function(chr) { return chr.age; });
3976 * // => { 'name': 'barney', 'age': 36 };
3978 * // using "_.pluck" callback shorthand
3979 * _.min(characters, 'age');
3980 * // => { 'name': 'barney', 'age': 36 };
3982 function min(collection, callback, thisArg) {
3983 var computed = Infinity,
3986 // allows working with functions like `_.map` without using
3987 // their `index` argument as a callback
3988 if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) {
3991 if (callback == null && isArray(collection)) {
3993 length = collection.length;
3995 while (++index < length) {
3996 var value = collection[index];
3997 if (value < result) {
4002 callback = (callback == null && isString(collection))
4004 : lodash.createCallback(callback, thisArg, 3);
4006 baseEach(collection, function(value, index, collection) {
4007 var current = callback(value, index, collection);
4008 if (current < computed) {
4018 * Retrieves the value of a specified property from all elements in the collection.
4023 * @category Collections
4024 * @param {Array|Object|string} collection The collection to iterate over.
4025 * @param {string} property The name of the property to pluck.
4026 * @returns {Array} Returns a new array of property values.
4029 * var characters = [
4030 * { 'name': 'barney', 'age': 36 },
4031 * { 'name': 'fred', 'age': 40 }
4034 * _.pluck(characters, 'name');
4035 * // => ['barney', 'fred']
4040 * Reduces a collection to a value which is the accumulated result of running
4041 * each element in the collection through the callback, where each successive
4042 * callback execution consumes the return value of the previous execution. If
4043 * `accumulator` is not provided the first element of the collection will be
4044 * used as the initial `accumulator` value. The callback is bound to `thisArg`
4045 * and invoked with four arguments; (accumulator, value, index|key, collection).
4049 * @alias foldl, inject
4050 * @category Collections
4051 * @param {Array|Object|string} collection The collection to iterate over.
4052 * @param {Function} [callback=identity] The function called per iteration.
4053 * @param {*} [accumulator] Initial value of the accumulator.
4054 * @param {*} [thisArg] The `this` binding of `callback`.
4055 * @returns {*} Returns the accumulated value.
4058 * var sum = _.reduce([1, 2, 3], function(sum, num) {
4063 * var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) {
4064 * result[key] = num * 3;
4067 * // => { 'a': 3, 'b': 6, 'c': 9 }
4069 function reduce(collection, callback, accumulator, thisArg) {
4070 var noaccum = arguments.length < 3;
4071 callback = lodash.createCallback(callback, thisArg, 4);
4073 if (isArray(collection)) {
4075 length = collection.length;
4078 accumulator = collection[++index];
4080 while (++index < length) {
4081 accumulator = callback(accumulator, collection[index], index, collection);
4084 baseEach(collection, function(value, index, collection) {
4085 accumulator = noaccum
4086 ? (noaccum = false, value)
4087 : callback(accumulator, value, index, collection)
4094 * This method is like `_.reduce` except that it iterates over elements
4095 * of a `collection` from right to left.
4100 * @category Collections
4101 * @param {Array|Object|string} collection The collection to iterate over.
4102 * @param {Function} [callback=identity] The function called per iteration.
4103 * @param {*} [accumulator] Initial value of the accumulator.
4104 * @param {*} [thisArg] The `this` binding of `callback`.
4105 * @returns {*} Returns the accumulated value.
4108 * var list = [[0, 1], [2, 3], [4, 5]];
4109 * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
4110 * // => [4, 5, 2, 3, 0, 1]
4112 function reduceRight(collection, callback, accumulator, thisArg) {
4113 var noaccum = arguments.length < 3;
4114 callback = lodash.createCallback(callback, thisArg, 4);
4115 forEachRight(collection, function(value, index, collection) {
4116 accumulator = noaccum
4117 ? (noaccum = false, value)
4118 : callback(accumulator, value, index, collection);
4124 * The opposite of `_.filter` this method returns the elements of a
4125 * collection that the callback does **not** return truey for.
4127 * If a property name is provided for `callback` the created "_.pluck" style
4128 * callback will return the property value of the given element.
4130 * If an object is provided for `callback` the created "_.where" style callback
4131 * will return `true` for elements that have the properties of the given object,
4136 * @category Collections
4137 * @param {Array|Object|string} collection The collection to iterate over.
4138 * @param {Function|Object|string} [callback=identity] The function called
4139 * per iteration. If a property name or object is provided it will be used
4140 * to create a "_.pluck" or "_.where" style callback, respectively.
4141 * @param {*} [thisArg] The `this` binding of `callback`.
4142 * @returns {Array} Returns a new array of elements that failed the callback check.
4145 * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
4148 * var characters = [
4149 * { 'name': 'barney', 'age': 36, 'blocked': false },
4150 * { 'name': 'fred', 'age': 40, 'blocked': true }
4153 * // using "_.pluck" callback shorthand
4154 * _.reject(characters, 'blocked');
4155 * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }]
4157 * // using "_.where" callback shorthand
4158 * _.reject(characters, { 'age': 36 });
4159 * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
4161 function reject(collection, callback, thisArg) {
4162 callback = lodash.createCallback(callback, thisArg, 3);
4163 return filter(collection, function(value, index, collection) {
4164 return !callback(value, index, collection);
4169 * Retrieves a random element or `n` random elements from a collection.
4173 * @category Collections
4174 * @param {Array|Object|string} collection The collection to sample.
4175 * @param {number} [n] The number of elements to sample.
4176 * @param- {Object} [guard] Allows working with functions like `_.map`
4177 * without using their `index` arguments as `n`.
4178 * @returns {Array} Returns the random sample(s) of `collection`.
4181 * _.sample([1, 2, 3, 4]);
4184 * _.sample([1, 2, 3, 4], 2);
4187 function sample(collection, n, guard) {
4188 if (collection && typeof collection.length != 'number') {
4189 collection = values(collection);
4190 } else if (support.unindexedChars && isString(collection)) {
4191 collection = collection.split('');
4193 if (n == null || guard) {
4194 return collection ? collection[baseRandom(0, collection.length - 1)] : undefined;
4196 var result = shuffle(collection);
4197 result.length = nativeMin(nativeMax(0, n), result.length);
4202 * Creates an array of shuffled values, using a version of the Fisher-Yates
4203 * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle.
4207 * @category Collections
4208 * @param {Array|Object|string} collection The collection to shuffle.
4209 * @returns {Array} Returns a new shuffled collection.
4212 * _.shuffle([1, 2, 3, 4, 5, 6]);
4213 * // => [4, 1, 6, 3, 5, 2]
4215 function shuffle(collection) {
4217 length = collection ? collection.length : 0,
4218 result = Array(typeof length == 'number' ? length : 0);
4220 forEach(collection, function(value) {
4221 var rand = baseRandom(0, ++index);
4222 result[index] = result[rand];
4223 result[rand] = value;
4229 * Gets the size of the `collection` by returning `collection.length` for arrays
4230 * and array-like objects or the number of own enumerable properties for objects.
4234 * @category Collections
4235 * @param {Array|Object|string} collection The collection to inspect.
4236 * @returns {number} Returns `collection.length` or number of own enumerable properties.
4242 * _.size({ 'one': 1, 'two': 2, 'three': 3 });
4245 * _.size('pebbles');
4248 function size(collection) {
4249 var length = collection ? collection.length : 0;
4250 return typeof length == 'number' ? length : keys(collection).length;
4254 * Checks if the callback returns a truey value for **any** element of a
4255 * collection. The function returns as soon as it finds a passing value and
4256 * does not iterate over the entire collection. The callback is bound to
4257 * `thisArg` and invoked with three arguments; (value, index|key, collection).
4259 * If a property name is provided for `callback` the created "_.pluck" style
4260 * callback will return the property value of the given element.
4262 * If an object is provided for `callback` the created "_.where" style callback
4263 * will return `true` for elements that have the properties of the given object,
4269 * @category Collections
4270 * @param {Array|Object|string} collection The collection to iterate over.
4271 * @param {Function|Object|string} [callback=identity] The function called
4272 * per iteration. If a property name or object is provided it will be used
4273 * to create a "_.pluck" or "_.where" style callback, respectively.
4274 * @param {*} [thisArg] The `this` binding of `callback`.
4275 * @returns {boolean} Returns `true` if any element passed the callback check,
4279 * _.some([null, 0, 'yes', false], Boolean);
4282 * var characters = [
4283 * { 'name': 'barney', 'age': 36, 'blocked': false },
4284 * { 'name': 'fred', 'age': 40, 'blocked': true }
4287 * // using "_.pluck" callback shorthand
4288 * _.some(characters, 'blocked');
4291 * // using "_.where" callback shorthand
4292 * _.some(characters, { 'age': 1 });
4295 function some(collection, callback, thisArg) {
4297 callback = lodash.createCallback(callback, thisArg, 3);
4299 if (isArray(collection)) {
4301 length = collection.length;
4303 while (++index < length) {
4304 if ((result = callback(collection[index], index, collection))) {
4309 baseEach(collection, function(value, index, collection) {
4310 return !(result = callback(value, index, collection));
4317 * Creates an array of elements, sorted in ascending order by the results of
4318 * running each element in a collection through the callback. This method
4319 * performs a stable sort, that is, it will preserve the original sort order
4320 * of equal elements. The callback is bound to `thisArg` and invoked with
4321 * three arguments; (value, index|key, collection).
4323 * If a property name is provided for `callback` the created "_.pluck" style
4324 * callback will return the property value of the given element.
4326 * If an array of property names is provided for `callback` the collection
4327 * will be sorted by each property value.
4329 * If an object is provided for `callback` the created "_.where" style callback
4330 * will return `true` for elements that have the properties of the given object,
4335 * @category Collections
4336 * @param {Array|Object|string} collection The collection to iterate over.
4337 * @param {Array|Function|Object|string} [callback=identity] The function called
4338 * per iteration. If a property name or object is provided it will be used
4339 * to create a "_.pluck" or "_.where" style callback, respectively.
4340 * @param {*} [thisArg] The `this` binding of `callback`.
4341 * @returns {Array} Returns a new array of sorted elements.
4344 * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); });
4347 * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
4350 * var characters = [
4351 * { 'name': 'barney', 'age': 36 },
4352 * { 'name': 'fred', 'age': 40 },
4353 * { 'name': 'barney', 'age': 26 },
4354 * { 'name': 'fred', 'age': 30 }
4357 * // using "_.pluck" callback shorthand
4358 * _.map(_.sortBy(characters, 'age'), _.values);
4359 * // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
4361 * // sorting by multiple properties
4362 * _.map(_.sortBy(characters, ['name', 'age']), _.values);
4363 * // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
4365 function sortBy(collection, callback, thisArg) {
4367 isArr = isArray(callback),
4368 length = collection ? collection.length : 0,
4369 result = Array(typeof length == 'number' ? length : 0);
4372 callback = lodash.createCallback(callback, thisArg, 3);
4374 forEach(collection, function(value, key, collection) {
4375 var object = result[++index] = getObject();
4377 object.criteria = map(callback, function(key) { return value[key]; });
4379 (object.criteria = getArray())[0] = callback(value, key, collection);
4381 object.index = index;
4382 object.value = value;
4385 length = result.length;
4386 result.sort(compareAscending);
4388 var object = result[length];
4389 result[length] = object.value;
4391 releaseArray(object.criteria);
4393 releaseObject(object);
4399 * Converts the `collection` to an array.
4403 * @category Collections
4404 * @param {Array|Object|string} collection The collection to convert.
4405 * @returns {Array} Returns the new converted array.
4408 * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4);
4411 function toArray(collection) {
4412 if (collection && typeof collection.length == 'number') {
4413 return (support.unindexedChars && isString(collection))
4414 ? collection.split('')
4415 : slice(collection);
4417 return values(collection);
4421 * Performs a deep comparison of each element in a `collection` to the given
4422 * `properties` object, returning an array of all elements that have equivalent
4428 * @category Collections
4429 * @param {Array|Object|string} collection The collection to iterate over.
4430 * @param {Object} props The object of property values to filter by.
4431 * @returns {Array} Returns a new array of elements that have the given properties.
4434 * var characters = [
4435 * { 'name': 'barney', 'age': 36, 'pets': ['hoppy'] },
4436 * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
4439 * _.where(characters, { 'age': 36 });
4440 * // => [{ 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }]
4442 * _.where(characters, { 'pets': ['dino'] });
4443 * // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
4447 /*--------------------------------------------------------------------------*/
4450 * Creates an array with all falsey values removed. The values `false`, `null`,
4451 * `0`, `""`, `undefined`, and `NaN` are all falsey.
4456 * @param {Array} array The array to compact.
4457 * @returns {Array} Returns a new array of filtered values.
4460 * _.compact([0, 1, false, 2, '', 3]);
4463 function compact(array) {
4465 length = array ? array.length : 0,
4468 while (++index < length) {
4469 var value = array[index];
4478 * Creates an array excluding all values of the provided arrays using strict
4479 * equality for comparisons, i.e. `===`.
4484 * @param {Array} array The array to process.
4485 * @param {...Array} [values] The arrays of values to exclude.
4486 * @returns {Array} Returns a new array of filtered values.
4489 * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
4492 function difference(array) {
4493 return baseDifference(array, baseFlatten(arguments, true, true, 1));
4497 * This method is like `_.find` except that it returns the index of the first
4498 * element that passes the callback check, instead of the element itself.
4500 * If a property name is provided for `callback` the created "_.pluck" style
4501 * callback will return the property value of the given element.
4503 * If an object is provided for `callback` the created "_.where" style callback
4504 * will return `true` for elements that have the properties of the given object,
4510 * @param {Array} array The array to search.
4511 * @param {Function|Object|string} [callback=identity] The function called
4512 * per iteration. If a property name or object is provided it will be used
4513 * to create a "_.pluck" or "_.where" style callback, respectively.
4514 * @param {*} [thisArg] The `this` binding of `callback`.
4515 * @returns {number} Returns the index of the found element, else `-1`.
4518 * var characters = [
4519 * { 'name': 'barney', 'age': 36, 'blocked': false },
4520 * { 'name': 'fred', 'age': 40, 'blocked': true },
4521 * { 'name': 'pebbles', 'age': 1, 'blocked': false }
4524 * _.findIndex(characters, function(chr) {
4525 * return chr.age < 20;
4529 * // using "_.where" callback shorthand
4530 * _.findIndex(characters, { 'age': 36 });
4533 * // using "_.pluck" callback shorthand
4534 * _.findIndex(characters, 'blocked');
4537 function findIndex(array, callback, thisArg) {
4539 length = array ? array.length : 0;
4541 callback = lodash.createCallback(callback, thisArg, 3);
4542 while (++index < length) {
4543 if (callback(array[index], index, array)) {
4551 * This method is like `_.findIndex` except that it iterates over elements
4552 * of a `collection` from right to left.
4554 * If a property name is provided for `callback` the created "_.pluck" style
4555 * callback will return the property value of the given element.
4557 * If an object is provided for `callback` the created "_.where" style callback
4558 * will return `true` for elements that have the properties of the given object,
4564 * @param {Array} array The array to search.
4565 * @param {Function|Object|string} [callback=identity] The function called
4566 * per iteration. If a property name or object is provided it will be used
4567 * to create a "_.pluck" or "_.where" style callback, respectively.
4568 * @param {*} [thisArg] The `this` binding of `callback`.
4569 * @returns {number} Returns the index of the found element, else `-1`.
4572 * var characters = [
4573 * { 'name': 'barney', 'age': 36, 'blocked': true },
4574 * { 'name': 'fred', 'age': 40, 'blocked': false },
4575 * { 'name': 'pebbles', 'age': 1, 'blocked': true }
4578 * _.findLastIndex(characters, function(chr) {
4579 * return chr.age > 30;
4583 * // using "_.where" callback shorthand
4584 * _.findLastIndex(characters, { 'age': 36 });
4587 * // using "_.pluck" callback shorthand
4588 * _.findLastIndex(characters, 'blocked');
4591 function findLastIndex(array, callback, thisArg) {
4592 var length = array ? array.length : 0;
4593 callback = lodash.createCallback(callback, thisArg, 3);
4595 if (callback(array[length], length, array)) {
4603 * Gets the first element or first `n` elements of an array. If a callback
4604 * is provided elements at the beginning of the array are returned as long
4605 * as the callback returns truey. The callback is bound to `thisArg` and
4606 * invoked with three arguments; (value, index, array).
4608 * If a property name is provided for `callback` the created "_.pluck" style
4609 * callback will return the property value of the given element.
4611 * If an object is provided for `callback` the created "_.where" style callback
4612 * will return `true` for elements that have the properties of the given object,
4619 * @param {Array} array The array to query.
4620 * @param {Function|Object|number|string} [callback] The function called
4621 * per element or the number of elements to return. If a property name or
4622 * object is provided it will be used to create a "_.pluck" or "_.where"
4623 * style callback, respectively.
4624 * @param {*} [thisArg] The `this` binding of `callback`.
4625 * @returns {*} Returns the first element(s) of `array`.
4628 * _.first([1, 2, 3]);
4631 * _.first([1, 2, 3], 2);
4634 * _.first([1, 2, 3], function(num) {
4639 * var characters = [
4640 * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
4641 * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
4642 * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
4645 * // using "_.pluck" callback shorthand
4646 * _.first(characters, 'blocked');
4647 * // => [{ 'name': 'barney', 'blocked': true, 'employer': 'slate' }]
4649 * // using "_.where" callback shorthand
4650 * _.pluck(_.first(characters, { 'employer': 'slate' }), 'name');
4651 * // => ['barney', 'fred']
4653 function first(array, callback, thisArg) {
4655 length = array ? array.length : 0;
4657 if (typeof callback != 'number' && callback != null) {
4659 callback = lodash.createCallback(callback, thisArg, 3);
4660 while (++index < length && callback(array[index], index, array)) {
4665 if (n == null || thisArg) {
4666 return array ? array[0] : undefined;
4669 return slice(array, 0, nativeMin(nativeMax(0, n), length));
4673 * Flattens a nested array (the nesting can be to any depth). If `isShallow`
4674 * is truey, the array will only be flattened a single level. If a callback
4675 * is provided each element of the array is passed through the callback before
4676 * flattening. The callback is bound to `thisArg` and invoked with three
4677 * arguments; (value, index, array).
4679 * If a property name is provided for `callback` the created "_.pluck" style
4680 * callback will return the property value of the given element.
4682 * If an object is provided for `callback` the created "_.where" style callback
4683 * will return `true` for elements that have the properties of the given object,
4689 * @param {Array} array The array to flatten.
4690 * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level.
4691 * @param {Function|Object|string} [callback=identity] The function called
4692 * per iteration. If a property name or object is provided it will be used
4693 * to create a "_.pluck" or "_.where" style callback, respectively.
4694 * @param {*} [thisArg] The `this` binding of `callback`.
4695 * @returns {Array} Returns a new flattened array.
4698 * _.flatten([1, [2], [3, [[4]]]]);
4699 * // => [1, 2, 3, 4];
4701 * _.flatten([1, [2], [3, [[4]]]], true);
4702 * // => [1, 2, 3, [[4]]];
4704 * var characters = [
4705 * { 'name': 'barney', 'age': 30, 'pets': ['hoppy'] },
4706 * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }
4709 * // using "_.pluck" callback shorthand
4710 * _.flatten(characters, 'pets');
4711 * // => ['hoppy', 'baby puss', 'dino']
4713 function flatten(array, isShallow, callback, thisArg) {
4715 if (typeof isShallow != 'boolean' && isShallow != null) {
4717 callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow;
4720 if (callback != null) {
4721 array = map(array, callback, thisArg);
4723 return baseFlatten(array, isShallow);
4727 * Gets the index at which the first occurrence of `value` is found using
4728 * strict equality for comparisons, i.e. `===`. If the array is already sorted
4729 * providing `true` for `fromIndex` will run a faster binary search.
4734 * @param {Array} array The array to search.
4735 * @param {*} value The value to search for.
4736 * @param {boolean|number} [fromIndex=0] The index to search from or `true`
4737 * to perform a binary search on a sorted array.
4738 * @returns {number} Returns the index of the matched value or `-1`.
4741 * _.indexOf([1, 2, 3, 1, 2, 3], 2);
4744 * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
4747 * _.indexOf([1, 1, 2, 2, 3, 3], 2, true);
4750 function indexOf(array, value, fromIndex) {
4751 if (typeof fromIndex == 'number') {
4752 var length = array ? array.length : 0;
4753 fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0);
4754 } else if (fromIndex) {
4755 var index = sortedIndex(array, value);
4756 return array[index] === value ? index : -1;
4758 return baseIndexOf(array, value, fromIndex);
4762 * Gets all but the last element or last `n` elements of an array. If a
4763 * callback is provided elements at the end of the array are excluded from
4764 * the result as long as the callback returns truey. The callback is bound
4765 * to `thisArg` and invoked with three arguments; (value, index, array).
4767 * If a property name is provided for `callback` the created "_.pluck" style
4768 * callback will return the property value of the given element.
4770 * If an object is provided for `callback` the created "_.where" style callback
4771 * will return `true` for elements that have the properties of the given object,
4777 * @param {Array} array The array to query.
4778 * @param {Function|Object|number|string} [callback=1] The function called
4779 * per element or the number of elements to exclude. If a property name or
4780 * object is provided it will be used to create a "_.pluck" or "_.where"
4781 * style callback, respectively.
4782 * @param {*} [thisArg] The `this` binding of `callback`.
4783 * @returns {Array} Returns a slice of `array`.
4786 * _.initial([1, 2, 3]);
4789 * _.initial([1, 2, 3], 2);
4792 * _.initial([1, 2, 3], function(num) {
4797 * var characters = [
4798 * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
4799 * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
4800 * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
4803 * // using "_.pluck" callback shorthand
4804 * _.initial(characters, 'blocked');
4805 * // => [{ 'name': 'barney', 'blocked': false, 'employer': 'slate' }]
4807 * // using "_.where" callback shorthand
4808 * _.pluck(_.initial(characters, { 'employer': 'na' }), 'name');
4809 * // => ['barney', 'fred']
4811 function initial(array, callback, thisArg) {
4813 length = array ? array.length : 0;
4815 if (typeof callback != 'number' && callback != null) {
4817 callback = lodash.createCallback(callback, thisArg, 3);
4818 while (index-- && callback(array[index], index, array)) {
4822 n = (callback == null || thisArg) ? 1 : callback || n;
4824 return slice(array, 0, nativeMin(nativeMax(0, length - n), length));
4828 * Creates an array of unique values present in all provided arrays using
4829 * strict equality for comparisons, i.e. `===`.
4834 * @param {...Array} [array] The arrays to inspect.
4835 * @returns {Array} Returns an array of shared values.
4838 * _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
4841 function intersection() {
4844 argsLength = arguments.length,
4845 caches = getArray(),
4846 indexOf = getIndexOf(),
4847 trustIndexOf = indexOf === baseIndexOf,
4850 while (++argsIndex < argsLength) {
4851 var value = arguments[argsIndex];
4852 if (isArray(value) || isArguments(value)) {
4854 caches.push(trustIndexOf && value.length >= largeArraySize &&
4855 createCache(argsIndex ? args[argsIndex] : seen));
4858 var array = args[0],
4860 length = array ? array.length : 0,
4864 while (++index < length) {
4865 var cache = caches[0];
4866 value = array[index];
4868 if ((cache ? cacheIndexOf(cache, value) : indexOf(seen, value)) < 0) {
4869 argsIndex = argsLength;
4870 (cache || seen).push(value);
4871 while (--argsIndex) {
4872 cache = caches[argsIndex];
4873 if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
4880 while (argsLength--) {
4881 cache = caches[argsLength];
4883 releaseObject(cache);
4886 releaseArray(caches);
4892 * Gets the last element or last `n` elements of an array. If a callback is
4893 * provided elements at the end of the array are returned as long as the
4894 * callback returns truey. The callback is bound to `thisArg` and invoked
4895 * with three arguments; (value, index, array).
4897 * If a property name is provided for `callback` the created "_.pluck" style
4898 * callback will return the property value of the given element.
4900 * If an object is provided for `callback` the created "_.where" style callback
4901 * will return `true` for elements that have the properties of the given object,
4907 * @param {Array} array The array to query.
4908 * @param {Function|Object|number|string} [callback] The function called
4909 * per element or the number of elements to return. If a property name or
4910 * object is provided it will be used to create a "_.pluck" or "_.where"
4911 * style callback, respectively.
4912 * @param {*} [thisArg] The `this` binding of `callback`.
4913 * @returns {*} Returns the last element(s) of `array`.
4916 * _.last([1, 2, 3]);
4919 * _.last([1, 2, 3], 2);
4922 * _.last([1, 2, 3], function(num) {
4927 * var characters = [
4928 * { 'name': 'barney', 'blocked': false, 'employer': 'slate' },
4929 * { 'name': 'fred', 'blocked': true, 'employer': 'slate' },
4930 * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
4933 * // using "_.pluck" callback shorthand
4934 * _.pluck(_.last(characters, 'blocked'), 'name');
4935 * // => ['fred', 'pebbles']
4937 * // using "_.where" callback shorthand
4938 * _.last(characters, { 'employer': 'na' });
4939 * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
4941 function last(array, callback, thisArg) {
4943 length = array ? array.length : 0;
4945 if (typeof callback != 'number' && callback != null) {
4947 callback = lodash.createCallback(callback, thisArg, 3);
4948 while (index-- && callback(array[index], index, array)) {
4953 if (n == null || thisArg) {
4954 return array ? array[length - 1] : undefined;
4957 return slice(array, nativeMax(0, length - n));
4961 * Gets the index at which the last occurrence of `value` is found using strict
4962 * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used
4963 * as the offset from the end of the collection.
4965 * If a property name is provided for `callback` the created "_.pluck" style
4966 * callback will return the property value of the given element.
4968 * If an object is provided for `callback` the created "_.where" style callback
4969 * will return `true` for elements that have the properties of the given object,
4975 * @param {Array} array The array to search.
4976 * @param {*} value The value to search for.
4977 * @param {number} [fromIndex=array.length-1] The index to search from.
4978 * @returns {number} Returns the index of the matched value or `-1`.
4981 * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
4984 * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
4987 function lastIndexOf(array, value, fromIndex) {
4988 var index = array ? array.length : 0;
4989 if (typeof fromIndex == 'number') {
4990 index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1;
4993 if (array[index] === value) {
5001 * Removes all provided values from the given array using strict equality for
5002 * comparisons, i.e. `===`.
5007 * @param {Array} array The array to modify.
5008 * @param {...*} [value] The values to remove.
5009 * @returns {Array} Returns `array`.
5012 * var array = [1, 2, 3, 1, 2, 3];
5013 * _.pull(array, 2, 3);
5014 * console.log(array);
5017 function pull(array) {
5018 var args = arguments,
5020 argsLength = args.length,
5021 length = array ? array.length : 0;
5023 while (++argsIndex < argsLength) {
5025 value = args[argsIndex];
5026 while (++index < length) {
5027 if (array[index] === value) {
5028 splice.call(array, index--, 1);
5037 * Creates an array of numbers (positive and/or negative) progressing from
5038 * `start` up to but not including `end`. If `start` is less than `stop` a
5039 * zero-length range is created unless a negative `step` is specified.
5044 * @param {number} [start=0] The start of the range.
5045 * @param {number} end The end of the range.
5046 * @param {number} [step=1] The value to increment or decrement by.
5047 * @returns {Array} Returns a new range array.
5051 * // => [0, 1, 2, 3]
5054 * // => [1, 2, 3, 4]
5056 * _.range(0, 20, 5);
5057 * // => [0, 5, 10, 15]
5059 * _.range(0, -4, -1);
5060 * // => [0, -1, -2, -3]
5068 function range(start, end, step) {
5069 start = +start || 0;
5070 step = typeof step == 'number' ? step : (+step || 1);
5076 // use `Array(length)` so engines like Chakra and V8 avoid slower modes
5077 // http://youtu.be/XAqIpGU8ZZk#t=17m25s
5079 length = nativeMax(0, ceil((end - start) / (step || 1))),
5080 result = Array(length);
5082 while (++index < length) {
5083 result[index] = start;
5090 * Removes all elements from an array that the callback returns truey for
5091 * and returns an array of removed elements. The callback is bound to `thisArg`
5092 * and invoked with three arguments; (value, index, array).
5094 * If a property name is provided for `callback` the created "_.pluck" style
5095 * callback will return the property value of the given element.
5097 * If an object is provided for `callback` the created "_.where" style callback
5098 * will return `true` for elements that have the properties of the given object,
5104 * @param {Array} array The array to modify.
5105 * @param {Function|Object|string} [callback=identity] The function called
5106 * per iteration. If a property name or object is provided it will be used
5107 * to create a "_.pluck" or "_.where" style callback, respectively.
5108 * @param {*} [thisArg] The `this` binding of `callback`.
5109 * @returns {Array} Returns a new array of removed elements.
5112 * var array = [1, 2, 3, 4, 5, 6];
5113 * var evens = _.remove(array, function(num) { return num % 2 == 0; });
5115 * console.log(array);
5118 * console.log(evens);
5121 function remove(array, callback, thisArg) {
5123 length = array ? array.length : 0,
5126 callback = lodash.createCallback(callback, thisArg, 3);
5127 while (++index < length) {
5128 var value = array[index];
5129 if (callback(value, index, array)) {
5131 splice.call(array, index--, 1);
5139 * The opposite of `_.initial` this method gets all but the first element or
5140 * first `n` elements of an array. If a callback function is provided elements
5141 * at the beginning of the array are excluded from the result as long as the
5142 * callback returns truey. The callback is bound to `thisArg` and invoked
5143 * with three arguments; (value, index, array).
5145 * If a property name is provided for `callback` the created "_.pluck" style
5146 * callback will return the property value of the given element.
5148 * If an object is provided for `callback` the created "_.where" style callback
5149 * will return `true` for elements that have the properties of the given object,
5156 * @param {Array} array The array to query.
5157 * @param {Function|Object|number|string} [callback=1] The function called
5158 * per element or the number of elements to exclude. If a property name or
5159 * object is provided it will be used to create a "_.pluck" or "_.where"
5160 * style callback, respectively.
5161 * @param {*} [thisArg] The `this` binding of `callback`.
5162 * @returns {Array} Returns a slice of `array`.
5165 * _.rest([1, 2, 3]);
5168 * _.rest([1, 2, 3], 2);
5171 * _.rest([1, 2, 3], function(num) {
5176 * var characters = [
5177 * { 'name': 'barney', 'blocked': true, 'employer': 'slate' },
5178 * { 'name': 'fred', 'blocked': false, 'employer': 'slate' },
5179 * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' }
5182 * // using "_.pluck" callback shorthand
5183 * _.pluck(_.rest(characters, 'blocked'), 'name');
5184 * // => ['fred', 'pebbles']
5186 * // using "_.where" callback shorthand
5187 * _.rest(characters, { 'employer': 'slate' });
5188 * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }]
5190 function rest(array, callback, thisArg) {
5191 if (typeof callback != 'number' && callback != null) {
5194 length = array ? array.length : 0;
5196 callback = lodash.createCallback(callback, thisArg, 3);
5197 while (++index < length && callback(array[index], index, array)) {
5201 n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
5203 return slice(array, n);
5207 * Uses a binary search to determine the smallest index at which a value
5208 * should be inserted into a given sorted array in order to maintain the sort
5209 * order of the array. If a callback is provided it will be executed for
5210 * `value` and each element of `array` to compute their sort ranking. The
5211 * callback is bound to `thisArg` and invoked with one argument; (value).
5213 * If a property name is provided for `callback` the created "_.pluck" style
5214 * callback will return the property value of the given element.
5216 * If an object is provided for `callback` the created "_.where" style callback
5217 * will return `true` for elements that have the properties of the given object,
5223 * @param {Array} array The array to inspect.
5224 * @param {*} value The value to evaluate.
5225 * @param {Function|Object|string} [callback=identity] The function called
5226 * per iteration. If a property name or object is provided it will be used
5227 * to create a "_.pluck" or "_.where" style callback, respectively.
5228 * @param {*} [thisArg] The `this` binding of `callback`.
5229 * @returns {number} Returns the index at which `value` should be inserted
5233 * _.sortedIndex([20, 30, 50], 40);
5236 * // using "_.pluck" callback shorthand
5237 * _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x');
5241 * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 }
5244 * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
5245 * return dict.wordToNumber[word];
5249 * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) {
5250 * return this.wordToNumber[word];
5254 function sortedIndex(array, value, callback, thisArg) {
5256 high = array ? array.length : low;
5258 // explicitly reference `identity` for better inlining in Firefox
5259 callback = callback ? lodash.createCallback(callback, thisArg, 1) : identity;
5260 value = callback(value);
5262 while (low < high) {
5263 var mid = (low + high) >>> 1;
5264 (callback(array[mid]) < value)
5272 * Creates an array of unique values, in order, of the provided arrays using
5273 * strict equality for comparisons, i.e. `===`.
5278 * @param {...Array} [array] The arrays to inspect.
5279 * @returns {Array} Returns an array of combined values.
5282 * _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
5283 * // => [1, 2, 3, 5, 4]
5286 return baseUniq(baseFlatten(arguments, true, true));
5290 * Creates a duplicate-value-free version of an array using strict equality
5291 * for comparisons, i.e. `===`. If the array is sorted, providing
5292 * `true` for `isSorted` will use a faster algorithm. If a callback is provided
5293 * each element of `array` is passed through the callback before uniqueness
5294 * is computed. The callback is bound to `thisArg` and invoked with three
5295 * arguments; (value, index, array).
5297 * If a property name is provided for `callback` the created "_.pluck" style
5298 * callback will return the property value of the given element.
5300 * If an object is provided for `callback` the created "_.where" style callback
5301 * will return `true` for elements that have the properties of the given object,
5308 * @param {Array} array The array to process.
5309 * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted.
5310 * @param {Function|Object|string} [callback=identity] The function called
5311 * per iteration. If a property name or object is provided it will be used
5312 * to create a "_.pluck" or "_.where" style callback, respectively.
5313 * @param {*} [thisArg] The `this` binding of `callback`.
5314 * @returns {Array} Returns a duplicate-value-free array.
5317 * _.uniq([1, 2, 1, 3, 1]);
5320 * _.uniq([1, 1, 2, 2, 3], true);
5323 * _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); });
5324 * // => ['A', 'b', 'C']
5326 * _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math);
5329 * // using "_.pluck" callback shorthand
5330 * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
5331 * // => [{ 'x': 1 }, { 'x': 2 }]
5333 function uniq(array, isSorted, callback, thisArg) {
5335 if (typeof isSorted != 'boolean' && isSorted != null) {
5337 callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted;
5340 if (callback != null) {
5341 callback = lodash.createCallback(callback, thisArg, 3);
5343 return baseUniq(array, isSorted, callback);
5347 * Creates an array excluding all provided values using strict equality for
5348 * comparisons, i.e. `===`.
5353 * @param {Array} array The array to filter.
5354 * @param {...*} [value] The values to exclude.
5355 * @returns {Array} Returns a new array of filtered values.
5358 * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
5361 function without(array) {
5362 return baseDifference(array, slice(arguments, 1));
5366 * Creates an array that is the symmetric difference of the provided arrays.
5367 * See http://en.wikipedia.org/wiki/Symmetric_difference.
5372 * @param {...Array} [array] The arrays to inspect.
5373 * @returns {Array} Returns an array of values.
5376 * _.xor([1, 2, 3], [5, 2, 1, 4]);
5379 * _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
5384 length = arguments.length;
5386 while (++index < length) {
5387 var array = arguments[index];
5388 if (isArray(array) || isArguments(array)) {
5390 ? baseUniq(baseDifference(result, array).concat(baseDifference(array, result)))
5394 return result || [];
5398 * Creates an array of grouped elements, the first of which contains the first
5399 * elements of the given arrays, the second of which contains the second
5400 * elements of the given arrays, and so on.
5406 * @param {...Array} [array] Arrays to process.
5407 * @returns {Array} Returns a new array of grouped elements.
5410 * _.zip(['fred', 'barney'], [30, 40], [true, false]);
5411 * // => [['fred', 30, true], ['barney', 40, false]]
5414 var array = arguments.length > 1 ? arguments : arguments[0],
5416 length = array ? max(pluck(array, 'length')) : 0,
5417 result = Array(length < 0 ? 0 : length);
5419 while (++index < length) {
5420 result[index] = pluck(array, index);
5426 * Creates an object composed from arrays of `keys` and `values`. Provide
5427 * either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]`
5428 * or two arrays, one of `keys` and one of corresponding `values`.
5434 * @param {Array} keys The array of keys.
5435 * @param {Array} [values=[]] The array of values.
5436 * @returns {Object} Returns an object composed of the given keys and
5437 * corresponding values.
5440 * _.zipObject(['fred', 'barney'], [30, 40]);
5441 * // => { 'fred': 30, 'barney': 40 }
5443 function zipObject(keys, values) {
5445 length = keys ? keys.length : 0,
5448 if (!values && length && !isArray(keys[0])) {
5451 while (++index < length) {
5452 var key = keys[index];
5454 result[key] = values[index];
5456 result[key[0]] = key[1];
5462 /*--------------------------------------------------------------------------*/
5465 * Creates a function that executes `func`, with the `this` binding and
5466 * arguments of the created function, only after being called `n` times.
5470 * @category Functions
5471 * @param {number} n The number of times the function must be called before
5472 * `func` is executed.
5473 * @param {Function} func The function to restrict.
5474 * @returns {Function} Returns the new restricted function.
5477 * var saves = ['profile', 'settings'];
5479 * var done = _.after(saves.length, function() {
5480 * console.log('Done saving!');
5483 * _.forEach(saves, function(type) {
5484 * asyncSave({ 'type': type, 'complete': done });
5486 * // => logs 'Done saving!', after all saves have completed
5488 function after(n, func) {
5489 if (!isFunction(func)) {
5490 throw new TypeError;
5494 return func.apply(this, arguments);
5500 * Creates a function that, when called, invokes `func` with the `this`
5501 * binding of `thisArg` and prepends any additional `bind` arguments to those
5502 * provided to the bound function.
5506 * @category Functions
5507 * @param {Function} func The function to bind.
5508 * @param {*} [thisArg] The `this` binding of `func`.
5509 * @param {...*} [arg] Arguments to be partially applied.
5510 * @returns {Function} Returns the new bound function.
5513 * var func = function(greeting) {
5514 * return greeting + ' ' + this.name;
5517 * func = _.bind(func, { 'name': 'fred' }, 'hi');
5521 function bind(func, thisArg) {
5522 return arguments.length > 2
5523 ? createWrapper(func, 17, slice(arguments, 2), null, thisArg)
5524 : createWrapper(func, 1, null, null, thisArg);
5528 * Binds methods of an object to the object itself, overwriting the existing
5529 * method. Method names may be specified as individual arguments or as arrays
5530 * of method names. If no method names are provided all the function properties
5531 * of `object` will be bound.
5535 * @category Functions
5536 * @param {Object} object The object to bind and assign the bound methods to.
5537 * @param {...string} [methodName] The object method names to
5538 * bind, specified as individual method names or arrays of method names.
5539 * @returns {Object} Returns `object`.
5544 * 'onClick': function() { console.log('clicked ' + this.label); }
5548 * jQuery('#docs').on('click', view.onClick);
5549 * // => logs 'clicked docs', when the button is clicked
5551 function bindAll(object) {
5552 var funcs = arguments.length > 1 ? baseFlatten(arguments, true, false, 1) : functions(object),
5554 length = funcs.length;
5556 while (++index < length) {
5557 var key = funcs[index];
5558 object[key] = createWrapper(object[key], 1, null, null, object);
5564 * Creates a function that, when called, invokes the method at `object[key]`
5565 * and prepends any additional `bindKey` arguments to those provided to the bound
5566 * function. This method differs from `_.bind` by allowing bound functions to
5567 * reference methods that will be redefined or don't yet exist.
5568 * See http://michaux.ca/articles/lazy-function-definition-pattern.
5572 * @category Functions
5573 * @param {Object} object The object the method belongs to.
5574 * @param {string} key The key of the method.
5575 * @param {...*} [arg] Arguments to be partially applied.
5576 * @returns {Function} Returns the new bound function.
5581 * 'greet': function(greeting) {
5582 * return greeting + ' ' + this.name;
5586 * var func = _.bindKey(object, 'greet', 'hi');
5590 * object.greet = function(greeting) {
5591 * return greeting + 'ya ' + this.name + '!';
5595 * // => 'hiya fred!'
5597 function bindKey(object, key) {
5598 return arguments.length > 2
5599 ? createWrapper(key, 19, slice(arguments, 2), null, object)
5600 : createWrapper(key, 3, null, null, object);
5604 * Creates a function that is the composition of the provided functions,
5605 * where each function consumes the return value of the function that follows.
5606 * For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`.
5607 * Each function is executed with the `this` binding of the composed function.
5611 * @category Functions
5612 * @param {...Function} [func] Functions to compose.
5613 * @returns {Function} Returns the new composed function.
5616 * var realNameMap = {
5617 * 'pebbles': 'penelope'
5620 * var format = function(name) {
5621 * name = realNameMap[name.toLowerCase()] || name;
5622 * return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
5625 * var greet = function(formatted) {
5626 * return 'Hiya ' + formatted + '!';
5629 * var welcome = _.compose(greet, format);
5630 * welcome('pebbles');
5631 * // => 'Hiya Penelope!'
5633 function compose() {
5634 var funcs = arguments,
5635 length = funcs.length;
5638 if (!isFunction(funcs[length])) {
5639 throw new TypeError;
5643 var args = arguments,
5644 length = funcs.length;
5647 args = [funcs[length].apply(this, args)];
5654 * Creates a function which accepts one or more arguments of `func` that when
5655 * invoked either executes `func` returning its result, if all `func` arguments
5656 * have been provided, or returns a function that accepts one or more of the
5657 * remaining `func` arguments, and so on. The arity of `func` can be specified
5658 * if `func.length` is not sufficient.
5662 * @category Functions
5663 * @param {Function} func The function to curry.
5664 * @param {number} [arity=func.length] The arity of `func`.
5665 * @returns {Function} Returns the new curried function.
5668 * var curried = _.curry(function(a, b, c) {
5669 * console.log(a + b + c);
5681 function curry(func, arity) {
5682 arity = typeof arity == 'number' ? arity : (+arity || func.length);
5683 return createWrapper(func, 4, null, null, null, arity);
5687 * Creates a function that will delay the execution of `func` until after
5688 * `wait` milliseconds have elapsed since the last time it was invoked.
5689 * Provide an options object to indicate that `func` should be invoked on
5690 * the leading and/or trailing edge of the `wait` timeout. Subsequent calls
5691 * to the debounced function will return the result of the last `func` call.
5693 * Note: If `leading` and `trailing` options are `true` `func` will be called
5694 * on the trailing edge of the timeout only if the the debounced function is
5695 * invoked more than once during the `wait` timeout.
5699 * @category Functions
5700 * @param {Function} func The function to debounce.
5701 * @param {number} wait The number of milliseconds to delay.
5702 * @param {Object} [options] The options object.
5703 * @param {boolean} [options.leading=false] Specify execution on the leading edge of the timeout.
5704 * @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's called.
5705 * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
5706 * @returns {Function} Returns the new debounced function.
5709 * // avoid costly calculations while the window size is in flux
5710 * var lazyLayout = _.debounce(calculateLayout, 150);
5711 * jQuery(window).on('resize', lazyLayout);
5713 * // execute `sendMail` when the click event is fired, debouncing subsequent calls
5714 * jQuery('#postbox').on('click', _.debounce(sendMail, 300, {
5719 * // ensure `batchLog` is executed once after 1 second of debounced calls
5720 * var source = new EventSource('/stream');
5721 * source.addEventListener('message', _.debounce(batchLog, 250, {
5725 function debounce(func, wait, options) {
5737 if (!isFunction(func)) {
5738 throw new TypeError;
5740 wait = nativeMax(0, wait) || 0;
5741 if (options === true) {
5744 } else if (isObject(options)) {
5745 leading = options.leading;
5746 maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
5747 trailing = 'trailing' in options ? options.trailing : trailing;
5749 var delayed = function() {
5750 var remaining = wait - (now() - stamp);
5751 if (remaining <= 0) {
5753 clearTimeout(maxTimeoutId);
5755 var isCalled = trailingCall;
5756 maxTimeoutId = timeoutId = trailingCall = undefined;
5759 result = func.apply(thisArg, args);
5760 if (!timeoutId && !maxTimeoutId) {
5761 args = thisArg = null;
5765 timeoutId = setTimeout(delayed, remaining);
5769 var maxDelayed = function() {
5771 clearTimeout(timeoutId);
5773 maxTimeoutId = timeoutId = trailingCall = undefined;
5774 if (trailing || (maxWait !== wait)) {
5776 result = func.apply(thisArg, args);
5777 if (!timeoutId && !maxTimeoutId) {
5778 args = thisArg = null;
5787 trailingCall = trailing && (timeoutId || !leading);
5789 if (maxWait === false) {
5790 var leadingCall = leading && !timeoutId;
5792 if (!maxTimeoutId && !leading) {
5795 var remaining = maxWait - (stamp - lastCalled),
5796 isCalled = remaining <= 0;
5800 maxTimeoutId = clearTimeout(maxTimeoutId);
5803 result = func.apply(thisArg, args);
5805 else if (!maxTimeoutId) {
5806 maxTimeoutId = setTimeout(maxDelayed, remaining);
5809 if (isCalled && timeoutId) {
5810 timeoutId = clearTimeout(timeoutId);
5812 else if (!timeoutId && wait !== maxWait) {
5813 timeoutId = setTimeout(delayed, wait);
5817 result = func.apply(thisArg, args);
5819 if (isCalled && !timeoutId && !maxTimeoutId) {
5820 args = thisArg = null;
5827 * Defers executing the `func` function until the current call stack has cleared.
5828 * Additional arguments will be provided to `func` when it is invoked.
5832 * @category Functions
5833 * @param {Function} func The function to defer.
5834 * @param {...*} [arg] Arguments to invoke the function with.
5835 * @returns {number} Returns the timer id.
5838 * _.defer(function(text) { console.log(text); }, 'deferred');
5839 * // logs 'deferred' after one or more milliseconds
5841 function defer(func) {
5842 if (!isFunction(func)) {
5843 throw new TypeError;
5845 var args = slice(arguments, 1);
5846 return setTimeout(function() { func.apply(undefined, args); }, 1);
5850 * Executes the `func` function after `wait` milliseconds. Additional arguments
5851 * will be provided to `func` when it is invoked.
5855 * @category Functions
5856 * @param {Function} func The function to delay.
5857 * @param {number} wait The number of milliseconds to delay execution.
5858 * @param {...*} [arg] Arguments to invoke the function with.
5859 * @returns {number} Returns the timer id.
5862 * _.delay(function(text) { console.log(text); }, 1000, 'later');
5863 * // => logs 'later' after one second
5865 function delay(func, wait) {
5866 if (!isFunction(func)) {
5867 throw new TypeError;
5869 var args = slice(arguments, 2);
5870 return setTimeout(function() { func.apply(undefined, args); }, wait);
5874 * Creates a function that memoizes the result of `func`. If `resolver` is
5875 * provided it will be used to determine the cache key for storing the result
5876 * based on the arguments provided to the memoized function. By default, the
5877 * first argument provided to the memoized function is used as the cache key.
5878 * The `func` is executed with the `this` binding of the memoized function.
5879 * The result cache is exposed as the `cache` property on the memoized function.
5883 * @category Functions
5884 * @param {Function} func The function to have its output memoized.
5885 * @param {Function} [resolver] A function used to resolve the cache key.
5886 * @returns {Function} Returns the new memoizing function.
5889 * var fibonacci = _.memoize(function(n) {
5890 * return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
5897 * 'fred': { 'name': 'fred', 'age': 40 },
5898 * 'pebbles': { 'name': 'pebbles', 'age': 1 }
5901 * // modifying the result cache
5902 * var get = _.memoize(function(name) { return data[name]; }, _.identity);
5904 * // => { 'name': 'pebbles', 'age': 1 }
5906 * get.cache.pebbles.name = 'penelope';
5908 * // => { 'name': 'penelope', 'age': 1 }
5910 function memoize(func, resolver) {
5911 if (!isFunction(func)) {
5912 throw new TypeError;
5914 var memoized = function() {
5915 var cache = memoized.cache,
5916 key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0];
5918 return hasOwnProperty.call(cache, key)
5920 : (cache[key] = func.apply(this, arguments));
5922 memoized.cache = {};
5927 * Creates a function that is restricted to execute `func` once. Repeat calls to
5928 * the function will return the value of the first call. The `func` is executed
5929 * with the `this` binding of the created function.
5933 * @category Functions
5934 * @param {Function} func The function to restrict.
5935 * @returns {Function} Returns the new restricted function.
5938 * var initialize = _.once(createApplication);
5941 * // `initialize` executes `createApplication` once
5943 function once(func) {
5947 if (!isFunction(func)) {
5948 throw new TypeError;
5955 result = func.apply(this, arguments);
5957 // clear the `func` variable so the function may be garbage collected
5964 * Creates a function that, when called, invokes `func` with any additional
5965 * `partial` arguments prepended to those provided to the new function. This
5966 * method is similar to `_.bind` except it does **not** alter the `this` binding.
5970 * @category Functions
5971 * @param {Function} func The function to partially apply arguments to.
5972 * @param {...*} [arg] Arguments to be partially applied.
5973 * @returns {Function} Returns the new partially applied function.
5976 * var greet = function(greeting, name) { return greeting + ' ' + name; };
5977 * var hi = _.partial(greet, 'hi');
5981 function partial(func) {
5982 return createWrapper(func, 16, slice(arguments, 1));
5986 * This method is like `_.partial` except that `partial` arguments are
5987 * appended to those provided to the new function.
5991 * @category Functions
5992 * @param {Function} func The function to partially apply arguments to.
5993 * @param {...*} [arg] Arguments to be partially applied.
5994 * @returns {Function} Returns the new partially applied function.
5997 * var defaultsDeep = _.partialRight(_.merge, _.defaults);
6000 * 'variable': 'data',
6001 * 'imports': { 'jq': $ }
6004 * defaultsDeep(options, _.templateSettings);
6010 * // => { '_': _, 'jq': $ }
6012 function partialRight(func) {
6013 return createWrapper(func, 32, null, slice(arguments, 1));
6017 * Creates a function that, when executed, will only call the `func` function
6018 * at most once per every `wait` milliseconds. Provide an options object to
6019 * indicate that `func` should be invoked on the leading and/or trailing edge
6020 * of the `wait` timeout. Subsequent calls to the throttled function will
6021 * return the result of the last `func` call.
6023 * Note: If `leading` and `trailing` options are `true` `func` will be called
6024 * on the trailing edge of the timeout only if the the throttled function is
6025 * invoked more than once during the `wait` timeout.
6029 * @category Functions
6030 * @param {Function} func The function to throttle.
6031 * @param {number} wait The number of milliseconds to throttle executions to.
6032 * @param {Object} [options] The options object.
6033 * @param {boolean} [options.leading=true] Specify execution on the leading edge of the timeout.
6034 * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout.
6035 * @returns {Function} Returns the new throttled function.
6038 * // avoid excessively updating the position while scrolling
6039 * var throttled = _.throttle(updatePosition, 100);
6040 * jQuery(window).on('scroll', throttled);
6042 * // execute `renewToken` when the click event is fired, but not more than once every 5 minutes
6043 * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, {
6047 function throttle(func, wait, options) {
6051 if (!isFunction(func)) {
6052 throw new TypeError;
6054 if (options === false) {
6056 } else if (isObject(options)) {
6057 leading = 'leading' in options ? options.leading : leading;
6058 trailing = 'trailing' in options ? options.trailing : trailing;
6060 debounceOptions.leading = leading;
6061 debounceOptions.maxWait = wait;
6062 debounceOptions.trailing = trailing;
6064 return debounce(func, wait, debounceOptions);
6068 * Creates a function that provides `value` to the wrapper function as its
6069 * first argument. Additional arguments provided to the function are appended
6070 * to those provided to the wrapper function. The wrapper is executed with
6071 * the `this` binding of the created function.
6075 * @category Functions
6076 * @param {*} value The value to wrap.
6077 * @param {Function} wrapper The wrapper function.
6078 * @returns {Function} Returns the new function.
6081 * var p = _.wrap(_.escape, function(func, text) {
6082 * return '<p>' + func(text) + '</p>';
6085 * p('Fred, Wilma, & Pebbles');
6086 * // => '<p>Fred, Wilma, & Pebbles</p>'
6088 function wrap(value, wrapper) {
6089 return createWrapper(wrapper, 16, [value]);
6092 /*--------------------------------------------------------------------------*/
6095 * Creates a function that returns `value`.
6099 * @category Utilities
6100 * @param {*} value The value to return from the new function.
6101 * @returns {Function} Returns the new function.
6104 * var object = { 'name': 'fred' };
6105 * var getter = _.constant(object);
6106 * getter() === object;
6109 function constant(value) {
6116 * Produces a callback bound to an optional `thisArg`. If `func` is a property
6117 * name the created callback will return the property value for a given element.
6118 * If `func` is an object the created callback will return `true` for elements
6119 * that contain the equivalent object properties, otherwise it will return `false`.
6123 * @category Utilities
6124 * @param {*} [func=identity] The value to convert to a callback.
6125 * @param {*} [thisArg] The `this` binding of the created callback.
6126 * @param {number} [argCount] The number of arguments the callback accepts.
6127 * @returns {Function} Returns a callback function.
6130 * var characters = [
6131 * { 'name': 'barney', 'age': 36 },
6132 * { 'name': 'fred', 'age': 40 }
6135 * // wrap to create custom callback shorthands
6136 * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) {
6137 * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
6138 * return !match ? func(callback, thisArg) : function(object) {
6139 * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
6143 * _.filter(characters, 'age__gt38');
6144 * // => [{ 'name': 'fred', 'age': 40 }]
6146 function createCallback(func, thisArg, argCount) {
6147 var type = typeof func;
6148 if (func == null || type == 'function') {
6149 return baseCreateCallback(func, thisArg, argCount);
6151 // handle "_.pluck" style callback shorthands
6152 if (type != 'object') {
6153 return property(func);
6155 var props = keys(func),
6159 // handle "_.where" style callback shorthands
6160 if (props.length == 1 && a === a && !isObject(a)) {
6161 // fast path the common case of providing an object with a single
6162 // property containing a primitive value
6163 return function(object) {
6164 var b = object[key];
6165 return a === b && (a !== 0 || (1 / a == 1 / b));
6168 return function(object) {
6169 var length = props.length,
6173 if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) {
6182 * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their
6183 * corresponding HTML entities.
6187 * @category Utilities
6188 * @param {string} string The string to escape.
6189 * @returns {string} Returns the escaped string.
6192 * _.escape('Fred, Wilma, & Pebbles');
6193 * // => 'Fred, Wilma, & Pebbles'
6195 function escape(string) {
6196 return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar);
6200 * This method returns the first argument provided to it.
6204 * @category Utilities
6205 * @param {*} value Any value.
6206 * @returns {*} Returns `value`.
6209 * var object = { 'name': 'fred' };
6210 * _.identity(object) === object;
6213 function identity(value) {
6218 * Adds function properties of a source object to the destination object.
6219 * If `object` is a function methods will be added to its prototype as well.
6223 * @category Utilities
6224 * @param {Function|Object} [object=lodash] object The destination object.
6225 * @param {Object} source The object of functions to add.
6226 * @param {Object} [options] The options object.
6227 * @param {boolean} [options.chain=true] Specify whether the functions added are chainable.
6230 * function capitalize(string) {
6231 * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
6234 * _.mixin({ 'capitalize': capitalize });
6235 * _.capitalize('fred');
6238 * _('fred').capitalize().value();
6241 * _.mixin({ 'capitalize': capitalize }, { 'chain': false });
6242 * _('fred').capitalize();
6245 function mixin(object, source, options) {
6247 methodNames = source && functions(source);
6249 if (!source || (!options && !methodNames.length)) {
6250 if (options == null) {
6253 ctor = lodashWrapper;
6256 methodNames = functions(source);
6258 if (options === false) {
6260 } else if (isObject(options) && 'chain' in options) {
6261 chain = options.chain;
6264 isFunc = isFunction(ctor);
6266 forEach(methodNames, function(methodName) {
6267 var func = object[methodName] = source[methodName];
6269 ctor.prototype[methodName] = function() {
6270 var chainAll = this.__chain__,
6271 value = this.__wrapped__,
6274 push.apply(args, arguments);
6275 var result = func.apply(object, args);
6276 if (chain || chainAll) {
6277 if (value === result && isObject(result)) {
6280 result = new ctor(result);
6281 result.__chain__ = chainAll;
6290 * Reverts the '_' variable to its previous value and returns a reference to
6291 * the `lodash` function.
6295 * @category Utilities
6296 * @returns {Function} Returns the `lodash` function.
6299 * var lodash = _.noConflict();
6301 function noConflict() {
6302 context._ = oldDash;
6307 * A no-operation function.
6311 * @category Utilities
6314 * var object = { 'name': 'fred' };
6315 * _.noop(object) === undefined;
6319 // no operation performed
6323 * Gets the number of milliseconds that have elapsed since the Unix epoch
6324 * (1 January 1970 00:00:00 UTC).
6328 * @category Utilities
6331 * var stamp = _.now();
6332 * _.defer(function() { console.log(_.now() - stamp); });
6333 * // => logs the number of milliseconds it took for the deferred function to be called
6335 var now = isNative(now = Date.now) && now || function() {
6336 return new Date().getTime();
6340 * Converts the given value into an integer of the specified radix.
6341 * If `radix` is `undefined` or `0` a `radix` of `10` is used unless the
6342 * `value` is a hexadecimal, in which case a `radix` of `16` is used.
6344 * Note: This method avoids differences in native ES3 and ES5 `parseInt`
6345 * implementations. See http://es5.github.io/#E.
6349 * @category Utilities
6350 * @param {string} value The value to parse.
6351 * @param {number} [radix] The radix used to interpret the value to parse.
6352 * @returns {number} Returns the new integer value.
6358 var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) {
6359 // Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt`
6360 return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0);
6364 * Creates a "_.pluck" style function, which returns the `key` value of a
6369 * @category Utilities
6370 * @param {string} key The name of the property to retrieve.
6371 * @returns {Function} Returns the new function.
6374 * var characters = [
6375 * { 'name': 'fred', 'age': 40 },
6376 * { 'name': 'barney', 'age': 36 }
6379 * var getName = _.property('name');
6381 * _.map(characters, getName);
6382 * // => ['barney', 'fred']
6384 * _.sortBy(characters, getName);
6385 * // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
6387 function property(key) {
6388 return function(object) {
6394 * Produces a random number between `min` and `max` (inclusive). If only one
6395 * argument is provided a number between `0` and the given number will be
6396 * returned. If `floating` is truey or either `min` or `max` are floats a
6397 * floating-point number will be returned instead of an integer.
6401 * @category Utilities
6402 * @param {number} [min=0] The minimum possible value.
6403 * @param {number} [max=1] The maximum possible value.
6404 * @param {boolean} [floating=false] Specify returning a floating-point number.
6405 * @returns {number} Returns a random number.
6409 * // => an integer between 0 and 5
6412 * // => also an integer between 0 and 5
6414 * _.random(5, true);
6415 * // => a floating-point number between 0 and 5
6417 * _.random(1.2, 5.2);
6418 * // => a floating-point number between 1.2 and 5.2
6420 function random(min, max, floating) {
6421 var noMin = min == null,
6422 noMax = max == null;
6424 if (floating == null) {
6425 if (typeof min == 'boolean' && noMax) {
6429 else if (!noMax && typeof max == 'boolean') {
6434 if (noMin && noMax) {
6444 if (floating || min % 1 || max % 1) {
6445 var rand = nativeRandom();
6446 return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1)))), max);
6448 return baseRandom(min, max);
6452 * Resolves the value of property `key` on `object`. If `key` is a function
6453 * it will be invoked with the `this` binding of `object` and its result returned,
6454 * else the property value is returned. If `object` is falsey then `undefined`
6459 * @category Utilities
6460 * @param {Object} object The object to inspect.
6461 * @param {string} key The name of the property to resolve.
6462 * @returns {*} Returns the resolved value.
6466 * 'cheese': 'crumpets',
6467 * 'stuff': function() {
6468 * return 'nonsense';
6472 * _.result(object, 'cheese');
6475 * _.result(object, 'stuff');
6478 function result(object, key) {
6480 var value = object[key];
6481 return isFunction(value) ? object[key]() : value;
6486 * A micro-templating method that handles arbitrary delimiters, preserves
6487 * whitespace, and correctly escapes quotes within interpolated code.
6489 * Note: In the development build, `_.template` utilizes sourceURLs for easier
6490 * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
6492 * For more information on precompiling templates see:
6493 * https://lodash.com/custom-builds
6495 * For more information on Chrome extension sandboxes see:
6496 * http://developer.chrome.com/stable/extensions/sandboxingEval.html
6500 * @category Utilities
6501 * @param {string} text The template text.
6502 * @param {Object} data The data object used to populate the text.
6503 * @param {Object} [options] The options object.
6504 * @param {RegExp} [options.escape] The "escape" delimiter.
6505 * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
6506 * @param {Object} [options.imports] An object to import into the template as local variables.
6507 * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
6508 * @param {string} [sourceURL] The sourceURL of the template's compiled source.
6509 * @param {string} [variable] The data object variable name.
6510 * @returns {Function|string} Returns a compiled function when no `data` object
6511 * is given, else it returns the interpolated text.
6514 * // using the "interpolate" delimiter to create a compiled template
6515 * var compiled = _.template('hello <%= name %>');
6516 * compiled({ 'name': 'fred' });
6517 * // => 'hello fred'
6519 * // using the "escape" delimiter to escape HTML in data property values
6520 * _.template('<b><%- value %></b>', { 'value': '<script>' });
6521 * // => '<b><script></b>'
6523 * // using the "evaluate" delimiter to generate HTML
6524 * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
6525 * _.template(list, { 'people': ['fred', 'barney'] });
6526 * // => '<li>fred</li><li>barney</li>'
6528 * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
6529 * _.template('hello ${ name }', { 'name': 'pebbles' });
6530 * // => 'hello pebbles'
6532 * // using the internal `print` function in "evaluate" delimiters
6533 * _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
6534 * // => 'hello barney!'
6536 * // using a custom template delimiters
6537 * _.templateSettings = {
6538 * 'interpolate': /{{([\s\S]+?)}}/g
6541 * _.template('hello {{ name }}!', { 'name': 'mustache' });
6542 * // => 'hello mustache!'
6544 * // using the `imports` option to import jQuery
6545 * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
6546 * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
6547 * // => '<li>fred</li><li>barney</li>'
6549 * // using the `sourceURL` option to specify a custom sourceURL for the template
6550 * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
6552 * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
6554 * // using the `variable` option to ensure a with-statement isn't used in the compiled template
6555 * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
6557 * // => function(data) {
6558 * var __t, __p = '', __e = _.escape;
6559 * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
6563 * // using the `source` property to inline compiled templates for meaningful
6564 * // line numbers in error messages and a stack trace
6565 * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
6567 * "main": ' + _.template(mainText).source + '\
6571 function template(text, data, options) {
6572 // based on John Resig's `tmpl` implementation
6573 // http://ejohn.org/blog/javascript-micro-templating/
6574 // and Laura Doktorova's doT.js
6575 // https://github.com/olado/doT
6576 var settings = lodash.templateSettings;
6577 text = String(text || '');
6579 // avoid missing dependencies when `iteratorTemplate` is not defined
6580 options = defaults({}, options, settings);
6582 var imports = defaults({}, options.imports, settings.imports),
6583 importsKeys = keys(imports),
6584 importsValues = values(imports);
6588 interpolate = options.interpolate || reNoMatch,
6589 source = "__p += '";
6591 // compile the regexp to match each delimiter
6592 var reDelimiters = RegExp(
6593 (options.escape || reNoMatch).source + '|' +
6594 interpolate.source + '|' +
6595 (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
6596 (options.evaluate || reNoMatch).source + '|$'
6599 text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
6600 interpolateValue || (interpolateValue = esTemplateValue);
6602 // escape characters that cannot be included in string literals
6603 source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
6605 // replace delimiters with snippets
6607 source += "' +\n__e(" + escapeValue + ") +\n'";
6609 if (evaluateValue) {
6610 isEvaluating = true;
6611 source += "';\n" + evaluateValue + ";\n__p += '";
6613 if (interpolateValue) {
6614 source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
6616 index = offset + match.length;
6618 // the JS engine embedded in Adobe products requires returning the `match`
6619 // string in order to produce the correct `offset` value
6625 // if `variable` is not specified, wrap a with-statement around the generated
6626 // code to add the data object to the top of the scope chain
6627 var variable = options.variable,
6628 hasVariable = variable;
6632 source = 'with (' + variable + ') {\n' + source + '\n}\n';
6634 // cleanup code by stripping empty strings
6635 source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
6636 .replace(reEmptyStringMiddle, '$1')
6637 .replace(reEmptyStringTrailing, '$1;');
6639 // frame code as the function body
6640 source = 'function(' + variable + ') {\n' +
6641 (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
6642 "var __t, __p = '', __e = _.escape" +
6644 ? ', __j = Array.prototype.join;\n' +
6645 "function print() { __p += __j.call(arguments, '') }\n"
6651 // Use a sourceURL for easier debugging.
6652 // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
6653 var sourceURL = '\n/*\n//# sourceURL=' + (options.sourceURL || '/lodash/template/source[' + (templateCounter++) + ']') + '\n*/';
6656 var result = Function(importsKeys, 'return ' + source + sourceURL).apply(undefined, importsValues);
6662 return result(data);
6664 // provide the compiled function's source by its `toString` method, in
6665 // supported environments, or the `source` property as a convenience for
6666 // inlining compiled templates during the build process
6667 result.source = source;
6672 * Executes the callback `n` times, returning an array of the results
6673 * of each callback execution. The callback is bound to `thisArg` and invoked
6674 * with one argument; (index).
6678 * @category Utilities
6679 * @param {number} n The number of times to execute the callback.
6680 * @param {Function} callback The function called per iteration.
6681 * @param {*} [thisArg] The `this` binding of `callback`.
6682 * @returns {Array} Returns an array of the results of each `callback` execution.
6685 * var diceRolls = _.times(3, _.partial(_.random, 1, 6));
6688 * _.times(3, function(n) { mage.castSpell(n); });
6689 * // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively
6691 * _.times(3, function(n) { this.cast(n); }, mage);
6692 * // => also calls `mage.castSpell(n)` three times
6694 function times(n, callback, thisArg) {
6695 n = (n = +n) > -1 ? n : 0;
6699 callback = baseCreateCallback(callback, thisArg, 1);
6700 while (++index < n) {
6701 result[index] = callback(index);
6707 * The inverse of `_.escape` this method converts the HTML entities
6708 * `&`, `<`, `>`, `"`, and `'` in `string` to their
6709 * corresponding characters.
6713 * @category Utilities
6714 * @param {string} string The string to unescape.
6715 * @returns {string} Returns the unescaped string.
6718 * _.unescape('Fred, Barney & Pebbles');
6719 * // => 'Fred, Barney & Pebbles'
6721 function unescape(string) {
6722 return string == null ? '' : String(string).replace(reEscapedHtml, unescapeHtmlChar);
6726 * Generates a unique ID. If `prefix` is provided the ID will be appended to it.
6730 * @category Utilities
6731 * @param {string} [prefix] The value to prefix the ID with.
6732 * @returns {string} Returns the unique ID.
6735 * _.uniqueId('contact_');
6736 * // => 'contact_104'
6741 function uniqueId(prefix) {
6742 var id = ++idCounter;
6743 return String(prefix == null ? '' : prefix) + id;
6746 /*--------------------------------------------------------------------------*/
6749 * Creates a `lodash` object that wraps the given value with explicit
6750 * method chaining enabled.
6754 * @category Chaining
6755 * @param {*} value The value to wrap.
6756 * @returns {Object} Returns the wrapper object.
6759 * var characters = [
6760 * { 'name': 'barney', 'age': 36 },
6761 * { 'name': 'fred', 'age': 40 },
6762 * { 'name': 'pebbles', 'age': 1 }
6765 * var youngest = _.chain(characters)
6767 * .map(function(chr) { return chr.name + ' is ' + chr.age; })
6770 * // => 'pebbles is 1'
6772 function chain(value) {
6773 value = new lodashWrapper(value);
6774 value.__chain__ = true;
6779 * Invokes `interceptor` with the `value` as the first argument and then
6780 * returns `value`. The purpose of this method is to "tap into" a method
6781 * chain in order to perform operations on intermediate results within
6786 * @category Chaining
6787 * @param {*} value The value to provide to `interceptor`.
6788 * @param {Function} interceptor The function to invoke.
6789 * @returns {*} Returns `value`.
6793 * .tap(function(array) { array.pop(); })
6798 function tap(value, interceptor) {
6804 * Enables explicit method chaining on the wrapper object.
6808 * @category Chaining
6809 * @returns {*} Returns the wrapper object.
6812 * var characters = [
6813 * { 'name': 'barney', 'age': 36 },
6814 * { 'name': 'fred', 'age': 40 }
6817 * // without explicit chaining
6818 * _(characters).first();
6819 * // => { 'name': 'barney', 'age': 36 }
6821 * // with explicit chaining
6822 * _(characters).chain()
6826 * // => { 'age': 36 }
6828 function wrapperChain() {
6829 this.__chain__ = true;
6834 * Produces the `toString` result of the wrapped value.
6838 * @category Chaining
6839 * @returns {string} Returns the string result.
6842 * _([1, 2, 3]).toString();
6845 function wrapperToString() {
6846 return String(this.__wrapped__);
6850 * Extracts the wrapped value.
6855 * @category Chaining
6856 * @returns {*} Returns the wrapped value.
6859 * _([1, 2, 3]).valueOf();
6862 function wrapperValueOf() {
6863 return this.__wrapped__;
6866 /*--------------------------------------------------------------------------*/
6868 // add functions that return wrapped values when chaining
6869 lodash.after = after;
6870 lodash.assign = assign;
6873 lodash.bindAll = bindAll;
6874 lodash.bindKey = bindKey;
6875 lodash.chain = chain;
6876 lodash.compact = compact;
6877 lodash.compose = compose;
6878 lodash.constant = constant;
6879 lodash.countBy = countBy;
6880 lodash.create = create;
6881 lodash.createCallback = createCallback;
6882 lodash.curry = curry;
6883 lodash.debounce = debounce;
6884 lodash.defaults = defaults;
6885 lodash.defer = defer;
6886 lodash.delay = delay;
6887 lodash.difference = difference;
6888 lodash.filter = filter;
6889 lodash.flatten = flatten;
6890 lodash.forEach = forEach;
6891 lodash.forEachRight = forEachRight;
6892 lodash.forIn = forIn;
6893 lodash.forInRight = forInRight;
6894 lodash.forOwn = forOwn;
6895 lodash.forOwnRight = forOwnRight;
6896 lodash.functions = functions;
6897 lodash.groupBy = groupBy;
6898 lodash.indexBy = indexBy;
6899 lodash.initial = initial;
6900 lodash.intersection = intersection;
6901 lodash.invert = invert;
6902 lodash.invoke = invoke;
6905 lodash.mapValues = mapValues;
6907 lodash.memoize = memoize;
6908 lodash.merge = merge;
6912 lodash.pairs = pairs;
6913 lodash.partial = partial;
6914 lodash.partialRight = partialRight;
6916 lodash.pluck = pluck;
6917 lodash.property = property;
6919 lodash.range = range;
6920 lodash.reject = reject;
6921 lodash.remove = remove;
6923 lodash.shuffle = shuffle;
6924 lodash.sortBy = sortBy;
6926 lodash.throttle = throttle;
6927 lodash.times = times;
6928 lodash.toArray = toArray;
6929 lodash.transform = transform;
6930 lodash.union = union;
6932 lodash.values = values;
6933 lodash.where = where;
6934 lodash.without = without;
6938 lodash.zipObject = zipObject;
6941 lodash.collect = map;
6943 lodash.each = forEach;
6944 lodash.eachRight = forEachRight;
6945 lodash.extend = assign;
6946 lodash.methods = functions;
6947 lodash.object = zipObject;
6948 lodash.select = filter;
6950 lodash.unique = uniq;
6953 // add functions to `lodash.prototype`
6956 /*--------------------------------------------------------------------------*/
6958 // add functions that return unwrapped values when chaining
6959 lodash.clone = clone;
6960 lodash.cloneDeep = cloneDeep;
6961 lodash.contains = contains;
6962 lodash.escape = escape;
6963 lodash.every = every;
6965 lodash.findIndex = findIndex;
6966 lodash.findKey = findKey;
6967 lodash.findLast = findLast;
6968 lodash.findLastIndex = findLastIndex;
6969 lodash.findLastKey = findLastKey;
6971 lodash.identity = identity;
6972 lodash.indexOf = indexOf;
6973 lodash.isArguments = isArguments;
6974 lodash.isArray = isArray;
6975 lodash.isBoolean = isBoolean;
6976 lodash.isDate = isDate;
6977 lodash.isElement = isElement;
6978 lodash.isEmpty = isEmpty;
6979 lodash.isEqual = isEqual;
6980 lodash.isFinite = isFinite;
6981 lodash.isFunction = isFunction;
6982 lodash.isNaN = isNaN;
6983 lodash.isNull = isNull;
6984 lodash.isNumber = isNumber;
6985 lodash.isObject = isObject;
6986 lodash.isPlainObject = isPlainObject;
6987 lodash.isRegExp = isRegExp;
6988 lodash.isString = isString;
6989 lodash.isUndefined = isUndefined;
6990 lodash.lastIndexOf = lastIndexOf;
6991 lodash.mixin = mixin;
6992 lodash.noConflict = noConflict;
6995 lodash.parseInt = parseInt;
6996 lodash.random = random;
6997 lodash.reduce = reduce;
6998 lodash.reduceRight = reduceRight;
6999 lodash.result = result;
7000 lodash.runInContext = runInContext;
7003 lodash.sortedIndex = sortedIndex;
7004 lodash.template = template;
7005 lodash.unescape = unescape;
7006 lodash.uniqueId = uniqueId;
7011 lodash.detect = find;
7012 lodash.findWhere = find;
7013 lodash.foldl = reduce;
7014 lodash.foldr = reduceRight;
7015 lodash.include = contains;
7016 lodash.inject = reduce;
7020 forOwn(lodash, function(func, methodName) {
7021 if (!lodash.prototype[methodName]) {
7022 source[methodName] = func;
7028 /*--------------------------------------------------------------------------*/
7030 // add functions capable of returning wrapped and unwrapped values when chaining
7031 lodash.first = first;
7033 lodash.sample = sample;
7036 lodash.take = first;
7037 lodash.head = first;
7039 forOwn(lodash, function(func, methodName) {
7040 var callbackable = methodName !== 'sample';
7041 if (!lodash.prototype[methodName]) {
7042 lodash.prototype[methodName]= function(n, guard) {
7043 var chainAll = this.__chain__,
7044 result = func(this.__wrapped__, n, guard);
7046 return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function')))
7048 : new lodashWrapper(result, chainAll);
7053 /*--------------------------------------------------------------------------*/
7056 * The semantic version number.
7062 lodash.VERSION = '2.4.2';
7064 // add "Chaining" functions to the wrapper
7065 lodash.prototype.chain = wrapperChain;
7066 lodash.prototype.toString = wrapperToString;
7067 lodash.prototype.value = wrapperValueOf;
7068 lodash.prototype.valueOf = wrapperValueOf;
7070 // add `Array` functions that return unwrapped values
7071 baseEach(['join', 'pop', 'shift'], function(methodName) {
7072 var func = arrayRef[methodName];
7073 lodash.prototype[methodName] = function() {
7074 var chainAll = this.__chain__,
7075 result = func.apply(this.__wrapped__, arguments);
7078 ? new lodashWrapper(result, chainAll)
7083 // add `Array` functions that return the existing wrapped value
7084 baseEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
7085 var func = arrayRef[methodName];
7086 lodash.prototype[methodName] = function() {
7087 func.apply(this.__wrapped__, arguments);
7092 // add `Array` functions that return new wrapped values
7093 baseEach(['concat', 'slice', 'splice'], function(methodName) {
7094 var func = arrayRef[methodName];
7095 lodash.prototype[methodName] = function() {
7096 return new lodashWrapper(func.apply(this.__wrapped__, arguments), this.__chain__);
7100 // avoid array-like object bugs with `Array#shift` and `Array#splice`
7101 // in IE < 9, Firefox < 10, Narwhal, and RingoJS
7102 if (!support.spliceObjects) {
7103 baseEach(['pop', 'shift', 'splice'], function(methodName) {
7104 var func = arrayRef[methodName],
7105 isSplice = methodName == 'splice';
7107 lodash.prototype[methodName] = function() {
7108 var chainAll = this.__chain__,
7109 value = this.__wrapped__,
7110 result = func.apply(value, arguments);
7112 if (value.length === 0) {
7115 return (chainAll || isSplice)
7116 ? new lodashWrapper(result, chainAll)
7125 /*--------------------------------------------------------------------------*/
7128 var _ = runInContext();
7130 // some AMD build optimizers like r.js check for condition patterns like the following:
7131 if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
7132 // Expose Lo-Dash to the global object even when an AMD loader is present in
7133 // case Lo-Dash is loaded with a RequireJS shim config.
7134 // See http://requirejs.org/docs/api.html#config-shim
7137 // define as an anonymous module so, through path mapping, it can be
7138 // referenced as the "underscore" module
7143 // check for `exports` after `define` in case a build optimizer adds an `exports` object
7144 else if (freeExports && freeModule) {
7145 // in Node.js or RingoJS
7146 if (moduleExports) {
7147 (freeModule.exports = _)._ = _;
7149 // in Narwhal or Rhino -require
7155 // in a browser or Rhino