Ticket #45785: 45785.diff
File 45785.diff, 149.8 KB (added by , 5 years ago) |
---|
-
wp-includes/js/underscore.js
1 // Underscore.js 1. 8.31 // Underscore.js 1.9.1 2 2 // http://underscorejs.org 3 // (c) 2009-201 5Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors3 // (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 4 4 // Underscore may be freely distributed under the MIT license. 5 5 6 6 (function() { 7 7 8 9 8 // Baseline setup 9 // -------------- 10 10 11 // Establish the root object, `window` in the browser, or `exports` on the server. 12 var root = this; 11 // Establish the root object, `window` (`self`) in the browser, `global` 12 // on the server, or `this` in some virtual machines. We use `self` 13 // instead of `window` for `WebWorker` support. 14 var root = typeof self == 'object' && self.self === self && self || 15 typeof global == 'object' && global.global === global && global || 16 this || {}; 13 17 14 15 18 // Save the previous value of the `_` variable. 19 var previousUnderscore = root._; 16 20 17 // Save bytes in the minified (but not gzipped) version: 18 var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype; 21 // Save bytes in the minified (but not gzipped) version: 22 var ArrayProto = Array.prototype, 23 ObjProto = Object.prototype; 24 var SymbolProto = typeof Symbol !== 'undefined' ? Symbol.prototype : null; 19 25 20 // Create quick reference variables for speed access to core prototypes. 21 var 22 push = ArrayProto.push, 23 slice = ArrayProto.slice, 24 toString = ObjProto.toString, 25 hasOwnProperty = ObjProto.hasOwnProperty; 26 // Create quick reference variables for speed access to core prototypes. 27 var push = ArrayProto.push, 28 slice = ArrayProto.slice, 29 toString = ObjProto.toString, 30 hasOwnProperty = ObjProto.hasOwnProperty; 26 31 27 // All **ECMAScript 5** native function implementations that we hope to use 28 // are declared here. 29 var 30 nativeIsArray = Array.isArray, 31 nativeKeys = Object.keys, 32 nativeBind = FuncProto.bind, 33 nativeCreate = Object.create; 32 // All **ECMAScript 5** native function implementations that we hope to use 33 // are declared here. 34 var nativeIsArray = Array.isArray, 35 nativeKeys = Object.keys, 36 nativeCreate = Object.create; 34 37 35 36 var Ctor = function(){};38 // Naked function reference for surrogate-prototype-swapping. 39 var Ctor = function() {}; 37 40 38 39 40 41 42 43 41 // Create a safe reference to the Underscore object for use below. 42 var _ = function(obj) { 43 if (obj instanceof _) return obj; 44 if (!(this instanceof _)) return new _(obj); 45 this._wrapped = obj; 46 }; 44 47 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 = _; 51 } 52 exports._ = _; 53 } else { 54 root._ = _; 55 } 48 // Export the Underscore object for **Node.js**, with 49 // backwards-compatibility for their old module API. If we're in 50 // the browser, add `_` as a global object. 51 // (`nodeType` is checked to ensure that `module` 52 // and `exports` are not HTML elements.) 53 if (typeof exports != 'undefined' && !exports.nodeType) { 54 if (typeof module != 'undefined' && !module.nodeType && module.exports) { 55 exports = module.exports = _; 56 } 57 exports._ = _; 58 } else { 59 root._ = _; 60 } 56 61 57 58 _.VERSION = '1.8.3';62 // Current version. 63 _.VERSION = '1.9.1'; 59 64 60 // Internal function that returns an efficient (for current engines) version 61 // of the passed-in callback, to be repeatedly applied in other Underscore 62 // functions. 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); 68 }; 69 case 2: return function(value, other) { 70 return func.call(context, value, other); 71 }; 72 case 3: return function(value, index, collection) { 73 return func.call(context, value, index, collection); 74 }; 75 case 4: return function(accumulator, value, index, collection) { 76 return func.call(context, accumulator, value, index, collection); 77 }; 78 } 79 return function() { 80 return func.apply(context, arguments); 81 }; 82 }; 65 // Internal function that returns an efficient (for current engines) version 66 // of the passed-in callback, to be repeatedly applied in other Underscore 67 // functions. 68 var optimizeCb = function(func, context, argCount) { 69 if (context === void 0) return func; 70 switch (argCount == null ? 3 : argCount) { 71 case 1: 72 return function(value) { 73 return func.call(context, value); 74 }; 75 // The 2-argument case is omitted because we’re not using it. 76 case 3: 77 return function(value, index, collection) { 78 return func.call(context, value, index, collection); 79 }; 80 case 4: 81 return function(accumulator, value, index, collection) { 82 return func.call(context, accumulator, value, index, collection); 83 }; 84 } 85 return function() { 86 return func.apply(context, arguments); 87 }; 88 }; 83 89 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); 92 }; 93 _.iteratee = function(value, context) { 94 return cb(value, context, Infinity); 95 }; 90 var builtinIteratee; 96 91 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), 105 l = keys.length; 106 for (var i = 0; i < l; i++) { 107 var key = keys[i]; 108 if (!undefinedOnly || obj[key] === void 0) obj[key] = source[key]; 109 } 110 } 111 return obj; 112 }; 113 }; 92 // An internal function to generate callbacks that can be applied to each 93 // element in a collection, returning the desired result — either `identity`, 94 // an arbitrary callback, a property matcher, or a property accessor. 95 var cb = function(value, context, argCount) { 96 if (_.iteratee !== builtinIteratee) return _.iteratee(value, context); 97 if (value == null) return _.identity; 98 if (_.isFunction(value)) return optimizeCb(value, context, argCount); 99 if (_.isObject(value) && !_.isArray(value)) return _.matcher(value); 100 return _.property(value); 101 }; 114 102 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; 122 return result; 123 }; 103 // External wrapper for our callback generator. Users may customize 104 // `_.iteratee` if they want additional predicate/iteratee shorthand styles. 105 // This abstraction hides the internal-only argCount argument. 106 _.iteratee = builtinIteratee = function(value, context) { 107 return cb(value, context, Infinity); 108 }; 124 109 125 var property = function(key) { 126 return function(obj) { 127 return obj == null ? void 0 : obj[key]; 128 }; 129 }; 110 // Some functions take a variable number of arguments, or a few expected 111 // arguments at the beginning and then a variable number of values to operate 112 // on. This helper accumulates all remaining arguments past the function’s 113 // argument length (or an explicit `startIndex`), into an array that becomes 114 // the last argument. Similar to ES6’s "rest parameter". 115 var restArguments = function(func, startIndex) { 116 startIndex = startIndex == null ? func.length - 1 : +startIndex; 117 return function() { 118 var length = Math.max(arguments.length - startIndex, 0), 119 rest = Array(length), 120 index = 0; 121 for (; index < length; index++) { 122 rest[index] = arguments[index + startIndex]; 123 } 124 switch (startIndex) { 125 case 0: 126 return func.call(this, rest); 127 case 1: 128 return func.call(this, arguments[0], rest); 129 case 2: 130 return func.call(this, arguments[0], arguments[1], rest); 131 } 132 var args = Array(startIndex + 1); 133 for (index = 0; index < startIndex; index++) { 134 args[index] = arguments[index]; 135 } 136 args[startIndex] = rest; 137 return func.apply(this, args); 138 }; 139 }; 130 140 131 // Helper for collection methods to determine whether a collection 132 // should be iterated as an array or as an object 133 // Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength 134 // Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094 135 var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1; 136 var getLength = property('length'); 137 var isArrayLike = function(collection) { 138 var length = getLength(collection); 139 return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX; 140 }; 141 // An internal function for creating a new object that inherits from another. 142 var baseCreate = function(prototype) { 143 if (!_.isObject(prototype)) return {}; 144 if (nativeCreate) return nativeCreate(prototype); 145 Ctor.prototype = prototype; 146 var result = new Ctor; 147 Ctor.prototype = null; 148 return result; 149 }; 141 150 142 // Collection Functions 143 // -------------------- 151 var shallowProperty = function(key) { 152 return function(obj) { 153 return obj == null ? void 0 : obj[key]; 154 }; 155 }; 144 156 145 // The cornerstone, an `each` implementation, aka `forEach`. 146 // Handles raw objects in addition to array-likes. Treats all 147 // sparse array-likes as if they were dense. 148 _.each = _.forEach = function(obj, iteratee, context) { 149 iteratee = optimizeCb(iteratee, context); 150 var i, length; 151 if (isArrayLike(obj)) { 152 for (i = 0, length = obj.length; i < length; i++) { 153 iteratee(obj[i], i, obj); 154 } 155 } else { 156 var keys = _.keys(obj); 157 for (i = 0, length = keys.length; i < length; i++) { 158 iteratee(obj[keys[i]], keys[i], obj); 159 } 160 } 161 return obj; 162 }; 157 var has = function(obj, path) { 158 return obj != null && hasOwnProperty.call(obj, path); 159 } 163 160 164 // Return the results of applying the iteratee to each element. 165 _.map = _.collect = function(obj, iteratee, context) { 166 iteratee = cb(iteratee, context); 167 var keys = !isArrayLike(obj) && _.keys(obj), 168 length = (keys || obj).length, 169 results = Array(length); 170 for (var index = 0; index < length; index++) { 171 var currentKey = keys ? keys[index] : index; 172 results[index] = iteratee(obj[currentKey], currentKey, obj); 173 } 174 return results; 175 }; 161 var deepGet = function(obj, path) { 162 var length = path.length; 163 for (var i = 0; i < length; i++) { 164 if (obj == null) return void 0; 165 obj = obj[path[i]]; 166 } 167 return length ? obj : void 0; 168 }; 176 169 177 // Create a reducing function iterating left or right. 178 function createReduce(dir) { 179 // Optimized iterator function as using arguments.length 180 // in the main function will deoptimize the, see #1991. 181 function iterator(obj, iteratee, memo, keys, index, length) { 182 for (; index >= 0 && index < length; index += dir) { 183 var currentKey = keys ? keys[index] : index; 184 memo = iteratee(memo, obj[currentKey], currentKey, obj); 185 } 186 return memo; 187 } 170 // Helper for collection methods to determine whether a collection 171 // should be iterated as an array or as an object. 172 // Related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength 173 // Avoids a very nasty iOS 8 JIT bug on ARM-64. #2094 174 var MAX_ARRAY_INDEX = Math.pow(2, 53) - 1; 175 var getLength = shallowProperty('length'); 176 var isArrayLike = function(collection) { 177 var length = getLength(collection); 178 return typeof length == 'number' && length >= 0 && length <= MAX_ARRAY_INDEX; 179 }; 188 180 189 return function(obj, iteratee, memo, context) { 190 iteratee = optimizeCb(iteratee, context, 4); 191 var keys = !isArrayLike(obj) && _.keys(obj), 192 length = (keys || obj).length, 193 index = dir > 0 ? 0 : length - 1; 194 // Determine the initial value if none is provided. 195 if (arguments.length < 3) { 196 memo = obj[keys ? keys[index] : index]; 197 index += dir; 198 } 199 return iterator(obj, iteratee, memo, keys, index, length); 200 }; 201 } 181 // Collection Functions 182 // -------------------- 202 183 203 // **Reduce** builds up a single result from a list of values, aka `inject`, 204 // or `foldl`. 205 _.reduce = _.foldl = _.inject = createReduce(1); 184 // The cornerstone, an `each` implementation, aka `forEach`. 185 // Handles raw objects in addition to array-likes. Treats all 186 // sparse array-likes as if they were dense. 187 _.each = _.forEach = function(obj, iteratee, context) { 188 iteratee = optimizeCb(iteratee, context); 189 var i, length; 190 if (isArrayLike(obj)) { 191 for (i = 0, length = obj.length; i < length; i++) { 192 iteratee(obj[i], i, obj); 193 } 194 } else { 195 var keys = _.keys(obj); 196 for (i = 0, length = keys.length; i < length; i++) { 197 iteratee(obj[keys[i]], keys[i], obj); 198 } 199 } 200 return obj; 201 }; 206 202 207 // The right-associative version of reduce, also known as `foldr`. 208 _.reduceRight = _.foldr = createReduce(-1); 203 // Return the results of applying the iteratee to each element. 204 _.map = _.collect = function(obj, iteratee, context) { 205 iteratee = cb(iteratee, context); 206 var keys = !isArrayLike(obj) && _.keys(obj), 207 length = (keys || obj).length, 208 results = Array(length); 209 for (var index = 0; index < length; index++) { 210 var currentKey = keys ? keys[index] : index; 211 results[index] = iteratee(obj[currentKey], currentKey, obj); 212 } 213 return results; 214 }; 209 215 210 // Return the first value which passes a truth test. Aliased as `detect`. 211 _.find = _.detect = function(obj, predicate, context) { 212 var key; 213 if (isArrayLike(obj)) { 214 key = _.findIndex(obj, predicate, context); 215 } else { 216 key = _.findKey(obj, predicate, context); 217 } 218 if (key !== void 0 && key !== -1) return obj[key]; 219 }; 216 // Create a reducing function iterating left or right. 217 var createReduce = function(dir) { 218 // Wrap code that reassigns argument variables in a separate function than 219 // the one that accesses `arguments.length` to avoid a perf hit. (#1991) 220 var reducer = function(obj, iteratee, memo, initial) { 221 var keys = !isArrayLike(obj) && _.keys(obj), 222 length = (keys || obj).length, 223 index = dir > 0 ? 0 : length - 1; 224 if (!initial) { 225 memo = obj[keys ? keys[index] : index]; 226 index += dir; 227 } 228 for (; index >= 0 && index < length; index += dir) { 229 var currentKey = keys ? keys[index] : index; 230 memo = iteratee(memo, obj[currentKey], currentKey, obj); 231 } 232 return memo; 233 }; 220 234 221 // Return all the elements that pass a truth test. 222 // Aliased as `select`. 223 _.filter = _.select = function(obj, predicate, context) { 224 var results = []; 225 predicate = cb(predicate, context); 226 _.each(obj, function(value, index, list) { 227 if (predicate(value, index, list)) results.push(value); 228 }); 229 return results; 230 }; 235 return function(obj, iteratee, memo, context) { 236 var initial = arguments.length >= 3; 237 return reducer(obj, optimizeCb(iteratee, context, 4), memo, initial); 238 }; 239 }; 231 240 232 // Return all the elements for which a truth test fails. 233 _.reject = function(obj, predicate, context) { 234 return _.filter(obj, _.negate(cb(predicate)), context); 235 }; 241 // **Reduce** builds up a single result from a list of values, aka `inject`, 242 // or `foldl`. 243 _.reduce = _.foldl = _.inject = createReduce(1); 236 244 237 // Determine whether all of the elements match a truth test. 238 // Aliased as `all`. 239 _.every = _.all = function(obj, predicate, context) { 240 predicate = cb(predicate, context); 241 var keys = !isArrayLike(obj) && _.keys(obj), 242 length = (keys || obj).length; 243 for (var index = 0; index < length; index++) { 244 var currentKey = keys ? keys[index] : index; 245 if (!predicate(obj[currentKey], currentKey, obj)) return false; 246 } 247 return true; 248 }; 245 // The right-associative version of reduce, also known as `foldr`. 246 _.reduceRight = _.foldr = createReduce(-1); 249 247 250 // Determine if at least one element in the object matches a truth test. 251 // Aliased as `any`. 252 _.some = _.any = function(obj, predicate, context) { 253 predicate = cb(predicate, context); 254 var keys = !isArrayLike(obj) && _.keys(obj), 255 length = (keys || obj).length; 256 for (var index = 0; index < length; index++) { 257 var currentKey = keys ? keys[index] : index; 258 if (predicate(obj[currentKey], currentKey, obj)) return true; 259 } 260 return false; 261 }; 248 // Return the first value which passes a truth test. Aliased as `detect`. 249 _.find = _.detect = function(obj, predicate, context) { 250 var keyFinder = isArrayLike(obj) ? _.findIndex : _.findKey; 251 var key = keyFinder(obj, predicate, context); 252 if (key !== void 0 && key !== -1) return obj[key]; 253 }; 262 254 263 // Determine if the array or object contains a given item (using `===`). 264 // Aliased as `includes` and `include`. 265 _.contains = _.includes = _.include = function(obj, item, fromIndex, guard) { 266 if (!isArrayLike(obj)) obj = _.values(obj); 267 if (typeof fromIndex != 'number' || guard) fromIndex = 0; 268 return _.indexOf(obj, item, fromIndex) >= 0; 269 }; 255 // Return all the elements that pass a truth test. 256 // Aliased as `select`. 257 _.filter = _.select = function(obj, predicate, context) { 258 var results = []; 259 predicate = cb(predicate, context); 260 _.each(obj, function(value, index, list) { 261 if (predicate(value, index, list)) results.push(value); 262 }); 263 return results; 264 }; 270 265 271 // Invoke a method (with arguments) on every item in a collection. 272 _.invoke = function(obj, method) { 273 var args = slice.call(arguments, 2); 274 var isFunc = _.isFunction(method); 275 return _.map(obj, function(value) { 276 var func = isFunc ? method : value[method]; 277 return func == null ? func : func.apply(value, args); 278 }); 279 }; 266 // Return all the elements for which a truth test fails. 267 _.reject = function(obj, predicate, context) { 268 return _.filter(obj, _.negate(cb(predicate)), context); 269 }; 280 270 281 // Convenience version of a common use case of `map`: fetching a property. 282 _.pluck = function(obj, key) { 283 return _.map(obj, _.property(key)); 284 }; 271 // Determine whether all of the elements match a truth test. 272 // Aliased as `all`. 273 _.every = _.all = function(obj, predicate, context) { 274 predicate = cb(predicate, context); 275 var keys = !isArrayLike(obj) && _.keys(obj), 276 length = (keys || obj).length; 277 for (var index = 0; index < length; index++) { 278 var currentKey = keys ? keys[index] : index; 279 if (!predicate(obj[currentKey], currentKey, obj)) return false; 280 } 281 return true; 282 }; 285 283 286 // Convenience version of a common use case of `filter`: selecting only objects 287 // containing specific `key:value` pairs. 288 _.where = function(obj, attrs) { 289 return _.filter(obj, _.matcher(attrs)); 290 }; 284 // Determine if at least one element in the object matches a truth test. 285 // Aliased as `any`. 286 _.some = _.any = function(obj, predicate, context) { 287 predicate = cb(predicate, context); 288 var keys = !isArrayLike(obj) && _.keys(obj), 289 length = (keys || obj).length; 290 for (var index = 0; index < length; index++) { 291 var currentKey = keys ? keys[index] : index; 292 if (predicate(obj[currentKey], currentKey, obj)) return true; 293 } 294 return false; 295 }; 291 296 292 // Convenience version of a common use case of `find`: getting the first object 293 // containing specific `key:value` pairs. 294 _.findWhere = function(obj, attrs) { 295 return _.find(obj, _.matcher(attrs)); 296 }; 297 // Determine if the array or object contains a given item (using `===`). 298 // Aliased as `includes` and `include`. 299 _.contains = _.includes = _.include = function(obj, item, fromIndex, guard) { 300 if (!isArrayLike(obj)) obj = _.values(obj); 301 if (typeof fromIndex != 'number' || guard) fromIndex = 0; 302 return _.indexOf(obj, item, fromIndex) >= 0; 303 }; 297 304 298 // Return the maximum element (or element-based computation). 299 _.max = function(obj, iteratee, context) { 300 var result = -Infinity, lastComputed = -Infinity, 301 value, computed; 302 if (iteratee == null && obj != null) { 303 obj = isArrayLike(obj) ? obj : _.values(obj); 304 for (var i = 0, length = obj.length; i < length; i++) { 305 value = obj[i]; 306 if (value > result) { 307 result = value; 308 } 309 } 310 } else { 311 iteratee = cb(iteratee, context); 312 _.each(obj, function(value, index, list) { 313 computed = iteratee(value, index, list); 314 if (computed > lastComputed || computed === -Infinity && result === -Infinity) { 315 result = value; 316 lastComputed = computed; 317 } 318 }); 319 } 320 return result; 321 }; 305 // Invoke a method (with arguments) on every item in a collection. 306 _.invoke = restArguments(function(obj, path, args) { 307 var contextPath, func; 308 if (_.isFunction(path)) { 309 func = path; 310 } else if (_.isArray(path)) { 311 contextPath = path.slice(0, -1); 312 path = path[path.length - 1]; 313 } 314 return _.map(obj, function(context) { 315 var method = func; 316 if (!method) { 317 if (contextPath && contextPath.length) { 318 context = deepGet(context, contextPath); 319 } 320 if (context == null) return void 0; 321 method = context[path]; 322 } 323 return method == null ? method : method.apply(context, args); 324 }); 325 }); 322 326 323 // Return the minimum element (or element-based computation). 324 _.min = function(obj, iteratee, context) { 325 var result = Infinity, lastComputed = Infinity, 326 value, computed; 327 if (iteratee == null && obj != null) { 328 obj = isArrayLike(obj) ? obj : _.values(obj); 329 for (var i = 0, length = obj.length; i < length; i++) { 330 value = obj[i]; 331 if (value < result) { 332 result = value; 333 } 334 } 335 } else { 336 iteratee = cb(iteratee, context); 337 _.each(obj, function(value, index, list) { 338 computed = iteratee(value, index, list); 339 if (computed < lastComputed || computed === Infinity && result === Infinity) { 340 result = value; 341 lastComputed = computed; 342 } 343 }); 344 } 345 return result; 346 }; 327 // Convenience version of a common use case of `map`: fetching a property. 328 _.pluck = function(obj, key) { 329 return _.map(obj, _.property(key)); 330 }; 347 331 348 // Shuffle a collection, using the modern version of the 349 // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle). 350 _.shuffle = function(obj) { 351 var set = isArrayLike(obj) ? obj : _.values(obj); 352 var length = set.length; 353 var shuffled = Array(length); 354 for (var index = 0, rand; index < length; index++) { 355 rand = _.random(0, index); 356 if (rand !== index) shuffled[index] = shuffled[rand]; 357 shuffled[rand] = set[index]; 358 } 359 return shuffled; 360 }; 332 // Convenience version of a common use case of `filter`: selecting only objects 333 // containing specific `key:value` pairs. 334 _.where = function(obj, attrs) { 335 return _.filter(obj, _.matcher(attrs)); 336 }; 361 337 362 // Sample **n** random values from a collection. 363 // If **n** is not specified, returns a single random element. 364 // The internal `guard` argument allows it to work with `map`. 365 _.sample = function(obj, n, guard) { 366 if (n == null || guard) { 367 if (!isArrayLike(obj)) obj = _.values(obj); 368 return obj[_.random(obj.length - 1)]; 369 } 370 return _.shuffle(obj).slice(0, Math.max(0, n)); 371 }; 338 // Convenience version of a common use case of `find`: getting the first object 339 // containing specific `key:value` pairs. 340 _.findWhere = function(obj, attrs) { 341 return _.find(obj, _.matcher(attrs)); 342 }; 372 343 373 // Sort the object's values by a criterion produced by an iteratee. 374 _.sortBy = function(obj, iteratee, context) { 375 iteratee = cb(iteratee, context); 376 return _.pluck(_.map(obj, function(value, index, list) { 377 return { 378 value: value, 379 index: index, 380 criteria: iteratee(value, index, list) 381 }; 382 }).sort(function(left, right) { 383 var a = left.criteria; 384 var b = right.criteria; 385 if (a !== b) { 386 if (a > b || a === void 0) return 1; 387 if (a < b || b === void 0) return -1; 388 } 389 return left.index - right.index; 390 }), 'value'); 391 }; 344 // Return the maximum element (or element-based computation). 345 _.max = function(obj, iteratee, context) { 346 var result = -Infinity, 347 lastComputed = -Infinity, 348 value, computed; 349 if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { 350 obj = isArrayLike(obj) ? obj : _.values(obj); 351 for (var i = 0, length = obj.length; i < length; i++) { 352 value = obj[i]; 353 if (value != null && value > result) { 354 result = value; 355 } 356 } 357 } else { 358 iteratee = cb(iteratee, context); 359 _.each(obj, function(v, index, list) { 360 computed = iteratee(v, index, list); 361 if (computed > lastComputed || computed === -Infinity && result === -Infinity) { 362 result = v; 363 lastComputed = computed; 364 } 365 }); 366 } 367 return result; 368 }; 392 369 393 // An internal function used for aggregate "group by" operations. 394 var group = function(behavior) { 395 return function(obj, iteratee, context) { 396 var result = {}; 397 iteratee = cb(iteratee, context); 398 _.each(obj, function(value, index) { 399 var key = iteratee(value, index, obj); 400 behavior(result, value, key); 401 }); 402 return result; 403 }; 404 }; 370 // Return the minimum element (or element-based computation). 371 _.min = function(obj, iteratee, context) { 372 var result = Infinity, 373 lastComputed = Infinity, 374 value, computed; 375 if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) { 376 obj = isArrayLike(obj) ? obj : _.values(obj); 377 for (var i = 0, length = obj.length; i < length; i++) { 378 value = obj[i]; 379 if (value != null && value < result) { 380 result = value; 381 } 382 } 383 } else { 384 iteratee = cb(iteratee, context); 385 _.each(obj, function(v, index, list) { 386 computed = iteratee(v, index, list); 387 if (computed < lastComputed || computed === Infinity && result === Infinity) { 388 result = v; 389 lastComputed = computed; 390 } 391 }); 392 } 393 return result; 394 }; 405 395 406 // Groups the object's values by a criterion. Pass either a string attribute 407 // to group by, or a function that returns the criterion. 408 _.groupBy = group(function(result, value, key) { 409 if (_.has(result, key)) result[key].push(value); else result[key] = [value]; 410 }); 396 // Shuffle a collection. 397 _.shuffle = function(obj) { 398 return _.sample(obj, Infinity); 399 }; 411 400 412 // Indexes the object's values by a criterion, similar to `groupBy`, but for 413 // when you know that your index values will be unique. 414 _.indexBy = group(function(result, value, key) { 415 result[key] = value; 416 }); 401 // Sample **n** random values from a collection using the modern version of the 402 // [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle). 403 // If **n** is not specified, returns a single random element. 404 // The internal `guard` argument allows it to work with `map`. 405 _.sample = function(obj, n, guard) { 406 if (n == null || guard) { 407 if (!isArrayLike(obj)) obj = _.values(obj); 408 return obj[_.random(obj.length - 1)]; 409 } 410 var sample = isArrayLike(obj) ? _.clone(obj) : _.values(obj); 411 var length = getLength(sample); 412 n = Math.max(Math.min(n, length), 0); 413 var last = length - 1; 414 for (var index = 0; index < n; index++) { 415 var rand = _.random(index, last); 416 var temp = sample[index]; 417 sample[index] = sample[rand]; 418 sample[rand] = temp; 419 } 420 return sample.slice(0, n); 421 }; 417 422 418 // Counts instances of an object that group by a certain criterion. Pass 419 // either a string attribute to count by, or a function that returns the 420 // criterion. 421 _.countBy = group(function(result, value, key) { 422 if (_.has(result, key)) result[key]++; else result[key] = 1; 423 }); 423 // Sort the object's values by a criterion produced by an iteratee. 424 _.sortBy = function(obj, iteratee, context) { 425 var index = 0; 426 iteratee = cb(iteratee, context); 427 return _.pluck(_.map(obj, function(value, key, list) { 428 return { 429 value: value, 430 index: index++, 431 criteria: iteratee(value, key, list) 432 }; 433 }).sort(function(left, right) { 434 var a = left.criteria; 435 var b = right.criteria; 436 if (a !== b) { 437 if (a > b || a === void 0) return 1; 438 if (a < b || b === void 0) return -1; 439 } 440 return left.index - right.index; 441 }), 'value'); 442 }; 424 443 425 // Safely create a real, live array from anything iterable. 426 _.toArray = function(obj) { 427 if (!obj) return []; 428 if (_.isArray(obj)) return slice.call(obj); 429 if (isArrayLike(obj)) return _.map(obj, _.identity); 430 return _.values(obj); 431 }; 444 // An internal function used for aggregate "group by" operations. 445 var group = function(behavior, partition) { 446 return function(obj, iteratee, context) { 447 var result = partition ? [ 448 [], 449 [] 450 ] : {}; 451 iteratee = cb(iteratee, context); 452 _.each(obj, function(value, index) { 453 var key = iteratee(value, index, obj); 454 behavior(result, value, key); 455 }); 456 return result; 457 }; 458 }; 432 459 433 // Return the number of elements in an object. 434 _.size = function(obj) { 435 if (obj == null) return 0; 436 return isArrayLike(obj) ? obj.length : _.keys(obj).length; 437 }; 460 // Groups the object's values by a criterion. Pass either a string attribute 461 // to group by, or a function that returns the criterion. 462 _.groupBy = group(function(result, value, key) { 463 if (has(result, key)) result[key].push(value); 464 else result[key] = [value]; 465 }); 438 466 439 // Split a collection into two arrays: one whose elements all satisfy the given 440 // predicate, and one whose elements all do not satisfy the predicate. 441 _.partition = function(obj, predicate, context) { 442 predicate = cb(predicate, context); 443 var pass = [], fail = []; 444 _.each(obj, function(value, key, obj) { 445 (predicate(value, key, obj) ? pass : fail).push(value); 446 }); 447 return [pass, fail]; 448 }; 467 // Indexes the object's values by a criterion, similar to `groupBy`, but for 468 // when you know that your index values will be unique. 469 _.indexBy = group(function(result, value, key) { 470 result[key] = value; 471 }); 449 472 450 // Array Functions 451 // --------------- 473 // Counts instances of an object that group by a certain criterion. Pass 474 // either a string attribute to count by, or a function that returns the 475 // criterion. 476 _.countBy = group(function(result, value, key) { 477 if (has(result, key)) result[key]++; 478 else result[key] = 1; 479 }); 452 480 453 // Get the first element of an array. Passing **n** will return the first N 454 // values in the array. Aliased as `head` and `take`. The **guard** check 455 // allows it to work with `_.map`. 456 _.first = _.head = _.take = function(array, n, guard) { 457 if (array == null) return void 0; 458 if (n == null || guard) return array[0]; 459 return _.initial(array, array.length - n); 460 }; 481 var reStrSymbol = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g; 482 // Safely create a real, live array from anything iterable. 483 _.toArray = function(obj) { 484 if (!obj) return []; 485 if (_.isArray(obj)) return slice.call(obj); 486 if (_.isString(obj)) { 487 // Keep surrogate pair characters together 488 return obj.match(reStrSymbol); 489 } 490 if (isArrayLike(obj)) return _.map(obj, _.identity); 491 return _.values(obj); 492 }; 461 493 462 // Returns everything but the last entry of the array. Especially useful on 463 // the arguments object. Passing **n** will return all the values in 464 // the array, excluding the last N. 465 _.initial = function(array, n, guard) { 466 return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n))); 467 }; 494 // Return the number of elements in an object. 495 _.size = function(obj) { 496 if (obj == null) return 0; 497 return isArrayLike(obj) ? obj.length : _.keys(obj).length; 498 }; 468 499 469 // Get the last element of an array. Passing **n** will return the last N 470 // values in the array. 471 _.last = function(array, n, guard) { 472 if (array == null) return void 0; 473 if (n == null || guard) return array[array.length - 1]; 474 return _.rest(array, Math.max(0, array.length - n)); 475 }; 500 // Split a collection into two arrays: one whose elements all satisfy the given 501 // predicate, and one whose elements all do not satisfy the predicate. 502 _.partition = group(function(result, value, pass) { 503 result[pass ? 0 : 1].push(value); 504 }, true); 476 505 477 // Returns everything but the first entry of the array. Aliased as `tail` and `drop`. 478 // Especially useful on the arguments object. Passing an **n** will return 479 // the rest N values in the array. 480 _.rest = _.tail = _.drop = function(array, n, guard) { 481 return slice.call(array, n == null || guard ? 1 : n); 482 }; 506 // Array Functions 507 // --------------- 483 508 484 // Trim out all falsy values from an array. 485 _.compact = function(array) { 486 return _.filter(array, _.identity); 487 }; 509 // Get the first element of an array. Passing **n** will return the first N 510 // values in the array. Aliased as `head` and `take`. The **guard** check 511 // allows it to work with `_.map`. 512 _.first = _.head = _.take = function(array, n, guard) { 513 if (array == null || array.length < 1) return n == null ? void 0 : []; 514 if (n == null || guard) return array[0]; 515 return _.initial(array, array.length - n); 516 }; 488 517 489 // Internal implementation of a recursive `flatten` function. 490 var flatten = function(input, shallow, strict, startIndex) { 491 var output = [], idx = 0; 492 for (var i = startIndex || 0, length = getLength(input); i < length; i++) { 493 var value = input[i]; 494 if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) { 495 //flatten current level of array or arguments object 496 if (!shallow) value = flatten(value, shallow, strict); 497 var j = 0, len = value.length; 498 output.length += len; 499 while (j < len) { 500 output[idx++] = value[j++]; 501 } 502 } else if (!strict) { 503 output[idx++] = value; 504 } 505 } 506 return output; 507 }; 518 // Returns everything but the last entry of the array. Especially useful on 519 // the arguments object. Passing **n** will return all the values in 520 // the array, excluding the last N. 521 _.initial = function(array, n, guard) { 522 return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n))); 523 }; 508 524 509 // Flatten out an array, either recursively (by default), or just one level. 510 _.flatten = function(array, shallow) { 511 return flatten(array, shallow, false); 512 }; 525 // Get the last element of an array. Passing **n** will return the last N 526 // values in the array. 527 _.last = function(array, n, guard) { 528 if (array == null || array.length < 1) return n == null ? void 0 : []; 529 if (n == null || guard) return array[array.length - 1]; 530 return _.rest(array, Math.max(0, array.length - n)); 531 }; 513 532 514 // Return a version of the array that does not contain the specified value(s). 515 _.without = function(array) { 516 return _.difference(array, slice.call(arguments, 1)); 517 }; 533 // Returns everything but the first entry of the array. Aliased as `tail` and `drop`. 534 // Especially useful on the arguments object. Passing an **n** will return 535 // the rest N values in the array. 536 _.rest = _.tail = _.drop = function(array, n, guard) { 537 return slice.call(array, n == null || guard ? 1 : n); 538 }; 518 539 519 // Produce a duplicate-free version of the array. If the array has already 520 // been sorted, you have the option of using a faster algorithm. 521 // Aliased as `unique`. 522 _.uniq = _.unique = function(array, isSorted, iteratee, context) { 523 if (!_.isBoolean(isSorted)) { 524 context = iteratee; 525 iteratee = isSorted; 526 isSorted = false; 527 } 528 if (iteratee != null) iteratee = cb(iteratee, context); 529 var result = []; 530 var seen = []; 531 for (var i = 0, length = getLength(array); i < length; i++) { 532 var value = array[i], 533 computed = iteratee ? iteratee(value, i, array) : value; 534 if (isSorted) { 535 if (!i || seen !== computed) result.push(value); 536 seen = computed; 537 } else if (iteratee) { 538 if (!_.contains(seen, computed)) { 539 seen.push(computed); 540 result.push(value); 541 } 542 } else if (!_.contains(result, value)) { 543 result.push(value); 544 } 545 } 546 return result; 547 }; 540 // Trim out all falsy values from an array. 541 _.compact = function(array) { 542 return _.filter(array, Boolean); 543 }; 548 544 549 // Produce an array that contains the union: each distinct element from all of 550 // the passed-in arrays. 551 _.union = function() { 552 return _.uniq(flatten(arguments, true, true)); 553 }; 545 // Internal implementation of a recursive `flatten` function. 546 var flatten = function(input, shallow, strict, output) { 547 output = output || []; 548 var idx = output.length; 549 for (var i = 0, length = getLength(input); i < length; i++) { 550 var value = input[i]; 551 if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) { 552 // Flatten current level of array or arguments object. 553 if (shallow) { 554 var j = 0, 555 len = value.length; 556 while (j < len) output[idx++] = value[j++]; 557 } else { 558 flatten(value, shallow, strict, output); 559 idx = output.length; 560 } 561 } else if (!strict) { 562 output[idx++] = value; 563 } 564 } 565 return output; 566 }; 554 567 555 // Produce an array that contains every item shared between all the 556 // passed-in arrays. 557 _.intersection = function(array) { 558 var result = []; 559 var argsLength = arguments.length; 560 for (var i = 0, length = getLength(array); i < length; i++) { 561 var item = array[i]; 562 if (_.contains(result, item)) continue; 563 for (var j = 1; j < argsLength; j++) { 564 if (!_.contains(arguments[j], item)) break; 565 } 566 if (j === argsLength) result.push(item); 567 } 568 return result; 569 }; 568 // Flatten out an array, either recursively (by default), or just one level. 569 _.flatten = function(array, shallow) { 570 return flatten(array, shallow, false); 571 }; 570 572 571 // Take the difference between one array and a number of other arrays. 572 // Only the elements present in just the first array will remain. 573 _.difference = function(array) { 574 var rest = flatten(arguments, true, true, 1); 575 return _.filter(array, function(value){ 576 return !_.contains(rest, value); 577 }); 578 }; 573 // Return a version of the array that does not contain the specified value(s). 574 _.without = restArguments(function(array, otherArrays) { 575 return _.difference(array, otherArrays); 576 }); 579 577 580 // Zip together multiple lists into a single array -- elements that share 581 // an index go together. 582 _.zip = function() { 583 return _.unzip(arguments); 584 }; 578 // Produce a duplicate-free version of the array. If the array has already 579 // been sorted, you have the option of using a faster algorithm. 580 // The faster algorithm will not work with an iteratee if the iteratee 581 // is not a one-to-one function, so providing an iteratee will disable 582 // the faster algorithm. 583 // Aliased as `unique`. 584 _.uniq = _.unique = function(array, isSorted, iteratee, context) { 585 if (!_.isBoolean(isSorted)) { 586 context = iteratee; 587 iteratee = isSorted; 588 isSorted = false; 589 } 590 if (iteratee != null) iteratee = cb(iteratee, context); 591 var result = []; 592 var seen = []; 593 for (var i = 0, length = getLength(array); i < length; i++) { 594 var value = array[i], 595 computed = iteratee ? iteratee(value, i, array) : value; 596 if (isSorted && !iteratee) { 597 if (!i || seen !== computed) result.push(value); 598 seen = computed; 599 } else if (iteratee) { 600 if (!_.contains(seen, computed)) { 601 seen.push(computed); 602 result.push(value); 603 } 604 } else if (!_.contains(result, value)) { 605 result.push(value); 606 } 607 } 608 return result; 609 }; 585 610 586 // Complement of _.zip. Unzip accepts an array of arrays and groups 587 // each array's elements on shared indices 588 _.unzip = function(array) {589 var length = array && _.max(array, getLength).length || 0;590 var result = Array(length);611 // Produce an array that contains the union: each distinct element from all of 612 // the passed-in arrays. 613 _.union = restArguments(function(arrays) { 614 return _.uniq(flatten(arrays, true, true)); 615 }); 591 616 592 for (var index = 0; index < length; index++) { 593 result[index] = _.pluck(array, index); 594 } 595 return result; 596 }; 617 // Produce an array that contains every item shared between all the 618 // passed-in arrays. 619 _.intersection = function(array) { 620 var result = []; 621 var argsLength = arguments.length; 622 for (var i = 0, length = getLength(array); i < length; i++) { 623 var item = array[i]; 624 if (_.contains(result, item)) continue; 625 var j; 626 for (j = 1; j < argsLength; j++) { 627 if (!_.contains(arguments[j], item)) break; 628 } 629 if (j === argsLength) result.push(item); 630 } 631 return result; 632 }; 597 633 598 // Converts lists into objects. Pass either a single array of `[key, value]` 599 // pairs, or two parallel arrays of the same length -- one of keys, and one of 600 // the corresponding values. 601 _.object = function(list, values) { 602 var result = {}; 603 for (var i = 0, length = getLength(list); i < length; i++) { 604 if (values) { 605 result[list[i]] = values[i]; 606 } else { 607 result[list[i][0]] = list[i][1]; 608 } 609 } 610 return result; 611 }; 634 // Take the difference between one array and a number of other arrays. 635 // Only the elements present in just the first array will remain. 636 _.difference = restArguments(function(array, rest) { 637 rest = flatten(rest, true, true); 638 return _.filter(array, function(value) { 639 return !_.contains(rest, value); 640 }); 641 }); 612 642 613 // Generator function to create the findIndex and findLastIndex functions 614 function createPredicateIndexFinder(dir) { 615 return function(array, predicate, context) { 616 predicate = cb(predicate, context); 617 var length = getLength(array); 618 var index = dir > 0 ? 0 : length - 1; 619 for (; index >= 0 && index < length; index += dir) { 620 if (predicate(array[index], index, array)) return index; 621 } 622 return -1; 623 }; 624 } 643 // Complement of _.zip. Unzip accepts an array of arrays and groups 644 // each array's elements on shared indices. 645 _.unzip = function(array) { 646 var length = array && _.max(array, getLength).length || 0; 647 var result = Array(length); 625 648 626 // Returns the first index on an array-like that passes a predicate test 627 _.findIndex = createPredicateIndexFinder(1); 628 _.findLastIndex = createPredicateIndexFinder(-1); 649 for (var index = 0; index < length; index++) { 650 result[index] = _.pluck(array, index); 651 } 652 return result; 653 }; 629 654 630 // Use a comparator function to figure out the smallest index at which 631 // an object should be inserted so as to maintain order. Uses binary search. 632 _.sortedIndex = function(array, obj, iteratee, context) { 633 iteratee = cb(iteratee, context, 1); 634 var value = iteratee(obj); 635 var low = 0, high = getLength(array); 636 while (low < high) { 637 var mid = Math.floor((low + high) / 2); 638 if (iteratee(array[mid]) < value) low = mid + 1; else high = mid; 639 } 640 return low; 641 }; 655 // Zip together multiple lists into a single array -- elements that share 656 // an index go together. 657 _.zip = restArguments(_.unzip); 642 658 643 // Generator function to create the indexOf and lastIndexOf functions 644 function createIndexFinder(dir, predicateFind, sortedIndex) { 645 return function(array, item, idx) { 646 var i = 0, length = getLength(array); 647 if (typeof idx == 'number') { 648 if (dir > 0) { 649 i = idx >= 0 ? idx : Math.max(idx + length, i); 650 } else { 651 length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1; 652 } 653 } else if (sortedIndex && idx && length) { 654 idx = sortedIndex(array, item); 655 return array[idx] === item ? idx : -1; 656 } 657 if (item !== item) { 658 idx = predicateFind(slice.call(array, i, length), _.isNaN); 659 return idx >= 0 ? idx + i : -1; 660 } 661 for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) { 662 if (array[idx] === item) return idx; 663 } 664 return -1; 665 }; 666 } 659 // Converts lists into objects. Pass either a single array of `[key, value]` 660 // pairs, or two parallel arrays of the same length -- one of keys, and one of 661 // the corresponding values. Passing by pairs is the reverse of _.pairs. 662 _.object = function(list, values) { 663 var result = {}; 664 for (var i = 0, length = getLength(list); i < length; i++) { 665 if (values) { 666 result[list[i]] = values[i]; 667 } else { 668 result[list[i][0]] = list[i][1]; 669 } 670 } 671 return result; 672 }; 667 673 668 // Return the position of the first occurrence of an item in an array, 669 // or -1 if the item is not included in the array. 670 // If the array is large and already in sort order, pass `true` 671 // for **isSorted** to use binary search. 672 _.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex); 673 _.lastIndexOf = createIndexFinder(-1, _.findLastIndex); 674 // Generator function to create the findIndex and findLastIndex functions. 675 var createPredicateIndexFinder = function(dir) { 676 return function(array, predicate, context) { 677 predicate = cb(predicate, context); 678 var length = getLength(array); 679 var index = dir > 0 ? 0 : length - 1; 680 for (; index >= 0 && index < length; index += dir) { 681 if (predicate(array[index], index, array)) return index; 682 } 683 return -1; 684 }; 685 }; 674 686 675 // Generate an integer Array containing an arithmetic progression. A port of 676 // the native Python `range()` function. See 677 // [the Python documentation](http://docs.python.org/library/functions.html#range). 678 _.range = function(start, stop, step) { 679 if (stop == null) { 680 stop = start || 0; 681 start = 0; 682 } 683 step = step || 1; 687 // Returns the first index on an array-like that passes a predicate test. 688 _.findIndex = createPredicateIndexFinder(1); 689 _.findLastIndex = createPredicateIndexFinder(-1); 684 690 685 var length = Math.max(Math.ceil((stop - start) / step), 0); 686 var range = Array(length); 691 // Use a comparator function to figure out the smallest index at which 692 // an object should be inserted so as to maintain order. Uses binary search. 693 _.sortedIndex = function(array, obj, iteratee, context) { 694 iteratee = cb(iteratee, context, 1); 695 var value = iteratee(obj); 696 var low = 0, 697 high = getLength(array); 698 while (low < high) { 699 var mid = Math.floor((low + high) / 2); 700 if (iteratee(array[mid]) < value) low = mid + 1; 701 else high = mid; 702 } 703 return low; 704 }; 687 705 688 for (var idx = 0; idx < length; idx++, start += step) { 689 range[idx] = start; 690 } 706 // Generator function to create the indexOf and lastIndexOf functions. 707 var createIndexFinder = function(dir, predicateFind, sortedIndex) { 708 return function(array, item, idx) { 709 var i = 0, 710 length = getLength(array); 711 if (typeof idx == 'number') { 712 if (dir > 0) { 713 i = idx >= 0 ? idx : Math.max(idx + length, i); 714 } else { 715 length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1; 716 } 717 } else if (sortedIndex && idx && length) { 718 idx = sortedIndex(array, item); 719 return array[idx] === item ? idx : -1; 720 } 721 if (item !== item) { 722 idx = predicateFind(slice.call(array, i, length), _.isNaN); 723 return idx >= 0 ? idx + i : -1; 724 } 725 for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) { 726 if (array[idx] === item) return idx; 727 } 728 return -1; 729 }; 730 }; 691 731 692 return range; 693 }; 732 // Return the position of the first occurrence of an item in an array, 733 // or -1 if the item is not included in the array. 734 // If the array is large and already in sort order, pass `true` 735 // for **isSorted** to use binary search. 736 _.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex); 737 _.lastIndexOf = createIndexFinder(-1, _.findLastIndex); 694 738 695 // Function (ahem) Functions 696 // ------------------ 739 // Generate an integer Array containing an arithmetic progression. A port of 740 // the native Python `range()` function. See 741 // [the Python documentation](http://docs.python.org/library/functions.html#range). 742 _.range = function(start, stop, step) { 743 if (stop == null) { 744 stop = start || 0; 745 start = 0; 746 } 747 if (!step) { 748 step = stop < start ? -1 : 1; 749 } 697 750 698 // Determines whether to execute a function as a constructor 699 // or a normal function with the provided arguments 700 var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) { 701 if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args); 702 var self = baseCreate(sourceFunc.prototype); 703 var result = sourceFunc.apply(self, args); 704 if (_.isObject(result)) return result; 705 return self; 706 }; 751 var length = Math.max(Math.ceil((stop - start) / step), 0); 752 var range = Array(length); 707 753 708 // Create a function bound to a given object (assigning `this`, and arguments, 709 // optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if 710 // available. 711 _.bind = function(func, context) { 712 if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1)); 713 if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function'); 714 var args = slice.call(arguments, 2); 715 var bound = function() { 716 return executeBound(func, bound, context, this, args.concat(slice.call(arguments))); 717 }; 718 return bound; 719 }; 754 for (var idx = 0; idx < length; idx++, start += step) { 755 range[idx] = start; 756 } 720 757 721 // Partially apply a function by creating a version that has had some of its 722 // arguments pre-filled, without changing its dynamic `this` context. _ acts 723 // as a placeholder, allowing any combination of arguments to be pre-filled. 724 _.partial = function(func) { 725 var boundArgs = slice.call(arguments, 1); 726 var bound = function() { 727 var position = 0, length = boundArgs.length; 728 var args = Array(length); 729 for (var i = 0; i < length; i++) { 730 args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i]; 731 } 732 while (position < arguments.length) args.push(arguments[position++]); 733 return executeBound(func, bound, this, this, args); 734 }; 735 return bound; 736 }; 758 return range; 759 }; 737 760 738 // Bind a number of an object's methods to that object. Remaining arguments 739 // are the method names to be bound. Useful for ensuring that all callbacks 740 // defined on an object belong to it. 741 _.bindAll = function(obj) { 742 var i, length = arguments.length, key;743 if (length <= 1) throw new Error('bindAll must be passed function names'); 744 for (i = 1; i < length; i++) { 745 key = arguments[i]; 746 obj[key] = _.bind(obj[key], obj);747 748 return obj;749 761 // Chunk a single array into multiple arrays, each containing `count` or fewer 762 // items. 763 _.chunk = function(array, count) { 764 if (count == null || count < 1) return []; 765 var result = []; 766 var i = 0, 767 length = array.length; 768 while (i < length) { 769 result.push(slice.call(array, i, i += count)); 770 } 771 return result; 772 }; 750 773 751 // Memoize an expensive function by storing its results. 752 _.memoize = function(func, hasher) { 753 var memoize = function(key) { 754 var cache = memoize.cache; 755 var address = '' + (hasher ? hasher.apply(this, arguments) : key); 756 if (!_.has(cache, address)) cache[address] = func.apply(this, arguments); 757 return cache[address]; 758 }; 759 memoize.cache = {}; 760 return memoize; 761 }; 774 // Function (ahem) Functions 775 // ------------------ 762 776 763 // Delays a function for the given number of milliseconds, and then calls 764 // it with the arguments supplied. 765 _.delay = function(func, wait) { 766 var args = slice.call(arguments, 2); 767 return setTimeout(function(){ 768 return func.apply(null, args); 769 }, wait); 770 }; 777 // Determines whether to execute a function as a constructor 778 // or a normal function with the provided arguments. 779 var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) { 780 if (!(callingContext instanceof boundFunc)) return sourceFunc.apply(context, args); 781 var self = baseCreate(sourceFunc.prototype); 782 var result = sourceFunc.apply(self, args); 783 if (_.isObject(result)) return result; 784 return self; 785 }; 771 786 772 // Defers a function, scheduling it to run after the current call stack has 773 // cleared. 774 _.defer = _.partial(_.delay, _, 1); 787 // Create a function bound to a given object (assigning `this`, and arguments, 788 // optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if 789 // available. 790 _.bind = restArguments(function(func, context, args) { 791 if (!_.isFunction(func)) throw new TypeError('Bind must be called on a function'); 792 var bound = restArguments(function(callArgs) { 793 return executeBound(func, bound, context, this, args.concat(callArgs)); 794 }); 795 return bound; 796 }); 775 797 776 // Returns a function, that, when invoked, will only be triggered at most once 777 // during a given window of time. Normally, the throttled function will run 778 // as much as it can, without ever going more than once per `wait` duration; 779 // but if you'd like to disable the execution on the leading edge, pass 780 // `{leading: false}`. To disable execution on the trailing edge, ditto. 781 _.throttle = function(func, wait, options) { 782 var context, args, result; 783 var timeout = null; 784 var previous = 0; 785 if (!options) options = {}; 786 var later = function() { 787 previous = options.leading === false ? 0 : _.now(); 788 timeout = null; 789 result = func.apply(context, args); 790 if (!timeout) context = args = null; 791 }; 792 return function() { 793 var now = _.now(); 794 if (!previous && options.leading === false) previous = now; 795 var remaining = wait - (now - previous); 796 context = this; 797 args = arguments; 798 if (remaining <= 0 || remaining > wait) { 799 if (timeout) { 800 clearTimeout(timeout); 801 timeout = null; 802 } 803 previous = now; 804 result = func.apply(context, args); 805 if (!timeout) context = args = null; 806 } else if (!timeout && options.trailing !== false) { 807 timeout = setTimeout(later, remaining); 808 } 809 return result; 810 }; 811 }; 798 // Partially apply a function by creating a version that has had some of its 799 // arguments pre-filled, without changing its dynamic `this` context. _ acts 800 // as a placeholder by default, allowing any combination of arguments to be 801 // pre-filled. Set `_.partial.placeholder` for a custom placeholder argument. 802 _.partial = restArguments(function(func, boundArgs) { 803 var placeholder = _.partial.placeholder; 804 var bound = function() { 805 var position = 0, 806 length = boundArgs.length; 807 var args = Array(length); 808 for (var i = 0; i < length; i++) { 809 args[i] = boundArgs[i] === placeholder ? arguments[position++] : boundArgs[i]; 810 } 811 while (position < arguments.length) args.push(arguments[position++]); 812 return executeBound(func, bound, this, this, args); 813 }; 814 return bound; 815 }); 812 816 813 // Returns a function, that, as long as it continues to be invoked, will not 814 // be triggered. The function will be called after it stops being called for 815 // N milliseconds. If `immediate` is passed, trigger the function on the 816 // leading edge, instead of the trailing. 817 _.debounce = function(func, wait, immediate) { 818 var timeout, args, context, timestamp, result; 817 _.partial.placeholder = _; 819 818 820 var later = function() { 821 var last = _.now() - timestamp; 819 // Bind a number of an object's methods to that object. Remaining arguments 820 // are the method names to be bound. Useful for ensuring that all callbacks 821 // defined on an object belong to it. 822 _.bindAll = restArguments(function(obj, keys) { 823 keys = flatten(keys, false, false); 824 var index = keys.length; 825 if (index < 1) throw new Error('bindAll must be passed function names'); 826 while (index--) { 827 var key = keys[index]; 828 obj[key] = _.bind(obj[key], obj); 829 } 830 }); 822 831 823 if (last < wait && last >= 0) { 824 timeout = setTimeout(later, wait - last); 825 } else { 826 timeout = null; 827 if (!immediate) { 828 result = func.apply(context, args); 829 if (!timeout) context = args = null; 830 } 831 } 832 }; 832 // Memoize an expensive function by storing its results. 833 _.memoize = function(func, hasher) { 834 var memoize = function(key) { 835 var cache = memoize.cache; 836 var address = '' + (hasher ? hasher.apply(this, arguments) : key); 837 if (!has(cache, address)) cache[address] = func.apply(this, arguments); 838 return cache[address]; 839 }; 840 memoize.cache = {}; 841 return memoize; 842 }; 833 843 834 return function() { 835 context = this; 836 args = arguments; 837 timestamp = _.now(); 838 var callNow = immediate && !timeout; 839 if (!timeout) timeout = setTimeout(later, wait); 840 if (callNow) { 841 result = func.apply(context, args); 842 context = args = null; 843 } 844 // Delays a function for the given number of milliseconds, and then calls 845 // it with the arguments supplied. 846 _.delay = restArguments(function(func, wait, args) { 847 return setTimeout(function() { 848 return func.apply(null, args); 849 }, wait); 850 }); 844 851 845 return result; 846 }; 847 };852 // Defers a function, scheduling it to run after the current call stack has 853 // cleared. 854 _.defer = _.partial(_.delay, _, 1); 848 855 849 // Returns the first function passed as an argument to the second, 850 // allowing you to adjust arguments, run code before and after, and 851 // conditionally execute the original function. 852 _.wrap = function(func, wrapper) { 853 return _.partial(wrapper, func); 854 }; 856 // Returns a function, that, when invoked, will only be triggered at most once 857 // during a given window of time. Normally, the throttled function will run 858 // as much as it can, without ever going more than once per `wait` duration; 859 // but if you'd like to disable the execution on the leading edge, pass 860 // `{leading: false}`. To disable execution on the trailing edge, ditto. 861 _.throttle = function(func, wait, options) { 862 var timeout, context, args, result; 863 var previous = 0; 864 if (!options) options = {}; 855 865 856 // Returns a negated version of the passed-in predicate. 857 _.negate = function(predicate) { 858 return function() { 859 return !predicate.apply(this, arguments);860 };861 866 var later = function() { 867 previous = options.leading === false ? 0 : _.now(); 868 timeout = null; 869 result = func.apply(context, args); 870 if (!timeout) context = args = null; 871 }; 862 872 863 // Returns a function that is the composition of a list of functions, each 864 // consuming the return value of the function that follows. 865 _.compose = function() { 866 var args = arguments; 867 var start = args.length - 1; 868 return function() { 869 var i = start; 870 var result = args[start].apply(this, arguments); 871 while (i--) result = args[i].call(this, result); 872 return result; 873 }; 874 }; 873 var throttled = function() { 874 var now = _.now(); 875 if (!previous && options.leading === false) previous = now; 876 var remaining = wait - (now - previous); 877 context = this; 878 args = arguments; 879 if (remaining <= 0 || remaining > wait) { 880 if (timeout) { 881 clearTimeout(timeout); 882 timeout = null; 883 } 884 previous = now; 885 result = func.apply(context, args); 886 if (!timeout) context = args = null; 887 } else if (!timeout && options.trailing !== false) { 888 timeout = setTimeout(later, remaining); 889 } 890 return result; 891 }; 875 892 876 // Returns a function that will only be executed on and after the Nth call. 877 _.after = function(times, func) { 878 return function() { 879 if (--times < 1) { 880 return func.apply(this, arguments); 881 } 882 }; 883 }; 893 throttled.cancel = function() { 894 clearTimeout(timeout); 895 previous = 0; 896 timeout = context = args = null; 897 }; 884 898 885 // Returns a function that will only be executed up to (but not including) the Nth call. 886 _.before = function(times, func) { 887 var memo; 888 return function() { 889 if (--times > 0) { 890 memo = func.apply(this, arguments); 891 } 892 if (times <= 1) func = null; 893 return memo; 894 }; 895 }; 899 return throttled; 900 }; 896 901 897 // Returns a function that will be executed at most one time, no matter how 898 // often you call it. Useful for lazy initialization. 899 _.once = _.partial(_.before, 2); 902 // Returns a function, that, as long as it continues to be invoked, will not 903 // be triggered. The function will be called after it stops being called for 904 // N milliseconds. If `immediate` is passed, trigger the function on the 905 // leading edge, instead of the trailing. 906 _.debounce = function(func, wait, immediate) { 907 var timeout, result; 900 908 901 // Object Functions 902 // ---------------- 909 var later = function(context, args) { 910 timeout = null; 911 if (args) result = func.apply(context, args); 912 }; 903 913 904 // Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed. 905 var hasEnumBug = !{toString: null}.propertyIsEnumerable('toString'); 906 var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString', 907 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString']; 914 var debounced = restArguments(function(args) { 915 if (timeout) clearTimeout(timeout); 916 if (immediate) { 917 var callNow = !timeout; 918 timeout = setTimeout(later, wait); 919 if (callNow) result = func.apply(this, args); 920 } else { 921 timeout = _.delay(later, wait, this, args); 922 } 908 923 909 function collectNonEnumProps(obj, keys) { 910 var nonEnumIdx = nonEnumerableProps.length; 911 var constructor = obj.constructor; 912 var proto = (_.isFunction(constructor) && constructor.prototype) || ObjProto; 924 return result; 925 }); 913 926 914 // Constructor is a special case. 915 var prop = 'constructor'; 916 if (_.has(obj, prop) && !_.contains(keys, prop)) keys.push(prop); 927 debounced.cancel = function() { 928 clearTimeout(timeout); 929 timeout = null; 930 }; 917 931 918 while (nonEnumIdx--) { 919 prop = nonEnumerableProps[nonEnumIdx]; 920 if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) { 921 keys.push(prop); 922 } 923 } 924 } 932 return debounced; 933 }; 925 934 926 // Retrieve the names of an object's own properties. 927 // Delegates to **ECMAScript 5**'s native `Object.keys` 928 _.keys = function(obj) { 929 if (!_.isObject(obj)) return []; 930 if (nativeKeys) return nativeKeys(obj); 931 var keys = []; 932 for (var key in obj) if (_.has(obj, key)) keys.push(key); 933 // Ahem, IE < 9. 934 if (hasEnumBug) collectNonEnumProps(obj, keys); 935 return keys; 936 }; 935 // Returns the first function passed as an argument to the second, 936 // allowing you to adjust arguments, run code before and after, and 937 // conditionally execute the original function. 938 _.wrap = function(func, wrapper) { 939 return _.partial(wrapper, func); 940 }; 937 941 938 // Retrieve all the property names of an object. 939 _.allKeys = function(obj) { 940 if (!_.isObject(obj)) return []; 941 var keys = []; 942 for (var key in obj) keys.push(key); 943 // Ahem, IE < 9. 944 if (hasEnumBug) collectNonEnumProps(obj, keys); 945 return keys; 946 }; 942 // Returns a negated version of the passed-in predicate. 943 _.negate = function(predicate) { 944 return function() { 945 return !predicate.apply(this, arguments); 946 }; 947 }; 947 948 948 // Retrieve the values of an object's properties. 949 _.values = function(obj) { 950 var keys = _.keys(obj); 951 var length = keys.length; 952 var values = Array(length); 953 for (var i = 0; i < length; i++) { 954 values[i] = obj[keys[i]]; 955 } 956 return values; 957 }; 949 // Returns a function that is the composition of a list of functions, each 950 // consuming the return value of the function that follows. 951 _.compose = function() { 952 var args = arguments; 953 var start = args.length - 1; 954 return function() { 955 var i = start; 956 var result = args[start].apply(this, arguments); 957 while (i--) result = args[i].call(this, result); 958 return result; 959 }; 960 }; 958 961 959 // Returns the results of applying the iteratee to each element of the object 960 // In contrast to _.map it returns an object 961 _.mapObject = function(obj, iteratee, context) { 962 iteratee = cb(iteratee, context); 963 var keys = _.keys(obj), 964 length = keys.length, 965 results = {}, 966 currentKey; 967 for (var index = 0; index < length; index++) { 968 currentKey = keys[index]; 969 results[currentKey] = iteratee(obj[currentKey], currentKey, obj); 970 } 971 return results; 972 }; 962 // Returns a function that will only be executed on and after the Nth call. 963 _.after = function(times, func) { 964 return function() { 965 if (--times < 1) { 966 return func.apply(this, arguments); 967 } 968 }; 969 }; 973 970 974 // Convert an object into a list of `[key, value]` pairs. 975 _.pairs = function(obj) { 976 var keys = _.keys(obj); 977 var length = keys.length; 978 var pairs = Array(length); 979 for (var i = 0; i < length; i++) { 980 pairs[i] = [keys[i], obj[keys[i]]]; 981 } 982 return pairs; 983 }; 971 // Returns a function that will only be executed up to (but not including) the Nth call. 972 _.before = function(times, func) { 973 var memo; 974 return function() { 975 if (--times > 0) { 976 memo = func.apply(this, arguments); 977 } 978 if (times <= 1) func = null; 979 return memo; 980 }; 981 }; 984 982 985 // Invert the keys and values of an object. The values must be serializable. 986 _.invert = function(obj) { 987 var result = {}; 988 var keys = _.keys(obj); 989 for (var i = 0, length = keys.length; i < length; i++) { 990 result[obj[keys[i]]] = keys[i]; 991 } 992 return result; 993 }; 983 // Returns a function that will be executed at most one time, no matter how 984 // often you call it. Useful for lazy initialization. 985 _.once = _.partial(_.before, 2); 994 986 995 // Return a sorted list of the function names available on the object. 996 // Aliased as `methods` 997 _.functions = _.methods = function(obj) { 998 var names = []; 999 for (var key in obj) { 1000 if (_.isFunction(obj[key])) names.push(key); 1001 } 1002 return names.sort(); 1003 }; 987 _.restArguments = restArguments; 1004 988 1005 // Extend a given object with all the properties in passed-in object(s). 1006 _.extend = createAssigner(_.allKeys); 989 // Object Functions 990 // ---------------- 1007 991 1008 // Assigns a given object with all the own properties in the passed-in object(s) 1009 // (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) 1010 _.extendOwn = _.assign = createAssigner(_.keys); 992 // Keys in IE < 9 that won't be iterated by `for key in ...` and thus missed. 993 var hasEnumBug = !{ 994 toString: null 995 }.propertyIsEnumerable('toString'); 996 var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString', 997 'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString' 998 ]; 1011 999 1012 // Returns the first key on an object that passes a predicate test 1013 _.findKey = function(obj, predicate, context) { 1014 predicate = cb(predicate, context); 1015 var keys = _.keys(obj), key; 1016 for (var i = 0, length = keys.length; i < length; i++) { 1017 key = keys[i]; 1018 if (predicate(obj[key], key, obj)) return key; 1019 } 1020 }; 1000 var collectNonEnumProps = function(obj, keys) { 1001 var nonEnumIdx = nonEnumerableProps.length; 1002 var constructor = obj.constructor; 1003 var proto = _.isFunction(constructor) && constructor.prototype || ObjProto; 1021 1004 1022 // Return a copy of the object only containing the whitelisted properties. 1023 _.pick = function(object, oiteratee, context) { 1024 var result = {}, obj = object, iteratee, keys; 1025 if (obj == null) return result; 1026 if (_.isFunction(oiteratee)) { 1027 keys = _.allKeys(obj); 1028 iteratee = optimizeCb(oiteratee, context); 1029 } else { 1030 keys = flatten(arguments, false, false, 1); 1031 iteratee = function(value, key, obj) { return key in obj; }; 1032 obj = Object(obj); 1033 } 1034 for (var i = 0, length = keys.length; i < length; i++) { 1035 var key = keys[i]; 1036 var value = obj[key]; 1037 if (iteratee(value, key, obj)) result[key] = value; 1038 } 1039 return result; 1040 }; 1005 // Constructor is a special case. 1006 var prop = 'constructor'; 1007 if (has(obj, prop) && !_.contains(keys, prop)) keys.push(prop); 1041 1008 1042 // Return a copy of the object without the blacklisted properties. 1043 _.omit = function(obj, iteratee, context) { 1044 if (_.isFunction(iteratee)) { 1045 iteratee = _.negate(iteratee); 1046 } else { 1047 var keys = _.map(flatten(arguments, false, false, 1), String); 1048 iteratee = function(value, key) { 1049 return !_.contains(keys, key); 1050 }; 1051 } 1052 return _.pick(obj, iteratee, context); 1053 }; 1009 while (nonEnumIdx--) { 1010 prop = nonEnumerableProps[nonEnumIdx]; 1011 if (prop in obj && obj[prop] !== proto[prop] && !_.contains(keys, prop)) { 1012 keys.push(prop); 1013 } 1014 } 1015 }; 1054 1016 1055 // Fill in a given object with default properties. 1056 _.defaults = createAssigner(_.allKeys, true); 1017 // Retrieve the names of an object's own properties. 1018 // Delegates to **ECMAScript 5**'s native `Object.keys`. 1019 _.keys = function(obj) { 1020 if (!_.isObject(obj)) return []; 1021 if (nativeKeys) return nativeKeys(obj); 1022 var keys = []; 1023 for (var key in obj) 1024 if (has(obj, key)) keys.push(key); 1025 // Ahem, IE < 9. 1026 if (hasEnumBug) collectNonEnumProps(obj, keys); 1027 return keys; 1028 }; 1057 1029 1058 // Creates an object that inherits from the given prototype object. 1059 // If additional properties are provided then they will be added to the 1060 // created object. 1061 _.create = function(prototype, props) { 1062 var result = baseCreate(prototype); 1063 if (props) _.extendOwn(result, props); 1064 return result; 1065 }; 1030 // Retrieve all the property names of an object. 1031 _.allKeys = function(obj) { 1032 if (!_.isObject(obj)) return []; 1033 var keys = []; 1034 for (var key in obj) keys.push(key); 1035 // Ahem, IE < 9. 1036 if (hasEnumBug) collectNonEnumProps(obj, keys); 1037 return keys; 1038 }; 1066 1039 1067 // Create a (shallow-cloned) duplicate of an object. 1068 _.clone = function(obj) { 1069 if (!_.isObject(obj)) return obj; 1070 return _.isArray(obj) ? obj.slice() : _.extend({}, obj); 1071 }; 1040 // Retrieve the values of an object's properties. 1041 _.values = function(obj) { 1042 var keys = _.keys(obj); 1043 var length = keys.length; 1044 var values = Array(length); 1045 for (var i = 0; i < length; i++) { 1046 values[i] = obj[keys[i]]; 1047 } 1048 return values; 1049 }; 1072 1050 1073 // Invokes interceptor with the obj, and then returns obj. 1074 // The primary purpose of this method is to "tap into" a method chain, in 1075 // order to perform operations on intermediate results within the chain. 1076 _.tap = function(obj, interceptor) { 1077 interceptor(obj); 1078 return obj; 1079 }; 1051 // Returns the results of applying the iteratee to each element of the object. 1052 // In contrast to _.map it returns an object. 1053 _.mapObject = function(obj, iteratee, context) { 1054 iteratee = cb(iteratee, context); 1055 var keys = _.keys(obj), 1056 length = keys.length, 1057 results = {}; 1058 for (var index = 0; index < length; index++) { 1059 var currentKey = keys[index]; 1060 results[currentKey] = iteratee(obj[currentKey], currentKey, obj); 1061 } 1062 return results; 1063 }; 1080 1064 1081 // Returns whether an object has a given set of `key:value` pairs.1082 _.isMatch = function(object, attrs) { 1083 var keys = _.keys(attrs), length = keys.length; 1084 if (object == null) return !length;1085 var obj = Object(object);1086 for (var i = 0; i < length; i++) { 1087 var key = keys[i]; 1088 if (attrs[key] !== obj[key] || !(key in obj)) return false;1089 1090 return true;1091 1065 // Convert an object into a list of `[key, value]` pairs. 1066 // The opposite of _.object. 1067 _.pairs = function(obj) { 1068 var keys = _.keys(obj); 1069 var length = keys.length; 1070 var pairs = Array(length); 1071 for (var i = 0; i < length; i++) { 1072 pairs[i] = [keys[i], obj[keys[i]]]; 1073 } 1074 return pairs; 1075 }; 1092 1076 1077 // Invert the keys and values of an object. The values must be serializable. 1078 _.invert = function(obj) { 1079 var result = {}; 1080 var keys = _.keys(obj); 1081 for (var i = 0, length = keys.length; i < length; i++) { 1082 result[obj[keys[i]]] = keys[i]; 1083 } 1084 return result; 1085 }; 1093 1086 1094 // Internal recursive comparison function for `isEqual`. 1095 var eq = function(a, b, aStack, bStack) { 1096 // Identical objects are equal. `0 === -0`, but they aren't identical. 1097 // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). 1098 if (a === b) return a !== 0 || 1 / a === 1 / b; 1099 // A strict comparison is necessary because `null == undefined`. 1100 if (a == null || b == null) return a === b; 1101 // Unwrap any wrapped objects. 1102 if (a instanceof _) a = a._wrapped; 1103 if (b instanceof _) b = b._wrapped; 1104 // Compare `[[Class]]` names. 1105 var className = toString.call(a); 1106 if (className !== toString.call(b)) return false; 1107 switch (className) { 1108 // Strings, numbers, regular expressions, dates, and booleans are compared by value. 1109 case '[object RegExp]': 1110 // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i') 1111 case '[object String]': 1112 // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is 1113 // equivalent to `new String("5")`. 1114 return '' + a === '' + b; 1115 case '[object Number]': 1116 // `NaN`s are equivalent, but non-reflexive. 1117 // Object(NaN) is equivalent to NaN 1118 if (+a !== +a) return +b !== +b; 1119 // An `egal` comparison is performed for other numeric values. 1120 return +a === 0 ? 1 / +a === 1 / b : +a === +b; 1121 case '[object Date]': 1122 case '[object Boolean]': 1123 // Coerce dates and booleans to numeric primitive values. Dates are compared by their 1124 // millisecond representations. Note that invalid dates with millisecond representations 1125 // of `NaN` are not equivalent. 1126 return +a === +b; 1127 } 1087 // Return a sorted list of the function names available on the object. 1088 // Aliased as `methods`. 1089 _.functions = _.methods = function(obj) { 1090 var names = []; 1091 for (var key in obj) { 1092 if (_.isFunction(obj[key])) names.push(key); 1093 } 1094 return names.sort(); 1095 }; 1128 1096 1129 var areArrays = className === '[object Array]'; 1130 if (!areArrays) { 1131 if (typeof a != 'object' || typeof b != 'object') return false; 1097 // An internal function for creating assigner functions. 1098 var createAssigner = function(keysFunc, defaults) { 1099 return function(obj) { 1100 var length = arguments.length; 1101 if (defaults) obj = Object(obj); 1102 if (length < 2 || obj == null) return obj; 1103 for (var index = 1; index < length; index++) { 1104 var source = arguments[index], 1105 keys = keysFunc(source), 1106 l = keys.length; 1107 for (var i = 0; i < l; i++) { 1108 var key = keys[i]; 1109 if (!defaults || obj[key] === void 0) obj[key] = source[key]; 1110 } 1111 } 1112 return obj; 1113 }; 1114 }; 1132 1115 1133 // Objects with different constructors are not equivalent, but `Object`s or `Array`s 1134 // from different frames are. 1135 var aCtor = a.constructor, bCtor = b.constructor; 1136 if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor && 1137 _.isFunction(bCtor) && bCtor instanceof bCtor) 1138 && ('constructor' in a && 'constructor' in b)) { 1139 return false; 1140 } 1141 } 1142 // Assume equality for cyclic structures. The algorithm for detecting cyclic 1143 // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`. 1116 // Extend a given object with all the properties in passed-in object(s). 1117 _.extend = createAssigner(_.allKeys); 1144 1118 1145 // Initializing stack of traversed objects. 1146 // It's done here since we only need them for objects and arrays comparison. 1147 aStack = aStack || []; 1148 bStack = bStack || []; 1149 var length = aStack.length; 1150 while (length--) { 1151 // Linear search. Performance is inversely proportional to the number of 1152 // unique nested structures. 1153 if (aStack[length] === a) return bStack[length] === b; 1154 } 1119 // Assigns a given object with all the own properties in the passed-in object(s). 1120 // (https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) 1121 _.extendOwn = _.assign = createAssigner(_.keys); 1155 1122 1156 // Add the first object to the stack of traversed objects. 1157 aStack.push(a); 1158 bStack.push(b); 1123 // Returns the first key on an object that passes a predicate test. 1124 _.findKey = function(obj, predicate, context) { 1125 predicate = cb(predicate, context); 1126 var keys = _.keys(obj), 1127 key; 1128 for (var i = 0, length = keys.length; i < length; i++) { 1129 key = keys[i]; 1130 if (predicate(obj[key], key, obj)) return key; 1131 } 1132 }; 1159 1133 1160 // Recursively compare objects and arrays. 1161 if (areArrays) { 1162 // Compare array lengths to determine if a deep comparison is necessary. 1163 length = a.length; 1164 if (length !== b.length) return false; 1165 // Deep compare the contents, ignoring non-numeric properties. 1166 while (length--) { 1167 if (!eq(a[length], b[length], aStack, bStack)) return false; 1168 } 1169 } else { 1170 // Deep compare objects. 1171 var keys = _.keys(a), key; 1172 length = keys.length; 1173 // Ensure that both objects contain the same number of properties before comparing deep equality. 1174 if (_.keys(b).length !== length) return false; 1175 while (length--) { 1176 // Deep compare each member 1177 key = keys[length]; 1178 if (!(_.has(b, key) && eq(a[key], b[key], aStack, bStack))) return false; 1179 } 1180 } 1181 // Remove the first object from the stack of traversed objects. 1182 aStack.pop(); 1183 bStack.pop(); 1184 return true; 1185 }; 1134 // Internal pick helper function to determine if `obj` has key `key`. 1135 var keyInObj = function(value, key, obj) { 1136 return key in obj; 1137 }; 1186 1138 1187 // Perform a deep comparison to check if two objects are equal. 1188 _.isEqual = function(a, b) { 1189 return eq(a, b); 1190 }; 1139 // Return a copy of the object only containing the whitelisted properties. 1140 _.pick = restArguments(function(obj, keys) { 1141 var result = {}, 1142 iteratee = keys[0]; 1143 if (obj == null) return result; 1144 if (_.isFunction(iteratee)) { 1145 if (keys.length > 1) iteratee = optimizeCb(iteratee, keys[1]); 1146 keys = _.allKeys(obj); 1147 } else { 1148 iteratee = keyInObj; 1149 keys = flatten(keys, false, false); 1150 obj = Object(obj); 1151 } 1152 for (var i = 0, length = keys.length; i < length; i++) { 1153 var key = keys[i]; 1154 var value = obj[key]; 1155 if (iteratee(value, key, obj)) result[key] = value; 1156 } 1157 return result; 1158 }); 1191 1159 1192 // Is a given array, string, or object empty? 1193 // An "empty" object has no enumerable own-properties. 1194 _.isEmpty = function(obj) { 1195 if (obj == null) return true; 1196 if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0; 1197 return _.keys(obj).length === 0; 1198 }; 1160 // Return a copy of the object without the blacklisted properties. 1161 _.omit = restArguments(function(obj, keys) { 1162 var iteratee = keys[0], 1163 context; 1164 if (_.isFunction(iteratee)) { 1165 iteratee = _.negate(iteratee); 1166 if (keys.length > 1) context = keys[1]; 1167 } else { 1168 keys = _.map(flatten(keys, false, false), String); 1169 iteratee = function(value, key) { 1170 return !_.contains(keys, key); 1171 }; 1172 } 1173 return _.pick(obj, iteratee, context); 1174 }); 1199 1175 1200 // Is a given value a DOM element? 1201 _.isElement = function(obj) { 1202 return !!(obj && obj.nodeType === 1); 1203 }; 1176 // Fill in a given object with default properties. 1177 _.defaults = createAssigner(_.allKeys, true); 1204 1178 1205 // Is a given value an array? 1206 // Delegates to ECMA5's native Array.isArray 1207 _.isArray = nativeIsArray || function(obj) { 1208 return toString.call(obj) === '[object Array]'; 1209 }; 1179 // Creates an object that inherits from the given prototype object. 1180 // If additional properties are provided then they will be added to the 1181 // created object. 1182 _.create = function(prototype, props) { 1183 var result = baseCreate(prototype); 1184 if (props) _.extendOwn(result, props); 1185 return result; 1186 }; 1210 1187 1211 // Is a given variable an object? 1212 _.isObject= function(obj) {1213 var type = typeofobj;1214 return type === 'function' || type === 'object' && !!obj;1215 1188 // Create a (shallow-cloned) duplicate of an object. 1189 _.clone = function(obj) { 1190 if (!_.isObject(obj)) return obj; 1191 return _.isArray(obj) ? obj.slice() : _.extend({}, obj); 1192 }; 1216 1193 1217 // Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError. 1218 _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) { 1219 _['is' + name] = function(obj) { 1220 return toString.call(obj) === '[object ' + name + ']'; 1221 }; 1222 }); 1194 // Invokes interceptor with the obj, and then returns obj. 1195 // The primary purpose of this method is to "tap into" a method chain, in 1196 // order to perform operations on intermediate results within the chain. 1197 _.tap = function(obj, interceptor) { 1198 interceptor(obj); 1199 return obj; 1200 }; 1223 1201 1224 // Define a fallback version of the method in browsers (ahem, IE < 9), where 1225 // there isn't any inspectable "Arguments" type. 1226 if (!_.isArguments(arguments)) { 1227 _.isArguments = function(obj) { 1228 return _.has(obj, 'callee'); 1229 }; 1230 } 1202 // Returns whether an object has a given set of `key:value` pairs. 1203 _.isMatch = function(object, attrs) { 1204 var keys = _.keys(attrs), 1205 length = keys.length; 1206 if (object == null) return !length; 1207 var obj = Object(object); 1208 for (var i = 0; i < length; i++) { 1209 var key = keys[i]; 1210 if (attrs[key] !== obj[key] || !(key in obj)) return false; 1211 } 1212 return true; 1213 }; 1231 1214 1232 // Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8,1233 // IE 11 (#1621), and in Safari 8 (#1929).1234 if (typeof /./ != 'function' && typeof Int8Array != 'object') {1235 _.isFunction = function(obj) {1236 return typeof obj == 'function' || false;1237 };1238 }1239 1215 1240 // Is a given object a finite number? 1241 _.isFinite = function(obj) { 1242 return isFinite(obj) && !isNaN(parseFloat(obj)); 1243 }; 1216 // Internal recursive comparison function for `isEqual`. 1217 var eq, deepEq; 1218 eq = function(a, b, aStack, bStack) { 1219 // Identical objects are equal. `0 === -0`, but they aren't identical. 1220 // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal). 1221 if (a === b) return a !== 0 || 1 / a === 1 / b; 1222 // `null` or `undefined` only equal to itself (strict comparison). 1223 if (a == null || b == null) return false; 1224 // `NaN`s are equivalent, but non-reflexive. 1225 if (a !== a) return b !== b; 1226 // Exhaust primitive checks 1227 var type = typeof a; 1228 if (type !== 'function' && type !== 'object' && typeof b != 'object') return false; 1229 return deepEq(a, b, aStack, bStack); 1230 }; 1244 1231 1245 // Is the given value `NaN`? (NaN is the only number which does not equal itself). 1246 _.isNaN = function(obj) { 1247 return _.isNumber(obj) && obj !== +obj; 1248 }; 1232 // Internal recursive comparison function for `isEqual`. 1233 deepEq = function(a, b, aStack, bStack) { 1234 // Unwrap any wrapped objects. 1235 if (a instanceof _) a = a._wrapped; 1236 if (b instanceof _) b = b._wrapped; 1237 // Compare `[[Class]]` names. 1238 var className = toString.call(a); 1239 if (className !== toString.call(b)) return false; 1240 switch (className) { 1241 // Strings, numbers, regular expressions, dates, and booleans are compared by value. 1242 case '[object RegExp]': 1243 // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i') 1244 case '[object String]': 1245 // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is 1246 // equivalent to `new String("5")`. 1247 return '' + a === '' + b; 1248 case '[object Number]': 1249 // `NaN`s are equivalent, but non-reflexive. 1250 // Object(NaN) is equivalent to NaN. 1251 if (+a !== +a) return +b !== +b; 1252 // An `egal` comparison is performed for other numeric values. 1253 return +a === 0 ? 1 / +a === 1 / b : +a === +b; 1254 case '[object Date]': 1255 case '[object Boolean]': 1256 // Coerce dates and booleans to numeric primitive values. Dates are compared by their 1257 // millisecond representations. Note that invalid dates with millisecond representations 1258 // of `NaN` are not equivalent. 1259 return +a === +b; 1260 case '[object Symbol]': 1261 return SymbolProto.valueOf.call(a) === SymbolProto.valueOf.call(b); 1262 } 1249 1263 1250 // Is a given value a boolean? 1251 _.isBoolean = function(obj) { 1252 return obj === true || obj === false || toString.call(obj) === '[object Boolean]'; 1253 }; 1264 var areArrays = className === '[object Array]'; 1265 if (!areArrays) { 1266 if (typeof a != 'object' || typeof b != 'object') return false; 1254 1267 1255 // Is a given value equal to null? 1256 _.isNull = function(obj) { 1257 return obj === null; 1258 }; 1268 // Objects with different constructors are not equivalent, but `Object`s or `Array`s 1269 // from different frames are. 1270 var aCtor = a.constructor, 1271 bCtor = b.constructor; 1272 if (aCtor !== bCtor && !(_.isFunction(aCtor) && aCtor instanceof aCtor && 1273 _.isFunction(bCtor) && bCtor instanceof bCtor) && 1274 ('constructor' in a && 'constructor' in b)) { 1275 return false; 1276 } 1277 } 1278 // Assume equality for cyclic structures. The algorithm for detecting cyclic 1279 // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`. 1259 1280 1260 // Is a given variable undefined? 1261 _.isUndefined = function(obj) { 1262 return obj === void 0; 1263 }; 1281 // Initializing stack of traversed objects. 1282 // It's done here since we only need them for objects and arrays comparison. 1283 aStack = aStack || []; 1284 bStack = bStack || []; 1285 var length = aStack.length; 1286 while (length--) { 1287 // Linear search. Performance is inversely proportional to the number of 1288 // unique nested structures. 1289 if (aStack[length] === a) return bStack[length] === b; 1290 } 1264 1291 1265 // Shortcut function for checking if an object has a given property directly 1266 // on itself (in other words, not on a prototype). 1267 _.has = function(obj, key) { 1268 return obj != null && hasOwnProperty.call(obj, key); 1269 }; 1292 // Add the first object to the stack of traversed objects. 1293 aStack.push(a); 1294 bStack.push(b); 1270 1295 1271 // Utility Functions 1272 // ----------------- 1296 // Recursively compare objects and arrays. 1297 if (areArrays) { 1298 // Compare array lengths to determine if a deep comparison is necessary. 1299 length = a.length; 1300 if (length !== b.length) return false; 1301 // Deep compare the contents, ignoring non-numeric properties. 1302 while (length--) { 1303 if (!eq(a[length], b[length], aStack, bStack)) return false; 1304 } 1305 } else { 1306 // Deep compare objects. 1307 var keys = _.keys(a), 1308 key; 1309 length = keys.length; 1310 // Ensure that both objects contain the same number of properties before comparing deep equality. 1311 if (_.keys(b).length !== length) return false; 1312 while (length--) { 1313 // Deep compare each member 1314 key = keys[length]; 1315 if (!(has(b, key) && eq(a[key], b[key], aStack, bStack))) return false; 1316 } 1317 } 1318 // Remove the first object from the stack of traversed objects. 1319 aStack.pop(); 1320 bStack.pop(); 1321 return true; 1322 }; 1273 1323 1274 // Run Underscore.js in *noConflict* mode, returning the `_` variable to its 1275 // previous owner. Returns a reference to the Underscore object. 1276 _.noConflict = function() { 1277 root._ = previousUnderscore; 1278 return this; 1279 }; 1324 // Perform a deep comparison to check if two objects are equal. 1325 _.isEqual = function(a, b) { 1326 return eq(a, b); 1327 }; 1280 1328 1281 // Keep the identity function around for default iteratees. 1282 _.identity = function(value) { 1283 return value; 1284 }; 1329 // Is a given array, string, or object empty? 1330 // An "empty" object has no enumerable own-properties. 1331 _.isEmpty = function(obj) { 1332 if (obj == null) return true; 1333 if (isArrayLike(obj) && (_.isArray(obj) || _.isString(obj) || _.isArguments(obj))) return obj.length === 0; 1334 return _.keys(obj).length === 0; 1335 }; 1285 1336 1286 // Predicate-generating functions. Often useful outside of Underscore. 1287 _.constant = function(value) { 1288 return function() { 1289 return value; 1290 }; 1291 }; 1337 // Is a given value a DOM element? 1338 _.isElement = function(obj) { 1339 return !!(obj && obj.nodeType === 1); 1340 }; 1292 1341 1293 _.noop = function(){}; 1342 // Is a given value an array? 1343 // Delegates to ECMA5's native Array.isArray 1344 _.isArray = nativeIsArray || function(obj) { 1345 return toString.call(obj) === '[object Array]'; 1346 }; 1294 1347 1295 _.property = property; 1348 // Is a given variable an object? 1349 _.isObject = function(obj) { 1350 var type = typeof obj; 1351 return type === 'function' || type === 'object' && !!obj; 1352 }; 1296 1353 1297 // Generates a function for a given object that returns a given property.1298 _.propertyOf = function(obj) {1299 return obj == null ? function(){} : function(key) {1300 return obj[key];1301 1302 };1354 // Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp, isError, isMap, isWeakMap, isSet, isWeakSet. 1355 _.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error', 'Symbol', 'Map', 'WeakMap', 'Set', 'WeakSet'], function(name) { 1356 _['is' + name] = function(obj) { 1357 return toString.call(obj) === '[object ' + name + ']'; 1358 }; 1359 }); 1303 1360 1304 // Returns a predicate for checking whether an object has a given set of 1305 // `key:value` pairs. 1306 _.matcher = _.matches = function(attrs) { 1307 attrs = _.extendOwn({}, attrs); 1308 return function(obj) { 1309 return _.isMatch(obj, attrs); 1310 }; 1311 }; 1361 // Define a fallback version of the method in browsers (ahem, IE < 9), where 1362 // there isn't any inspectable "Arguments" type. 1363 if (!_.isArguments(arguments)) { 1364 _.isArguments = function(obj) { 1365 return has(obj, 'callee'); 1366 }; 1367 } 1312 1368 1313 // Run a function **n** times. 1314 _.times = function(n, iteratee, context) { 1315 var accum = Array(Math.max(0, n)); 1316 iteratee = optimizeCb(iteratee, context, 1); 1317 for (var i = 0; i < n; i++) accum[i] = iteratee(i); 1318 return accum; 1319 }; 1369 // Optimize `isFunction` if appropriate. Work around some typeof bugs in old v8, 1370 // IE 11 (#1621), Safari 8 (#1929), and PhantomJS (#2236). 1371 var nodelist = root.document && root.document.childNodes; 1372 if (typeof /./ != 'function' && typeof Int8Array != 'object' && typeof nodelist != 'function') { 1373 _.isFunction = function(obj) { 1374 return typeof obj == 'function' || false; 1375 }; 1376 } 1320 1377 1321 // Return a random integer between min and max (inclusive). 1322 _.random = function(min, max) { 1323 if (max == null) { 1324 max = min; 1325 min = 0; 1326 } 1327 return min + Math.floor(Math.random() * (max - min + 1)); 1328 }; 1378 // Is a given object a finite number? 1379 _.isFinite = function(obj) { 1380 return !_.isSymbol(obj) && isFinite(obj) && !isNaN(parseFloat(obj)); 1381 }; 1329 1382 1330 // A (possibly faster) way to get the current timestamp as an integer. 1331 _.now = Date.now || function() {1332 return new Date().getTime();1333 1383 // Is the given value `NaN`? 1384 _.isNaN = function(obj) { 1385 return _.isNumber(obj) && isNaN(obj); 1386 }; 1334 1387 1335 // List of HTML entities for escaping. 1336 var escapeMap = { 1337 '&': '&', 1338 '<': '<', 1339 '>': '>', 1340 '"': '"', 1341 "'": ''', 1342 '`': '`' 1343 }; 1344 var unescapeMap = _.invert(escapeMap); 1388 // Is a given value a boolean? 1389 _.isBoolean = function(obj) { 1390 return obj === true || obj === false || toString.call(obj) === '[object Boolean]'; 1391 }; 1345 1392 1346 // Functions for escaping and unescaping strings to/from HTML interpolation. 1347 var createEscaper = function(map) { 1348 var escaper = function(match) { 1349 return map[match]; 1350 }; 1351 // Regexes for identifying a key that needs to be escaped 1352 var source = '(?:' + _.keys(map).join('|') + ')'; 1353 var testRegexp = RegExp(source); 1354 var replaceRegexp = RegExp(source, 'g'); 1355 return function(string) { 1356 string = string == null ? '' : '' + string; 1357 return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string; 1358 }; 1359 }; 1360 _.escape = createEscaper(escapeMap); 1361 _.unescape = createEscaper(unescapeMap); 1393 // Is a given value equal to null? 1394 _.isNull = function(obj) { 1395 return obj === null; 1396 }; 1362 1397 1363 // If the value of the named `property` is a function then invoke it with the 1364 // `object` as context; otherwise, return it. 1365 _.result = function(object, property, fallback) { 1366 var value = object == null ? void 0 : object[property]; 1367 if (value === void 0) { 1368 value = fallback; 1369 } 1370 return _.isFunction(value) ? value.call(object) : value; 1371 }; 1398 // Is a given variable undefined? 1399 _.isUndefined = function(obj) { 1400 return obj === void 0; 1401 }; 1372 1402 1373 // Generate a unique integer id (unique within the entire client session). 1374 // Useful for temporary DOM ids. 1375 var idCounter = 0; 1376 _.uniqueId = function(prefix) { 1377 var id = ++idCounter + ''; 1378 return prefix ? prefix + id : id; 1379 }; 1403 // Shortcut function for checking if an object has a given property directly 1404 // on itself (in other words, not on a prototype). 1405 _.has = function(obj, path) { 1406 if (!_.isArray(path)) { 1407 return has(obj, path); 1408 } 1409 var length = path.length; 1410 for (var i = 0; i < length; i++) { 1411 var key = path[i]; 1412 if (obj == null || !hasOwnProperty.call(obj, key)) { 1413 return false; 1414 } 1415 obj = obj[key]; 1416 } 1417 return !!length; 1418 }; 1380 1419 1381 // By default, Underscore uses ERB-style template delimiters, change the 1382 // following template settings to use alternative delimiters. 1383 _.templateSettings = { 1384 evaluate : /<%([\s\S]+?)%>/g, 1385 interpolate : /<%=([\s\S]+?)%>/g, 1386 escape : /<%-([\s\S]+?)%>/g 1387 }; 1420 // Utility Functions 1421 // ----------------- 1388 1422 1389 // When customizing `templateSettings`, if you don't want to define an 1390 // interpolation, evaluation or escaping regex, we need one that is 1391 // guaranteed not to match. 1392 var noMatch = /(.)^/; 1423 // Run Underscore.js in *noConflict* mode, returning the `_` variable to its 1424 // previous owner. Returns a reference to the Underscore object. 1425 _.noConflict = function() { 1426 root._ = previousUnderscore; 1427 return this; 1428 }; 1393 1429 1394 // Certain characters need to be escaped so that they can be put into a 1395 // string literal. 1396 var escapes = { 1397 "'": "'", 1398 '\\': '\\', 1399 '\r': 'r', 1400 '\n': 'n', 1401 '\u2028': 'u2028', 1402 '\u2029': 'u2029' 1403 }; 1430 // Keep the identity function around for default iteratees. 1431 _.identity = function(value) { 1432 return value; 1433 }; 1404 1434 1405 var escaper = /\\|'|\r|\n|\u2028|\u2029/g; 1435 // Predicate-generating functions. Often useful outside of Underscore. 1436 _.constant = function(value) { 1437 return function() { 1438 return value; 1439 }; 1440 }; 1406 1441 1407 var escapeChar = function(match) { 1408 return '\\' + escapes[match]; 1409 }; 1442 _.noop = function() {}; 1410 1443 1411 // JavaScript micro-templating, similar to John Resig's implementation. 1412 // Underscore templating handles arbitrary delimiters, preserves whitespace, 1413 // and correctly escapes quotes within interpolated code. 1414 // NB: `oldSettings` only exists for backwards compatibility. 1415 _.template = function(text, settings, oldSettings) { 1416 if (!settings && oldSettings) settings = oldSettings; 1417 settings = _.defaults({}, settings, _.templateSettings); 1444 // Creates a function that, when passed an object, will traverse that object’s 1445 // properties down the given `path`, specified as an array of keys or indexes. 1446 _.property = function(path) { 1447 if (!_.isArray(path)) { 1448 return shallowProperty(path); 1449 } 1450 return function(obj) { 1451 return deepGet(obj, path); 1452 }; 1453 }; 1418 1454 1419 // Combine delimiters into one regular expression via alternation. 1420 var matcher = RegExp([ 1421 (settings.escape || noMatch).source, 1422 (settings.interpolate || noMatch).source, 1423 (settings.evaluate || noMatch).source 1424 ].join('|') + '|$', 'g'); 1455 // Generates a function for a given object that returns a given property. 1456 _.propertyOf = function(obj) { 1457 if (obj == null) { 1458 return function() {}; 1459 } 1460 return function(path) { 1461 return !_.isArray(path) ? obj[path] : deepGet(obj, path); 1462 }; 1463 }; 1425 1464 1426 // Compile the template source, escaping string literals appropriately. 1427 var index = 0; 1428 var source = "__p+='"; 1429 text.replace(matcher, function(match, escape, interpolate, evaluate, offset) { 1430 source += text.slice(index, offset).replace(escaper, escapeChar); 1431 index = offset + match.length; 1465 // Returns a predicate for checking whether an object has a given set of 1466 // `key:value` pairs. 1467 _.matcher = _.matches = function(attrs) { 1468 attrs = _.extendOwn({}, attrs); 1469 return function(obj) { 1470 return _.isMatch(obj, attrs); 1471 }; 1472 }; 1432 1473 1433 if (escape) { 1434 source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'"; 1435 } else if (interpolate) { 1436 source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";1437 } else if (evaluate) { 1438 source += "';\n" + evaluate + "\n__p+='";1439 } 1474 // Run a function **n** times. 1475 _.times = function(n, iteratee, context) { 1476 var accum = Array(Math.max(0, n)); 1477 iteratee = optimizeCb(iteratee, context, 1); 1478 for (var i = 0; i < n; i++) accum[i] = iteratee(i); 1479 return accum; 1480 }; 1440 1481 1441 // Adobe VMs need the match returned to produce the correct offest. 1442 return match; 1443 }); 1444 source += "';\n"; 1482 // Return a random integer between min and max (inclusive). 1483 _.random = function(min, max) { 1484 if (max == null) { 1485 max = min; 1486 min = 0; 1487 } 1488 return min + Math.floor(Math.random() * (max - min + 1)); 1489 }; 1445 1490 1446 // If a variable is not specified, place data values in local scope. 1447 if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; 1491 // A (possibly faster) way to get the current timestamp as an integer. 1492 _.now = Date.now || function() { 1493 return new Date().getTime(); 1494 }; 1448 1495 1449 source = "var __t,__p='',__j=Array.prototype.join," + 1450 "print=function(){__p+=__j.call(arguments,'');};\n" + 1451 source + 'return __p;\n'; 1496 // List of HTML entities for escaping. 1497 var escapeMap = { 1498 '&': '&', 1499 '<': '<', 1500 '>': '>', 1501 '"': '"', 1502 "'": ''', 1503 '`': '`' 1504 }; 1505 var unescapeMap = _.invert(escapeMap); 1452 1506 1453 try { 1454 var render = new Function(settings.variable || 'obj', '_', source); 1455 } catch (e) { 1456 e.source = source; 1457 throw e; 1458 } 1507 // Functions for escaping and unescaping strings to/from HTML interpolation. 1508 var createEscaper = function(map) { 1509 var escaper = function(match) { 1510 return map[match]; 1511 }; 1512 // Regexes for identifying a key that needs to be escaped. 1513 var source = '(?:' + _.keys(map).join('|') + ')'; 1514 var testRegexp = RegExp(source); 1515 var replaceRegexp = RegExp(source, 'g'); 1516 return function(string) { 1517 string = string == null ? '' : '' + string; 1518 return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string; 1519 }; 1520 }; 1521 _.escape = createEscaper(escapeMap); 1522 _.unescape = createEscaper(unescapeMap); 1459 1523 1460 var template = function(data) { 1461 return render.call(this, data, _); 1462 }; 1524 // Traverses the children of `obj` along `path`. If a child is a function, it 1525 // is invoked with its parent as context. Returns the value of the final 1526 // child, or `fallback` if any child is undefined. 1527 _.result = function(obj, path, fallback) { 1528 if (!_.isArray(path)) path = [path]; 1529 var length = path.length; 1530 if (!length) { 1531 return _.isFunction(fallback) ? fallback.call(obj) : fallback; 1532 } 1533 for (var i = 0; i < length; i++) { 1534 var prop = obj == null ? void 0 : obj[path[i]]; 1535 if (prop === void 0) { 1536 prop = fallback; 1537 i = length; // Ensure we don't continue iterating. 1538 } 1539 obj = _.isFunction(prop) ? prop.call(obj) : prop; 1540 } 1541 return obj; 1542 }; 1463 1543 1464 // Provide the compiled source as a convenience for precompilation. 1465 var argument = settings.variable || 'obj'; 1466 template.source = 'function(' + argument + '){\n' + source + '}'; 1544 // Generate a unique integer id (unique within the entire client session). 1545 // Useful for temporary DOM ids. 1546 var idCounter = 0; 1547 _.uniqueId = function(prefix) { 1548 var id = ++idCounter + ''; 1549 return prefix ? prefix + id : id; 1550 }; 1467 1551 1468 return template; 1469 }; 1552 // By default, Underscore uses ERB-style template delimiters, change the 1553 // following template settings to use alternative delimiters. 1554 _.templateSettings = { 1555 evaluate: /<%([\s\S]+?)%>/g, 1556 interpolate: /<%=([\s\S]+?)%>/g, 1557 escape: /<%-([\s\S]+?)%>/g 1558 }; 1470 1559 1471 // Add a "chain" function. Start chaining a wrapped Underscore object. 1472 _.chain = function(obj) { 1473 var instance = _(obj); 1474 instance._chain = true; 1475 return instance; 1476 }; 1560 // When customizing `templateSettings`, if you don't want to define an 1561 // interpolation, evaluation or escaping regex, we need one that is 1562 // guaranteed not to match. 1563 var noMatch = /(.)^/; 1477 1564 1478 // OOP 1479 // --------------- 1480 // If Underscore is called as a function, it returns a wrapped object that 1481 // can be used OO-style. This wrapper holds altered versions of all the 1482 // underscore functions. Wrapped objects may be chained. 1565 // Certain characters need to be escaped so that they can be put into a 1566 // string literal. 1567 var escapes = { 1568 "'": "'", 1569 '\\': '\\', 1570 '\r': 'r', 1571 '\n': 'n', 1572 '\u2028': 'u2028', 1573 '\u2029': 'u2029' 1574 }; 1483 1575 1484 // Helper function to continue chaining intermediate results. 1485 var result = function(instance, obj) { 1486 return instance._chain ? _(obj).chain() : obj; 1487 }; 1576 var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g; 1488 1577 1489 // Add your own custom functions to the Underscore object. 1490 _.mixin = function(obj) { 1491 _.each(_.functions(obj), function(name) { 1492 var func = _[name] = obj[name]; 1493 _.prototype[name] = function() { 1494 var args = [this._wrapped]; 1495 push.apply(args, arguments); 1496 return result(this, func.apply(_, args)); 1497 }; 1498 }); 1499 }; 1578 var escapeChar = function(match) { 1579 return '\\' + escapes[match]; 1580 }; 1500 1581 1501 // Add all of the Underscore functions to the wrapper object. 1502 _.mixin(_); 1582 // JavaScript micro-templating, similar to John Resig's implementation. 1583 // Underscore templating handles arbitrary delimiters, preserves whitespace, 1584 // and correctly escapes quotes within interpolated code. 1585 // NB: `oldSettings` only exists for backwards compatibility. 1586 _.template = function(text, settings, oldSettings) { 1587 if (!settings && oldSettings) settings = oldSettings; 1588 settings = _.defaults({}, settings, _.templateSettings); 1503 1589 1504 // Add all mutator Array functions to the wrapper. 1505 _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) { 1506 var method = ArrayProto[name]; 1507 _.prototype[name] = function() { 1508 var obj = this._wrapped; 1509 method.apply(obj, arguments); 1510 if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0]; 1511 return result(this, obj); 1512 }; 1513 }); 1590 // Combine delimiters into one regular expression via alternation. 1591 var matcher = RegExp([ 1592 (settings.escape || noMatch).source, 1593 (settings.interpolate || noMatch).source, 1594 (settings.evaluate || noMatch).source 1595 ].join('|') + '|$', 'g'); 1514 1596 1515 // Add all accessor Array functions to the wrapper. 1516 _.each(['concat', 'join', 'slice'], function(name) { 1517 var method = ArrayProto[name]; 1518 _.prototype[name] = function() { 1519 return result(this, method.apply(this._wrapped, arguments)); 1520 }; 1521 }); 1597 // Compile the template source, escaping string literals appropriately. 1598 var index = 0; 1599 var source = "__p+='"; 1600 text.replace(matcher, function(match, escape, interpolate, evaluate, offset) { 1601 source += text.slice(index, offset).replace(escapeRegExp, escapeChar); 1602 index = offset + match.length; 1522 1603 1523 // Extracts the result from a wrapped and chained object. 1524 _.prototype.value = function() { 1525 return this._wrapped; 1526 }; 1604 if (escape) { 1605 source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'"; 1606 } else if (interpolate) { 1607 source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; 1608 } else if (evaluate) { 1609 source += "';\n" + evaluate + "\n__p+='"; 1610 } 1527 1611 1528 // Provide unwrapping proxy for some methods used in engine operations 1529 // such as arithmetic and JSON stringification. 1530 _.prototype.valueOf = _.prototype.toJSON = _.prototype.value; 1612 // Adobe VMs need the match returned to produce the correct offset. 1613 return match; 1614 }); 1615 source += "';\n"; 1531 1616 1532 _.prototype.toString = function() { 1533 return '' + this._wrapped; 1534 }; 1617 // If a variable is not specified, place data values in local scope. 1618 if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n'; 1535 1619 1536 // AMD registration happens at the end for compatibility with AMD loaders 1537 // that may not enforce next-turn semantics on modules. Even though general 1538 // practice for AMD registration is to be anonymous, underscore registers 1539 // as a named module because, like jQuery, it is a base library that is 1540 // popular enough to be bundled in a third party lib, but not be part of 1541 // an AMD load request. Those cases could generate an error when an 1542 // anonymous define() is called outside of a loader request. 1543 if (typeof define === 'function' && define.amd) { 1544 define('underscore', [], function() { 1545 return _; 1546 }); 1547 } 1548 }.call(this)); 1620 source = "var __t,__p='',__j=Array.prototype.join," + 1621 "print=function(){__p+=__j.call(arguments,'');};\n" + 1622 source + 'return __p;\n'; 1623 1624 var render; 1625 try { 1626 render = new Function(settings.variable || 'obj', '_', source); 1627 } catch (e) { 1628 e.source = source; 1629 throw e; 1630 } 1631 1632 var template = function(data) { 1633 return render.call(this, data, _); 1634 }; 1635 1636 // Provide the compiled source as a convenience for precompilation. 1637 var argument = settings.variable || 'obj'; 1638 template.source = 'function(' + argument + '){\n' + source + '}'; 1639 1640 return template; 1641 }; 1642 1643 // Add a "chain" function. Start chaining a wrapped Underscore object. 1644 _.chain = function(obj) { 1645 var instance = _(obj); 1646 instance._chain = true; 1647 return instance; 1648 }; 1649 1650 // OOP 1651 // --------------- 1652 // If Underscore is called as a function, it returns a wrapped object that 1653 // can be used OO-style. This wrapper holds altered versions of all the 1654 // underscore functions. Wrapped objects may be chained. 1655 1656 // Helper function to continue chaining intermediate results. 1657 var chainResult = function(instance, obj) { 1658 return instance._chain ? _(obj).chain() : obj; 1659 }; 1660 1661 // Add your own custom functions to the Underscore object. 1662 _.mixin = function(obj) { 1663 _.each(_.functions(obj), function(name) { 1664 var func = _[name] = obj[name]; 1665 _.prototype[name] = function() { 1666 var args = [this._wrapped]; 1667 push.apply(args, arguments); 1668 return chainResult(this, func.apply(_, args)); 1669 }; 1670 }); 1671 return _; 1672 }; 1673 1674 // Add all of the Underscore functions to the wrapper object. 1675 _.mixin(_); 1676 1677 // Add all mutator Array functions to the wrapper. 1678 _.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) { 1679 var method = ArrayProto[name]; 1680 _.prototype[name] = function() { 1681 var obj = this._wrapped; 1682 method.apply(obj, arguments); 1683 if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0]; 1684 return chainResult(this, obj); 1685 }; 1686 }); 1687 1688 // Add all accessor Array functions to the wrapper. 1689 _.each(['concat', 'join', 'slice'], function(name) { 1690 var method = ArrayProto[name]; 1691 _.prototype[name] = function() { 1692 return chainResult(this, method.apply(this._wrapped, arguments)); 1693 }; 1694 }); 1695 1696 // Extracts the result from a wrapped and chained object. 1697 _.prototype.value = function() { 1698 return this._wrapped; 1699 }; 1700 1701 // Provide unwrapping proxy for some methods used in engine operations 1702 // such as arithmetic and JSON stringification. 1703 _.prototype.valueOf = _.prototype.toJSON = _.prototype.value; 1704 1705 _.prototype.toString = function() { 1706 return String(this._wrapped); 1707 }; 1708 1709 // AMD registration happens at the end for compatibility with AMD loaders 1710 // that may not enforce next-turn semantics on modules. Even though general 1711 // practice for AMD registration is to be anonymous, underscore registers 1712 // as a named module because, like jQuery, it is a base library that is 1713 // popular enough to be bundled in a third party lib, but not be part of 1714 // an AMD load request. Those cases could generate an error when an 1715 // anonymous define() is called outside of a loader request. 1716 if (typeof define == 'function' && define.amd) { 1717 define('underscore', [], function() { 1718 return _; 1719 }); 1720 } 1721 }()); 1722 No newline at end of file -
wp-includes/js/underscore.min.js
1 (function(){function a(a){function b(b,c,d,e,f,g){for(;f>=0&&f<g;f+=a){var h=e?e[f]:f;d=c(d,b[h],h,b)}return d}return function(c,d,e,f){d=t(d,f,4);var g=!A(c)&&s.keys(c),h=(g||c).length,i=a>0?0:h-1;return arguments.length<3&&(e=c[g?g[i]:i],i+=a),b(c,d,e,g,i,h)}}function b(a){return function(b,c,d){c=u(c,d);for(var e=z(b),f=a>0?0:e-1;f>=0&&f<e;f+=a)if(c(b[f],f,b))return f;return-1}}function c(a,b,c){return function(d,e,f){var g=0,h=z(d);if("number"==typeof f)a>0?g=f>=0?f:Math.max(f+h,g):h=f>=0?Math.min(f+1,h):f+h+1;else if(c&&f&&h)return f=c(d,e),d[f]===e?f:-1;if(e!==e)return f=b(k.call(d,g,h),s.isNaN),f>=0?f+g:-1;for(f=a>0?g:h-1;f>=0&&f<h;f+=a)if(d[f]===e)return f;return-1}}function d(a,b){var c=F.length,d=a.constructor,e=s.isFunction(d)&&d.prototype||h,f="constructor";for(s.has(a,f)&&!s.contains(b,f)&&b.push(f);c--;)f=F[c],f in a&&a[f]!==e[f]&&!s.contains(b,f)&&b.push(f)}var e=this,f=e._,g=Array.prototype,h=Object.prototype,i=Function.prototype,j=g.push,k=g.slice,l=h.toString,m=h.hasOwnProperty,n=Array.isArray,o=Object.keys,p=i.bind,q=Object.create,r=function(){},s=function(a){return a instanceof s?a:this instanceof s?void(this._wrapped=a):new s(a)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=s),exports._=s):e._=s,s.VERSION="1.8.3";var t=function(a,b,c){if(void 0===b)return a;switch(null==c?3:c){case 1:return function(c){return a.call(b,c)};case 2:return function(c,d){return a.call(b,c,d)};case 3:return function(c,d,e){return a.call(b,c,d,e)};case 4:return function(c,d,e,f){return a.call(b,c,d,e,f)}}return function(){return a.apply(b,arguments)}},u=function(a,b,c){return null==a?s.identity:s.isFunction(a)?t(a,b,c):s.isObject(a)?s.matcher(a):s.property(a)};s.iteratee=function(a,b){return u(a,b,1/0)};var v=function(a,b){return function(c){var d=arguments.length;if(d<2||null==c)return c;for(var e=1;e<d;e++)for(var f=arguments[e],g=a(f),h=g.length,i=0;i<h;i++){var j=g[i];b&&void 0!==c[j]||(c[j]=f[j])}return c}},w=function(a){if(!s.isObject(a))return{};if(q)return q(a);r.prototype=a;var b=new r;return r.prototype=null,b},x=function(a){return function(b){return null==b?void 0:b[a]}},y=Math.pow(2,53)-1,z=x("length"),A=function(a){var b=z(a);return"number"==typeof b&&b>=0&&b<=y};s.each=s.forEach=function(a,b,c){b=t(b,c);var d,e;if(A(a))for(d=0,e=a.length;d<e;d++)b(a[d],d,a);else{var f=s.keys(a);for(d=0,e=f.length;d<e;d++)b(a[f[d]],f[d],a)}return a},s.map=s.collect=function(a,b,c){b=u(b,c);for(var d=!A(a)&&s.keys(a),e=(d||a).length,f=Array(e),g=0;g<e;g++){var h=d?d[g]:g;f[g]=b(a[h],h,a)}return f},s.reduce=s.foldl=s.inject=a(1),s.reduceRight=s.foldr=a(-1),s.find=s.detect=function(a,b,c){var d;if(d=A(a)?s.findIndex(a,b,c):s.findKey(a,b,c),void 0!==d&&d!==-1)return a[d]},s.filter=s.select=function(a,b,c){var d=[];return b=u(b,c),s.each(a,function(a,c,e){b(a,c,e)&&d.push(a)}),d},s.reject=function(a,b,c){return s.filter(a,s.negate(u(b)),c)},s.every=s.all=function(a,b,c){b=u(b,c);for(var d=!A(a)&&s.keys(a),e=(d||a).length,f=0;f<e;f++){var g=d?d[f]:f;if(!b(a[g],g,a))return!1}return!0},s.some=s.any=function(a,b,c){b=u(b,c);for(var d=!A(a)&&s.keys(a),e=(d||a).length,f=0;f<e;f++){var g=d?d[f]:f;if(b(a[g],g,a))return!0}return!1},s.contains=s.includes=s.include=function(a,b,c,d){return A(a)||(a=s.values(a)),("number"!=typeof c||d)&&(c=0),s.indexOf(a,b,c)>=0},s.invoke=function(a,b){var c=k.call(arguments,2),d=s.isFunction(b);return s.map(a,function(a){var e=d?b:a[b];return null==e?e:e.apply(a,c)})},s.pluck=function(a,b){return s.map(a,s.property(b))},s.where=function(a,b){return s.filter(a,s.matcher(b))},s.findWhere=function(a,b){return s.find(a,s.matcher(b))},s.max=function(a,b,c){var d,e,f=-(1/0),g=-(1/0);if(null==b&&null!=a){a=A(a)?a:s.values(a);for(var h=0,i=a.length;h<i;h++)d=a[h],d>f&&(f=d)}else b=u(b,c),s.each(a,function(a,c,d){e=b(a,c,d),(e>g||e===-(1/0)&&f===-(1/0))&&(f=a,g=e)});return f},s.min=function(a,b,c){var d,e,f=1/0,g=1/0;if(null==b&&null!=a){a=A(a)?a:s.values(a);for(var h=0,i=a.length;h<i;h++)d=a[h],d<f&&(f=d)}else b=u(b,c),s.each(a,function(a,c,d){e=b(a,c,d),(e<g||e===1/0&&f===1/0)&&(f=a,g=e)});return f},s.shuffle=function(a){for(var b,c=A(a)?a:s.values(a),d=c.length,e=Array(d),f=0;f<d;f++)b=s.random(0,f),b!==f&&(e[f]=e[b]),e[b]=c[f];return e},s.sample=function(a,b,c){return null==b||c?(A(a)||(a=s.values(a)),a[s.random(a.length-1)]):s.shuffle(a).slice(0,Math.max(0,b))},s.sortBy=function(a,b,c){return b=u(b,c),s.pluck(s.map(a,function(a,c,d){return{value:a,index:c,criteria:b(a,c,d)}}).sort(function(a,b){var c=a.criteria,d=b.criteria;if(c!==d){if(c>d||void 0===c)return 1;if(c<d||void 0===d)return-1}return a.index-b.index}),"value")};var B=function(a){return function(b,c,d){var e={};return c=u(c,d),s.each(b,function(d,f){var g=c(d,f,b);a(e,d,g)}),e}};s.groupBy=B(function(a,b,c){s.has(a,c)?a[c].push(b):a[c]=[b]}),s.indexBy=B(function(a,b,c){a[c]=b}),s.countBy=B(function(a,b,c){s.has(a,c)?a[c]++:a[c]=1}),s.toArray=function(a){return a?s.isArray(a)?k.call(a):A(a)?s.map(a,s.identity):s.values(a):[]},s.size=function(a){return null==a?0:A(a)?a.length:s.keys(a).length},s.partition=function(a,b,c){b=u(b,c);var d=[],e=[];return s.each(a,function(a,c,f){(b(a,c,f)?d:e).push(a)}),[d,e]},s.first=s.head=s.take=function(a,b,c){if(null!=a)return null==b||c?a[0]:s.initial(a,a.length-b)},s.initial=function(a,b,c){return k.call(a,0,Math.max(0,a.length-(null==b||c?1:b)))},s.last=function(a,b,c){if(null!=a)return null==b||c?a[a.length-1]:s.rest(a,Math.max(0,a.length-b))},s.rest=s.tail=s.drop=function(a,b,c){return k.call(a,null==b||c?1:b)},s.compact=function(a){return s.filter(a,s.identity)};var C=function(a,b,c,d){for(var e=[],f=0,g=d||0,h=z(a);g<h;g++){var i=a[g];if(A(i)&&(s.isArray(i)||s.isArguments(i))){b||(i=C(i,b,c));var j=0,k=i.length;for(e.length+=k;j<k;)e[f++]=i[j++]}else c||(e[f++]=i)}return e};s.flatten=function(a,b){return C(a,b,!1)},s.without=function(a){return s.difference(a,k.call(arguments,1))},s.uniq=s.unique=function(a,b,c,d){s.isBoolean(b)||(d=c,c=b,b=!1),null!=c&&(c=u(c,d));for(var e=[],f=[],g=0,h=z(a);g<h;g++){var i=a[g],j=c?c(i,g,a):i;b?(g&&f===j||e.push(i),f=j):c?s.contains(f,j)||(f.push(j),e.push(i)):s.contains(e,i)||e.push(i)}return e},s.union=function(){return s.uniq(C(arguments,!0,!0))},s.intersection=function(a){for(var b=[],c=arguments.length,d=0,e=z(a);d<e;d++){var f=a[d];if(!s.contains(b,f)){for(var g=1;g<c&&s.contains(arguments[g],f);g++);g===c&&b.push(f)}}return b},s.difference=function(a){var b=C(arguments,!0,!0,1);return s.filter(a,function(a){return!s.contains(b,a)})},s.zip=function(){return s.unzip(arguments)},s.unzip=function(a){for(var b=a&&s.max(a,z).length||0,c=Array(b),d=0;d<b;d++)c[d]=s.pluck(a,d);return c},s.object=function(a,b){for(var c={},d=0,e=z(a);d<e;d++)b?c[a[d]]=b[d]:c[a[d][0]]=a[d][1];return c},s.findIndex=b(1),s.findLastIndex=b(-1),s.sortedIndex=function(a,b,c,d){c=u(c,d,1);for(var e=c(b),f=0,g=z(a);f<g;){var h=Math.floor((f+g)/2);c(a[h])<e?f=h+1:g=h}return f},s.indexOf=c(1,s.findIndex,s.sortedIndex),s.lastIndexOf=c(-1,s.findLastIndex),s.range=function(a,b,c){null==b&&(b=a||0,a=0),c=c||1;for(var d=Math.max(Math.ceil((b-a)/c),0),e=Array(d),f=0;f<d;f++,a+=c)e[f]=a;return e};var D=function(a,b,c,d,e){if(!(d instanceof b))return a.apply(c,e);var f=w(a.prototype),g=a.apply(f,e);return s.isObject(g)?g:f};s.bind=function(a,b){if(p&&a.bind===p)return p.apply(a,k.call(arguments,1));if(!s.isFunction(a))throw new TypeError("Bind must be called on a function");var c=k.call(arguments,2),d=function(){return D(a,d,b,this,c.concat(k.call(arguments)))};return d},s.partial=function(a){var b=k.call(arguments,1),c=function(){for(var d=0,e=b.length,f=Array(e),g=0;g<e;g++)f[g]=b[g]===s?arguments[d++]:b[g];for(;d<arguments.length;)f.push(arguments[d++]);return D(a,c,this,this,f)};return c},s.bindAll=function(a){var b,c,d=arguments.length;if(d<=1)throw new Error("bindAll must be passed function names");for(b=1;b<d;b++)c=arguments[b],a[c]=s.bind(a[c],a);return a},s.memoize=function(a,b){var c=function(d){var e=c.cache,f=""+(b?b.apply(this,arguments):d);return s.has(e,f)||(e[f]=a.apply(this,arguments)),e[f]};return c.cache={},c},s.delay=function(a,b){var c=k.call(arguments,2);return setTimeout(function(){return a.apply(null,c)},b)},s.defer=s.partial(s.delay,s,1),s.throttle=function(a,b,c){var d,e,f,g=null,h=0;c||(c={});var i=function(){h=c.leading===!1?0:s.now(),g=null,f=a.apply(d,e),g||(d=e=null)};return function(){var j=s.now();h||c.leading!==!1||(h=j);var k=b-(j-h);return d=this,e=arguments,k<=0||k>b?(g&&(clearTimeout(g),g=null),h=j,f=a.apply(d,e),g||(d=e=null)):g||c.trailing===!1||(g=setTimeout(i,k)),f}},s.debounce=function(a,b,c){var d,e,f,g,h,i=function(){var j=s.now()-g;j<b&&j>=0?d=setTimeout(i,b-j):(d=null,c||(h=a.apply(f,e),d||(f=e=null)))};return function(){f=this,e=arguments,g=s.now();var j=c&&!d;return d||(d=setTimeout(i,b)),j&&(h=a.apply(f,e),f=e=null),h}},s.wrap=function(a,b){return s.partial(b,a)},s.negate=function(a){return function(){return!a.apply(this,arguments)}},s.compose=function(){var a=arguments,b=a.length-1;return function(){for(var c=b,d=a[b].apply(this,arguments);c--;)d=a[c].call(this,d);return d}},s.after=function(a,b){return function(){if(--a<1)return b.apply(this,arguments)}},s.before=function(a,b){var c;return function(){return--a>0&&(c=b.apply(this,arguments)),a<=1&&(b=null),c}},s.once=s.partial(s.before,2);var E=!{toString:null}.propertyIsEnumerable("toString"),F=["valueOf","isPrototypeOf","toString","propertyIsEnumerable","hasOwnProperty","toLocaleString"];s.keys=function(a){if(!s.isObject(a))return[];if(o)return o(a);var b=[];for(var c in a)s.has(a,c)&&b.push(c);return E&&d(a,b),b},s.allKeys=function(a){if(!s.isObject(a))return[];var b=[];for(var c in a)b.push(c);return E&&d(a,b),b},s.values=function(a){for(var b=s.keys(a),c=b.length,d=Array(c),e=0;e<c;e++)d[e]=a[b[e]];return d},s.mapObject=function(a,b,c){b=u(b,c);for(var d,e=s.keys(a),f=e.length,g={},h=0;h<f;h++)d=e[h],g[d]=b(a[d],d,a);return g},s.pairs=function(a){for(var b=s.keys(a),c=b.length,d=Array(c),e=0;e<c;e++)d[e]=[b[e],a[b[e]]];return d},s.invert=function(a){for(var b={},c=s.keys(a),d=0,e=c.length;d<e;d++)b[a[c[d]]]=c[d];return b},s.functions=s.methods=function(a){var b=[];for(var c in a)s.isFunction(a[c])&&b.push(c);return b.sort()},s.extend=v(s.allKeys),s.extendOwn=s.assign=v(s.keys),s.findKey=function(a,b,c){b=u(b,c);for(var d,e=s.keys(a),f=0,g=e.length;f<g;f++)if(d=e[f],b(a[d],d,a))return d},s.pick=function(a,b,c){var d,e,f={},g=a;if(null==g)return f;s.isFunction(b)?(e=s.allKeys(g),d=t(b,c)):(e=C(arguments,!1,!1,1),d=function(a,b,c){return b in c},g=Object(g));for(var h=0,i=e.length;h<i;h++){var j=e[h],k=g[j];d(k,j,g)&&(f[j]=k)}return f},s.omit=function(a,b,c){if(s.isFunction(b))b=s.negate(b);else{var d=s.map(C(arguments,!1,!1,1),String);b=function(a,b){return!s.contains(d,b)}}return s.pick(a,b,c)},s.defaults=v(s.allKeys,!0),s.create=function(a,b){var c=w(a);return b&&s.extendOwn(c,b),c},s.clone=function(a){return s.isObject(a)?s.isArray(a)?a.slice():s.extend({},a):a},s.tap=function(a,b){return b(a),a},s.isMatch=function(a,b){var c=s.keys(b),d=c.length;if(null==a)return!d;for(var e=Object(a),f=0;f<d;f++){var g=c[f];if(b[g]!==e[g]||!(g in e))return!1}return!0};var G=function(a,b,c,d){if(a===b)return 0!==a||1/a===1/b;if(null==a||null==b)return a===b;a instanceof s&&(a=a._wrapped),b instanceof s&&(b=b._wrapped);var e=l.call(a);if(e!==l.call(b))return!1;switch(e){case"[object RegExp]":case"[object String]":return""+a==""+b;case"[object Number]":return+a!==+a?+b!==+b:0===+a?1/+a===1/b:+a===+b;case"[object Date]":case"[object Boolean]":return+a===+b}var f="[object Array]"===e;if(!f){if("object"!=typeof a||"object"!=typeof b)return!1;var g=a.constructor,h=b.constructor;if(g!==h&&!(s.isFunction(g)&&g instanceof g&&s.isFunction(h)&&h instanceof h)&&"constructor"in a&&"constructor"in b)return!1}c=c||[],d=d||[];for(var i=c.length;i--;)if(c[i]===a)return d[i]===b;if(c.push(a),d.push(b),f){if(i=a.length,i!==b.length)return!1;for(;i--;)if(!G(a[i],b[i],c,d))return!1}else{var j,k=s.keys(a);if(i=k.length,s.keys(b).length!==i)return!1;for(;i--;)if(j=k[i],!s.has(b,j)||!G(a[j],b[j],c,d))return!1}return c.pop(),d.pop(),!0};s.isEqual=function(a,b){return G(a,b)},s.isEmpty=function(a){return null==a||(A(a)&&(s.isArray(a)||s.isString(a)||s.isArguments(a))?0===a.length:0===s.keys(a).length)},s.isElement=function(a){return!(!a||1!==a.nodeType)},s.isArray=n||function(a){return"[object Array]"===l.call(a)},s.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a},s.each(["Arguments","Function","String","Number","Date","RegExp","Error"],function(a){s["is"+a]=function(b){return l.call(b)==="[object "+a+"]"}}),s.isArguments(arguments)||(s.isArguments=function(a){return s.has(a,"callee")}),"function"!=typeof/./&&"object"!=typeof Int8Array&&(s.isFunction=function(a){return"function"==typeof a||!1}),s.isFinite=function(a){return isFinite(a)&&!isNaN(parseFloat(a))},s.isNaN=function(a){return s.isNumber(a)&&a!==+a},s.isBoolean=function(a){return a===!0||a===!1||"[object Boolean]"===l.call(a)},s.isNull=function(a){return null===a},s.isUndefined=function(a){return void 0===a},s.has=function(a,b){return null!=a&&m.call(a,b)},s.noConflict=function(){return e._=f,this},s.identity=function(a){return a},s.constant=function(a){return function(){return a}},s.noop=function(){},s.property=x,s.propertyOf=function(a){return null==a?function(){}:function(b){return a[b]}},s.matcher=s.matches=function(a){return a=s.extendOwn({},a),function(b){return s.isMatch(b,a)}},s.times=function(a,b,c){var d=Array(Math.max(0,a));b=t(b,c,1);for(var e=0;e<a;e++)d[e]=b(e);return d},s.random=function(a,b){return null==b&&(b=a,a=0),a+Math.floor(Math.random()*(b-a+1))},s.now=Date.now||function(){return(new Date).getTime()};var H={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},I=s.invert(H),J=function(a){var b=function(b){return a[b]},c="(?:"+s.keys(a).join("|")+")",d=RegExp(c),e=RegExp(c,"g");return function(a){return a=null==a?"":""+a,d.test(a)?a.replace(e,b):a}};s.escape=J(H),s.unescape=J(I),s.result=function(a,b,c){var d=null==a?void 0:a[b];return void 0===d&&(d=c),s.isFunction(d)?d.call(a):d};var K=0;s.uniqueId=function(a){var b=++K+"";return a?a+b:b},s.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var L=/(.)^/,M={"'":"'","\\":"\\","\r":"r","\n":"n","\u2028":"u2028","\u2029":"u2029"},N=/\\|'|\r|\n|\u2028|\u2029/g,O=function(a){return"\\"+M[a]};s.template=function(a,b,c){!b&&c&&(b=c),b=s.defaults({},b,s.templateSettings);var d=RegExp([(b.escape||L).source,(b.interpolate||L).source,(b.evaluate||L).source].join("|")+"|$","g"),e=0,f="__p+='";a.replace(d,function(b,c,d,g,h){return f+=a.slice(e,h).replace(N,O),e=h+b.length,c?f+="'+\n((__t=("+c+"))==null?'':_.escape(__t))+\n'":d?f+="'+\n((__t=("+d+"))==null?'':__t)+\n'":g&&(f+="';\n"+g+"\n__p+='"),b}),f+="';\n",b.variable||(f="with(obj||{}){\n"+f+"}\n"),f="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+f+"return __p;\n";try{var g=new Function(b.variable||"obj","_",f)}catch(h){throw h.source=f,h}var i=function(a){return g.call(this,a,s)},j=b.variable||"obj";return i.source="function("+j+"){\n"+f+"}",i},s.chain=function(a){var b=s(a);return b._chain=!0,b};var P=function(a,b){return a._chain?s(b).chain():b};s.mixin=function(a){s.each(s.functions(a),function(b){var c=s[b]=a[b];s.prototype[b]=function(){var a=[this._wrapped];return j.apply(a,arguments),P(this,c.apply(s,a))}})},s.mixin(s),s.each(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var b=g[a];s.prototype[a]=function(){var c=this._wrapped;return b.apply(c,arguments),"shift"!==a&&"splice"!==a||0!==c.length||delete c[0],P(this,c)}}),s.each(["concat","join","slice"],function(a){var b=g[a];s.prototype[a]=function(){return P(this,b.apply(this._wrapped,arguments))}}),s.prototype.value=function(){return this._wrapped},s.prototype.valueOf=s.prototype.toJSON=s.prototype.value,s.prototype.toString=function(){return""+this._wrapped},"function"==typeof define&&define.amd&&define("underscore",[],function(){return s})}).call(this); 2 No newline at end of file 1 // Underscore.js 1.9.1 2 // http://underscorejs.org 3 // (c) 2009-2018 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 4 // Underscore may be freely distributed under the MIT license. 5 ! function() { 6 var n = "object" == typeof self && self.self === self && self || "object" == typeof global && global.global === global && global || this || {}, 7 r = n._, 8 e = Array.prototype, 9 o = Object.prototype, 10 s = "undefined" != typeof Symbol ? Symbol.prototype : null, 11 u = e.push, 12 c = e.slice, 13 p = o.toString, 14 i = o.hasOwnProperty, 15 t = Array.isArray, 16 a = Object.keys, 17 l = Object.create, 18 f = function() {}, 19 h = function(n) { 20 return n instanceof h ? n : this instanceof h ? void(this._wrapped = n) : new h(n) 21 }; 22 "undefined" == typeof exports || exports.nodeType ? n._ = h : ("undefined" != typeof module && !module.nodeType && module.exports && (exports = module.exports = h), exports._ = h), h.VERSION = "1.9.1"; 23 var v, y = function(u, i, n) { 24 if (void 0 === i) return u; 25 switch (null == n ? 3 : n) { 26 case 1: 27 return function(n) { 28 return u.call(i, n) 29 }; 30 case 3: 31 return function(n, r, t) { 32 return u.call(i, n, r, t) 33 }; 34 case 4: 35 return function(n, r, t, e) { 36 return u.call(i, n, r, t, e) 37 } 38 } 39 return function() { 40 return u.apply(i, arguments) 41 } 42 }, 43 d = function(n, r, t) { 44 return h.iteratee !== v ? h.iteratee(n, r) : null == n ? h.identity : h.isFunction(n) ? y(n, r, t) : h.isObject(n) && !h.isArray(n) ? h.matcher(n) : h.property(n) 45 }; 46 h.iteratee = v = function(n, r) { 47 return d(n, r, 1 / 0) 48 }; 49 var g = function(u, i) { 50 return i = null == i ? u.length - 1 : +i, 51 function() { 52 for (var n = Math.max(arguments.length - i, 0), r = Array(n), t = 0; t < n; t++) r[t] = arguments[t + i]; 53 switch (i) { 54 case 0: 55 return u.call(this, r); 56 case 1: 57 return u.call(this, arguments[0], r); 58 case 2: 59 return u.call(this, arguments[0], arguments[1], r) 60 } 61 var e = Array(i + 1); 62 for (t = 0; t < i; t++) e[t] = arguments[t]; 63 return e[i] = r, u.apply(this, e) 64 } 65 }, 66 m = function(n) { 67 if (!h.isObject(n)) return {}; 68 if (l) return l(n); 69 f.prototype = n; 70 var r = new f; 71 return f.prototype = null, r 72 }, 73 b = function(r) { 74 return function(n) { 75 return null == n ? void 0 : n[r] 76 } 77 }, 78 j = function(n, r) { 79 return null != n && i.call(n, r) 80 }, 81 x = function(n, r) { 82 for (var t = r.length, e = 0; e < t; e++) { 83 if (null == n) return; 84 n = n[r[e]] 85 } 86 return t ? n : void 0 87 }, 88 _ = Math.pow(2, 53) - 1, 89 A = b("length"), 90 w = function(n) { 91 var r = A(n); 92 return "number" == typeof r && 0 <= r && r <= _ 93 }; 94 h.each = h.forEach = function(n, r, t) { 95 var e, u; 96 if (r = y(r, t), w(n)) 97 for (e = 0, u = n.length; e < u; e++) r(n[e], e, n); 98 else { 99 var i = h.keys(n); 100 for (e = 0, u = i.length; e < u; e++) r(n[i[e]], i[e], n) 101 } 102 return n 103 }, h.map = h.collect = function(n, r, t) { 104 r = d(r, t); 105 for (var e = !w(n) && h.keys(n), u = (e || n).length, i = Array(u), o = 0; o < u; o++) { 106 var a = e ? e[o] : o; 107 i[o] = r(n[a], a, n) 108 } 109 return i 110 }; 111 var O = function(c) { 112 return function(n, r, t, e) { 113 var u = 3 <= arguments.length; 114 return function(n, r, t, e) { 115 var u = !w(n) && h.keys(n), 116 i = (u || n).length, 117 o = 0 < c ? 0 : i - 1; 118 for (e || (t = n[u ? u[o] : o], o += c); 0 <= o && o < i; o += c) { 119 var a = u ? u[o] : o; 120 t = r(t, n[a], a, n) 121 } 122 return t 123 }(n, y(r, e, 4), t, u) 124 } 125 }; 126 h.reduce = h.foldl = h.inject = O(1), h.reduceRight = h.foldr = O(-1), h.find = h.detect = function(n, r, t) { 127 var e = (w(n) ? h.findIndex : h.findKey)(n, r, t); 128 if (void 0 !== e && -1 !== e) return n[e] 129 }, h.filter = h.select = function(n, e, r) { 130 var u = []; 131 return e = d(e, r), h.each(n, function(n, r, t) { 132 e(n, r, t) && u.push(n) 133 }), u 134 }, h.reject = function(n, r, t) { 135 return h.filter(n, h.negate(d(r)), t) 136 }, h.every = h.all = function(n, r, t) { 137 r = d(r, t); 138 for (var e = !w(n) && h.keys(n), u = (e || n).length, i = 0; i < u; i++) { 139 var o = e ? e[i] : i; 140 if (!r(n[o], o, n)) return !1 141 } 142 return !0 143 }, h.some = h.any = function(n, r, t) { 144 r = d(r, t); 145 for (var e = !w(n) && h.keys(n), u = (e || n).length, i = 0; i < u; i++) { 146 var o = e ? e[i] : i; 147 if (r(n[o], o, n)) return !0 148 } 149 return !1 150 }, h.contains = h.includes = h.include = function(n, r, t, e) { 151 return w(n) || (n = h.values(n)), ("number" != typeof t || e) && (t = 0), 0 <= h.indexOf(n, r, t) 152 }, h.invoke = g(function(n, t, e) { 153 var u, i; 154 return h.isFunction(t) ? i = t : h.isArray(t) && (u = t.slice(0, -1), t = t[t.length - 1]), h.map(n, function(n) { 155 var r = i; 156 if (!r) { 157 if (u && u.length && (n = x(n, u)), null == n) return; 158 r = n[t] 159 } 160 return null == r ? r : r.apply(n, e) 161 }) 162 }), h.pluck = function(n, r) { 163 return h.map(n, h.property(r)) 164 }, h.where = function(n, r) { 165 return h.filter(n, h.matcher(r)) 166 }, h.findWhere = function(n, r) { 167 return h.find(n, h.matcher(r)) 168 }, h.max = function(n, e, r) { 169 var t, u, i = -1 / 0, 170 o = -1 / 0; 171 if (null == e || "number" == typeof e && "object" != typeof n[0] && null != n) 172 for (var a = 0, c = (n = w(n) ? n : h.values(n)).length; a < c; a++) null != (t = n[a]) && i < t && (i = t); 173 else e = d(e, r), h.each(n, function(n, r, t) { 174 u = e(n, r, t), (o < u || u === -1 / 0 && i === -1 / 0) && (i = n, o = u) 175 }); 176 return i 177 }, h.min = function(n, e, r) { 178 var t, u, i = 1 / 0, 179 o = 1 / 0; 180 if (null == e || "number" == typeof e && "object" != typeof n[0] && null != n) 181 for (var a = 0, c = (n = w(n) ? n : h.values(n)).length; a < c; a++) null != (t = n[a]) && t < i && (i = t); 182 else e = d(e, r), h.each(n, function(n, r, t) { 183 ((u = e(n, r, t)) < o || u === 1 / 0 && i === 1 / 0) && (i = n, o = u) 184 }); 185 return i 186 }, h.shuffle = function(n) { 187 return h.sample(n, 1 / 0) 188 }, h.sample = function(n, r, t) { 189 if (null == r || t) return w(n) || (n = h.values(n)), n[h.random(n.length - 1)]; 190 var e = w(n) ? h.clone(n) : h.values(n), 191 u = A(e); 192 r = Math.max(Math.min(r, u), 0); 193 for (var i = u - 1, o = 0; o < r; o++) { 194 var a = h.random(o, i), 195 c = e[o]; 196 e[o] = e[a], e[a] = c 197 } 198 return e.slice(0, r) 199 }, h.sortBy = function(n, e, r) { 200 var u = 0; 201 return e = d(e, r), h.pluck(h.map(n, function(n, r, t) { 202 return { 203 value: n, 204 index: u++, 205 criteria: e(n, r, t) 206 } 207 }).sort(function(n, r) { 208 var t = n.criteria, 209 e = r.criteria; 210 if (t !== e) { 211 if (e < t || void 0 === t) return 1; 212 if (t < e || void 0 === e) return -1 213 } 214 return n.index - r.index 215 }), "value") 216 }; 217 var k = function(o, r) { 218 return function(e, u, n) { 219 var i = r ? [ 220 [], 221 [] 222 ] : {}; 223 return u = d(u, n), h.each(e, function(n, r) { 224 var t = u(n, r, e); 225 o(i, n, t) 226 }), i 227 } 228 }; 229 h.groupBy = k(function(n, r, t) { 230 j(n, t) ? n[t].push(r) : n[t] = [r] 231 }), h.indexBy = k(function(n, r, t) { 232 n[t] = r 233 }), h.countBy = k(function(n, r, t) { 234 j(n, t) ? n[t]++ : n[t] = 1 235 }); 236 var S = /[^\ud800-\udfff]|[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]/g; 237 h.toArray = function(n) { 238 return n ? h.isArray(n) ? c.call(n) : h.isString(n) ? n.match(S) : w(n) ? h.map(n, h.identity) : h.values(n) : [] 239 }, h.size = function(n) { 240 return null == n ? 0 : w(n) ? n.length : h.keys(n).length 241 }, h.partition = k(function(n, r, t) { 242 n[t ? 0 : 1].push(r) 243 }, !0), h.first = h.head = h.take = function(n, r, t) { 244 return null == n || n.length < 1 ? null == r ? void 0 : [] : null == r || t ? n[0] : h.initial(n, n.length - r) 245 }, h.initial = function(n, r, t) { 246 return c.call(n, 0, Math.max(0, n.length - (null == r || t ? 1 : r))) 247 }, h.last = function(n, r, t) { 248 return null == n || n.length < 1 ? null == r ? void 0 : [] : null == r || t ? n[n.length - 1] : h.rest(n, Math.max(0, n.length - r)) 249 }, h.rest = h.tail = h.drop = function(n, r, t) { 250 return c.call(n, null == r || t ? 1 : r) 251 }, h.compact = function(n) { 252 return h.filter(n, Boolean) 253 }; 254 var M = function(n, r, t, e) { 255 for (var u = (e = e || []).length, i = 0, o = A(n); i < o; i++) { 256 var a = n[i]; 257 if (w(a) && (h.isArray(a) || h.isArguments(a))) 258 if (r) 259 for (var c = 0, l = a.length; c < l;) e[u++] = a[c++]; 260 else M(a, r, t, e), u = e.length; 261 else t || (e[u++] = a) 262 } 263 return e 264 }; 265 h.flatten = function(n, r) { 266 return M(n, r, !1) 267 }, h.without = g(function(n, r) { 268 return h.difference(n, r) 269 }), h.uniq = h.unique = function(n, r, t, e) { 270 h.isBoolean(r) || (e = t, t = r, r = !1), null != t && (t = d(t, e)); 271 for (var u = [], i = [], o = 0, a = A(n); o < a; o++) { 272 var c = n[o], 273 l = t ? t(c, o, n) : c; 274 r && !t ? (o && i === l || u.push(c), i = l) : t ? h.contains(i, l) || (i.push(l), u.push(c)) : h.contains(u, c) || u.push(c) 275 } 276 return u 277 }, h.union = g(function(n) { 278 return h.uniq(M(n, !0, !0)) 279 }), h.intersection = function(n) { 280 for (var r = [], t = arguments.length, e = 0, u = A(n); e < u; e++) { 281 var i = n[e]; 282 if (!h.contains(r, i)) { 283 var o; 284 for (o = 1; o < t && h.contains(arguments[o], i); o++); 285 o === t && r.push(i) 286 } 287 } 288 return r 289 }, h.difference = g(function(n, r) { 290 return r = M(r, !0, !0), h.filter(n, function(n) { 291 return !h.contains(r, n) 292 }) 293 }), h.unzip = function(n) { 294 for (var r = n && h.max(n, A).length || 0, t = Array(r), e = 0; e < r; e++) t[e] = h.pluck(n, e); 295 return t 296 }, h.zip = g(h.unzip), h.object = function(n, r) { 297 for (var t = {}, e = 0, u = A(n); e < u; e++) r ? t[n[e]] = r[e] : t[n[e][0]] = n[e][1]; 298 return t 299 }; 300 var F = function(i) { 301 return function(n, r, t) { 302 r = d(r, t); 303 for (var e = A(n), u = 0 < i ? 0 : e - 1; 0 <= u && u < e; u += i) 304 if (r(n[u], u, n)) return u; 305 return -1 306 } 307 }; 308 h.findIndex = F(1), h.findLastIndex = F(-1), h.sortedIndex = function(n, r, t, e) { 309 for (var u = (t = d(t, e, 1))(r), i = 0, o = A(n); i < o;) { 310 var a = Math.floor((i + o) / 2); 311 t(n[a]) < u ? i = a + 1 : o = a 312 } 313 return i 314 }; 315 var E = function(i, o, a) { 316 return function(n, r, t) { 317 var e = 0, 318 u = A(n); 319 if ("number" == typeof t) 0 < i ? e = 0 <= t ? t : Math.max(t + u, e) : u = 0 <= t ? Math.min(t + 1, u) : t + u + 1; 320 else if (a && t && u) return n[t = a(n, r)] === r ? t : -1; 321 if (r != r) return 0 <= (t = o(c.call(n, e, u), h.isNaN)) ? t + e : -1; 322 for (t = 0 < i ? e : u - 1; 0 <= t && t < u; t += i) 323 if (n[t] === r) return t; 324 return -1 325 } 326 }; 327 h.indexOf = E(1, h.findIndex, h.sortedIndex), h.lastIndexOf = E(-1, h.findLastIndex), h.range = function(n, r, t) { 328 null == r && (r = n || 0, n = 0), t || (t = r < n ? -1 : 1); 329 for (var e = Math.max(Math.ceil((r - n) / t), 0), u = Array(e), i = 0; i < e; i++, n += t) u[i] = n; 330 return u 331 }, h.chunk = function(n, r) { 332 if (null == r || r < 1) return []; 333 for (var t = [], e = 0, u = n.length; e < u;) t.push(c.call(n, e, e += r)); 334 return t 335 }; 336 var N = function(n, r, t, e, u) { 337 if (!(e instanceof r)) return n.apply(t, u); 338 var i = m(n.prototype), 339 o = n.apply(i, u); 340 return h.isObject(o) ? o : i 341 }; 342 h.bind = g(function(r, t, e) { 343 if (!h.isFunction(r)) throw new TypeError("Bind must be called on a function"); 344 var u = g(function(n) { 345 return N(r, u, t, this, e.concat(n)) 346 }); 347 return u 348 }), h.partial = g(function(u, i) { 349 var o = h.partial.placeholder, 350 a = function() { 351 for (var n = 0, r = i.length, t = Array(r), e = 0; e < r; e++) t[e] = i[e] === o ? arguments[n++] : i[e]; 352 for (; n < arguments.length;) t.push(arguments[n++]); 353 return N(u, a, this, this, t) 354 }; 355 return a 356 }), (h.partial.placeholder = h).bindAll = g(function(n, r) { 357 var t = (r = M(r, !1, !1)).length; 358 if (t < 1) throw new Error("bindAll must be passed function names"); 359 for (; t--;) { 360 var e = r[t]; 361 n[e] = h.bind(n[e], n) 362 } 363 }), h.memoize = function(e, u) { 364 var i = function(n) { 365 var r = i.cache, 366 t = "" + (u ? u.apply(this, arguments) : n); 367 return j(r, t) || (r[t] = e.apply(this, arguments)), r[t] 368 }; 369 return i.cache = {}, i 370 }, h.delay = g(function(n, r, t) { 371 return setTimeout(function() { 372 return n.apply(null, t) 373 }, r) 374 }), h.defer = h.partial(h.delay, h, 1), h.throttle = function(t, e, u) { 375 var i, o, a, c, l = 0; 376 u || (u = {}); 377 var f = function() { 378 l = !1 === u.leading ? 0 : h.now(), i = null, c = t.apply(o, a), i || (o = a = null) 379 }, 380 n = function() { 381 var n = h.now(); 382 l || !1 !== u.leading || (l = n); 383 var r = e - (n - l); 384 return o = this, a = arguments, r <= 0 || e < r ? (i && (clearTimeout(i), i = null), l = n, c = t.apply(o, a), i || (o = a = null)) : i || !1 === u.trailing || (i = setTimeout(f, r)), c 385 }; 386 return n.cancel = function() { 387 clearTimeout(i), l = 0, i = o = a = null 388 }, n 389 }, h.debounce = function(t, e, u) { 390 var i, o, a = function(n, r) { 391 i = null, r && (o = t.apply(n, r)) 392 }, 393 n = g(function(n) { 394 if (i && clearTimeout(i), u) { 395 var r = !i; 396 i = setTimeout(a, e), r && (o = t.apply(this, n)) 397 } else i = h.delay(a, e, this, n); 398 return o 399 }); 400 return n.cancel = function() { 401 clearTimeout(i), i = null 402 }, n 403 }, h.wrap = function(n, r) { 404 return h.partial(r, n) 405 }, h.negate = function(n) { 406 return function() { 407 return !n.apply(this, arguments) 408 } 409 }, h.compose = function() { 410 var t = arguments, 411 e = t.length - 1; 412 return function() { 413 for (var n = e, r = t[e].apply(this, arguments); n--;) r = t[n].call(this, r); 414 return r 415 } 416 }, h.after = function(n, r) { 417 return function() { 418 if (--n < 1) return r.apply(this, arguments) 419 } 420 }, h.before = function(n, r) { 421 var t; 422 return function() { 423 return 0 < --n && (t = r.apply(this, arguments)), n <= 1 && (r = null), t 424 } 425 }, h.once = h.partial(h.before, 2), h.restArguments = g; 426 var I = !{ 427 toString: null 428 }.propertyIsEnumerable("toString"), 429 T = ["valueOf", "isPrototypeOf", "toString", "propertyIsEnumerable", "hasOwnProperty", "toLocaleString"], 430 B = function(n, r) { 431 var t = T.length, 432 e = n.constructor, 433 u = h.isFunction(e) && e.prototype || o, 434 i = "constructor"; 435 for (j(n, i) && !h.contains(r, i) && r.push(i); t--;)(i = T[t]) in n && n[i] !== u[i] && !h.contains(r, i) && r.push(i) 436 }; 437 h.keys = function(n) { 438 if (!h.isObject(n)) return []; 439 if (a) return a(n); 440 var r = []; 441 for (var t in n) j(n, t) && r.push(t); 442 return I && B(n, r), r 443 }, h.allKeys = function(n) { 444 if (!h.isObject(n)) return []; 445 var r = []; 446 for (var t in n) r.push(t); 447 return I && B(n, r), r 448 }, h.values = function(n) { 449 for (var r = h.keys(n), t = r.length, e = Array(t), u = 0; u < t; u++) e[u] = n[r[u]]; 450 return e 451 }, h.mapObject = function(n, r, t) { 452 r = d(r, t); 453 for (var e = h.keys(n), u = e.length, i = {}, o = 0; o < u; o++) { 454 var a = e[o]; 455 i[a] = r(n[a], a, n) 456 } 457 return i 458 }, h.pairs = function(n) { 459 for (var r = h.keys(n), t = r.length, e = Array(t), u = 0; u < t; u++) e[u] = [r[u], n[r[u]]]; 460 return e 461 }, h.invert = function(n) { 462 for (var r = {}, t = h.keys(n), e = 0, u = t.length; e < u; e++) r[n[t[e]]] = t[e]; 463 return r 464 }, h.functions = h.methods = function(n) { 465 var r = []; 466 for (var t in n) h.isFunction(n[t]) && r.push(t); 467 return r.sort() 468 }; 469 var R = function(c, l) { 470 return function(n) { 471 var r = arguments.length; 472 if (l && (n = Object(n)), r < 2 || null == n) return n; 473 for (var t = 1; t < r; t++) 474 for (var e = arguments[t], u = c(e), i = u.length, o = 0; o < i; o++) { 475 var a = u[o]; 476 l && void 0 !== n[a] || (n[a] = e[a]) 477 } 478 return n 479 } 480 }; 481 h.extend = R(h.allKeys), h.extendOwn = h.assign = R(h.keys), h.findKey = function(n, r, t) { 482 r = d(r, t); 483 for (var e, u = h.keys(n), i = 0, o = u.length; i < o; i++) 484 if (r(n[e = u[i]], e, n)) return e 485 }; 486 var q, K, z = function(n, r, t) { 487 return r in t 488 }; 489 h.pick = g(function(n, r) { 490 var t = {}, 491 e = r[0]; 492 if (null == n) return t; 493 h.isFunction(e) ? (1 < r.length && (e = y(e, r[1])), r = h.allKeys(n)) : (e = z, r = M(r, !1, !1), n = Object(n)); 494 for (var u = 0, i = r.length; u < i; u++) { 495 var o = r[u], 496 a = n[o]; 497 e(a, o, n) && (t[o] = a) 498 } 499 return t 500 }), h.omit = g(function(n, t) { 501 var r, e = t[0]; 502 return h.isFunction(e) ? (e = h.negate(e), 1 < t.length && (r = t[1])) : (t = h.map(M(t, !1, !1), String), e = function(n, r) { 503 return !h.contains(t, r) 504 }), h.pick(n, e, r) 505 }), h.defaults = R(h.allKeys, !0), h.create = function(n, r) { 506 var t = m(n); 507 return r && h.extendOwn(t, r), t 508 }, h.clone = function(n) { 509 return h.isObject(n) ? h.isArray(n) ? n.slice() : h.extend({}, n) : n 510 }, h.tap = function(n, r) { 511 return r(n), n 512 }, h.isMatch = function(n, r) { 513 var t = h.keys(r), 514 e = t.length; 515 if (null == n) return !e; 516 for (var u = Object(n), i = 0; i < e; i++) { 517 var o = t[i]; 518 if (r[o] !== u[o] || !(o in u)) return !1 519 } 520 return !0 521 }, q = function(n, r, t, e) { 522 if (n === r) return 0 !== n || 1 / n == 1 / r; 523 if (null == n || null == r) return !1; 524 if (n != n) return r != r; 525 var u = typeof n; 526 return ("function" === u || "object" === u || "object" == typeof r) && K(n, r, t, e) 527 }, K = function(n, r, t, e) { 528 n instanceof h && (n = n._wrapped), r instanceof h && (r = r._wrapped); 529 var u = p.call(n); 530 if (u !== p.call(r)) return !1; 531 switch (u) { 532 case "[object RegExp]": 533 case "[object String]": 534 return "" + n == "" + r; 535 case "[object Number]": 536 return +n != +n ? +r != +r : 0 == +n ? 1 / +n == 1 / r : +n == +r; 537 case "[object Date]": 538 case "[object Boolean]": 539 return +n == +r; 540 case "[object Symbol]": 541 return s.valueOf.call(n) === s.valueOf.call(r) 542 } 543 var i = "[object Array]" === u; 544 if (!i) { 545 if ("object" != typeof n || "object" != typeof r) return !1; 546 var o = n.constructor, 547 a = r.constructor; 548 if (o !== a && !(h.isFunction(o) && o instanceof o && h.isFunction(a) && a instanceof a) && "constructor" in n && "constructor" in r) return !1 549 } 550 e = e || []; 551 for (var c = (t = t || []).length; c--;) 552 if (t[c] === n) return e[c] === r; 553 if (t.push(n), e.push(r), i) { 554 if ((c = n.length) !== r.length) return !1; 555 for (; c--;) 556 if (!q(n[c], r[c], t, e)) return !1 557 } else { 558 var l, f = h.keys(n); 559 if (c = f.length, h.keys(r).length !== c) return !1; 560 for (; c--;) 561 if (l = f[c], !j(r, l) || !q(n[l], r[l], t, e)) return !1 562 } 563 return t.pop(), e.pop(), !0 564 }, h.isEqual = function(n, r) { 565 return q(n, r) 566 }, h.isEmpty = function(n) { 567 return null == n || (w(n) && (h.isArray(n) || h.isString(n) || h.isArguments(n)) ? 0 === n.length : 0 === h.keys(n).length) 568 }, h.isElement = function(n) { 569 return !(!n || 1 !== n.nodeType) 570 }, h.isArray = t || function(n) { 571 return "[object Array]" === p.call(n) 572 }, h.isObject = function(n) { 573 var r = typeof n; 574 return "function" === r || "object" === r && !!n 575 }, h.each(["Arguments", "Function", "String", "Number", "Date", "RegExp", "Error", "Symbol", "Map", "WeakMap", "Set", "WeakSet"], function(r) { 576 h["is" + r] = function(n) { 577 return p.call(n) === "[object " + r + "]" 578 } 579 }), h.isArguments(arguments) || (h.isArguments = function(n) { 580 return j(n, "callee") 581 }); 582 var D = n.document && n.document.childNodes; 583 "function" != typeof /./ && "object" != typeof Int8Array && "function" != typeof D && (h.isFunction = function(n) { 584 return "function" == typeof n || !1 585 }), h.isFinite = function(n) { 586 return !h.isSymbol(n) && isFinite(n) && !isNaN(parseFloat(n)) 587 }, h.isNaN = function(n) { 588 return h.isNumber(n) && isNaN(n) 589 }, h.isBoolean = function(n) { 590 return !0 === n || !1 === n || "[object Boolean]" === p.call(n) 591 }, h.isNull = function(n) { 592 return null === n 593 }, h.isUndefined = function(n) { 594 return void 0 === n 595 }, h.has = function(n, r) { 596 if (!h.isArray(r)) return j(n, r); 597 for (var t = r.length, e = 0; e < t; e++) { 598 var u = r[e]; 599 if (null == n || !i.call(n, u)) return !1; 600 n = n[u] 601 } 602 return !!t 603 }, h.noConflict = function() { 604 return n._ = r, this 605 }, h.identity = function(n) { 606 return n 607 }, h.constant = function(n) { 608 return function() { 609 return n 610 } 611 }, h.noop = function() {}, h.property = function(r) { 612 return h.isArray(r) ? function(n) { 613 return x(n, r) 614 } : b(r) 615 }, h.propertyOf = function(r) { 616 return null == r ? function() {} : function(n) { 617 return h.isArray(n) ? x(r, n) : r[n] 618 } 619 }, h.matcher = h.matches = function(r) { 620 return r = h.extendOwn({}, r), 621 function(n) { 622 return h.isMatch(n, r) 623 } 624 }, h.times = function(n, r, t) { 625 var e = Array(Math.max(0, n)); 626 r = y(r, t, 1); 627 for (var u = 0; u < n; u++) e[u] = r(u); 628 return e 629 }, h.random = function(n, r) { 630 return null == r && (r = n, n = 0), n + Math.floor(Math.random() * (r - n + 1)) 631 }, h.now = Date.now || function() { 632 return (new Date).getTime() 633 }; 634 var L = { 635 "&": "&", 636 "<": "<", 637 ">": ">", 638 '"': """, 639 "'": "'", 640 "`": "`" 641 }, 642 P = h.invert(L), 643 W = function(r) { 644 var t = function(n) { 645 return r[n] 646 }, 647 n = "(?:" + h.keys(r).join("|") + ")", 648 e = RegExp(n), 649 u = RegExp(n, "g"); 650 return function(n) { 651 return n = null == n ? "" : "" + n, e.test(n) ? n.replace(u, t) : n 652 } 653 }; 654 h.escape = W(L), h.unescape = W(P), h.result = function(n, r, t) { 655 h.isArray(r) || (r = [r]); 656 var e = r.length; 657 if (!e) return h.isFunction(t) ? t.call(n) : t; 658 for (var u = 0; u < e; u++) { 659 var i = null == n ? void 0 : n[r[u]]; 660 void 0 === i && (i = t, u = e), n = h.isFunction(i) ? i.call(n) : i 661 } 662 return n 663 }; 664 var C = 0; 665 h.uniqueId = function(n) { 666 var r = ++C + ""; 667 return n ? n + r : r 668 }, h.templateSettings = { 669 evaluate: /<%([\s\S]+?)%>/g, 670 interpolate: /<%=([\s\S]+?)%>/g, 671 escape: /<%-([\s\S]+?)%>/g 672 }; 673 var J = /(.)^/, 674 U = { 675 "'": "'", 676 "\\": "\\", 677 "\r": "r", 678 "\n": "n", 679 "\u2028": "u2028", 680 "\u2029": "u2029" 681 }, 682 V = /\\|'|\r|\n|\u2028|\u2029/g, 683 $ = function(n) { 684 return "\\" + U[n] 685 }; 686 h.template = function(i, n, r) { 687 !n && r && (n = r), n = h.defaults({}, n, h.templateSettings); 688 var t, e = RegExp([(n.escape || J).source, (n.interpolate || J).source, (n.evaluate || J).source].join("|") + "|$", "g"), 689 o = 0, 690 a = "__p+='"; 691 i.replace(e, function(n, r, t, e, u) { 692 return a += i.slice(o, u).replace(V, $), o = u + n.length, r ? a += "'+\n((__t=(" + r + "))==null?'':_.escape(__t))+\n'" : t ? a += "'+\n((__t=(" + t + "))==null?'':__t)+\n'" : e && (a += "';\n" + e + "\n__p+='"), n 693 }), a += "';\n", n.variable || (a = "with(obj||{}){\n" + a + "}\n"), a = "var __t,__p='',__j=Array.prototype.join," + "print=function(){__p+=__j.call(arguments,'');};\n" + a + "return __p;\n"; 694 try { 695 t = new Function(n.variable || "obj", "_", a) 696 } catch (n) { 697 throw n.source = a, n 698 } 699 var u = function(n) { 700 return t.call(this, n, h) 701 }, 702 c = n.variable || "obj"; 703 return u.source = "function(" + c + "){\n" + a + "}", u 704 }, h.chain = function(n) { 705 var r = h(n); 706 return r._chain = !0, r 707 }; 708 var G = function(n, r) { 709 return n._chain ? h(r).chain() : r 710 }; 711 h.mixin = function(t) { 712 return h.each(h.functions(t), function(n) { 713 var r = h[n] = t[n]; 714 h.prototype[n] = function() { 715 var n = [this._wrapped]; 716 return u.apply(n, arguments), G(this, r.apply(h, n)) 717 } 718 }), h 719 }, h.mixin(h), h.each(["pop", "push", "reverse", "shift", "sort", "splice", "unshift"], function(r) { 720 var t = e[r]; 721 h.prototype[r] = function() { 722 var n = this._wrapped; 723 return t.apply(n, arguments), "shift" !== r && "splice" !== r || 0 !== n.length || delete n[0], G(this, n) 724 } 725 }), h.each(["concat", "join", "slice"], function(n) { 726 var r = e[n]; 727 h.prototype[n] = function() { 728 return G(this, r.apply(this._wrapped, arguments)) 729 } 730 }), h.prototype.value = function() { 731 return this._wrapped 732 }, h.prototype.valueOf = h.prototype.toJSON = h.prototype.value, h.prototype.toString = function() { 733 return String(this._wrapped) 734 }, "function" == typeof define && define.amd && define("underscore", [], function() { 735 return h 736 }) 737 }(); 738 No newline at end of file -
wp-includes/script-loader.php
1162 1162 $scripts->add( 'json2', "/wp-includes/js/json2$suffix.js", array(), '2015-05-03' ); 1163 1163 did_action( 'init' ) && $scripts->add_data( 'json2', 'conditional', 'lt IE 8' ); 1164 1164 1165 $scripts->add( 'underscore', "/wp-includes/js/underscore$dev_suffix.js", array(), '1. 8.3', 1 );1165 $scripts->add( 'underscore', "/wp-includes/js/underscore$dev_suffix.js", array(), '1.9.1', 1 ); 1166 1166 $scripts->add( 'backbone', "/wp-includes/js/backbone$dev_suffix.js", array( 'underscore', 'jquery' ), '1.2.3', 1 ); 1167 1167 1168 1168 $scripts->add( 'wp-util', "/wp-includes/js/wp-util$suffix.js", array( 'underscore', 'jquery' ), false, 1 );