2 // http://underscorejs.org
3 // (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
4 // Underscore may be freely distributed under the MIT license.
11 // Establish the root object, `window` in the browser, or `exports` on the server.
14 // Save the previous value of the `_` variable.
15 var previousUnderscore = root._;
17 // Save bytes in the minified (but not gzipped) version:
18 var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
20 // Create quick reference variables for speed access to core prototypes.
22 push = ArrayProto.push,
23 slice = ArrayProto.slice,
24 toString = ObjProto.toString,
25 hasOwnProperty = ObjProto.hasOwnProperty;
27 // All **ECMAScript 5** native function implementations that we hope to use
30 nativeIsArray = Array.isArray,
31 nativeKeys = Object.keys,
32 nativeBind = FuncProto.bind,
33 nativeCreate = Object.create;
35 // Naked function reference for surrogate-prototype-swapping.
36 var Ctor = function(){};
38 // Create a safe reference to the Underscore object for use below.
39 var _ = function(obj) {
40 if (obj instanceof _) return obj;
41 if (!(this instanceof _)) return new _(obj);
45 // Export the Underscore object for **Node.js**, with
46 // backwards-compatibility for the old `require()` API. If we're in
47 // the browser, add `_` as a global object.
48 if (typeof exports !== 'undefined') {
49 if (typeof module !== 'undefined' && module.exports) {
50 exports = module.exports = _;
60 // Internal function that returns an efficient (for current engines) version
61 // of the passed-in callback, to be repeatedly applied in other Underscore
63 var optimizeCb = function(func, context, argCount) {
64 if (context === void 0) return func;
65 switch (argCount == null ? 3 : argCount) {
66 case 1: return function(value) {
67 return func.call(context, value);
69 case 2: return function(value, other) {
70 return func.call(context, value, other);
72 case 3: return function(value, index, collection) {
73 return func.call(context, value, index, collection);
75 case 4: return function(accumulator, value, index, collection) {
76 return func.call(context, accumulator, value, index, collection);
80 return func.apply(context, arguments);
84 // A mostly-internal function to generate callbacks that can be applied
85 // to each element in a collection, returning the desired result — either
86 // identity, an arbitrary callback, a property matcher, or a property accessor.
87 var cb = function(value, context, argCount) {
88 if (value == null) return _.identity;
89 if (_.isFunction(value)) return optimizeCb(value, context, argCount);
90 if (_.isObject(value)) return _.matcher(value);
91 return _.property(value);
93 _.iteratee = function(value, context) {
94 return cb(value, context, Infinity);
97 // An internal function for creating assigner functions.
98 var createAssigner = function(keysFunc, undefinedOnly) {
99 return function(obj) {
100 var length = arguments.length;
101 if (length < 2 || obj == null) return obj;
102 for (var index = 1; index < length; index++) {
103 var source = arguments[index],
104 keys = keysFunc(source),
106 for (var i = 0; i < l; i++) {
108 if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key];
115 // An internal function for creating a new object that inherits from another.
116 var baseCreate = function(prototype) {
117 if (!_.isObject(prototype)) return {};
118 if (nativeCreate) return nativeCreate(prototype);
119 Ctor.prototype = prototype;
120 var result = new Ctor;
121 Ctor.prototype = null;
125 // Helper for collection methods to determine whether a collection
126 // should be iterated as an array or as an object
127 // Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
128 var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
129 var isArrayLike = function(collection) {
130 var length = collection && collection.length;
131 return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
134 // Collection Functions
135 // --------------------
137 // The cornerstone, an `each` implementation, aka `forEach`.
138 // Handles raw objects in addition to array-likes. Treats all
139 // sparse array-likes as if they were dense.
140 _.each = _.forEach = function(obj, iteratee, context) {
141 iteratee = optimizeCb(iteratee, context);
143 if (isArrayLike(obj)) {
144 for (i = 0, length = obj.length; i < length; i++) {
145 iteratee(obj[i], i, obj);
148 var keys = _.keys(obj);
149 for (i = 0, length = keys.length; i < length; i++) {
150 iteratee(obj[keys[i]], keys[i], obj);
156 // Return the results of applying the iteratee to each element.
157 _.map = _.collect = function(obj, iteratee, context) {
158 iteratee = cb(iteratee, context);
159 var keys = !isArrayLike(obj) && _.keys(obj),
160 length = (keys || obj).length,
161 results = Array(length);
162 for (var index = 0; index < length; index++) {
163 var currentKey = keys ? keys[index] : index;
164 results[index] = iteratee(obj[currentKey], currentKey, obj);
169 // Create a reducing function iterating left or right.
170 function createReduce(dir) {
171 // Optimized iterator function as using arguments.length
172 // in the main function will deoptimize the, see #1991.
173 function iterator(obj, iteratee, memo, keys, index, length) {
174 for (; index >= 0 && index < length; index += dir) {
175 var currentKey = keys ? keys[index] : index;
176 memo = iteratee(memo, obj[currentKey], currentKey, obj);
181 return function(obj, iteratee, memo, context) {
182 iteratee = optimizeCb(iteratee, context, 4);
183 var keys = !isArrayLike(obj) && _.keys(obj),
184 length = (keys || obj).length,
185 index = dir > 0 ? 0 : length - 1;
186 // Determine the initial value if none is provided.
187 if (arguments.length < 3) {
188 memo = obj[keys ? keys[index] : index];
191 return iterator(obj, iteratee, memo, keys, index, length);
195 // **Reduce** builds up a single result from a list of values, aka `inject`,
197 _.reduce = _.foldl = _.inject = createReduce(1);
199 // The right-associative version of reduce, also known as `foldr`.
200 _.reduceRight = _.foldr = createReduce(-1);
202 // Return the first value which passes a truth test. Aliased as `detect`.
203 _.find = _.detect = function(obj, predicate, context) {
205 if (isArrayLike(obj)) {
206 key = _.findIndex(obj, predicate, context);
208 key = _.findKey(obj, predicate, context);
210 if (key !== void 0 && key !== -1) return obj[key];
213 // Return all the elements that pass a truth test.
214 // Aliased as `select`.
215 _.filter = _.select = function(obj, predicate, context) {
217 predicate = cb(predicate, context);
218 _.each(obj, function(value, index, list) {
219 if (predicate(value, index, list)) results.push(value);
224 // Return all the elements for which a truth test fails.
225 _.reject = function(obj, predicate, context) {
226 return _.filter(obj, _.negate(cb(predicate)), context);
229 // Determine whether all of the elements match a truth test.
231 _.every = _.all = function(obj, predicate, context) {
232 predicate = cb(predicate, context);
233 var keys = !isArrayLike(obj) && _.keys(obj),
234 length = (keys || obj).length;
235 for (var index = 0; index < length; index++) {
236 var currentKey = keys ? keys[index] : index;
237 if (!predicate(obj[currentKey], currentKey, obj)) return false;
242 // Determine if at least one element in the object matches a truth test.
244 _.some = _.any = function(obj, predicate, context) {
245 predicate = cb(predicate, context);
246 var keys = !isArrayLike(obj) && _.keys(obj),
247 length = (keys || obj).length;
248 for (var index = 0; index < length; index++) {
249 var currentKey = keys ? keys[index] : index;
250 if (predicate(obj[currentKey], currentKey, obj)) return true;
255 // Determine if the array or object contains a given value (using `===`).
256 // Aliased as `includes` and `include`.
257 _.contains = _.includes = _.include = function(obj, target, fromIndex) {
258 if (!isArrayLike(obj)) obj = _.values(obj);
259 return _.indexOf(obj, target, typeof fromIndex == 'number' && fromIndex) >= 0;
262 // Invoke a method (with arguments) on every item in a collection.
263 _.invoke = function(obj, method) {
264 var args = slice.call(arguments, 2);
265 var isFunc = _.isFunction(method);
266 return _.map(obj, function(value) {
267 var func = isFunc ? method : value[method];
268 return func == null ? func : func.apply(value, args);
272 // Convenience version of a common use case of `map`: fetching a property.
273 _.pluck = function(obj, key) {
274 return _.map(obj, _.property(key));
277 // Convenience version of a common use case of `filter`: selecting only objects
278 // containing specific `key:value` pairs.
279 _.where = function(obj, attrs) {
280 return _.filter(obj, _.matcher(attrs));
283 // Convenience version of a common use case of `find`: getting the first object
284 // containing specific `key:value` pairs.
285 _.findWhere = function(obj, attrs) {
286 return _.find(obj, _.matcher(attrs));
289 // Return the maximum element (or element-based computation).
290 _.max = function(obj, iteratee, context) {
291 var result = -Infinity, lastComputed = -Infinity,
293 if (iteratee == null && obj != null) {
294 obj = isArrayLike(obj) ? obj : _.values(obj);
295 for (var i = 0, length = obj.length; i < length; i++) {
297 if (value > result) {
302 iteratee = cb(iteratee, context);
303 _.each(obj, function(value, index, list) {
304 computed = iteratee(value, index, list);
305 if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
307 lastComputed = computed;
314 // Return the minimum element (or element-based computation).
315 _.min = function(obj, iteratee, context) {
316 var result = Infinity, lastComputed = Infinity,
318 if (iteratee == null && obj != null) {
319 obj = isArrayLike(obj) ? obj : _.values(obj);
320 for (var i = 0, length = obj.length; i < length; i++) {
322 if (value < result) {
327 iteratee = cb(iteratee, context);
328 _.each(obj, function(value, index, list) {
329 computed = iteratee(value, index, list);
330 if (computed < lastComputed || computed === Infinity && result === Infinity) {
332 lastComputed = computed;
339 // Shuffle a collection, using the modern version of the
340 // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
341 _.shuffle = function(obj) {
342 var set = isArrayLike(obj) ? obj : _.values(obj);
343 var length = set.length;
344 var shuffled = Array(length);
345 for (var index = 0, rand; index < length; index++) {
346 rand = _.random(0, index);
347 if (rand !== index) shuffled[index] = shuffled[rand];
348 shuffled[rand] = set[index];
353 // Sample **n** random values from a collection.
354 // If **n** is not specified, returns a single random element.
355 // The internal `guard` argument allows it to work with `map`.
356 _.sample = function(obj, n, guard) {
357 if (n == null || guard) {
358 if (!isArrayLike(obj)) obj = _.values(obj);
359 return obj[_.random(obj.length - 1)];
361 return _.shuffle(obj).slice(0, Math.max(0, n));
364 // Sort the object's values by a criterion produced by an iteratee.
365 _.sortBy = function(obj, iteratee, context) {
366 iteratee = cb(iteratee, context);
367 return _.pluck(_.map(obj, function(value, index, list) {
371 criteria: iteratee(value, index, list)
373 }).sort(function(left, right) {
374 var a = left.criteria;
375 var b = right.criteria;
377 if (a > b || a === void 0) return 1;
378 if (a < b || b === void 0) return -1;
380 return left.index - right.index;
384 // An internal function used for aggregate "group by" operations.
385 var group = function(behavior) {
386 return function(obj, iteratee, context) {
388 iteratee = cb(iteratee, context);
389 _.each(obj, function(value, index) {
390 var key = iteratee(value, index, obj);
391 behavior(result, value, key);
397 // Groups the object's values by a criterion. Pass either a string attribute
398 // to group by, or a function that returns the criterion.
399 _.groupBy = group(function(result, value, key) {
400 if (_.has(result, key)) result[key].push(value); else result[key] = [value];
403 // Indexes the object's values by a criterion, similar to `groupBy`, but for
404 // when you know that your index values will be unique.
405 _.indexBy = group(function(result, value, key) {
409 // Counts instances of an object that group by a certain criterion. Pass
410 // either a string attribute to count by, or a function that returns the
412 _.countBy = group(function(result, value, key) {
413 if (_.has(result, key)) result[key]++; else result[key] = 1;
416 // Safely create a real, live array from anything iterable.
417 _.toArray = function(obj) {
419 if (_.isArray(obj)) return slice.call(obj);
420 if (isArrayLike(obj)) return _.map(obj, _.identity);
421 return _.values(obj);
424 // Return the number of elements in an object.
425 _.size = function(obj) {
426 if (obj == null) return 0;
427 return isArrayLike(obj) ? obj.length : _.keys(obj).length;
430 // Split a collection into two arrays: one whose elements all satisfy the given
431 // predicate, and one whose elements all do not satisfy the predicate.
432 _.partition = function(obj, predicate, context) {
433 predicate = cb(predicate, context);
434 var pass = [], fail = [];
435 _.each(obj, function(value, key, obj) {
436 (predicate(value, key, obj) ? pass : fail).push(value);
444 // Get the first element of an array. Passing **n** will return the first N
445 // values in the array. Aliased as `head` and `take`. The **guard** check
446 // allows it to work with `_.map`.
447 _.first = _.head = _.take = function(array, n, guard) {
448 if (array == null) return void 0;
449 if (n == null || guard) return array[0];
450 return _.initial(array, array.length - n);
453 // Returns everything but the last entry of the array. Especially useful on
454 // the arguments object. Passing **n** will return all the values in
455 // the array, excluding the last N.
456 _.initial = function(array, n, guard) {
457 return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
460 // Get the last element of an array. Passing **n** will return the last N
461 // values in the array.
462 _.last = function(array, n, guard) {
463 if (array == null) return void 0;
464 if (n == null || guard) return array[array.length - 1];
465 return _.rest(array, Math.max(0, array.length - n));
468 // Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
469 // Especially useful on the arguments object. Passing an **n** will return
470 // the rest N values in the array.
471 _.rest = _.tail = _.drop = function(array, n, guard) {
472 return slice.call(array, n == null || guard ? 1 : n);
475 // Trim out all falsy values from an array.
476 _.compact = function(array) {
477 return _.filter(array, _.identity);
480 // Internal implementation of a recursive `flatten` function.
481 var flatten = function(input, shallow, strict, startIndex) {
482 var output = [], idx = 0;
483 for (var i = startIndex || 0, length = input && input.length; i < length; i++) {
484 var value = input[i];
485 if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
486 //flatten current level of array or arguments object
487 if (!shallow) value = flatten(value, shallow, strict);
488 var j = 0, len = value.length;
489 output.length += len;
491 output[idx++] = value[j++];
493 } else if (!strict) {
494 output[idx++] = value;
500 // Flatten out an array, either recursively (by default), or just one level.
501 _.flatten = function(array, shallow) {
502 return flatten(array, shallow, false);
505 // Return a version of the array that does not contain the specified value(s).
506 _.without = function(array) {
507 return _.difference(array, slice.call(arguments, 1));
510 // Produce a duplicate-free version of the array. If the array has already
511 // been sorted, you have the option of using a faster algorithm.
512 // Aliased as `unique`.
513 _.uniq = _.unique = function(array, isSorted, iteratee, context) {
514 if (array == null) return [];
515 if (!_.isBoolean(isSorted)) {
520 if (iteratee != null) iteratee = cb(iteratee, context);
523 for (var i = 0, length = array.length; i < length; i++) {
524 var value = array[i],
525 computed = iteratee ? iteratee(value, i, array) : value;
527 if (!i || seen !== computed) result.push(value);
529 } else if (iteratee) {
530 if (!_.contains(seen, computed)) {
534 } else if (!_.contains(result, value)) {
541 // Produce an array that contains the union: each distinct element from all of
542 // the passed-in arrays.
543 _.union = function() {
544 return _.uniq(flatten(arguments, true, true));
547 // Produce an array that contains every item shared between all the
549 _.intersection = function(array) {
550 if (array == null) return [];
552 var argsLength = arguments.length;
553 for (var i = 0, length = array.length; i < length; i++) {
555 if (_.contains(result, item)) continue;
556 for (var j = 1; j < argsLength; j++) {
557 if (!_.contains(arguments[j], item)) break;
559 if (j === argsLength) result.push(item);
564 // Take the difference between one array and a number of other arrays.
565 // Only the elements present in just the first array will remain.
566 _.difference = function(array) {
567 var rest = flatten(arguments, true, true, 1);
568 return _.filter(array, function(value){
569 return !_.contains(rest, value);
573 // Zip together multiple lists into a single array -- elements that share
574 // an index go together.
576 return _.unzip(arguments);
579 // Complement of _.zip. Unzip accepts an array of arrays and groups
580 // each array's elements on shared indices
581 _.unzip = function(array) {
582 var length = array && _.max(array, 'length').length || 0;
583 var result = Array(length);
585 for (var index = 0; index < length; index++) {
586 result[index] = _.pluck(array, index);
591 // Converts lists into objects. Pass either a single array of `[key, value]`
592 // pairs, or two parallel arrays of the same length -- one of keys, and one of
593 // the corresponding values.
594 _.object = function(list, values) {
596 for (var i = 0, length = list && list.length; i < length; i++) {
598 result[list[i]] = values[i];
600 result[list[i][0]] = list[i][1];
606 // Return the position of the first occurrence of an item in an array,
607 // or -1 if the item is not included in the array.
608 // If the array is large and already in sort order, pass `true`
609 // for **isSorted** to use binary search.
610 _.indexOf = function(array, item, isSorted) {
611 var i = 0, length = array && array.length;
612 if (typeof isSorted == 'number') {
613 i = isSorted < 0 ? Math.max(0, length + isSorted) : isSorted;
614 } else if (isSorted && length) {
615 i = _.sortedIndex(array, item);
616 return array[i] === item ? i : -1;
619 return _.findIndex(slice.call(array, i), _.isNaN);
621 for (; i < length; i++) if (array[i] === item) return i;
625 _.lastIndexOf = function(array, item, from) {
626 var idx = array ? array.length : 0;
627 if (typeof from == 'number') {
628 idx = from < 0 ? idx + from + 1 : Math.min(idx, from + 1);
631 return _.findLastIndex(slice.call(array, 0, idx), _.isNaN);
633 while (--idx >= 0) if (array[idx] === item) return idx;
637 // Generator function to create the findIndex and findLastIndex functions
638 function createIndexFinder(dir) {
639 return function(array, predicate, context) {
640 predicate = cb(predicate, context);
641 var length = array != null && array.length;
642 var index = dir > 0 ? 0 : length - 1;
643 for (; index >= 0 && index < length; index += dir) {
644 if (predicate(array[index], index, array)) return index;
650 // Returns the first index on an array-like that passes a predicate test
651 _.findIndex = createIndexFinder(1);
653 _.findLastIndex = createIndexFinder(-1);
655 // Use a comparator function to figure out the smallest index at which
656 // an object should be inserted so as to maintain order. Uses binary search.
657 _.sortedIndex = function(array, obj, iteratee, context) {
658 iteratee = cb(iteratee, context, 1);
659 var value = iteratee(obj);
660 var low = 0, high = array.length;
662 var mid = Math.floor((low + high) / 2);
663 if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
668 // Generate an integer Array containing an arithmetic progression. A port of
669 // the native Python `range()` function. See
670 // [the Python documentation](http://docs.python.org/library/functions.html#range).
671 _.range = function(start, stop, step) {
672 if (arguments.length <= 1) {
678 var length = Math.max(Math.ceil((stop - start) / step), 0);
679 var range = Array(length);
681 for (var idx = 0; idx < length; idx++, start += step) {
688 // Function (ahem) Functions
689 // ------------------
691 // Determines whether to execute a function as a constructor
692 // or a normal function with the provided arguments
693 var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
694 if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
695 var self = baseCreate(sourceFunc.prototype);
696 var result = sourceFunc.apply(self, args);
697 if (_.isObject(result)) return result;
701 // Create a function bound to a given object (assigning `this`, and arguments,
702 // optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
704 _.bind = function(func, context) {
705 if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
706 if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function');
707 var args = slice.call(arguments, 2);
708 var bound = function() {
709 return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
714 // Partially apply a function by creating a version that has had some of its
715 // arguments pre-filled, without changing its dynamic `this` context. _ acts
716 // as a placeholder, allowing any combination of arguments to be pre-filled.
717 _.partial = function(func) {
718 var boundArgs = slice.call(arguments, 1);
719 var bound = function() {
720 var position = 0, length = boundArgs.length;
721 var args = Array(length);
722 for (var i = 0; i < length; i++) {
723 args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i];
725 while (position < arguments.length) args.push(arguments[position++]);
726 return executeBound(func, bound, this, this, args);
731 // Bind a number of an object's methods to that object. Remaining arguments
732 // are the method names to be bound. Useful for ensuring that all callbacks
733 // defined on an object belong to it.
734 _.bindAll = function(obj) {
735 var i, length = arguments.length, key;
736 if (length <= 1) throw new Error('bindAll must be passed function names');
737 for (i = 1; i < length; i++) {
739 obj[key] = _.bind(obj[key], obj);
744 // Memoize an expensive function by storing its results.
745 _.memoize = function(func, hasher) {
746 var memoize = function(key) {
747 var cache = memoize.cache;
748 var address = '' + (hasher ? hasher.apply(this, arguments) : key);
749 if (!_.has(cache, address)) cache[address] = func.apply(this, arguments);
750 return cache[address];
756 // Delays a function for the given number of milliseconds, and then calls
757 // it with the arguments supplied.
758 _.delay = function(func, wait) {
759 var args = slice.call(arguments, 2);
760 return setTimeout(function(){
761 return func.apply(null, args);
765 // Defers a function, scheduling it to run after the current call stack has
767 _.defer = _.partial(_.delay, _, 1);
769 // Returns a function, that, when invoked, will only be triggered at most once
770 // during a given window of time. Normally, the throttled function will run
771 // as much as it can, without ever going more than once per `wait` duration;
772 // but if you'd like to disable the execution on the leading edge, pass
773 // `{leading: false}`. To disable execution on the trailing edge, ditto.
774 _.throttle = function(func, wait, options) {
775 var context, args, result;
778 if (!options) options = {};
779 var later = function() {
780 previous = options.leading === false ? 0 : _.now();
782 result = func.apply(context, args);
783 if (!timeout) context = args = null;
787 if (!previous && options.leading === false) previous = now;
788 var remaining = wait - (now - previous);
791 if (remaining <= 0 || remaining > wait) {
793 clearTimeout(timeout);
797 result = func.apply(context, args);
798 if (!timeout) context = args = null;
799 } else if (!timeout && options.trailing !== false) {
800 timeout = setTimeout(later, remaining);
806 // Returns a function, that, as long as it continues to be invoked, will not
807 // be triggered. The function will be called after it stops being called for
808 // N milliseconds. If `immediate` is passed, trigger the function on the
809 // leading edge, instead of the trailing.
810 _.debounce = function(func, wait, immediate) {
811 var timeout, args, context, timestamp, result;
813 var later = function() {
814 var last = _.now() - timestamp;
816 if (last < wait && last >= 0) {
817 timeout = setTimeout(later, wait - last);
821 result = func.apply(context, args);
822 if (!timeout) context = args = null;
831 var callNow = immediate && !timeout;
832 if (!timeout) timeout = setTimeout(later, wait);
834 result = func.apply(context, args);
835 context = args = null;
842 // Returns the first function passed as an argument to the second,
843 // allowing you to adjust arguments, run code before and after, and
844 // conditionally execute the original function.
845 _.wrap = function(func, wrapper) {
846 return _.partial(wrapper, func);
849 // Returns a negated version of the passed-in predicate.
850 _.negate = function(predicate) {
852 return !predicate.apply(this, arguments);
856 // Returns a function that is the composition of a list of functions, each
857 // consuming the return value of the function that follows.
858 _.compose = function() {
859 var args = arguments;
860 var start = args.length - 1;
863 var result = args[start].apply(this, arguments);
864 while (i--) result = args[i].call(this, result);
869 // Returns a function that will only be executed on and after the Nth call.
870 _.after = function(times, func) {
873 return func.apply(this, arguments);
878 // Returns a function that will only be executed up to (but not including) the Nth call.
879 _.before = function(times, func) {
883 memo = func.apply(this, arguments);
885 if (times <= 1) func = null;
890 // Returns a function that will be executed at most one time, no matter how
891 // often you call it. Useful for lazy initialization.
892 _.once = _.partial(_.before, 2);
897 // Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.
898 var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString');
899 var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
900 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
902 function collectNonEnumProps(obj, keys) {
903 var nonEnumIdx = nonEnumerableProps.length;
904 var constructor = obj.constructor;
905 var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto;
907 // Constructor is a special case.
908 var prop = 'constructor';
909 if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop);
911 while (nonEnumIdx--) {
912 prop = nonEnumerableProps[nonEnumIdx];
913 if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) {
919 // Retrieve the names of an object's own properties.
920 // Delegates to **ECMAScript 5**'s native `Object.keys`
921 _.keys = function(obj) {
922 if (!_.isObject(obj)) return [];
923 if (nativeKeys) return nativeKeys(obj);
925 for (var key in obj) if (_.has(obj, key)) keys.push(key);
927 if (hasEnumBug) collectNonEnumProps(obj, keys);
931 // Retrieve all the property names of an object.
932 _.allKeys = function(obj) {
933 if (!_.isObject(obj)) return [];
935 for (var key in obj) keys.push(key);
937 if (hasEnumBug) collectNonEnumProps(obj, keys);
941 // Retrieve the values of an object's properties.
942 _.values = function(obj) {
943 var keys = _.keys(obj);
944 var length = keys.length;
945 var values = Array(length);
946 for (var i = 0; i < length; i++) {
947 values[i] = obj[keys[i]];
952 // Returns the results of applying the iteratee to each element of the object
953 // In contrast to _.map it returns an object
954 _.mapObject = function(obj, iteratee, context) {
955 iteratee = cb(iteratee, context);
956 var keys = _.keys(obj),
957 length = keys.length,
960 for (var index = 0; index < length; index++) {
961 currentKey = keys[index];
962 results[currentKey] = iteratee(obj[currentKey], currentKey, obj);
967 // Convert an object into a list of `[key, value]` pairs.
968 _.pairs = function(obj) {
969 var keys = _.keys(obj);
970 var length = keys.length;
971 var pairs = Array(length);
972 for (var i = 0; i < length; i++) {
973 pairs[i] = [keys[i], obj[keys[i]]];
978 // Invert the keys and values of an object. The values must be serializable.
979 _.invert = function(obj) {
981 var keys = _.keys(obj);
982 for (var i = 0, length = keys.length; i < length; i++) {
983 result[obj[keys[i]]] = keys[i];
988 // Return a sorted list of the function names available on the object.
989 // Aliased as `methods`
990 _.functions = _.methods = function(obj) {
992 for (var key in obj) {
993 if (_.isFunction(obj[key])) names.push(key);
998 // Extend a given object with all the properties in passed-in object(s).
999 _.extend = createAssigner(_.allKeys);
1001 // Assigns a given object with all the own properties in the passed-in object(s)
1002 // (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
1003 _.extendOwn = _.assign = createAssigner(_.keys);
1005 // Returns the first key on an object that passes a predicate test
1006 _.findKey = function(obj, predicate, context) {
1007 predicate = cb(predicate, context);
1008 var keys = _.keys(obj), key;
1009 for (var i = 0, length = keys.length; i < length; i++) {
1011 if (predicate(obj[key], key, obj)) return key;
1015 // Return a copy of the object only containing the whitelisted properties.
1016 _.pick = function(object, oiteratee, context) {
1017 var result = {}, obj = object, iteratee, keys;
1018 if (obj == null) return result;
1019 if (_.isFunction(oiteratee)) {
1020 keys = _.allKeys(obj);
1021 iteratee = optimizeCb(oiteratee, context);
1023 keys = flatten(arguments, false, false, 1);
1024 iteratee = function(value, key, obj) { return key in obj; };
1027 for (var i = 0, length = keys.length; i < length; i++) {
1029 var value = obj[key];
1030 if (iteratee(value, key, obj)) result[key] = value;
1035 // Return a copy of the object without the blacklisted properties.
1036 _.omit = function(obj, iteratee, context) {
1037 if (_.isFunction(iteratee)) {
1038 iteratee = _.negate(iteratee);
1040 var keys = _.map(flatten(arguments, false, false, 1), String);
1041 iteratee = function(value, key) {
1042 return !_.contains(keys, key);
1045 return _.pick(obj, iteratee, context);
1048 // Fill in a given object with default properties.
1049 _.defaults = createAssigner(_.allKeys, true);
1051 // Create a (shallow-cloned) duplicate of an object.
1052 _.clone = function(obj) {
1053 if (!_.isObject(obj)) return obj;
1054 return _.isArray(obj) ? obj.slice() : _.extend({}, obj);
1057 // Invokes interceptor with the obj, and then returns obj.
1058 // The primary purpose of this method is to "tap into" a method chain, in
1059 // order to perform operations on intermediate results within the chain.
1060 _.tap = function(obj, interceptor) {
1065 // Returns whether an object has a given set of `key:value` pairs.
1066 _.isMatch = function(object, attrs) {
1067 var keys = _.keys(attrs), length = keys.length;
1068 if (object == null) return !length;
1069 var obj = Object(object);
1070 for (var i = 0; i < length; i++) {
1072 if (attrs[key] !== obj[key] || !(key in obj)) return false;
1078 // Internal recursive comparison function for `isEqual`.
1079 var eq = function(a, b, aStack, bStack) {
1080 // Identical objects are equal. `0 === -0`, but they aren't identical.
1081 // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
1082 if (a === b) return a !== 0 || 1 / a === 1 / b;
1083 // A strict comparison is necessary because `null == undefined`.
1084 if (a == null || b == null) return a === b;
1085 // Unwrap any wrapped objects.
1086 if (a instanceof _) a = a._wrapped;
1087 if (b instanceof _) b = b._wrapped;
1088 // Compare `[[Class]]` names.
1089 var className = toString.call(a);
1090 if (className !== toString.call(b)) return false;
1091 switch (className) {
1092 // Strings, numbers, regular expressions, dates, and booleans are compared by value.
1093 case '[object RegExp]':
1094 // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
1095 case '[object String]':
1096 // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
1097 // equivalent to `new String("5")`.
1098 return '' + a === '' + b;
1099 case '[object Number]':
1100 // `NaN`s are equivalent, but non-reflexive.
1101 // Object(NaN) is equivalent to NaN
1102 if (+a !== +a) return +b !== +b;
1103 // An `egal` comparison is performed for other numeric values.
1104 return +a === 0 ? 1 / +a === 1 / b : +a === +b;
1105 case '[object Date]':
1106 case '[object Boolean]':
1107 // Coerce dates and booleans to numeric primitive values. Dates are compared by their
1108 // millisecond representations. Note that invalid dates with millisecond representations
1109 // of `NaN` are not equivalent.
1113 var areArrays = className === '[object Array]';
1115 if (typeof a != 'object' || typeof b != 'object') return false;
1117 // Objects with different constructors are not equivalent, but `Object`s or `Array`s
1118 // from different frames are.
1119 var aCtor = a.constructor, bCtor = b.constructor;
1120 if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor &&
1121 _.isFunction(bCtor) && bCtor instanceof bCtor)
1122 && ('constructor' in a && 'constructor' in b)) {
1126 // Assume equality for cyclic structures. The algorithm for detecting cyclic
1127 // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
1129 // Initializing stack of traversed objects.
1130 // It's done here since we only need them for objects and arrays comparison.
1131 aStack = aStack || [];
1132 bStack = bStack || [];
1133 var length = aStack.length;
1135 // Linear search. Performance is inversely proportional to the number of
1136 // unique nested structures.
1137 if (aStack[length] === a) return bStack[length] === b;
1140 // Add the first object to the stack of traversed objects.
1144 // Recursively compare objects and arrays.
1146 // Compare array lengths to determine if a deep comparison is necessary.
1148 if (length !== b.length) return false;
1149 // Deep compare the contents, ignoring non-numeric properties.
1151 if (!eq(a[length], b[length], aStack, bStack)) return false;
1154 // Deep compare objects.
1155 var keys = _.keys(a), key;
1156 length = keys.length;
1157 // Ensure that both objects contain the same number of properties before comparing deep equality.
1158 if (_.keys(b).length !== length) return false;
1160 // Deep compare each member
1162 if (!(_.has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
1165 // Remove the first object from the stack of traversed objects.
1171 // Perform a deep comparison to check if two objects are equal.
1172 _.isEqual = function(a, b) {
1176 // Is a given array, string, or object empty?
1177 // An "empty" object has no enumerable own-properties.
1178 _.isEmpty = function(obj) {
1179 if (obj == null) return true;
1180 if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0;
1181 return _.keys(obj).length === 0;
1184 // Is a given value a DOM element?
1185 _.isElement = function(obj) {
1186 return !!(obj && obj.nodeType === 1);
1189 // Is a given value an array?
1190 // Delegates to ECMA5's native Array.isArray
1191 _.isArray = nativeIsArray || function(obj) {
1192 return toString.call(obj) === '[object Array]';
1195 // Is a given variable an object?
1196 _.isObject = function(obj) {
1197 var type = typeof obj;
1198 return type === 'function' || type === 'object' && !!obj;
1201 // Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError.
1202 _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) {
1203 _['is' + name] = function(obj) {
1204 return toString.call(obj) === '[object ' + name + ']';
1208 // Define a fallback version of the method in browsers (ahem, IE < 9), where
1209 // there isn't any inspectable "Arguments" type.
1210 if (!_.isArguments(arguments)) {
1211 _.isArguments = function(obj) {
1212 return _.has(obj, 'callee');
1216 // Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,
1217 // IE 11 (#1621), and in Safari 8 (#1929).
1218 if (typeof /./ != 'function' && typeof Int8Array != 'object') {
1219 _.isFunction = function(obj) {
1220 return typeof obj == 'function' || false;
1224 // Is a given object a finite number?
1225 _.isFinite = function(obj) {
1226 return isFinite(obj) && !isNaN(parseFloat(obj));
1229 // Is the given value `NaN`? (NaN is the only number which does not equal itself).
1230 _.isNaN = function(obj) {
1231 return _.isNumber(obj) && obj !== +obj;
1234 // Is a given value a boolean?
1235 _.isBoolean = function(obj) {
1236 return obj === true || obj === false || toString.call(obj) === '[object Boolean]';
1239 // Is a given value equal to null?
1240 _.isNull = function(obj) {
1241 return obj === null;
1244 // Is a given variable undefined?
1245 _.isUndefined = function(obj) {
1246 return obj === void 0;
1249 // Shortcut function for checking if an object has a given property directly
1250 // on itself (in other words, not on a prototype).
1251 _.has = function(obj, key) {
1252 return obj != null && hasOwnProperty.call(obj, key);
1255 // Utility Functions
1256 // -----------------
1258 // Run Underscore.js in *noConflict* mode, returning the `_` variable to its
1259 // previous owner. Returns a reference to the Underscore object.
1260 _.noConflict = function() {
1261 root._ = previousUnderscore;
1265 // Keep the identity function around for default iteratees.
1266 _.identity = function(value) {
1270 // Predicate-generating functions. Often useful outside of Underscore.
1271 _.constant = function(value) {
1277 _.noop = function(){};
1279 _.property = function(key) {
1280 return function(obj) {
1281 return obj == null ? void 0 : obj[key];
1285 // Generates a function for a given object that returns a given property.
1286 _.propertyOf = function(obj) {
1287 return obj == null ? function(){} : function(key) {
1292 // Returns a predicate for checking whether an object has a given set of
1293 // `key:value` pairs.
1294 _.matcher = _.matches = function(attrs) {
1295 attrs = _.extendOwn({}, attrs);
1296 return function(obj) {
1297 return _.isMatch(obj, attrs);
1301 // Run a function **n** times.
1302 _.times = function(n, iteratee, context) {
1303 var accum = Array(Math.max(0, n));
1304 iteratee = optimizeCb(iteratee, context, 1);
1305 for (var i = 0; i < n; i++) accum[i] = iteratee(i);
1309 // Return a random integer between min and max (inclusive).
1310 _.random = function(min, max) {
1315 return min + Math.floor(Math.random() * (max - min + 1));
1318 // A (possibly faster) way to get the current timestamp as an integer.
1319 _.now = Date.now || function() {
1320 return new Date().getTime();
1323 // List of HTML entities for escaping.
1332 var unescapeMap = _.invert(escapeMap);
1334 // Functions for escaping and unescaping strings to/from HTML interpolation.
1335 var createEscaper = function(map) {
1336 var escaper = function(match) {
1339 // Regexes for identifying a key that needs to be escaped
1340 var source = '(?:' + _.keys(map).join('|') + ')';
1341 var testRegexp = RegExp(source);
1342 var replaceRegexp = RegExp(source, 'g');
1343 return function(string) {
1344 string = string == null ? '' : '' + string;
1345 return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
1348 _.escape = createEscaper(escapeMap);
1349 _.unescape = createEscaper(unescapeMap);
1351 // If the value of the named `property` is a function then invoke it with the
1352 // `object` as context; otherwise, return it.
1353 _.result = function(object, property, fallback) {
1354 var value = object == null ? void 0 : object[property];
1355 if (value === void 0) {
1358 return _.isFunction(value) ? value.call(object) : value;
1361 // Generate a unique integer id (unique within the entire client session).
1362 // Useful for temporary DOM ids.
1364 _.uniqueId = function(prefix) {
1365 var id = ++idCounter + '';
1366 return prefix ? prefix + id : id;
1369 // By default, Underscore uses ERB-style template delimiters, change the
1370 // following template settings to use alternative delimiters.
1371 _.templateSettings = {
1372 evaluate : /<%([\s\S]+?)%>/g,
1373 interpolate : /<%=([\s\S]+?)%>/g,
1374 escape : /<%-([\s\S]+?)%>/g
1377 // When customizing `templateSettings`, if you don't want to define an
1378 // interpolation, evaluation or escaping regex, we need one that is
1379 // guaranteed not to match.
1380 var noMatch = /(.)^/;
1382 // Certain characters need to be escaped so that they can be put into a
1393 var escaper = /\\|'|\r|\n|\u2028|\u2029/g;
1395 var escapeChar = function(match) {
1396 return '\\' + escapes[match];
1399 // JavaScript micro-templating, similar to John Resig's implementation.
1400 // Underscore templating handles arbitrary delimiters, preserves whitespace,
1401 // and correctly escapes quotes within interpolated code.
1402 // NB: `oldSettings` only exists for backwards compatibility.
1403 _.template = function(text, settings, oldSettings) {
1404 if (!settings && oldSettings) settings = oldSettings;
1405 settings = _.defaults({}, settings, _.templateSettings);
1407 // Combine delimiters into one regular expression via alternation.
1408 var matcher = RegExp([
1409 (settings.escape || noMatch).source,
1410 (settings.interpolate || noMatch).source,
1411 (settings.evaluate || noMatch).source
1412 ].join('|') + '|$', 'g');
1414 // Compile the template source, escaping string literals appropriately.
1416 var source = "__p+='";
1417 text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
1418 source += text.slice(index, offset).replace(escaper, escapeChar);
1419 index = offset + match.length;
1422 source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
1423 } else if (interpolate) {
1424 source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
1425 } else if (evaluate) {
1426 source += "';\n" + evaluate + "\n__p+='";
1429 // Adobe VMs need the match returned to produce the correct offest.
1434 // If a variable is not specified, place data values in local scope.
1435 if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
1437 source = "var __t,__p='',__j=Array.prototype.join," +
1438 "print=function(){__p+=__j.call(arguments,'');};\n" +
1439 source + 'return __p;\n';
1442 var render = new Function(settings.variable || 'obj', '_', source);
1448 var template = function(data) {
1449 return render.call(this, data, _);
1452 // Provide the compiled source as a convenience for precompilation.
1453 var argument = settings.variable || 'obj';
1454 template.source = 'function(' + argument + '){\n' + source + '}';
1459 // Add a "chain" function. Start chaining a wrapped Underscore object.
1460 _.chain = function(obj) {
1461 var instance = _(obj);
1462 instance._chain = true;
1468 // If Underscore is called as a function, it returns a wrapped object that
1469 // can be used OO-style. This wrapper holds altered versions of all the
1470 // underscore functions. Wrapped objects may be chained.
1472 // Helper function to continue chaining intermediate results.
1473 var result = function(instance, obj) {
1474 return instance._chain ? _(obj).chain() : obj;
1477 // Add your own custom functions to the Underscore object.
1478 _.mixin = function(obj) {
1479 _.each(_.functions(obj), function(name) {
1480 var func = _[name] = obj[name];
1481 _.prototype[name] = function() {
1482 var args = [this._wrapped];
1483 push.apply(args, arguments);
1484 return result(this, func.apply(_, args));
1489 // Add all of the Underscore functions to the wrapper object.
1492 // Add all mutator Array functions to the wrapper.
1493 _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
1494 var method = ArrayProto[name];
1495 _.prototype[name] = function() {
1496 var obj = this._wrapped;
1497 method.apply(obj, arguments);
1498 if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
1499 return result(this, obj);
1503 // Add all accessor Array functions to the wrapper.
1504 _.each(['concat', 'join', 'slice'], function(name) {
1505 var method = ArrayProto[name];
1506 _.prototype[name] = function() {
1507 return result(this, method.apply(this._wrapped, arguments));
1511 // Extracts the result from a wrapped and chained object.
1512 _.prototype.value = function() {
1513 return this._wrapped;
1516 // Provide unwrapping proxy for some methods used in engine operations
1517 // such as arithmetic and JSON stringification.
1518 _.prototype.valueOf = _.prototype.toJSON = _.prototype.value;
1520 _.prototype.toString = function() {
1521 return '' + this._wrapped;
1524 // AMD registration happens at the end for compatibility with AMD loaders
1525 // that may not enforce next-turn semantics on modules. Even though general
1526 // practice for AMD registration is to be anonymous, underscore registers
1527 // as a named module because, like jQuery, it is a base library that is
1528 // popular enough to be bundled in a third party lib, but not be part of
1529 // an AMD load request. Those cases could generate an error when an
1530 // anonymous define() is called outside of a loader request.
1531 if (typeof define === 'function' && define.amd) {
1532 define('underscore', [], function() {