-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunderscore.js
More file actions
2234 lines (1911 loc) · 65.2 KB
/
Copy pathunderscore.js
File metadata and controls
2234 lines (1911 loc) · 65.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Underscore.js 1.9.8
// http://underscorejs.org
// (c) 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(function() {
var current = global._;
var exports = factory();
global._ = exports;
exports.noConflict = function() { global._ = current; return exports; };
})();
}(this, (function () { 'use strict';
// quick reference variables for speed access
//-------------------------------------------
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype;
var ObjProto = Object.prototype;
var SymbolProto = typeof Symbol !== 'undefined' ? Symbol.prototype : null;
// Create quick reference variables for speed access to core prototypes.
var push = ArrayProto.push;
var slice = ArrayProto.slice;
var toString = ObjProto.toString;
var hasOwnProperty = ObjProto.hasOwnProperty;
// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
var nativeIsArray = Array.isArray;
var nativeKeys = Object.keys;
var nativeCreate = Object.create;
// Naked function reference for surrogate-prototype-swapping.
var Ctor = function Ctor() {};
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
// `_isObject` : an object's function
// -----------------------------------
// Is a given variable an object?
function _isObject (obj) {
var type = typeof obj === 'undefined' ? 'undefined' : _typeof(obj);
return type === 'function' || type === 'object' && !!obj;
}
// `_has` : an object's function
// ------------------------------
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
function _has (obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
}
// `_forEach` : a collection's function
// ------------------------------------
// Handles raw objects in addition to array-likes. Treats all
// sparse array-likes as if they were dense.
function _each (obj, iteratee, context) {
iteratee = optimizeCb(iteratee, context);
var i = void 0,
length = void 0;
if (isArrayLike(obj)) {
for (i = 0, length = obj.length; i < length; i++) {
iteratee(obj[i], i, obj);
}
} else {
var keys = _keys(obj);
for (i = 0, length = keys.length; i < length; i++) {
iteratee(obj[keys[i]], keys[i], obj);
}
}
return obj;
}
// `_findIndex` : an array's function
// -----------------------------------
// Returns the first index on an array-like that passes a predicate test.
var _findIndex = createPredicateIndexFinder(1);
// `_sortedIndex` : an array's function
// -------------------------------------
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
function _sortedIndex (array, obj, iteratee, context) {
iteratee = cb(iteratee, context, 1);
var value = iteratee(obj);
var low = 0,
high = getLength(array);
while (low < high) {
var mid = Math.floor((low + high) / 2);
if (iteratee(array[mid]) < value) low = mid + 1;else high = mid;
}
return low;
}
// `_indexOf` : an array's function
// ---------------------------------
// Return the position of the first occurrence of an item in an array,
// or -1 if the item is not included in the array.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
var _indexOf = createIndexFinder(1, _findIndex, _sortedIndex);
// `_values` : an object's function
// ---------------------------------
// Retrieve the values of an object's properties.
function _values (obj) {
var keys = _keys(obj);
var length = keys.length;
var values = Array(length);
for (var i = 0; i < length; i++) {
values[i] = obj[keys[i]];
}
return values;
}
// `_include` : a collection's function
// -------------------------------------
// Determine if the array or object contains a given item (using `===`).
function _contains (obj, item, fromIndex, guard) {
if (!isArrayLike(obj)) obj = _values(obj);
if (typeof fromIndex != 'number' || guard) fromIndex = 0;
return _indexOf(obj, item, fromIndex) >= 0;
}
// `_isArray` : an object's function
// ----------------------------------
// Is a given value an array?
// Delegates to ECMA5's native Array.isArray
var _isArray = nativeIsArray || function (obj) {
return toString.call(obj) === '[object Array]';
};
// `_isFunction` : an object's function
// -------------------------------------
// Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,
// IE 11 (#1621), Safari 8 (#1929), and PhantomJS (#2236).
function customFunction() {
if (typeof /./ != 'function' && (typeof Int8Array === 'undefined' ? 'undefined' : _typeof(Int8Array)) != 'object' && typeof document !== 'undefined' && typeof document.childNodes != 'function') {
return function (obj) {
return typeof obj == 'function' || false;
};
}
return null;
}
// Is a given value a function?
var _isFunction = customFunction() || function (obj) {
return toString.call(obj) === '[object Function]';
};
// `_isArguments` : an object's function
// --------------------------------------
// Define a fallback version of the method in browsers (ahem, IE < 9), where
// there isn't any inspectable "Arguments" type.
function customArguments() {
if (toString.call(arguments) === '[object Arguments]') return null;
return function (obj) {
return _has(obj, 'callee');
};
}
// Is a given value an arguments object?
var _isArguments = customArguments() || function (obj) {
return toString.call(obj) === '[object Arguments]';
};
// `_isNumber` : an object's function
// -----------------------------------
// Is a given value a number?
function _isNumber (obj) {
return toString.call(obj) === '[object Number]';
}
// `_isNaN` : an object's function
// --------------------------------
// Is the given value `NaN`?
function _isNaN (obj) {
return _isNumber(obj) && isNaN(obj);
}
// `_invert` : an object's function
// ---------------------------------
// Invert the keys and values of an object. The values must be serializable.
function _invert (obj) {
var result = {};
var keys = _keys(obj);
for (var i = 0, length = keys.length; i < length; i++) {
result[obj[keys[i]]] = keys[i];
}
return result;
}
// `_iteratee` : an utility's function
// ------------------------------------
// External wrapper for our callback generator. Users may customize
// `_.iteratee` if they want additional predicate/iteratee shorthand styles.
// This abstraction hides the internal-only argCount argument.
var _iteratee = builtinIteratee;
function _setIteratee(fn) {
_iteratee = _isFunction(fn) ? fn : builtinIteratee;
}
// `_identity` : an utility's function
// ------------------------------------
// Keep the identity function around for default iteratees.
function _identity (value) {
return value;
}
// `_extendOwn` : an object's function
// ------------------------------------
// Extend a given object with the properties in passed-in object(s).
var _extendOwn = createAssigner(_keys);
// `_isMatch` : an object's function
// ----------------------------------
// Returns whether an object has a given set of `key:value` pairs.
function _isMatch (object, attrs) {
var keys = _keys(attrs),
length = keys.length;
if (object == null) return !length;
var obj = Object(object);
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
}
// `_matches` : an utility's function
// -----------------------------------
// Returns a predicate for checking whether an object has a given set of
// `key:value` pairs.
function _matcher (attrs) {
attrs = _extendOwn({}, attrs);
return function (obj) {
return _isMatch(obj, attrs);
};
}
// `_` : base namespace and constructor for underscore's object
// ----------------------------------------------------------------------------------------------
// Baseline setup
function _$1(obj) {
if (obj instanceof _$1) return obj;
if (!(this instanceof _$1)) return new _$1(obj);
this._wrapped = obj;
}
// Current version.
_$1.VERSION = '1.9.8';
// Internal functions
//--------------------
// Internal function that returns an efficient (for current engines) version
// of the passed-in callback, to be repeatedly applied in other Underscore
// functions.
function optimizeCb(func, context, argCount) {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1:
return function (value) {
return func.call(context, value);
};
// The 2-parameter case has been omitted only because no current consumers
// made use of it.
case 3:
return function (value, index, collection) {
return func.call(context, value, index, collection);
};
case 4:
return function (accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function () {
return func.apply(context, arguments);
};
}
// for callback generator.
// This abstraction is use to hide the internal-only argCount argument.
function builtinIteratee(value, context) {
return cb(value, context, Infinity);
}
// An internal function to generate callbacks that can be applied to each
// element in a collection, returning the desired result — either `identity`,
// an arbitrary callback, a property matcher, or a property accessor.
function cb(value, context, argCount) {
if (_iteratee !== builtinIteratee) return _iteratee(value, context);
if (value == null) return _identity;
if (_isFunction(value)) return optimizeCb(value, context, argCount);
if (_isObject(value)) return _matcher(value);
return _property(value);
}
// Similar to ES6's rest param (http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html)
// This accumulates the arguments passed into an array, after a given index.
function restArgs(func, startIndex) {
startIndex = startIndex == null ? func.length - 1 : +startIndex;
return function () {
var length = Math.max(arguments.length - startIndex, 0),
rest = Array(length),
index = 0;
for (; index < length; index++) {
rest[index] = arguments[index + startIndex];
}
switch (startIndex) {
case 0:
return func.call(this, rest);
case 1:
return func.call(this, arguments[0], rest);
case 2:
return func.call(this, arguments[0], arguments[1], rest);
}
var args = Array(startIndex + 1);
for (index = 0; index < startIndex; index++) {
args[index] = arguments[index];
}
args[startIndex] = rest;
return func.apply(this, args);
};
}
// An internal function for creating a new object that inherits from another.
// @TODO from quickaccess
function baseCreate(prototype) {
if (!_isObject(prototype)) return {};
if (nativeCreate) return nativeCreate(prototype);
Ctor.prototype = prototype;
var result = new Ctor();
Ctor.prototype = null;
return result;
}
// An internal function used for get key's value from an object.
function _property(key) {
return function (obj) {
return obj == null ? void 0 : obj[key];
};
}
// Helper for collection methods to determine whether a collection
// should be iterated as an array or as an object.
// Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength
// Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094
var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
var getLength = _property('length');
var isArrayLike = function isArrayLike(collection) {
// @TODO simplify to function
var length = getLength(collection);
return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX;
};
// Create a reducing function iterating left or right.
function createReduce(dir) {
// Wrap code that reassigns argument variables in a separate function than
// the one that accesses `arguments.length` to avoid a perf hit. (#1991)
var reducer = function reducer(obj, iteratee, memo, initial) {
var keys = !isArrayLike(obj) && _keys(obj),
length = (keys || obj).length,
index = dir > 0 ? 0 : length - 1;
if (!initial) {
memo = obj[keys ? keys[index] : index];
index += dir;
}
for (; index >= 0 && index < length; index += dir) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
return memo;
};
return function (obj, iteratee, memo, context) {
var initial = arguments.length >= 3;
return reducer(obj, optimizeCb(iteratee, context, 4), memo, initial);
};
}
// An internal function used for aggregate "group by" operations.
function group(behavior, partition) {
return function (obj, iteratee, context) {
var result = partition ? [[], []] : {};
iteratee = cb(iteratee, context);
_each(obj, function (value, index) {
var key = iteratee(value, index, obj);
behavior(result, value, key);
});
return result;
};
}
// Internal implementation of a recursive `flatten` function.
function flatten(input, shallow, strict, output) {
output = output || [];
var idx = output.length;
for (var i = 0, length = getLength(input); i < length; i++) {
var value = input[i];
if (isArrayLike(value) && (_isArray(value) || _isArguments(value))) {
// Flatten current level of array or arguments object.
if (shallow) {
var j = 0,
len = value.length;
while (j < len) {
output[idx++] = value[j++];
}
} else {
flatten(value, shallow, strict, output);
idx = output.length;
}
} else if (!strict) {
output[idx++] = value;
}
}
return output;
}
// Generator function to create the findIndex and findLastIndex functions.
function createPredicateIndexFinder(dir) {
return function (array, predicate, context) {
predicate = cb(predicate, context);
var length = getLength(array);
var index = dir > 0 ? 0 : length - 1;
for (; index >= 0 && index < length; index += dir) {
if (predicate(array[index], index, array)) return index;
}
return -1;
};
}
// Generator function to create the indexOf and lastIndexOf functions.
function createIndexFinder(dir, predicateFind, sortedIndex) {
return function (array, item, idx) {
var i = 0,
length = getLength(array);
if (typeof idx == 'number') {
if (dir > 0) {
i = idx >= 0 ? idx : Math.max(idx + length, i);
} else {
length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
}
} else if (sortedIndex && idx && length) {
idx = sortedIndex(array, item);
return array[idx] === item ? idx : -1;
}
if (item !== item) {
idx = predicateFind(slice.call(array, i, length), _isNaN);
return idx >= 0 ? idx + i : -1;
}
for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
if (array[idx] === item) return idx;
}
return -1;
};
}
// Determines whether to execute a function as a constructor
// or a normal function with the provided arguments.
function executeBound(sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype);
var result = sourceFunc.apply(self, args);
if (_isObject(result)) return result;
return self;
}
// Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed.
// @TODO move to _quickaccess to prevent inappropriate cyclic dependency with `keys` and `allkeys`
// @FUTURE remove this hack when the will ignore IE<9 since the goal is now ES6 and beyond.
var hasEnumBug = !{ toString: null }.propertyIsEnumerable('toString');
var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString', 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
// hack for enumerating bug
function collectNonEnumProps(obj, keys) {
var nonEnumIdx = nonEnumerableProps.length;
var constructor = obj.constructor;
var proto = _isFunction(constructor) && constructor.prototype || ObjProto;
// Constructor is a special case.
var prop = 'constructor';
if (_has(obj, prop) && !_contains(keys, prop)) keys.push(prop);
while (nonEnumIdx--) {
prop = nonEnumerableProps[nonEnumIdx];
if (prop in obj && obj[prop] !== proto[prop] && !_contains(keys, prop)) {
keys.push(prop);
}
}
}
// An internal function for creating assigner functions.
function createAssigner(keysFunc, defaults) {
return function (obj) {
var length = arguments.length;
if (defaults) obj = Object(obj);
if (length < 2 || obj == null) return obj;
for (var index = 1; index < length; index++) {
var source = arguments[index],
keys = keysFunc(source),
l = keys.length;
for (var i = 0; i < l; i++) {
var key = keys[i];
if (_isObject(obj) && (!defaults || obj[key] === void 0)) obj[key] = source[key];
}
}
return obj;
};
}
// Internal pick helper function to determine if `obj` has key `key`.
function keyInObj(value, key, obj) {
return key in obj;
}
// Internal recursive comparison function for `isEqual`.
function eq(a, b, aStack, bStack) {
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) return a !== 0 || 1 / a === 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a == null || b == null) return a === b;
// `NaN`s are equivalent, but non-reflexive.
if (a !== a) return b !== b;
// Exhaust primitive checks
var type = typeof a === 'undefined' ? 'undefined' : _typeof(a);
if (type !== 'function' && type !== 'object' && (typeof b === 'undefined' ? 'undefined' : _typeof(b)) != 'object') return false;
return deepEq(a, b, aStack, bStack);
}
// Internal recursive comparison function for `isEqual`.
// @TODO from quickaccess
function deepEq(a, b, aStack, bStack) {
// Unwrap any wrapped objects.
if (a instanceof _$1) a = a._wrapped;
if (b instanceof _$1) b = b._wrapped;
// Compare `[[Class]]` names.
var className = toString.call(a);
if (className !== toString.call(b)) return false;
switch (className) {
// Strings, numbers, regular expressions, dates, and booleans are compared by value.
case '[object RegExp]':
// RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
case '[object String]':
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return '' + a === '' + b;
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive.
// Object(NaN) is equivalent to NaN.
if (+a !== +a) return +b !== +b;
// An `egal` comparison is performed for other numeric values.
return +a === 0 ? 1 / +a === 1 / b : +a === +b;
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a === +b;
case '[object Symbol]':
return SymbolProto.valueOf.call(a) === SymbolProto.valueOf.call(b);
}
var areArrays = className === '[object Array]';
if (!areArrays) {
if ((typeof a === 'undefined' ? 'undefined' : _typeof(a)) != 'object' || (typeof b === 'undefined' ? 'undefined' : _typeof(b)) != 'object') return false;
// Objects with different constructors are not equivalent, but `Object`s or `Array`s
// from different frames are.
var aCtor = a.constructor,
bCtor = b.constructor;
if (aCtor !== bCtor && !(_$1.isFunction(aCtor) && aCtor instanceof aCtor && _$1.isFunction(bCtor) && bCtor instanceof bCtor) && 'constructor' in a && 'constructor' in b) {
return false;
}
}
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
// Initializing stack of traversed objects.
// It's done here since we only need them for objects and arrays comparison.
aStack = aStack || [];
bStack = bStack || [];
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] === a) return bStack[length] === b;
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
bStack.push(b);
// Recursively compare objects and arrays.
if (areArrays) {
// Compare array lengths to determine if a deep comparison is necessary.
length = a.length;
if (length !== b.length) return false;
// Deep compare the contents, ignoring non-numeric properties.
while (length--) {
if (!eq(a[length], b[length], aStack, bStack)) return false;
}
} else {
// Deep compare objects.
var keys = _$1.keys(a),
key = void 0;
length = keys.length;
// Ensure that both objects contain the same number of properties before comparing deep equality.
if (_$1.keys(b).length !== length) return false;
while (length--) {
// Deep compare each member
key = keys[length];
if (!(_$1.has(b, key) && eq(a[key], b[key], aStack, bStack))) return false;
}
}
// Remove the first object from the stack of traversed objects.
aStack.pop();
bStack.pop();
return true;
}
// can be use to keep surrogate pair characters together (see `toArray` function for usage example)
var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g;
// List of HTML entities for escaping.
var escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
var unescapeMap = _invert(escapeMap);
// Functions for escaping and unescaping strings to/from HTML interpolation.
function createEscaper(map) {
var escaper = function escaper(match) {
return map[match];
};
// Regexes for identifying a key that needs to be escaped.
var source = '(?:' + _keys(map).join('|') + ')';
var testRegexp = RegExp(source);
var replaceRegexp = RegExp(source, 'g');
return function (string) {
string = string == null ? '' : '' + string;
return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
};
}
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
// function to escaped some characters
function escapeChar(match) {
return '\\' + escapes[match];
}
// `_keys` : an object's function
// -------------------------------
// Retrieve the names of an object's own properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`.
function _keys (obj) {
if (!_isObject(obj)) return [];
if (nativeKeys) return nativeKeys(obj);
var keys = [];
for (var key in obj) {
if (_has(obj, key)) keys.push(key);
} // Ahem, IE < 9.
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;
}
// `_all` : a collection's function
// ---------------------------------
// Determine whether all of the elements match a truth test.
function _every (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (!predicate(obj[currentKey], currentKey, obj)) return false;
}
return true;
}
// `_any` : a collection's function
// ---------------------------------
// Determine if at least one element in the object matches a truth test.
// Aliased as `any`.
function _some (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = !isArrayLike(obj) && _keys(obj),
length = (keys || obj).length;
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
if (predicate(obj[currentKey], currentKey, obj)) return true;
}
return false;
}
// `_collect` : a collection's function
// -------------------------------------
// Return the results of applying the iteratee to each element.
function _map (obj, iteratee, context) {
iteratee = cb(iteratee, context);
var keys = !isArrayLike(obj) && _keys(obj),
length = (keys || obj).length,
results = Array(length);
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
results[index] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
}
// `_countBy` : a collection's function
// -------------------------------------
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
var _countBy = group(function (result, value, key) {
if (_has(result, key)) result[key]++;else result[key] = 1;
});
// `_findKey` : an object's function
// ----------------------------------
// Returns the first key on an object that passes a predicate test.
function _findKey (obj, predicate, context) {
predicate = cb(predicate, context);
var keys = _keys(obj),
key = void 0;
for (var i = 0, length = keys.length; i < length; i++) {
key = keys[i];
if (predicate(obj[key], key, obj)) return key;
}
}
// `_detect` : a collection's function
// ------------------------------------
// Return the first value which passes a truth test. Aliased as `detect`.
function _find (obj, predicate, context) {
var keyFinder = isArrayLike(obj) ? _findIndex : _findKey;
var key = keyFinder(obj, predicate, context);
if (key !== void 0 && key !== -1) return obj[key];
}
// `_select` : a collection's function
// ------------------------------------
// Return all the elements that pass a truth test.
function _filter (obj, predicate, context) {
var results = [];
predicate = cb(predicate, context);
_each(obj, function (value, index, list) {
if (predicate(value, index, list)) results.push(value);
});
return results;
}
// `_findWhere` : a collection's function
// ---------------------------------------
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
function _findWhere (obj, attrs) {
return _find(obj, _matcher(attrs));
}
// `_initial` : an array's function
// ---------------------------------
// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N.
function _initial (array, n, guard) {
return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
}
// `_take` : an array's function
// ------------------------------
// Get the first element of an array. Passing **n** will return the first N
// values in the array. The **guard** check allows it to work with `_map`.
function _take (array, n, guard) {
if (array == null || array.length < 1) return void 0;
if (n == null || guard) return array[0];
return _initial(array, array.length - n);
}
// `_inject` : a collection's function
// ------------------------------------
// **Reduce** builds up a single result from a list of values.
var _inject = createReduce(1);
// `_foldr` : a collection's function
// -----------------------------------
// The right-associative version of reduce, also known as `foldr`
var _foldr = createReduce(-1);
// `_groupBy` : a collection's function
// -------------------------------------
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
var _groupBy = group(function (result, value, key) {
if (_has(result, key)) result[key].push(value);else result[key] = [value];
});
// `_indexBy` : a collection's function
// ------------------------------------
// Indexes the object's values by a criterion, similar to `groupBy`, but for
// when you know that your index values will be unique.
var _indexBy = group(function (result, value, key) {
result[key] = value;
});
// `_invoke` : a collection's function
// ------------------------------------
// Invoke a method (with arguments) on every item in a collection.
var _invoke = restArgs(function (obj, method, args) {
var isFunc = _isFunction(method);
return _map(obj, function (value) {
var func = isFunc ? method : value[method];
return func == null ? func : func.apply(value, args);
});
});
// `_max` : a collection's function
// ------------------------------------
// Return the maximum element (or element-based computation).
function _max (obj, iteratee, context) {
var result = -Infinity,
lastComputed = -Infinity,
value = void 0,
computed = void 0;
if (iteratee == null || typeof iteratee == 'number' && _typeof(obj[0]) != 'object' && obj != null) {
obj = isArrayLike(obj) ? obj : _values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value != null && value > result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_each(obj, function (v, index, list) {
computed = iteratee(v, index, list);
if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
result = v;
lastComputed = computed;
}
});
}
return result;
}
// `_min` : a collection's function
// ---------------------------------
// Return the minimum element (or element-based computation).
function _min (obj, iteratee, context) {
var result = Infinity,
lastComputed = Infinity,
value,
computed;
if (iteratee == null || typeof iteratee == 'number' && _typeof(obj[0]) != 'object' && obj != null) {
obj = isArrayLike(obj) ? obj : _values(obj);
for (var i = 0, length = obj.length; i < length; i++) {
value = obj[i];
if (value != null && value < result) {
result = value;
}
}
} else {
iteratee = cb(iteratee, context);
_each(obj, function (v, index, list) {
computed = iteratee(v, index, list);
if (computed < lastComputed || computed === Infinity && result === Infinity) {
result = v;
lastComputed = computed;
}
});
}
return result;
}
// `_partition` : a collection's function
// ---------------------------------------
// Split a collection into two arrays: one whose elements all satisfy the given
// predicate, and one whose elements all do not satisfy the predicate.
var _partition = group(function (result, value, pass) {
result[pass ? 0 : 1].push(value);
}, true);
// `_pluck` : a collection's function
// ------------------------------------
// Convenience version of a common use case of `map`: fetching a property.
function _pluck (obj, key) {
return _map(obj, _property(key));
}
// `_negate` : (ahem) a function's function
// -----------------------------------------
// Returns a negated version of the passed-in predicate.
function _negate (predicate) {
return function () {
return !predicate.apply(this, arguments);
};
}
// `_reject` : a collection's function
// ------- ----------------------------
// Return all the elements for which a truth test fails.
function _reject (obj, predicate, context) {
return _filter(obj, _negate(cb(predicate)), context);
}
// `_random` : an utility's function
// ----------------------------------
// Return a random integer between min and max (inclusive).
function _random (min, max) {
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
}
// `_allKeys` : an object's function
// ---------------------------------
// Retrieve all the property names of an object.
function _allKeys (obj) {
if (!_isObject(obj)) return [];
var keys = [];
for (var key in obj) {
keys.push(key);
} // Ahem, IE < 9.
if (hasEnumBug) collectNonEnumProps(obj, keys);
return keys;