6ca5bddc |
1 | /** |
2 | * @license |
3 | * Lo-Dash 2.4.1 <http://lodash.com/> |
4 | * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/> |
5 | * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE> |
6 | * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors |
7 | * Available under MIT license <http://lodash.com/license> |
8 | */ |
9 | ;(function() { |
10 | |
11 | /** Used as a safe reference for `undefined` in pre ES5 environments */ |
12 | var undefined; |
13 | |
14 | /** Used to pool arrays and objects used internally */ |
15 | var arrayPool = [], |
16 | objectPool = []; |
17 | |
18 | /** Used to generate unique IDs */ |
19 | var idCounter = 0; |
20 | |
21 | /** Used internally to indicate various things */ |
22 | var indicatorObject = {}; |
23 | |
24 | /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */ |
25 | var keyPrefix = +new Date + ''; |
26 | |
27 | /** Used as the size when optimizations are enabled for large arrays */ |
28 | var largeArraySize = 75; |
29 | |
30 | /** Used as the max size of the `arrayPool` and `objectPool` */ |
31 | var maxPoolSize = 40; |
32 | |
33 | /** Used to detect and test whitespace */ |
34 | var whitespace = ( |
35 | // whitespace |
36 | ' \t\x0B\f\xA0\ufeff' + |
37 | |
38 | // line terminators |
39 | '\n\r\u2028\u2029' + |
40 | |
41 | // unicode category "Zs" space separators |
42 | '\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000' |
43 | ); |
44 | |
45 | /** Used to match empty string literals in compiled template source */ |
46 | var reEmptyStringLeading = /\b__p \+= '';/g, |
47 | reEmptyStringMiddle = /\b(__p \+=) '' \+/g, |
48 | reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g; |
49 | |
50 | /** |
51 | * Used to match ES6 template delimiters |
52 | * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals |
53 | */ |
54 | var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g; |
55 | |
56 | /** Used to match regexp flags from their coerced string values */ |
57 | var reFlags = /\w*$/; |
58 | |
59 | /** Used to detected named functions */ |
60 | var reFuncName = /^\s*function[ \n\r\t]+\w/; |
61 | |
62 | /** Used to match "interpolate" template delimiters */ |
63 | var reInterpolate = /<%=([\s\S]+?)%>/g; |
64 | |
65 | /** Used to match leading whitespace and zeros to be removed */ |
66 | var reLeadingSpacesAndZeros = RegExp('^[' + whitespace + ']*0+(?=.$)'); |
67 | |
68 | /** Used to ensure capturing order of template delimiters */ |
69 | var reNoMatch = /($^)/; |
70 | |
71 | /** Used to detect functions containing a `this` reference */ |
72 | var reThis = /\bthis\b/; |
73 | |
74 | /** Used to match unescaped characters in compiled string literals */ |
75 | var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g; |
76 | |
77 | /** Used to assign default `context` object properties */ |
78 | var contextProps = [ |
79 | 'Array', 'Boolean', 'Date', 'Error', 'Function', 'Math', 'Number', 'Object', |
80 | 'RegExp', 'String', '_', 'attachEvent', 'clearTimeout', 'isFinite', 'isNaN', |
81 | 'parseInt', 'setTimeout' |
82 | ]; |
83 | |
84 | /** Used to fix the JScript [[DontEnum]] bug */ |
85 | var shadowedProps = [ |
86 | 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', |
87 | 'toLocaleString', 'toString', 'valueOf' |
88 | ]; |
89 | |
90 | /** Used to make template sourceURLs easier to identify */ |
91 | var templateCounter = 0; |
92 | |
93 | /** `Object#toString` result shortcuts */ |
94 | var argsClass = '[object Arguments]', |
95 | arrayClass = '[object Array]', |
96 | boolClass = '[object Boolean]', |
97 | dateClass = '[object Date]', |
98 | errorClass = '[object Error]', |
99 | funcClass = '[object Function]', |
100 | numberClass = '[object Number]', |
101 | objectClass = '[object Object]', |
102 | regexpClass = '[object RegExp]', |
103 | stringClass = '[object String]'; |
104 | |
105 | /** Used to identify object classifications that `_.clone` supports */ |
106 | var cloneableClasses = {}; |
107 | cloneableClasses[funcClass] = false; |
108 | cloneableClasses[argsClass] = cloneableClasses[arrayClass] = |
109 | cloneableClasses[boolClass] = cloneableClasses[dateClass] = |
110 | cloneableClasses[numberClass] = cloneableClasses[objectClass] = |
111 | cloneableClasses[regexpClass] = cloneableClasses[stringClass] = true; |
112 | |
113 | /** Used as an internal `_.debounce` options object */ |
114 | var debounceOptions = { |
115 | 'leading': false, |
116 | 'maxWait': 0, |
117 | 'trailing': false |
118 | }; |
119 | |
120 | /** Used as the property descriptor for `__bindData__` */ |
121 | var descriptor = { |
122 | 'configurable': false, |
123 | 'enumerable': false, |
124 | 'value': null, |
125 | 'writable': false |
126 | }; |
127 | |
128 | /** Used as the data object for `iteratorTemplate` */ |
129 | var iteratorData = { |
130 | 'args': '', |
131 | 'array': null, |
132 | 'bottom': '', |
133 | 'firstArg': '', |
134 | 'init': '', |
135 | 'keys': null, |
136 | 'loop': '', |
137 | 'shadowedProps': null, |
138 | 'support': null, |
139 | 'top': '', |
140 | 'useHas': false |
141 | }; |
142 | |
143 | /** Used to determine if values are of the language type Object */ |
144 | var objectTypes = { |
145 | 'boolean': false, |
146 | 'function': true, |
147 | 'object': true, |
148 | 'number': false, |
149 | 'string': false, |
150 | 'undefined': false |
151 | }; |
152 | |
153 | /** Used to escape characters for inclusion in compiled string literals */ |
154 | var stringEscapes = { |
155 | '\\': '\\', |
156 | "'": "'", |
157 | '\n': 'n', |
158 | '\r': 'r', |
159 | '\t': 't', |
160 | '\u2028': 'u2028', |
161 | '\u2029': 'u2029' |
162 | }; |
163 | |
164 | /** Used as a reference to the global object */ |
165 | var root = (objectTypes[typeof window] && window) || this; |
166 | |
167 | /** Detect free variable `exports` */ |
168 | var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; |
169 | |
170 | /** Detect free variable `module` */ |
171 | var freeModule = objectTypes[typeof module] && module && !module.nodeType && module; |
172 | |
173 | /** Detect the popular CommonJS extension `module.exports` */ |
174 | var moduleExports = freeModule && freeModule.exports === freeExports && freeExports; |
175 | |
176 | /** Detect free variable `global` from Node.js or Browserified code and use it as `root` */ |
177 | var freeGlobal = objectTypes[typeof global] && global; |
178 | if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) { |
179 | root = freeGlobal; |
180 | } |
181 | |
182 | /*--------------------------------------------------------------------------*/ |
183 | |
184 | /** |
185 | * The base implementation of `_.indexOf` without support for binary searches |
186 | * or `fromIndex` constraints. |
187 | * |
188 | * @private |
189 | * @param {Array} array The array to search. |
190 | * @param {*} value The value to search for. |
191 | * @param {number} [fromIndex=0] The index to search from. |
192 | * @returns {number} Returns the index of the matched value or `-1`. |
193 | */ |
194 | function baseIndexOf(array, value, fromIndex) { |
195 | var index = (fromIndex || 0) - 1, |
196 | length = array ? array.length : 0; |
197 | |
198 | while (++index < length) { |
199 | if (array[index] === value) { |
200 | return index; |
201 | } |
202 | } |
203 | return -1; |
204 | } |
205 | |
206 | /** |
207 | * An implementation of `_.contains` for cache objects that mimics the return |
208 | * signature of `_.indexOf` by returning `0` if the value is found, else `-1`. |
209 | * |
210 | * @private |
211 | * @param {Object} cache The cache object to inspect. |
212 | * @param {*} value The value to search for. |
213 | * @returns {number} Returns `0` if `value` is found, else `-1`. |
214 | */ |
215 | function cacheIndexOf(cache, value) { |
216 | var type = typeof value; |
217 | cache = cache.cache; |
218 | |
219 | if (type == 'boolean' || value == null) { |
220 | return cache[value] ? 0 : -1; |
221 | } |
222 | if (type != 'number' && type != 'string') { |
223 | type = 'object'; |
224 | } |
225 | var key = type == 'number' ? value : keyPrefix + value; |
226 | cache = (cache = cache[type]) && cache[key]; |
227 | |
228 | return type == 'object' |
229 | ? (cache && baseIndexOf(cache, value) > -1 ? 0 : -1) |
230 | : (cache ? 0 : -1); |
231 | } |
232 | |
233 | /** |
234 | * Adds a given value to the corresponding cache object. |
235 | * |
236 | * @private |
237 | * @param {*} value The value to add to the cache. |
238 | */ |
239 | function cachePush(value) { |
240 | var cache = this.cache, |
241 | type = typeof value; |
242 | |
243 | if (type == 'boolean' || value == null) { |
244 | cache[value] = true; |
245 | } else { |
246 | if (type != 'number' && type != 'string') { |
247 | type = 'object'; |
248 | } |
249 | var key = type == 'number' ? value : keyPrefix + value, |
250 | typeCache = cache[type] || (cache[type] = {}); |
251 | |
252 | if (type == 'object') { |
253 | (typeCache[key] || (typeCache[key] = [])).push(value); |
254 | } else { |
255 | typeCache[key] = true; |
256 | } |
257 | } |
258 | } |
259 | |
260 | /** |
261 | * Used by `_.max` and `_.min` as the default callback when a given |
262 | * collection is a string value. |
263 | * |
264 | * @private |
265 | * @param {string} value The character to inspect. |
266 | * @returns {number} Returns the code unit of given character. |
267 | */ |
268 | function charAtCallback(value) { |
269 | return value.charCodeAt(0); |
270 | } |
271 | |
272 | /** |
273 | * Used by `sortBy` to compare transformed `collection` elements, stable sorting |
274 | * them in ascending order. |
275 | * |
276 | * @private |
277 | * @param {Object} a The object to compare to `b`. |
278 | * @param {Object} b The object to compare to `a`. |
279 | * @returns {number} Returns the sort order indicator of `1` or `-1`. |
280 | */ |
281 | function compareAscending(a, b) { |
282 | var ac = a.criteria, |
283 | bc = b.criteria, |
284 | index = -1, |
285 | length = ac.length; |
286 | |
287 | while (++index < length) { |
288 | var value = ac[index], |
289 | other = bc[index]; |
290 | |
291 | if (value !== other) { |
292 | if (value > other || typeof value == 'undefined') { |
293 | return 1; |
294 | } |
295 | if (value < other || typeof other == 'undefined') { |
296 | return -1; |
297 | } |
298 | } |
299 | } |
300 | // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications |
301 | // that causes it, under certain circumstances, to return the same value for |
302 | // `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247 |
303 | // |
304 | // This also ensures a stable sort in V8 and other engines. |
305 | // See http://code.google.com/p/v8/issues/detail?id=90 |
306 | return a.index - b.index; |
307 | } |
308 | |
309 | /** |
310 | * Creates a cache object to optimize linear searches of large arrays. |
311 | * |
312 | * @private |
313 | * @param {Array} [array=[]] The array to search. |
314 | * @returns {null|Object} Returns the cache object or `null` if caching should not be used. |
315 | */ |
316 | function createCache(array) { |
317 | var index = -1, |
318 | length = array.length, |
319 | first = array[0], |
320 | mid = array[(length / 2) | 0], |
321 | last = array[length - 1]; |
322 | |
323 | if (first && typeof first == 'object' && |
324 | mid && typeof mid == 'object' && last && typeof last == 'object') { |
325 | return false; |
326 | } |
327 | var cache = getObject(); |
328 | cache['false'] = cache['null'] = cache['true'] = cache['undefined'] = false; |
329 | |
330 | var result = getObject(); |
331 | result.array = array; |
332 | result.cache = cache; |
333 | result.push = cachePush; |
334 | |
335 | while (++index < length) { |
336 | result.push(array[index]); |
337 | } |
338 | return result; |
339 | } |
340 | |
341 | /** |
342 | * Used by `template` to escape characters for inclusion in compiled |
343 | * string literals. |
344 | * |
345 | * @private |
346 | * @param {string} match The matched character to escape. |
347 | * @returns {string} Returns the escaped character. |
348 | */ |
349 | function escapeStringChar(match) { |
350 | return '\\' + stringEscapes[match]; |
351 | } |
352 | |
353 | /** |
354 | * Gets an array from the array pool or creates a new one if the pool is empty. |
355 | * |
356 | * @private |
357 | * @returns {Array} The array from the pool. |
358 | */ |
359 | function getArray() { |
360 | return arrayPool.pop() || []; |
361 | } |
362 | |
363 | /** |
364 | * Gets an object from the object pool or creates a new one if the pool is empty. |
365 | * |
366 | * @private |
367 | * @returns {Object} The object from the pool. |
368 | */ |
369 | function getObject() { |
370 | return objectPool.pop() || { |
371 | 'array': null, |
372 | 'cache': null, |
373 | 'criteria': null, |
374 | 'false': false, |
375 | 'index': 0, |
376 | 'null': false, |
377 | 'number': null, |
378 | 'object': null, |
379 | 'push': null, |
380 | 'string': null, |
381 | 'true': false, |
382 | 'undefined': false, |
383 | 'value': null |
384 | }; |
385 | } |
386 | |
387 | /** |
388 | * Checks if `value` is a DOM node in IE < 9. |
389 | * |
390 | * @private |
391 | * @param {*} value The value to check. |
392 | * @returns {boolean} Returns `true` if the `value` is a DOM node, else `false`. |
393 | */ |
394 | function isNode(value) { |
395 | // IE < 9 presents DOM nodes as `Object` objects except they have `toString` |
396 | // methods that are `typeof` "string" and still can coerce nodes to strings |
397 | return typeof value.toString != 'function' && typeof (value + '') == 'string'; |
398 | } |
399 | |
400 | /** |
401 | * Releases the given array back to the array pool. |
402 | * |
403 | * @private |
404 | * @param {Array} [array] The array to release. |
405 | */ |
406 | function releaseArray(array) { |
407 | array.length = 0; |
408 | if (arrayPool.length < maxPoolSize) { |
409 | arrayPool.push(array); |
410 | } |
411 | } |
412 | |
413 | /** |
414 | * Releases the given object back to the object pool. |
415 | * |
416 | * @private |
417 | * @param {Object} [object] The object to release. |
418 | */ |
419 | function releaseObject(object) { |
420 | var cache = object.cache; |
421 | if (cache) { |
422 | releaseObject(cache); |
423 | } |
424 | object.array = object.cache = object.criteria = object.object = object.number = object.string = object.value = null; |
425 | if (objectPool.length < maxPoolSize) { |
426 | objectPool.push(object); |
427 | } |
428 | } |
429 | |
430 | /** |
431 | * Slices the `collection` from the `start` index up to, but not including, |
432 | * the `end` index. |
433 | * |
434 | * Note: This function is used instead of `Array#slice` to support node lists |
435 | * in IE < 9 and to ensure dense arrays are returned. |
436 | * |
437 | * @private |
438 | * @param {Array|Object|string} collection The collection to slice. |
439 | * @param {number} start The start index. |
440 | * @param {number} end The end index. |
441 | * @returns {Array} Returns the new array. |
442 | */ |
443 | function slice(array, start, end) { |
444 | start || (start = 0); |
445 | if (typeof end == 'undefined') { |
446 | end = array ? array.length : 0; |
447 | } |
448 | var index = -1, |
449 | length = end - start || 0, |
450 | result = Array(length < 0 ? 0 : length); |
451 | |
452 | while (++index < length) { |
453 | result[index] = array[start + index]; |
454 | } |
455 | return result; |
456 | } |
457 | |
458 | /*--------------------------------------------------------------------------*/ |
459 | |
460 | /** |
461 | * Create a new `lodash` function using the given context object. |
462 | * |
463 | * @static |
464 | * @memberOf _ |
465 | * @category Utilities |
466 | * @param {Object} [context=root] The context object. |
467 | * @returns {Function} Returns the `lodash` function. |
468 | */ |
469 | function runInContext(context) { |
470 | // Avoid issues with some ES3 environments that attempt to use values, named |
471 | // after built-in constructors like `Object`, for the creation of literals. |
472 | // ES5 clears this up by stating that literals must use built-in constructors. |
473 | // See http://es5.github.io/#x11.1.5. |
474 | context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root; |
475 | |
476 | /** Native constructor references */ |
477 | var Array = context.Array, |
478 | Boolean = context.Boolean, |
479 | Date = context.Date, |
480 | Error = context.Error, |
481 | Function = context.Function, |
482 | Math = context.Math, |
483 | Number = context.Number, |
484 | Object = context.Object, |
485 | RegExp = context.RegExp, |
486 | String = context.String, |
487 | TypeError = context.TypeError; |
488 | |
489 | /** |
490 | * Used for `Array` method references. |
491 | * |
492 | * Normally `Array.prototype` would suffice, however, using an array literal |
493 | * avoids issues in Narwhal. |
494 | */ |
495 | var arrayRef = []; |
496 | |
497 | /** Used for native method references */ |
498 | var errorProto = Error.prototype, |
499 | objectProto = Object.prototype, |
500 | stringProto = String.prototype; |
501 | |
502 | /** Used to restore the original `_` reference in `noConflict` */ |
503 | var oldDash = context._; |
504 | |
505 | /** Used to resolve the internal [[Class]] of values */ |
506 | var toString = objectProto.toString; |
507 | |
508 | /** Used to detect if a method is native */ |
509 | var reNative = RegExp('^' + |
510 | String(toString) |
511 | .replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
512 | .replace(/toString| for [^\]]+/g, '.*?') + '$' |
513 | ); |
514 | |
515 | /** Native method shortcuts */ |
516 | var ceil = Math.ceil, |
517 | clearTimeout = context.clearTimeout, |
518 | floor = Math.floor, |
519 | fnToString = Function.prototype.toString, |
520 | getPrototypeOf = isNative(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf, |
521 | hasOwnProperty = objectProto.hasOwnProperty, |
522 | push = arrayRef.push, |
523 | propertyIsEnumerable = objectProto.propertyIsEnumerable, |
524 | setTimeout = context.setTimeout, |
525 | splice = arrayRef.splice, |
526 | unshift = arrayRef.unshift; |
527 | |
528 | /** Used to set meta data on functions */ |
529 | var defineProperty = (function() { |
530 | // IE 8 only accepts DOM elements |
531 | try { |
532 | var o = {}, |
533 | func = isNative(func = Object.defineProperty) && func, |
534 | result = func(o, o, o) && func; |
535 | } catch(e) { } |
536 | return result; |
537 | }()); |
538 | |
539 | /* Native method shortcuts for methods with the same name as other `lodash` methods */ |
540 | var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate, |
541 | nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray, |
542 | nativeIsFinite = context.isFinite, |
543 | nativeIsNaN = context.isNaN, |
544 | nativeKeys = isNative(nativeKeys = Object.keys) && nativeKeys, |
545 | nativeMax = Math.max, |
546 | nativeMin = Math.min, |
547 | nativeParseInt = context.parseInt, |
548 | nativeRandom = Math.random; |
549 | |
550 | /** Used to lookup a built-in constructor by [[Class]] */ |
551 | var ctorByClass = {}; |
552 | ctorByClass[arrayClass] = Array; |
553 | ctorByClass[boolClass] = Boolean; |
554 | ctorByClass[dateClass] = Date; |
555 | ctorByClass[funcClass] = Function; |
556 | ctorByClass[objectClass] = Object; |
557 | ctorByClass[numberClass] = Number; |
558 | ctorByClass[regexpClass] = RegExp; |
559 | ctorByClass[stringClass] = String; |
560 | |
561 | /** Used to avoid iterating non-enumerable properties in IE < 9 */ |
562 | var nonEnumProps = {}; |
563 | nonEnumProps[arrayClass] = nonEnumProps[dateClass] = nonEnumProps[numberClass] = { 'constructor': true, 'toLocaleString': true, 'toString': true, 'valueOf': true }; |
564 | nonEnumProps[boolClass] = nonEnumProps[stringClass] = { 'constructor': true, 'toString': true, 'valueOf': true }; |
565 | nonEnumProps[errorClass] = nonEnumProps[funcClass] = nonEnumProps[regexpClass] = { 'constructor': true, 'toString': true }; |
566 | nonEnumProps[objectClass] = { 'constructor': true }; |
567 | |
568 | (function() { |
569 | var length = shadowedProps.length; |
570 | while (length--) { |
571 | var key = shadowedProps[length]; |
572 | for (var className in nonEnumProps) { |
573 | if (hasOwnProperty.call(nonEnumProps, className) && !hasOwnProperty.call(nonEnumProps[className], key)) { |
574 | nonEnumProps[className][key] = false; |
575 | } |
576 | } |
577 | } |
578 | }()); |
579 | |
580 | /*--------------------------------------------------------------------------*/ |
581 | |
582 | /** |
583 | * Creates a `lodash` object which wraps the given value to enable intuitive |
584 | * method chaining. |
585 | * |
586 | * In addition to Lo-Dash methods, wrappers also have the following `Array` methods: |
587 | * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`, |
588 | * and `unshift` |
589 | * |
590 | * Chaining is supported in custom builds as long as the `value` method is |
591 | * implicitly or explicitly included in the build. |
592 | * |
593 | * The chainable wrapper functions are: |
594 | * `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`, |
595 | * `compose`, `concat`, `countBy`, `create`, `createCallback`, `curry`, |
596 | * `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`, |
597 | * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`, |
598 | * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`, |
599 | * `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`, |
600 | * `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `pull`, `push`, |
601 | * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, |
602 | * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`, |
603 | * `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`, |
604 | * and `zip` |
605 | * |
606 | * The non-chainable wrapper functions are: |
607 | * `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `findIndex`, |
608 | * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `has`, `identity`, |
609 | * `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`, |
610 | * `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, `isNull`, `isNumber`, |
611 | * `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`, `join`, |
612 | * `lastIndexOf`, `mixin`, `noConflict`, `parseInt`, `pop`, `random`, `reduce`, |
613 | * `reduceRight`, `result`, `shift`, `size`, `some`, `sortedIndex`, `runInContext`, |
614 | * `template`, `unescape`, `uniqueId`, and `value` |
615 | * |
616 | * The wrapper functions `first` and `last` return wrapped values when `n` is |
617 | * provided, otherwise they return unwrapped values. |
618 | * |
619 | * Explicit chaining can be enabled by using the `_.chain` method. |
620 | * |
621 | * @name _ |
622 | * @constructor |
623 | * @category Chaining |
624 | * @param {*} value The value to wrap in a `lodash` instance. |
625 | * @returns {Object} Returns a `lodash` instance. |
626 | * @example |
627 | * |
628 | * var wrapped = _([1, 2, 3]); |
629 | * |
630 | * // returns an unwrapped value |
631 | * wrapped.reduce(function(sum, num) { |
632 | * return sum + num; |
633 | * }); |
634 | * // => 6 |
635 | * |
636 | * // returns a wrapped value |
637 | * var squares = wrapped.map(function(num) { |
638 | * return num * num; |
639 | * }); |
640 | * |
641 | * _.isArray(squares); |
642 | * // => false |
643 | * |
644 | * _.isArray(squares.value()); |
645 | * // => true |
646 | */ |
647 | function lodash(value) { |
648 | // don't wrap if already wrapped, even if wrapped by a different `lodash` constructor |
649 | return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__')) |
650 | ? value |
651 | : new lodashWrapper(value); |
652 | } |
653 | |
654 | /** |
655 | * A fast path for creating `lodash` wrapper objects. |
656 | * |
657 | * @private |
658 | * @param {*} value The value to wrap in a `lodash` instance. |
659 | * @param {boolean} chainAll A flag to enable chaining for all methods |
660 | * @returns {Object} Returns a `lodash` instance. |
661 | */ |
662 | function lodashWrapper(value, chainAll) { |
663 | this.__chain__ = !!chainAll; |
664 | this.__wrapped__ = value; |
665 | } |
666 | // ensure `new lodashWrapper` is an instance of `lodash` |
667 | lodashWrapper.prototype = lodash.prototype; |
668 | |
669 | /** |
670 | * An object used to flag environments features. |
671 | * |
672 | * @static |
673 | * @memberOf _ |
674 | * @type Object |
675 | */ |
676 | var support = lodash.support = {}; |
677 | |
678 | (function() { |
679 | var ctor = function() { this.x = 1; }, |
680 | object = { '0': 1, 'length': 1 }, |
681 | props = []; |
682 | |
683 | ctor.prototype = { 'valueOf': 1, 'y': 1 }; |
684 | for (var key in new ctor) { props.push(key); } |
685 | for (key in arguments) { } |
686 | |
687 | /** |
688 | * Detect if an `arguments` object's [[Class]] is resolvable (all but Firefox < 4, IE < 9). |
689 | * |
690 | * @memberOf _.support |
691 | * @type boolean |
692 | */ |
693 | support.argsClass = toString.call(arguments) == argsClass; |
694 | |
695 | /** |
696 | * Detect if `arguments` objects are `Object` objects (all but Narwhal and Opera < 10.5). |
697 | * |
698 | * @memberOf _.support |
699 | * @type boolean |
700 | */ |
701 | support.argsObject = arguments.constructor == Object && !(arguments instanceof Array); |
702 | |
703 | /** |
704 | * Detect if `name` or `message` properties of `Error.prototype` are |
705 | * enumerable by default. (IE < 9, Safari < 5.1) |
706 | * |
707 | * @memberOf _.support |
708 | * @type boolean |
709 | */ |
710 | support.enumErrorProps = propertyIsEnumerable.call(errorProto, 'message') || propertyIsEnumerable.call(errorProto, 'name'); |
711 | |
712 | /** |
713 | * Detect if `prototype` properties are enumerable by default. |
714 | * |
715 | * Firefox < 3.6, Opera > 9.50 - Opera < 11.60, and Safari < 5.1 |
716 | * (if the prototype or a property on the prototype has been set) |
717 | * incorrectly sets a function's `prototype` property [[Enumerable]] |
718 | * value to `true`. |
719 | * |
720 | * @memberOf _.support |
721 | * @type boolean |
722 | */ |
723 | support.enumPrototypes = propertyIsEnumerable.call(ctor, 'prototype'); |
724 | |
725 | /** |
726 | * Detect if functions can be decompiled by `Function#toString` |
727 | * (all but PS3 and older Opera mobile browsers & avoided in Windows 8 apps). |
728 | * |
729 | * @memberOf _.support |
730 | * @type boolean |
731 | */ |
732 | support.funcDecomp = !isNative(context.WinRTError) && reThis.test(runInContext); |
733 | |
734 | /** |
735 | * Detect if `Function#name` is supported (all but IE). |
736 | * |
737 | * @memberOf _.support |
738 | * @type boolean |
739 | */ |
740 | support.funcNames = typeof Function.name == 'string'; |
741 | |
742 | /** |
743 | * Detect if `arguments` object indexes are non-enumerable |
744 | * (Firefox < 4, IE < 9, PhantomJS, Safari < 5.1). |
745 | * |
746 | * @memberOf _.support |
747 | * @type boolean |
748 | */ |
749 | support.nonEnumArgs = key != 0; |
750 | |
751 | /** |
752 | * Detect if properties shadowing those on `Object.prototype` are non-enumerable. |
753 | * |
754 | * In IE < 9 an objects own properties, shadowing non-enumerable ones, are |
755 | * made non-enumerable as well (a.k.a the JScript [[DontEnum]] bug). |
756 | * |
757 | * @memberOf _.support |
758 | * @type boolean |
759 | */ |
760 | support.nonEnumShadows = !/valueOf/.test(props); |
761 | |
762 | /** |
763 | * Detect if own properties are iterated after inherited properties (all but IE < 9). |
764 | * |
765 | * @memberOf _.support |
766 | * @type boolean |
767 | */ |
768 | support.ownLast = props[0] != 'x'; |
769 | |
770 | /** |
771 | * Detect if `Array#shift` and `Array#splice` augment array-like objects correctly. |
772 | * |
773 | * Firefox < 10, IE compatibility mode, and IE < 9 have buggy Array `shift()` |
774 | * and `splice()` functions that fail to remove the last element, `value[0]`, |
775 | * of array-like objects even though the `length` property is set to `0`. |
776 | * The `shift()` method is buggy in IE 8 compatibility mode, while `splice()` |
777 | * is buggy regardless of mode in IE < 9 and buggy in compatibility mode in IE 9. |
778 | * |
779 | * @memberOf _.support |
780 | * @type boolean |
781 | */ |
782 | support.spliceObjects = (arrayRef.splice.call(object, 0, 1), !object[0]); |
783 | |
784 | /** |
785 | * Detect lack of support for accessing string characters by index. |
786 | * |
787 | * IE < 8 can't access characters by index and IE 8 can only access |
788 | * characters by index on string literals. |
789 | * |
790 | * @memberOf _.support |
791 | * @type boolean |
792 | */ |
793 | support.unindexedChars = ('x'[0] + Object('x')[0]) != 'xx'; |
794 | |
795 | /** |
796 | * Detect if a DOM node's [[Class]] is resolvable (all but IE < 9) |
797 | * and that the JS engine errors when attempting to coerce an object to |
798 | * a string without a `toString` function. |
799 | * |
800 | * @memberOf _.support |
801 | * @type boolean |
802 | */ |
803 | try { |
804 | support.nodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + '')); |
805 | } catch(e) { |
806 | support.nodeClass = true; |
807 | } |
808 | }(1)); |
809 | |
810 | /** |
811 | * By default, the template delimiters used by Lo-Dash are similar to those in |
812 | * embedded Ruby (ERB). Change the following template settings to use alternative |
813 | * delimiters. |
814 | * |
815 | * @static |
816 | * @memberOf _ |
817 | * @type Object |
818 | */ |
819 | lodash.templateSettings = { |
820 | |
821 | /** |
822 | * Used to detect `data` property values to be HTML-escaped. |
823 | * |
824 | * @memberOf _.templateSettings |
825 | * @type RegExp |
826 | */ |
827 | 'escape': /<%-([\s\S]+?)%>/g, |
828 | |
829 | /** |
830 | * Used to detect code to be evaluated. |
831 | * |
832 | * @memberOf _.templateSettings |
833 | * @type RegExp |
834 | */ |
835 | 'evaluate': /<%([\s\S]+?)%>/g, |
836 | |
837 | /** |
838 | * Used to detect `data` property values to inject. |
839 | * |
840 | * @memberOf _.templateSettings |
841 | * @type RegExp |
842 | */ |
843 | 'interpolate': reInterpolate, |
844 | |
845 | /** |
846 | * Used to reference the data object in the template text. |
847 | * |
848 | * @memberOf _.templateSettings |
849 | * @type string |
850 | */ |
851 | 'variable': '', |
852 | |
853 | /** |
854 | * Used to import variables into the compiled template. |
855 | * |
856 | * @memberOf _.templateSettings |
857 | * @type Object |
858 | */ |
859 | 'imports': { |
860 | |
861 | /** |
862 | * A reference to the `lodash` function. |
863 | * |
864 | * @memberOf _.templateSettings.imports |
865 | * @type Function |
866 | */ |
867 | '_': lodash |
868 | } |
869 | }; |
870 | |
871 | /*--------------------------------------------------------------------------*/ |
872 | |
873 | /** |
874 | * The template used to create iterator functions. |
875 | * |
876 | * @private |
877 | * @param {Object} data The data object used to populate the text. |
878 | * @returns {string} Returns the interpolated text. |
879 | */ |
880 | var iteratorTemplate = template( |
881 | // the `iterable` may be reassigned by the `top` snippet |
882 | 'var index, iterable = <%= firstArg %>, ' + |
883 | // assign the `result` variable an initial value |
884 | 'result = <%= init %>;\n' + |
885 | // exit early if the first argument is falsey |
886 | 'if (!iterable) return result;\n' + |
887 | // add code before the iteration branches |
888 | '<%= top %>;' + |
889 | |
890 | // array-like iteration: |
891 | '<% if (array) { %>\n' + |
892 | 'var length = iterable.length; index = -1;\n' + |
893 | 'if (<%= array %>) {' + |
894 | |
895 | // add support for accessing string characters by index if needed |
896 | ' <% if (support.unindexedChars) { %>\n' + |
897 | ' if (isString(iterable)) {\n' + |
898 | " iterable = iterable.split('')\n" + |
899 | ' }' + |
900 | ' <% } %>\n' + |
901 | |
902 | // iterate over the array-like value |
903 | ' while (++index < length) {\n' + |
904 | ' <%= loop %>;\n' + |
905 | ' }\n' + |
906 | '}\n' + |
907 | 'else {' + |
908 | |
909 | // object iteration: |
910 | // add support for iterating over `arguments` objects if needed |
911 | ' <% } else if (support.nonEnumArgs) { %>\n' + |
912 | ' var length = iterable.length; index = -1;\n' + |
913 | ' if (length && isArguments(iterable)) {\n' + |
914 | ' while (++index < length) {\n' + |
915 | " index += '';\n" + |
916 | ' <%= loop %>;\n' + |
917 | ' }\n' + |
918 | ' } else {' + |
919 | ' <% } %>' + |
920 | |
921 | // avoid iterating over `prototype` properties in older Firefox, Opera, and Safari |
922 | ' <% if (support.enumPrototypes) { %>\n' + |
923 | " var skipProto = typeof iterable == 'function';\n" + |
924 | ' <% } %>' + |
925 | |
926 | // avoid iterating over `Error.prototype` properties in older IE and Safari |
927 | ' <% if (support.enumErrorProps) { %>\n' + |
928 | ' var skipErrorProps = iterable === errorProto || iterable instanceof Error;\n' + |
929 | ' <% } %>' + |
930 | |
931 | // define conditions used in the loop |
932 | ' <%' + |
933 | ' var conditions = [];' + |
934 | ' if (support.enumPrototypes) { conditions.push(\'!(skipProto && index == "prototype")\'); }' + |
935 | ' if (support.enumErrorProps) { conditions.push(\'!(skipErrorProps && (index == "message" || index == "name"))\'); }' + |
936 | ' %>' + |
937 | |
938 | // iterate own properties using `Object.keys` |
939 | ' <% if (useHas && keys) { %>\n' + |
940 | ' var ownIndex = -1,\n' + |
941 | ' ownProps = objectTypes[typeof iterable] && keys(iterable),\n' + |
942 | ' length = ownProps ? ownProps.length : 0;\n\n' + |
943 | ' while (++ownIndex < length) {\n' + |
944 | ' index = ownProps[ownIndex];\n<%' + |
945 | " if (conditions.length) { %> if (<%= conditions.join(' && ') %>) {\n <% } %>" + |
946 | ' <%= loop %>;' + |
947 | ' <% if (conditions.length) { %>\n }<% } %>\n' + |
948 | ' }' + |
949 | |
950 | // else using a for-in loop |
951 | ' <% } else { %>\n' + |
952 | ' for (index in iterable) {\n<%' + |
953 | ' if (useHas) { conditions.push("hasOwnProperty.call(iterable, index)"); }' + |
954 | " if (conditions.length) { %> if (<%= conditions.join(' && ') %>) {\n <% } %>" + |
955 | ' <%= loop %>;' + |
956 | ' <% if (conditions.length) { %>\n }<% } %>\n' + |
957 | ' }' + |
958 | |
959 | // Because IE < 9 can't set the `[[Enumerable]]` attribute of an |
960 | // existing property and the `constructor` property of a prototype |
961 | // defaults to non-enumerable, Lo-Dash skips the `constructor` |
962 | // property when it infers it's iterating over a `prototype` object. |
963 | ' <% if (support.nonEnumShadows) { %>\n\n' + |
964 | ' if (iterable !== objectProto) {\n' + |
965 | " var ctor = iterable.constructor,\n" + |
966 | ' isProto = iterable === (ctor && ctor.prototype),\n' + |
967 | ' className = iterable === stringProto ? stringClass : iterable === errorProto ? errorClass : toString.call(iterable),\n' + |
968 | ' nonEnum = nonEnumProps[className];\n' + |
969 | ' <% for (k = 0; k < 7; k++) { %>\n' + |
970 | " index = '<%= shadowedProps[k] %>';\n" + |
971 | ' if ((!(isProto && nonEnum[index]) && hasOwnProperty.call(iterable, index))<%' + |
972 | ' if (!useHas) { %> || (!nonEnum[index] && iterable[index] !== objectProto[index])<% }' + |
973 | ' %>) {\n' + |
974 | ' <%= loop %>;\n' + |
975 | ' }' + |
976 | ' <% } %>\n' + |
977 | ' }' + |
978 | ' <% } %>' + |
979 | ' <% } %>' + |
980 | ' <% if (array || support.nonEnumArgs) { %>\n}<% } %>\n' + |
981 | |
982 | // add code to the bottom of the iteration function |
983 | '<%= bottom %>;\n' + |
984 | // finally, return the `result` |
985 | 'return result' |
986 | ); |
987 | |
988 | /*--------------------------------------------------------------------------*/ |
989 | |
990 | /** |
991 | * The base implementation of `_.bind` that creates the bound function and |
992 | * sets its meta data. |
993 | * |
994 | * @private |
995 | * @param {Array} bindData The bind data array. |
996 | * @returns {Function} Returns the new bound function. |
997 | */ |
998 | function baseBind(bindData) { |
999 | var func = bindData[0], |
1000 | partialArgs = bindData[2], |
1001 | thisArg = bindData[4]; |
1002 | |
1003 | function bound() { |
1004 | // `Function#bind` spec |
1005 | // http://es5.github.io/#x15.3.4.5 |
1006 | if (partialArgs) { |
1007 | // avoid `arguments` object deoptimizations by using `slice` instead |
1008 | // of `Array.prototype.slice.call` and not assigning `arguments` to a |
1009 | // variable as a ternary expression |
1010 | var args = slice(partialArgs); |
1011 | push.apply(args, arguments); |
1012 | } |
1013 | // mimic the constructor's `return` behavior |
1014 | // http://es5.github.io/#x13.2.2 |
1015 | if (this instanceof bound) { |
1016 | // ensure `new bound` is an instance of `func` |
1017 | var thisBinding = baseCreate(func.prototype), |
1018 | result = func.apply(thisBinding, args || arguments); |
1019 | return isObject(result) ? result : thisBinding; |
1020 | } |
1021 | return func.apply(thisArg, args || arguments); |
1022 | } |
1023 | setBindData(bound, bindData); |
1024 | return bound; |
1025 | } |
1026 | |
1027 | /** |
1028 | * The base implementation of `_.clone` without argument juggling or support |
1029 | * for `thisArg` binding. |
1030 | * |
1031 | * @private |
1032 | * @param {*} value The value to clone. |
1033 | * @param {boolean} [isDeep=false] Specify a deep clone. |
1034 | * @param {Function} [callback] The function to customize cloning values. |
1035 | * @param {Array} [stackA=[]] Tracks traversed source objects. |
1036 | * @param {Array} [stackB=[]] Associates clones with source counterparts. |
1037 | * @returns {*} Returns the cloned value. |
1038 | */ |
1039 | function baseClone(value, isDeep, callback, stackA, stackB) { |
1040 | if (callback) { |
1041 | var result = callback(value); |
1042 | if (typeof result != 'undefined') { |
1043 | return result; |
1044 | } |
1045 | } |
1046 | // inspect [[Class]] |
1047 | var isObj = isObject(value); |
1048 | if (isObj) { |
1049 | var className = toString.call(value); |
1050 | if (!cloneableClasses[className] || (!support.nodeClass && isNode(value))) { |
1051 | return value; |
1052 | } |
1053 | var ctor = ctorByClass[className]; |
1054 | switch (className) { |
1055 | case boolClass: |
1056 | case dateClass: |
1057 | return new ctor(+value); |
1058 | |
1059 | case numberClass: |
1060 | case stringClass: |
1061 | return new ctor(value); |
1062 | |
1063 | case regexpClass: |
1064 | result = ctor(value.source, reFlags.exec(value)); |
1065 | result.lastIndex = value.lastIndex; |
1066 | return result; |
1067 | } |
1068 | } else { |
1069 | return value; |
1070 | } |
1071 | var isArr = isArray(value); |
1072 | if (isDeep) { |
1073 | // check for circular references and return corresponding clone |
1074 | var initedStack = !stackA; |
1075 | stackA || (stackA = getArray()); |
1076 | stackB || (stackB = getArray()); |
1077 | |
1078 | var length = stackA.length; |
1079 | while (length--) { |
1080 | if (stackA[length] == value) { |
1081 | return stackB[length]; |
1082 | } |
1083 | } |
1084 | result = isArr ? ctor(value.length) : {}; |
1085 | } |
1086 | else { |
1087 | result = isArr ? slice(value) : assign({}, value); |
1088 | } |
1089 | // add array properties assigned by `RegExp#exec` |
1090 | if (isArr) { |
1091 | if (hasOwnProperty.call(value, 'index')) { |
1092 | result.index = value.index; |
1093 | } |
1094 | if (hasOwnProperty.call(value, 'input')) { |
1095 | result.input = value.input; |
1096 | } |
1097 | } |
1098 | // exit for shallow clone |
1099 | if (!isDeep) { |
1100 | return result; |
1101 | } |
1102 | // add the source value to the stack of traversed objects |
1103 | // and associate it with its clone |
1104 | stackA.push(value); |
1105 | stackB.push(result); |
1106 | |
1107 | // recursively populate clone (susceptible to call stack limits) |
1108 | (isArr ? baseEach : forOwn)(value, function(objValue, key) { |
1109 | result[key] = baseClone(objValue, isDeep, callback, stackA, stackB); |
1110 | }); |
1111 | |
1112 | if (initedStack) { |
1113 | releaseArray(stackA); |
1114 | releaseArray(stackB); |
1115 | } |
1116 | return result; |
1117 | } |
1118 | |
1119 | /** |
1120 | * The base implementation of `_.create` without support for assigning |
1121 | * properties to the created object. |
1122 | * |
1123 | * @private |
1124 | * @param {Object} prototype The object to inherit from. |
1125 | * @returns {Object} Returns the new object. |
1126 | */ |
1127 | function baseCreate(prototype, properties) { |
1128 | return isObject(prototype) ? nativeCreate(prototype) : {}; |
1129 | } |
1130 | // fallback for browsers without `Object.create` |
1131 | if (!nativeCreate) { |
1132 | baseCreate = (function() { |
1133 | function Object() {} |
1134 | return function(prototype) { |
1135 | if (isObject(prototype)) { |
1136 | Object.prototype = prototype; |
1137 | var result = new Object; |
1138 | Object.prototype = null; |
1139 | } |
1140 | return result || context.Object(); |
1141 | }; |
1142 | }()); |
1143 | } |
1144 | |
1145 | /** |
1146 | * The base implementation of `_.createCallback` without support for creating |
1147 | * "_.pluck" or "_.where" style callbacks. |
1148 | * |
1149 | * @private |
1150 | * @param {*} [func=identity] The value to convert to a callback. |
1151 | * @param {*} [thisArg] The `this` binding of the created callback. |
1152 | * @param {number} [argCount] The number of arguments the callback accepts. |
1153 | * @returns {Function} Returns a callback function. |
1154 | */ |
1155 | function baseCreateCallback(func, thisArg, argCount) { |
1156 | if (typeof func != 'function') { |
1157 | return identity; |
1158 | } |
1159 | // exit early for no `thisArg` or already bound by `Function#bind` |
1160 | if (typeof thisArg == 'undefined' || !('prototype' in func)) { |
1161 | return func; |
1162 | } |
1163 | var bindData = func.__bindData__; |
1164 | if (typeof bindData == 'undefined') { |
1165 | if (support.funcNames) { |
1166 | bindData = !func.name; |
1167 | } |
1168 | bindData = bindData || !support.funcDecomp; |
1169 | if (!bindData) { |
1170 | var source = fnToString.call(func); |
1171 | if (!support.funcNames) { |
1172 | bindData = !reFuncName.test(source); |
1173 | } |
1174 | if (!bindData) { |
1175 | // checks if `func` references the `this` keyword and stores the result |
1176 | bindData = reThis.test(source); |
1177 | setBindData(func, bindData); |
1178 | } |
1179 | } |
1180 | } |
1181 | // exit early if there are no `this` references or `func` is bound |
1182 | if (bindData === false || (bindData !== true && bindData[1] & 1)) { |
1183 | return func; |
1184 | } |
1185 | switch (argCount) { |
1186 | case 1: return function(value) { |
1187 | return func.call(thisArg, value); |
1188 | }; |
1189 | case 2: return function(a, b) { |
1190 | return func.call(thisArg, a, b); |
1191 | }; |
1192 | case 3: return function(value, index, collection) { |
1193 | return func.call(thisArg, value, index, collection); |
1194 | }; |
1195 | case 4: return function(accumulator, value, index, collection) { |
1196 | return func.call(thisArg, accumulator, value, index, collection); |
1197 | }; |
1198 | } |
1199 | return bind(func, thisArg); |
1200 | } |
1201 | |
1202 | /** |
1203 | * The base implementation of `createWrapper` that creates the wrapper and |
1204 | * sets its meta data. |
1205 | * |
1206 | * @private |
1207 | * @param {Array} bindData The bind data array. |
1208 | * @returns {Function} Returns the new function. |
1209 | */ |
1210 | function baseCreateWrapper(bindData) { |
1211 | var func = bindData[0], |
1212 | bitmask = bindData[1], |
1213 | partialArgs = bindData[2], |
1214 | partialRightArgs = bindData[3], |
1215 | thisArg = bindData[4], |
1216 | arity = bindData[5]; |
1217 | |
1218 | var isBind = bitmask & 1, |
1219 | isBindKey = bitmask & 2, |
1220 | isCurry = bitmask & 4, |
1221 | isCurryBound = bitmask & 8, |
1222 | key = func; |
1223 | |
1224 | function bound() { |
1225 | var thisBinding = isBind ? thisArg : this; |
1226 | if (partialArgs) { |
1227 | var args = slice(partialArgs); |
1228 | push.apply(args, arguments); |
1229 | } |
1230 | if (partialRightArgs || isCurry) { |
1231 | args || (args = slice(arguments)); |
1232 | if (partialRightArgs) { |
1233 | push.apply(args, partialRightArgs); |
1234 | } |
1235 | if (isCurry && args.length < arity) { |
1236 | bitmask |= 16 & ~32; |
1237 | return baseCreateWrapper([func, (isCurryBound ? bitmask : bitmask & ~3), args, null, thisArg, arity]); |
1238 | } |
1239 | } |
1240 | args || (args = arguments); |
1241 | if (isBindKey) { |
1242 | func = thisBinding[key]; |
1243 | } |
1244 | if (this instanceof bound) { |
1245 | thisBinding = baseCreate(func.prototype); |
1246 | var result = func.apply(thisBinding, args); |
1247 | return isObject(result) ? result : thisBinding; |
1248 | } |
1249 | return func.apply(thisBinding, args); |
1250 | } |
1251 | setBindData(bound, bindData); |
1252 | return bound; |
1253 | } |
1254 | |
1255 | /** |
1256 | * The base implementation of `_.difference` that accepts a single array |
1257 | * of values to exclude. |
1258 | * |
1259 | * @private |
1260 | * @param {Array} array The array to process. |
1261 | * @param {Array} [values] The array of values to exclude. |
1262 | * @returns {Array} Returns a new array of filtered values. |
1263 | */ |
1264 | function baseDifference(array, values) { |
1265 | var index = -1, |
1266 | indexOf = getIndexOf(), |
1267 | length = array ? array.length : 0, |
1268 | isLarge = length >= largeArraySize && indexOf === baseIndexOf, |
1269 | result = []; |
1270 | |
1271 | if (isLarge) { |
1272 | var cache = createCache(values); |
1273 | if (cache) { |
1274 | indexOf = cacheIndexOf; |
1275 | values = cache; |
1276 | } else { |
1277 | isLarge = false; |
1278 | } |
1279 | } |
1280 | while (++index < length) { |
1281 | var value = array[index]; |
1282 | if (indexOf(values, value) < 0) { |
1283 | result.push(value); |
1284 | } |
1285 | } |
1286 | if (isLarge) { |
1287 | releaseObject(values); |
1288 | } |
1289 | return result; |
1290 | } |
1291 | |
1292 | /** |
1293 | * The base implementation of `_.flatten` without support for callback |
1294 | * shorthands or `thisArg` binding. |
1295 | * |
1296 | * @private |
1297 | * @param {Array} array The array to flatten. |
1298 | * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level. |
1299 | * @param {boolean} [isStrict=false] A flag to restrict flattening to arrays and `arguments` objects. |
1300 | * @param {number} [fromIndex=0] The index to start from. |
1301 | * @returns {Array} Returns a new flattened array. |
1302 | */ |
1303 | function baseFlatten(array, isShallow, isStrict, fromIndex) { |
1304 | var index = (fromIndex || 0) - 1, |
1305 | length = array ? array.length : 0, |
1306 | result = []; |
1307 | |
1308 | while (++index < length) { |
1309 | var value = array[index]; |
1310 | |
1311 | if (value && typeof value == 'object' && typeof value.length == 'number' |
1312 | && (isArray(value) || isArguments(value))) { |
1313 | // recursively flatten arrays (susceptible to call stack limits) |
1314 | if (!isShallow) { |
1315 | value = baseFlatten(value, isShallow, isStrict); |
1316 | } |
1317 | var valIndex = -1, |
1318 | valLength = value.length, |
1319 | resIndex = result.length; |
1320 | |
1321 | result.length += valLength; |
1322 | while (++valIndex < valLength) { |
1323 | result[resIndex++] = value[valIndex]; |
1324 | } |
1325 | } else if (!isStrict) { |
1326 | result.push(value); |
1327 | } |
1328 | } |
1329 | return result; |
1330 | } |
1331 | |
1332 | /** |
1333 | * The base implementation of `_.isEqual`, without support for `thisArg` binding, |
1334 | * that allows partial "_.where" style comparisons. |
1335 | * |
1336 | * @private |
1337 | * @param {*} a The value to compare. |
1338 | * @param {*} b The other value to compare. |
1339 | * @param {Function} [callback] The function to customize comparing values. |
1340 | * @param {Function} [isWhere=false] A flag to indicate performing partial comparisons. |
1341 | * @param {Array} [stackA=[]] Tracks traversed `a` objects. |
1342 | * @param {Array} [stackB=[]] Tracks traversed `b` objects. |
1343 | * @returns {boolean} Returns `true` if the values are equivalent, else `false`. |
1344 | */ |
1345 | function baseIsEqual(a, b, callback, isWhere, stackA, stackB) { |
1346 | // used to indicate that when comparing objects, `a` has at least the properties of `b` |
1347 | if (callback) { |
1348 | var result = callback(a, b); |
1349 | if (typeof result != 'undefined') { |
1350 | return !!result; |
1351 | } |
1352 | } |
1353 | // exit early for identical values |
1354 | if (a === b) { |
1355 | // treat `+0` vs. `-0` as not equal |
1356 | return a !== 0 || (1 / a == 1 / b); |
1357 | } |
1358 | var type = typeof a, |
1359 | otherType = typeof b; |
1360 | |
1361 | // exit early for unlike primitive values |
1362 | if (a === a && |
1363 | !(a && objectTypes[type]) && |
1364 | !(b && objectTypes[otherType])) { |
1365 | return false; |
1366 | } |
1367 | // exit early for `null` and `undefined` avoiding ES3's Function#call behavior |
1368 | // http://es5.github.io/#x15.3.4.4 |
1369 | if (a == null || b == null) { |
1370 | return a === b; |
1371 | } |
1372 | // compare [[Class]] names |
1373 | var className = toString.call(a), |
1374 | otherClass = toString.call(b); |
1375 | |
1376 | if (className == argsClass) { |
1377 | className = objectClass; |
1378 | } |
1379 | if (otherClass == argsClass) { |
1380 | otherClass = objectClass; |
1381 | } |
1382 | if (className != otherClass) { |
1383 | return false; |
1384 | } |
1385 | switch (className) { |
1386 | case boolClass: |
1387 | case dateClass: |
1388 | // coerce dates and booleans to numbers, dates to milliseconds and booleans |
1389 | // to `1` or `0` treating invalid dates coerced to `NaN` as not equal |
1390 | return +a == +b; |
1391 | |
1392 | case numberClass: |
1393 | // treat `NaN` vs. `NaN` as equal |
1394 | return (a != +a) |
1395 | ? b != +b |
1396 | // but treat `+0` vs. `-0` as not equal |
1397 | : (a == 0 ? (1 / a == 1 / b) : a == +b); |
1398 | |
1399 | case regexpClass: |
1400 | case stringClass: |
1401 | // coerce regexes to strings (http://es5.github.io/#x15.10.6.4) |
1402 | // treat string primitives and their corresponding object instances as equal |
1403 | return a == String(b); |
1404 | } |
1405 | var isArr = className == arrayClass; |
1406 | if (!isArr) { |
1407 | // unwrap any `lodash` wrapped values |
1408 | var aWrapped = hasOwnProperty.call(a, '__wrapped__'), |
1409 | bWrapped = hasOwnProperty.call(b, '__wrapped__'); |
1410 | |
1411 | if (aWrapped || bWrapped) { |
1412 | return baseIsEqual(aWrapped ? a.__wrapped__ : a, bWrapped ? b.__wrapped__ : b, callback, isWhere, stackA, stackB); |
1413 | } |
1414 | // exit for functions and DOM nodes |
1415 | if (className != objectClass || (!support.nodeClass && (isNode(a) || isNode(b)))) { |
1416 | return false; |
1417 | } |
1418 | // in older versions of Opera, `arguments` objects have `Array` constructors |
1419 | var ctorA = !support.argsObject && isArguments(a) ? Object : a.constructor, |
1420 | ctorB = !support.argsObject && isArguments(b) ? Object : b.constructor; |
1421 | |
1422 | // non `Object` object instances with different constructors are not equal |
1423 | if (ctorA != ctorB && |
1424 | !(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) && |
1425 | ('constructor' in a && 'constructor' in b) |
1426 | ) { |
1427 | return false; |
1428 | } |
1429 | } |
1430 | // assume cyclic structures are equal |
1431 | // the algorithm for detecting cyclic structures is adapted from ES 5.1 |
1432 | // section 15.12.3, abstract operation `JO` (http://es5.github.io/#x15.12.3) |
1433 | var initedStack = !stackA; |
1434 | stackA || (stackA = getArray()); |
1435 | stackB || (stackB = getArray()); |
1436 | |
1437 | var length = stackA.length; |
1438 | while (length--) { |
1439 | if (stackA[length] == a) { |
1440 | return stackB[length] == b; |
1441 | } |
1442 | } |
1443 | var size = 0; |
1444 | result = true; |
1445 | |
1446 | // add `a` and `b` to the stack of traversed objects |
1447 | stackA.push(a); |
1448 | stackB.push(b); |
1449 | |
1450 | // recursively compare objects and arrays (susceptible to call stack limits) |
1451 | if (isArr) { |
1452 | // compare lengths to determine if a deep comparison is necessary |
1453 | length = a.length; |
1454 | size = b.length; |
1455 | result = size == length; |
1456 | |
1457 | if (result || isWhere) { |
1458 | // deep compare the contents, ignoring non-numeric properties |
1459 | while (size--) { |
1460 | var index = length, |
1461 | value = b[size]; |
1462 | |
1463 | if (isWhere) { |
1464 | while (index--) { |
1465 | if ((result = baseIsEqual(a[index], value, callback, isWhere, stackA, stackB))) { |
1466 | break; |
1467 | } |
1468 | } |
1469 | } else if (!(result = baseIsEqual(a[size], value, callback, isWhere, stackA, stackB))) { |
1470 | break; |
1471 | } |
1472 | } |
1473 | } |
1474 | } |
1475 | else { |
1476 | // deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys` |
1477 | // which, in this case, is more costly |
1478 | forIn(b, function(value, key, b) { |
1479 | if (hasOwnProperty.call(b, key)) { |
1480 | // count the number of properties. |
1481 | size++; |
1482 | // deep compare each property value. |
1483 | return (result = hasOwnProperty.call(a, key) && baseIsEqual(a[key], value, callback, isWhere, stackA, stackB)); |
1484 | } |
1485 | }); |
1486 | |
1487 | if (result && !isWhere) { |
1488 | // ensure both objects have the same number of properties |
1489 | forIn(a, function(value, key, a) { |
1490 | if (hasOwnProperty.call(a, key)) { |
1491 | // `size` will be `-1` if `a` has more properties than `b` |
1492 | return (result = --size > -1); |
1493 | } |
1494 | }); |
1495 | } |
1496 | } |
1497 | stackA.pop(); |
1498 | stackB.pop(); |
1499 | |
1500 | if (initedStack) { |
1501 | releaseArray(stackA); |
1502 | releaseArray(stackB); |
1503 | } |
1504 | return result; |
1505 | } |
1506 | |
1507 | /** |
1508 | * The base implementation of `_.merge` without argument juggling or support |
1509 | * for `thisArg` binding. |
1510 | * |
1511 | * @private |
1512 | * @param {Object} object The destination object. |
1513 | * @param {Object} source The source object. |
1514 | * @param {Function} [callback] The function to customize merging properties. |
1515 | * @param {Array} [stackA=[]] Tracks traversed source objects. |
1516 | * @param {Array} [stackB=[]] Associates values with source counterparts. |
1517 | */ |
1518 | function baseMerge(object, source, callback, stackA, stackB) { |
1519 | (isArray(source) ? forEach : forOwn)(source, function(source, key) { |
1520 | var found, |
1521 | isArr, |
1522 | result = source, |
1523 | value = object[key]; |
1524 | |
1525 | if (source && ((isArr = isArray(source)) || isPlainObject(source))) { |
1526 | // avoid merging previously merged cyclic sources |
1527 | var stackLength = stackA.length; |
1528 | while (stackLength--) { |
1529 | if ((found = stackA[stackLength] == source)) { |
1530 | value = stackB[stackLength]; |
1531 | break; |
1532 | } |
1533 | } |
1534 | if (!found) { |
1535 | var isShallow; |
1536 | if (callback) { |
1537 | result = callback(value, source); |
1538 | if ((isShallow = typeof result != 'undefined')) { |
1539 | value = result; |
1540 | } |
1541 | } |
1542 | if (!isShallow) { |
1543 | value = isArr |
1544 | ? (isArray(value) ? value : []) |
1545 | : (isPlainObject(value) ? value : {}); |
1546 | } |
1547 | // add `source` and associated `value` to the stack of traversed objects |
1548 | stackA.push(source); |
1549 | stackB.push(value); |
1550 | |
1551 | // recursively merge objects and arrays (susceptible to call stack limits) |
1552 | if (!isShallow) { |
1553 | baseMerge(value, source, callback, stackA, stackB); |
1554 | } |
1555 | } |
1556 | } |
1557 | else { |
1558 | if (callback) { |
1559 | result = callback(value, source); |
1560 | if (typeof result == 'undefined') { |
1561 | result = source; |
1562 | } |
1563 | } |
1564 | if (typeof result != 'undefined') { |
1565 | value = result; |
1566 | } |
1567 | } |
1568 | object[key] = value; |
1569 | }); |
1570 | } |
1571 | |
1572 | /** |
1573 | * The base implementation of `_.random` without argument juggling or support |
1574 | * for returning floating-point numbers. |
1575 | * |
1576 | * @private |
1577 | * @param {number} min The minimum possible value. |
1578 | * @param {number} max The maximum possible value. |
1579 | * @returns {number} Returns a random number. |
1580 | */ |
1581 | function baseRandom(min, max) { |
1582 | return min + floor(nativeRandom() * (max - min + 1)); |
1583 | } |
1584 | |
1585 | /** |
1586 | * The base implementation of `_.uniq` without support for callback shorthands |
1587 | * or `thisArg` binding. |
1588 | * |
1589 | * @private |
1590 | * @param {Array} array The array to process. |
1591 | * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted. |
1592 | * @param {Function} [callback] The function called per iteration. |
1593 | * @returns {Array} Returns a duplicate-value-free array. |
1594 | */ |
1595 | function baseUniq(array, isSorted, callback) { |
1596 | var index = -1, |
1597 | indexOf = getIndexOf(), |
1598 | length = array ? array.length : 0, |
1599 | result = []; |
1600 | |
1601 | var isLarge = !isSorted && length >= largeArraySize && indexOf === baseIndexOf, |
1602 | seen = (callback || isLarge) ? getArray() : result; |
1603 | |
1604 | if (isLarge) { |
1605 | var cache = createCache(seen); |
1606 | indexOf = cacheIndexOf; |
1607 | seen = cache; |
1608 | } |
1609 | while (++index < length) { |
1610 | var value = array[index], |
1611 | computed = callback ? callback(value, index, array) : value; |
1612 | |
1613 | if (isSorted |
1614 | ? !index || seen[seen.length - 1] !== computed |
1615 | : indexOf(seen, computed) < 0 |
1616 | ) { |
1617 | if (callback || isLarge) { |
1618 | seen.push(computed); |
1619 | } |
1620 | result.push(value); |
1621 | } |
1622 | } |
1623 | if (isLarge) { |
1624 | releaseArray(seen.array); |
1625 | releaseObject(seen); |
1626 | } else if (callback) { |
1627 | releaseArray(seen); |
1628 | } |
1629 | return result; |
1630 | } |
1631 | |
1632 | /** |
1633 | * Creates a function that aggregates a collection, creating an object composed |
1634 | * of keys generated from the results of running each element of the collection |
1635 | * through a callback. The given `setter` function sets the keys and values |
1636 | * of the composed object. |
1637 | * |
1638 | * @private |
1639 | * @param {Function} setter The setter function. |
1640 | * @returns {Function} Returns the new aggregator function. |
1641 | */ |
1642 | function createAggregator(setter) { |
1643 | return function(collection, callback, thisArg) { |
1644 | var result = {}; |
1645 | callback = lodash.createCallback(callback, thisArg, 3); |
1646 | |
1647 | if (isArray(collection)) { |
1648 | var index = -1, |
1649 | length = collection.length; |
1650 | |
1651 | while (++index < length) { |
1652 | var value = collection[index]; |
1653 | setter(result, value, callback(value, index, collection), collection); |
1654 | } |
1655 | } else { |
1656 | baseEach(collection, function(value, key, collection) { |
1657 | setter(result, value, callback(value, key, collection), collection); |
1658 | }); |
1659 | } |
1660 | return result; |
1661 | }; |
1662 | } |
1663 | |
1664 | /** |
1665 | * Creates a function that, when called, either curries or invokes `func` |
1666 | * with an optional `this` binding and partially applied arguments. |
1667 | * |
1668 | * @private |
1669 | * @param {Function|string} func The function or method name to reference. |
1670 | * @param {number} bitmask The bitmask of method flags to compose. |
1671 | * The bitmask may be composed of the following flags: |
1672 | * 1 - `_.bind` |
1673 | * 2 - `_.bindKey` |
1674 | * 4 - `_.curry` |
1675 | * 8 - `_.curry` (bound) |
1676 | * 16 - `_.partial` |
1677 | * 32 - `_.partialRight` |
1678 | * @param {Array} [partialArgs] An array of arguments to prepend to those |
1679 | * provided to the new function. |
1680 | * @param {Array} [partialRightArgs] An array of arguments to append to those |
1681 | * provided to the new function. |
1682 | * @param {*} [thisArg] The `this` binding of `func`. |
1683 | * @param {number} [arity] The arity of `func`. |
1684 | * @returns {Function} Returns the new function. |
1685 | */ |
1686 | function createWrapper(func, bitmask, partialArgs, partialRightArgs, thisArg, arity) { |
1687 | var isBind = bitmask & 1, |
1688 | isBindKey = bitmask & 2, |
1689 | isCurry = bitmask & 4, |
1690 | isCurryBound = bitmask & 8, |
1691 | isPartial = bitmask & 16, |
1692 | isPartialRight = bitmask & 32; |
1693 | |
1694 | if (!isBindKey && !isFunction(func)) { |
1695 | throw new TypeError; |
1696 | } |
1697 | if (isPartial && !partialArgs.length) { |
1698 | bitmask &= ~16; |
1699 | isPartial = partialArgs = false; |
1700 | } |
1701 | if (isPartialRight && !partialRightArgs.length) { |
1702 | bitmask &= ~32; |
1703 | isPartialRight = partialRightArgs = false; |
1704 | } |
1705 | var bindData = func && func.__bindData__; |
1706 | if (bindData && bindData !== true) { |
1707 | // clone `bindData` |
1708 | bindData = slice(bindData); |
1709 | if (bindData[2]) { |
1710 | bindData[2] = slice(bindData[2]); |
1711 | } |
1712 | if (bindData[3]) { |
1713 | bindData[3] = slice(bindData[3]); |
1714 | } |
1715 | // set `thisBinding` is not previously bound |
1716 | if (isBind && !(bindData[1] & 1)) { |
1717 | bindData[4] = thisArg; |
1718 | } |
1719 | // set if previously bound but not currently (subsequent curried functions) |
1720 | if (!isBind && bindData[1] & 1) { |
1721 | bitmask |= 8; |
1722 | } |
1723 | // set curried arity if not yet set |
1724 | if (isCurry && !(bindData[1] & 4)) { |
1725 | bindData[5] = arity; |
1726 | } |
1727 | // append partial left arguments |
1728 | if (isPartial) { |
1729 | push.apply(bindData[2] || (bindData[2] = []), partialArgs); |
1730 | } |
1731 | // append partial right arguments |
1732 | if (isPartialRight) { |
1733 | unshift.apply(bindData[3] || (bindData[3] = []), partialRightArgs); |
1734 | } |
1735 | // merge flags |
1736 | bindData[1] |= bitmask; |
1737 | return createWrapper.apply(null, bindData); |
1738 | } |
1739 | // fast path for `_.bind` |
1740 | var creater = (bitmask == 1 || bitmask === 17) ? baseBind : baseCreateWrapper; |
1741 | return creater([func, bitmask, partialArgs, partialRightArgs, thisArg, arity]); |
1742 | } |
1743 | |
1744 | /** |
1745 | * Creates compiled iteration functions. |
1746 | * |
1747 | * @private |
1748 | * @param {...Object} [options] The compile options object(s). |
1749 | * @param {string} [options.array] Code to determine if the iterable is an array or array-like. |
1750 | * @param {boolean} [options.useHas] Specify using `hasOwnProperty` checks in the object loop. |
1751 | * @param {Function} [options.keys] A reference to `_.keys` for use in own property iteration. |
1752 | * @param {string} [options.args] A comma separated string of iteration function arguments. |
1753 | * @param {string} [options.top] Code to execute before the iteration branches. |
1754 | * @param {string} [options.loop] Code to execute in the object loop. |
1755 | * @param {string} [options.bottom] Code to execute after the iteration branches. |
1756 | * @returns {Function} Returns the compiled function. |
1757 | */ |
1758 | function createIterator() { |
1759 | // data properties |
1760 | iteratorData.shadowedProps = shadowedProps; |
1761 | iteratorData.support = support; |
1762 | |
1763 | // iterator options |
1764 | iteratorData.array = iteratorData.bottom = iteratorData.loop = iteratorData.top = ''; |
1765 | iteratorData.init = 'iterable'; |
1766 | iteratorData.useHas = true; |
1767 | |
1768 | // merge options into a template data object |
1769 | for (var object, index = 0; object = arguments[index]; index++) { |
1770 | for (var key in object) { |
1771 | iteratorData[key] = object[key]; |
1772 | } |
1773 | } |
1774 | var args = iteratorData.args; |
1775 | iteratorData.firstArg = /^[^,]+/.exec(args)[0]; |
1776 | |
1777 | // create the function factory |
1778 | var factory = Function( |
1779 | 'baseCreateCallback, errorClass, errorProto, hasOwnProperty, ' + |
1780 | 'indicatorObject, isArguments, isArray, isString, keys, objectProto, ' + |
1781 | 'objectTypes, nonEnumProps, stringClass, stringProto, toString', |
1782 | 'return function(' + args + ') {\n' + iteratorTemplate(iteratorData) + '\n}' |
1783 | ); |
1784 | |
1785 | // return the compiled function |
1786 | return factory( |
1787 | baseCreateCallback, errorClass, errorProto, hasOwnProperty, |
1788 | indicatorObject, isArguments, isArray, isString, iteratorData.keys, objectProto, |
1789 | objectTypes, nonEnumProps, stringClass, stringProto, toString |
1790 | ); |
1791 | } |
1792 | |
1793 | /** |
1794 | * Used by `escape` to convert characters to HTML entities. |
1795 | * |
1796 | * @private |
1797 | * @param {string} match The matched character to escape. |
1798 | * @returns {string} Returns the escaped character. |
1799 | */ |
1800 | function escapeHtmlChar(match) { |
1801 | return htmlEscapes[match]; |
1802 | } |
1803 | |
1804 | /** |
1805 | * Gets the appropriate "indexOf" function. If the `_.indexOf` method is |
1806 | * customized, this method returns the custom method, otherwise it returns |
1807 | * the `baseIndexOf` function. |
1808 | * |
1809 | * @private |
1810 | * @returns {Function} Returns the "indexOf" function. |
1811 | */ |
1812 | function getIndexOf() { |
1813 | var result = (result = lodash.indexOf) === indexOf ? baseIndexOf : result; |
1814 | return result; |
1815 | } |
1816 | |
1817 | /** |
1818 | * Checks if `value` is a native function. |
1819 | * |
1820 | * @private |
1821 | * @param {*} value The value to check. |
1822 | * @returns {boolean} Returns `true` if the `value` is a native function, else `false`. |
1823 | */ |
1824 | function isNative(value) { |
1825 | return typeof value == 'function' && reNative.test(value); |
1826 | } |
1827 | |
1828 | /** |
1829 | * Sets `this` binding data on a given function. |
1830 | * |
1831 | * @private |
1832 | * @param {Function} func The function to set data on. |
1833 | * @param {Array} value The data array to set. |
1834 | */ |
1835 | var setBindData = !defineProperty ? noop : function(func, value) { |
1836 | descriptor.value = value; |
1837 | defineProperty(func, '__bindData__', descriptor); |
1838 | }; |
1839 | |
1840 | /** |
1841 | * A fallback implementation of `isPlainObject` which checks if a given value |
1842 | * is an object created by the `Object` constructor, assuming objects created |
1843 | * by the `Object` constructor have no inherited enumerable properties and that |
1844 | * there are no `Object.prototype` extensions. |
1845 | * |
1846 | * @private |
1847 | * @param {*} value The value to check. |
1848 | * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. |
1849 | */ |
1850 | function shimIsPlainObject(value) { |
1851 | var ctor, |
1852 | result; |
1853 | |
1854 | // avoid non Object objects, `arguments` objects, and DOM elements |
1855 | if (!(value && toString.call(value) == objectClass) || |
1856 | (ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor)) || |
1857 | (!support.argsClass && isArguments(value)) || |
1858 | (!support.nodeClass && isNode(value))) { |
1859 | return false; |
1860 | } |
1861 | // IE < 9 iterates inherited properties before own properties. If the first |
1862 | // iterated property is an object's own property then there are no inherited |
1863 | // enumerable properties. |
1864 | if (support.ownLast) { |
1865 | forIn(value, function(value, key, object) { |
1866 | result = hasOwnProperty.call(object, key); |
1867 | return false; |
1868 | }); |
1869 | return result !== false; |
1870 | } |
1871 | // In most environments an object's own properties are iterated before |
1872 | // its inherited properties. If the last iterated property is an object's |
1873 | // own property then there are no inherited enumerable properties. |
1874 | forIn(value, function(value, key) { |
1875 | result = key; |
1876 | }); |
1877 | return typeof result == 'undefined' || hasOwnProperty.call(value, result); |
1878 | } |
1879 | |
1880 | /** |
1881 | * Used by `unescape` to convert HTML entities to characters. |
1882 | * |
1883 | * @private |
1884 | * @param {string} match The matched character to unescape. |
1885 | * @returns {string} Returns the unescaped character. |
1886 | */ |
1887 | function unescapeHtmlChar(match) { |
1888 | return htmlUnescapes[match]; |
1889 | } |
1890 | |
1891 | /*--------------------------------------------------------------------------*/ |
1892 | |
1893 | /** |
1894 | * Checks if `value` is an `arguments` object. |
1895 | * |
1896 | * @static |
1897 | * @memberOf _ |
1898 | * @category Objects |
1899 | * @param {*} value The value to check. |
1900 | * @returns {boolean} Returns `true` if the `value` is an `arguments` object, else `false`. |
1901 | * @example |
1902 | * |
1903 | * (function() { return _.isArguments(arguments); })(1, 2, 3); |
1904 | * // => true |
1905 | * |
1906 | * _.isArguments([1, 2, 3]); |
1907 | * // => false |
1908 | */ |
1909 | function isArguments(value) { |
1910 | return value && typeof value == 'object' && typeof value.length == 'number' && |
1911 | toString.call(value) == argsClass || false; |
1912 | } |
1913 | // fallback for browsers that can't detect `arguments` objects by [[Class]] |
1914 | if (!support.argsClass) { |
1915 | isArguments = function(value) { |
1916 | return value && typeof value == 'object' && typeof value.length == 'number' && |
1917 | hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee') || false; |
1918 | }; |
1919 | } |
1920 | |
1921 | /** |
1922 | * Checks if `value` is an array. |
1923 | * |
1924 | * @static |
1925 | * @memberOf _ |
1926 | * @type Function |
1927 | * @category Objects |
1928 | * @param {*} value The value to check. |
1929 | * @returns {boolean} Returns `true` if the `value` is an array, else `false`. |
1930 | * @example |
1931 | * |
1932 | * (function() { return _.isArray(arguments); })(); |
1933 | * // => false |
1934 | * |
1935 | * _.isArray([1, 2, 3]); |
1936 | * // => true |
1937 | */ |
1938 | var isArray = nativeIsArray || function(value) { |
1939 | return value && typeof value == 'object' && typeof value.length == 'number' && |
1940 | toString.call(value) == arrayClass || false; |
1941 | }; |
1942 | |
1943 | /** |
1944 | * A fallback implementation of `Object.keys` which produces an array of the |
1945 | * given object's own enumerable property names. |
1946 | * |
1947 | * @private |
1948 | * @type Function |
1949 | * @param {Object} object The object to inspect. |
1950 | * @returns {Array} Returns an array of property names. |
1951 | */ |
1952 | var shimKeys = createIterator({ |
1953 | 'args': 'object', |
1954 | 'init': '[]', |
1955 | 'top': 'if (!(objectTypes[typeof object])) return result', |
1956 | 'loop': 'result.push(index)' |
1957 | }); |
1958 | |
1959 | /** |
1960 | * Creates an array composed of the own enumerable property names of an object. |
1961 | * |
1962 | * @static |
1963 | * @memberOf _ |
1964 | * @category Objects |
1965 | * @param {Object} object The object to inspect. |
1966 | * @returns {Array} Returns an array of property names. |
1967 | * @example |
1968 | * |
1969 | * _.keys({ 'one': 1, 'two': 2, 'three': 3 }); |
1970 | * // => ['one', 'two', 'three'] (property order is not guaranteed across environments) |
1971 | */ |
1972 | var keys = !nativeKeys ? shimKeys : function(object) { |
1973 | if (!isObject(object)) { |
1974 | return []; |
1975 | } |
1976 | if ((support.enumPrototypes && typeof object == 'function') || |
1977 | (support.nonEnumArgs && object.length && isArguments(object))) { |
1978 | return shimKeys(object); |
1979 | } |
1980 | return nativeKeys(object); |
1981 | }; |
1982 | |
1983 | /** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */ |
1984 | var eachIteratorOptions = { |
1985 | 'args': 'collection, callback, thisArg', |
1986 | 'top': "callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3)", |
1987 | 'array': "typeof length == 'number'", |
1988 | 'keys': keys, |
1989 | 'loop': 'if (callback(iterable[index], index, collection) === false) return result' |
1990 | }; |
1991 | |
1992 | /** Reusable iterator options for `assign` and `defaults` */ |
1993 | var defaultsIteratorOptions = { |
1994 | 'args': 'object, source, guard', |
1995 | 'top': |
1996 | 'var args = arguments,\n' + |
1997 | ' argsIndex = 0,\n' + |
1998 | " argsLength = typeof guard == 'number' ? 2 : args.length;\n" + |
1999 | 'while (++argsIndex < argsLength) {\n' + |
2000 | ' iterable = args[argsIndex];\n' + |
2001 | ' if (iterable && objectTypes[typeof iterable]) {', |
2002 | 'keys': keys, |
2003 | 'loop': "if (typeof result[index] == 'undefined') result[index] = iterable[index]", |
2004 | 'bottom': ' }\n}' |
2005 | }; |
2006 | |
2007 | /** Reusable iterator options for `forIn` and `forOwn` */ |
2008 | var forOwnIteratorOptions = { |
2009 | 'top': 'if (!objectTypes[typeof iterable]) return result;\n' + eachIteratorOptions.top, |
2010 | 'array': false |
2011 | }; |
2012 | |
2013 | /** |
2014 | * Used to convert characters to HTML entities: |
2015 | * |
2016 | * Though the `>` character is escaped for symmetry, characters like `>` and `/` |
2017 | * don't require escaping in HTML and have no special meaning unless they're part |
2018 | * of a tag or an unquoted attribute value. |
2019 | * http://mathiasbynens.be/notes/ambiguous-ampersands (under "semi-related fun fact") |
2020 | */ |
2021 | var htmlEscapes = { |
2022 | '&': '&', |
2023 | '<': '<', |
2024 | '>': '>', |
2025 | '"': '"', |
2026 | "'": ''' |
2027 | }; |
2028 | |
2029 | /** Used to convert HTML entities to characters */ |
2030 | var htmlUnescapes = invert(htmlEscapes); |
2031 | |
2032 | /** Used to match HTML entities and HTML characters */ |
2033 | var reEscapedHtml = RegExp('(' + keys(htmlUnescapes).join('|') + ')', 'g'), |
2034 | reUnescapedHtml = RegExp('[' + keys(htmlEscapes).join('') + ']', 'g'); |
2035 | |
2036 | /** |
2037 | * A function compiled to iterate `arguments` objects, arrays, objects, and |
2038 | * strings consistenly across environments, executing the callback for each |
2039 | * element in the collection. The callback is bound to `thisArg` and invoked |
2040 | * with three arguments; (value, index|key, collection). Callbacks may exit |
2041 | * iteration early by explicitly returning `false`. |
2042 | * |
2043 | * @private |
2044 | * @type Function |
2045 | * @param {Array|Object|string} collection The collection to iterate over. |
2046 | * @param {Function} [callback=identity] The function called per iteration. |
2047 | * @param {*} [thisArg] The `this` binding of `callback`. |
2048 | * @returns {Array|Object|string} Returns `collection`. |
2049 | */ |
2050 | var baseEach = createIterator(eachIteratorOptions); |
2051 | |
2052 | /*--------------------------------------------------------------------------*/ |
2053 | |
2054 | /** |
2055 | * Assigns own enumerable properties of source object(s) to the destination |
2056 | * object. Subsequent sources will overwrite property assignments of previous |
2057 | * sources. If a callback is provided it will be executed to produce the |
2058 | * assigned values. The callback is bound to `thisArg` and invoked with two |
2059 | * arguments; (objectValue, sourceValue). |
2060 | * |
2061 | * @static |
2062 | * @memberOf _ |
2063 | * @type Function |
2064 | * @alias extend |
2065 | * @category Objects |
2066 | * @param {Object} object The destination object. |
2067 | * @param {...Object} [source] The source objects. |
2068 | * @param {Function} [callback] The function to customize assigning values. |
2069 | * @param {*} [thisArg] The `this` binding of `callback`. |
2070 | * @returns {Object} Returns the destination object. |
2071 | * @example |
2072 | * |
2073 | * _.assign({ 'name': 'fred' }, { 'employer': 'slate' }); |
2074 | * // => { 'name': 'fred', 'employer': 'slate' } |
2075 | * |
2076 | * var defaults = _.partialRight(_.assign, function(a, b) { |
2077 | * return typeof a == 'undefined' ? b : a; |
2078 | * }); |
2079 | * |
2080 | * var object = { 'name': 'barney' }; |
2081 | * defaults(object, { 'name': 'fred', 'employer': 'slate' }); |
2082 | * // => { 'name': 'barney', 'employer': 'slate' } |
2083 | */ |
2084 | var assign = createIterator(defaultsIteratorOptions, { |
2085 | 'top': |
2086 | defaultsIteratorOptions.top.replace(';', |
2087 | ';\n' + |
2088 | "if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {\n" + |
2089 | ' var callback = baseCreateCallback(args[--argsLength - 1], args[argsLength--], 2);\n' + |
2090 | "} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {\n" + |
2091 | ' callback = args[--argsLength];\n' + |
2092 | '}' |
2093 | ), |
2094 | 'loop': 'result[index] = callback ? callback(result[index], iterable[index]) : iterable[index]' |
2095 | }); |
2096 | |
2097 | /** |
2098 | * Creates a clone of `value`. If `isDeep` is `true` nested objects will also |
2099 | * be cloned, otherwise they will be assigned by reference. If a callback |
2100 | * is provided it will be executed to produce the cloned values. If the |
2101 | * callback returns `undefined` cloning will be handled by the method instead. |
2102 | * The callback is bound to `thisArg` and invoked with one argument; (value). |
2103 | * |
2104 | * @static |
2105 | * @memberOf _ |
2106 | * @category Objects |
2107 | * @param {*} value The value to clone. |
2108 | * @param {boolean} [isDeep=false] Specify a deep clone. |
2109 | * @param {Function} [callback] The function to customize cloning values. |
2110 | * @param {*} [thisArg] The `this` binding of `callback`. |
2111 | * @returns {*} Returns the cloned value. |
2112 | * @example |
2113 | * |
2114 | * var characters = [ |
2115 | * { 'name': 'barney', 'age': 36 }, |
2116 | * { 'name': 'fred', 'age': 40 } |
2117 | * ]; |
2118 | * |
2119 | * var shallow = _.clone(characters); |
2120 | * shallow[0] === characters[0]; |
2121 | * // => true |
2122 | * |
2123 | * var deep = _.clone(characters, true); |
2124 | * deep[0] === characters[0]; |
2125 | * // => false |
2126 | * |
2127 | * _.mixin({ |
2128 | * 'clone': _.partialRight(_.clone, function(value) { |
2129 | * return _.isElement(value) ? value.cloneNode(false) : undefined; |
2130 | * }) |
2131 | * }); |
2132 | * |
2133 | * var clone = _.clone(document.body); |
2134 | * clone.childNodes.length; |
2135 | * // => 0 |
2136 | */ |
2137 | function clone(value, isDeep, callback, thisArg) { |
2138 | // allows working with "Collections" methods without using their `index` |
2139 | // and `collection` arguments for `isDeep` and `callback` |
2140 | if (typeof isDeep != 'boolean' && isDeep != null) { |
2141 | thisArg = callback; |
2142 | callback = isDeep; |
2143 | isDeep = false; |
2144 | } |
2145 | return baseClone(value, isDeep, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1)); |
2146 | } |
2147 | |
2148 | /** |
2149 | * Creates a deep clone of `value`. If a callback is provided it will be |
2150 | * executed to produce the cloned values. If the callback returns `undefined` |
2151 | * cloning will be handled by the method instead. The callback is bound to |
2152 | * `thisArg` and invoked with one argument; (value). |
2153 | * |
2154 | * Note: This method is loosely based on the structured clone algorithm. Functions |
2155 | * and DOM nodes are **not** cloned. The enumerable properties of `arguments` objects and |
2156 | * objects created by constructors other than `Object` are cloned to plain `Object` objects. |
2157 | * See http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm. |
2158 | * |
2159 | * @static |
2160 | * @memberOf _ |
2161 | * @category Objects |
2162 | * @param {*} value The value to deep clone. |
2163 | * @param {Function} [callback] The function to customize cloning values. |
2164 | * @param {*} [thisArg] The `this` binding of `callback`. |
2165 | * @returns {*} Returns the deep cloned value. |
2166 | * @example |
2167 | * |
2168 | * var characters = [ |
2169 | * { 'name': 'barney', 'age': 36 }, |
2170 | * { 'name': 'fred', 'age': 40 } |
2171 | * ]; |
2172 | * |
2173 | * var deep = _.cloneDeep(characters); |
2174 | * deep[0] === characters[0]; |
2175 | * // => false |
2176 | * |
2177 | * var view = { |
2178 | * 'label': 'docs', |
2179 | * 'node': element |
2180 | * }; |
2181 | * |
2182 | * var clone = _.cloneDeep(view, function(value) { |
2183 | * return _.isElement(value) ? value.cloneNode(true) : undefined; |
2184 | * }); |
2185 | * |
2186 | * clone.node == view.node; |
2187 | * // => false |
2188 | */ |
2189 | function cloneDeep(value, callback, thisArg) { |
2190 | return baseClone(value, true, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1)); |
2191 | } |
2192 | |
2193 | /** |
2194 | * Creates an object that inherits from the given `prototype` object. If a |
2195 | * `properties` object is provided its own enumerable properties are assigned |
2196 | * to the created object. |
2197 | * |
2198 | * @static |
2199 | * @memberOf _ |
2200 | * @category Objects |
2201 | * @param {Object} prototype The object to inherit from. |
2202 | * @param {Object} [properties] The properties to assign to the object. |
2203 | * @returns {Object} Returns the new object. |
2204 | * @example |
2205 | * |
2206 | * function Shape() { |
2207 | * this.x = 0; |
2208 | * this.y = 0; |
2209 | * } |
2210 | * |
2211 | * function Circle() { |
2212 | * Shape.call(this); |
2213 | * } |
2214 | * |
2215 | * Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle }); |
2216 | * |
2217 | * var circle = new Circle; |
2218 | * circle instanceof Circle; |
2219 | * // => true |
2220 | * |
2221 | * circle instanceof Shape; |
2222 | * // => true |
2223 | */ |
2224 | function create(prototype, properties) { |
2225 | var result = baseCreate(prototype); |
2226 | return properties ? assign(result, properties) : result; |
2227 | } |
2228 | |
2229 | /** |
2230 | * Assigns own enumerable properties of source object(s) to the destination |
2231 | * object for all destination properties that resolve to `undefined`. Once a |
2232 | * property is set, additional defaults of the same property will be ignored. |
2233 | * |
2234 | * @static |
2235 | * @memberOf _ |
2236 | * @type Function |
2237 | * @category Objects |
2238 | * @param {Object} object The destination object. |
2239 | * @param {...Object} [source] The source objects. |
2240 | * @param- {Object} [guard] Allows working with `_.reduce` without using its |
2241 | * `key` and `object` arguments as sources. |
2242 | * @returns {Object} Returns the destination object. |
2243 | * @example |
2244 | * |
2245 | * var object = { 'name': 'barney' }; |
2246 | * _.defaults(object, { 'name': 'fred', 'employer': 'slate' }); |
2247 | * // => { 'name': 'barney', 'employer': 'slate' } |
2248 | */ |
2249 | var defaults = createIterator(defaultsIteratorOptions); |
2250 | |
2251 | /** |
2252 | * This method is like `_.findIndex` except that it returns the key of the |
2253 | * first element that passes the callback check, instead of the element itself. |
2254 | * |
2255 | * If a property name is provided for `callback` the created "_.pluck" style |
2256 | * callback will return the property value of the given element. |
2257 | * |
2258 | * If an object is provided for `callback` the created "_.where" style callback |
2259 | * will return `true` for elements that have the properties of the given object, |
2260 | * else `false`. |
2261 | * |
2262 | * @static |
2263 | * @memberOf _ |
2264 | * @category Objects |
2265 | * @param {Object} object The object to search. |
2266 | * @param {Function|Object|string} [callback=identity] The function called per |
2267 | * iteration. If a property name or object is provided it will be used to |
2268 | * create a "_.pluck" or "_.where" style callback, respectively. |
2269 | * @param {*} [thisArg] The `this` binding of `callback`. |
2270 | * @returns {string|undefined} Returns the key of the found element, else `undefined`. |
2271 | * @example |
2272 | * |
2273 | * var characters = { |
2274 | * 'barney': { 'age': 36, 'blocked': false }, |
2275 | * 'fred': { 'age': 40, 'blocked': true }, |
2276 | * 'pebbles': { 'age': 1, 'blocked': false } |
2277 | * }; |
2278 | * |
2279 | * _.findKey(characters, function(chr) { |
2280 | * return chr.age < 40; |
2281 | * }); |
2282 | * // => 'barney' (property order is not guaranteed across environments) |
2283 | * |
2284 | * // using "_.where" callback shorthand |
2285 | * _.findKey(characters, { 'age': 1 }); |
2286 | * // => 'pebbles' |
2287 | * |
2288 | * // using "_.pluck" callback shorthand |
2289 | * _.findKey(characters, 'blocked'); |
2290 | * // => 'fred' |
2291 | */ |
2292 | function findKey(object, callback, thisArg) { |
2293 | var result; |
2294 | callback = lodash.createCallback(callback, thisArg, 3); |
2295 | forOwn(object, function(value, key, object) { |
2296 | if (callback(value, key, object)) { |
2297 | result = key; |
2298 | return false; |
2299 | } |
2300 | }); |
2301 | return result; |
2302 | } |
2303 | |
2304 | /** |
2305 | * This method is like `_.findKey` except that it iterates over elements |
2306 | * of a `collection` in the opposite order. |
2307 | * |
2308 | * If a property name is provided for `callback` the created "_.pluck" style |
2309 | * callback will return the property value of the given element. |
2310 | * |
2311 | * If an object is provided for `callback` the created "_.where" style callback |
2312 | * will return `true` for elements that have the properties of the given object, |
2313 | * else `false`. |
2314 | * |
2315 | * @static |
2316 | * @memberOf _ |
2317 | * @category Objects |
2318 | * @param {Object} object The object to search. |
2319 | * @param {Function|Object|string} [callback=identity] The function called per |
2320 | * iteration. If a property name or object is provided it will be used to |
2321 | * create a "_.pluck" or "_.where" style callback, respectively. |
2322 | * @param {*} [thisArg] The `this` binding of `callback`. |
2323 | * @returns {string|undefined} Returns the key of the found element, else `undefined`. |
2324 | * @example |
2325 | * |
2326 | * var characters = { |
2327 | * 'barney': { 'age': 36, 'blocked': true }, |
2328 | * 'fred': { 'age': 40, 'blocked': false }, |
2329 | * 'pebbles': { 'age': 1, 'blocked': true } |
2330 | * }; |
2331 | * |
2332 | * _.findLastKey(characters, function(chr) { |
2333 | * return chr.age < 40; |
2334 | * }); |
2335 | * // => returns `pebbles`, assuming `_.findKey` returns `barney` |
2336 | * |
2337 | * // using "_.where" callback shorthand |
2338 | * _.findLastKey(characters, { 'age': 40 }); |
2339 | * // => 'fred' |
2340 | * |
2341 | * // using "_.pluck" callback shorthand |
2342 | * _.findLastKey(characters, 'blocked'); |
2343 | * // => 'pebbles' |
2344 | */ |
2345 | function findLastKey(object, callback, thisArg) { |
2346 | var result; |
2347 | callback = lodash.createCallback(callback, thisArg, 3); |
2348 | forOwnRight(object, function(value, key, object) { |
2349 | if (callback(value, key, object)) { |
2350 | result = key; |
2351 | return false; |
2352 | } |
2353 | }); |
2354 | return result; |
2355 | } |
2356 | |
2357 | /** |
2358 | * Iterates over own and inherited enumerable properties of an object, |
2359 | * executing the callback for each property. The callback is bound to `thisArg` |
2360 | * and invoked with three arguments; (value, key, object). Callbacks may exit |
2361 | * iteration early by explicitly returning `false`. |
2362 | * |
2363 | * @static |
2364 | * @memberOf _ |
2365 | * @type Function |
2366 | * @category Objects |
2367 | * @param {Object} object The object to iterate over. |
2368 | * @param {Function} [callback=identity] The function called per iteration. |
2369 | * @param {*} [thisArg] The `this` binding of `callback`. |
2370 | * @returns {Object} Returns `object`. |
2371 | * @example |
2372 | * |
2373 | * function Shape() { |
2374 | * this.x = 0; |
2375 | * this.y = 0; |
2376 | * } |
2377 | * |
2378 | * Shape.prototype.move = function(x, y) { |
2379 | * this.x += x; |
2380 | * this.y += y; |
2381 | * }; |
2382 | * |
2383 | * _.forIn(new Shape, function(value, key) { |
2384 | * console.log(key); |
2385 | * }); |
2386 | * // => logs 'x', 'y', and 'move' (property order is not guaranteed across environments) |
2387 | */ |
2388 | var forIn = createIterator(eachIteratorOptions, forOwnIteratorOptions, { |
2389 | 'useHas': false |
2390 | }); |
2391 | |
2392 | /** |
2393 | * This method is like `_.forIn` except that it iterates over elements |
2394 | * of a `collection` in the opposite order. |
2395 | * |
2396 | * @static |
2397 | * @memberOf _ |
2398 | * @category Objects |
2399 | * @param {Object} object The object to iterate over. |
2400 | * @param {Function} [callback=identity] The function called per iteration. |
2401 | * @param {*} [thisArg] The `this` binding of `callback`. |
2402 | * @returns {Object} Returns `object`. |
2403 | * @example |
2404 | * |
2405 | * function Shape() { |
2406 | * this.x = 0; |
2407 | * this.y = 0; |
2408 | * } |
2409 | * |
2410 | * Shape.prototype.move = function(x, y) { |
2411 | * this.x += x; |
2412 | * this.y += y; |
2413 | * }; |
2414 | * |
2415 | * _.forInRight(new Shape, function(value, key) { |
2416 | * console.log(key); |
2417 | * }); |
2418 | * // => logs 'move', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'move' |
2419 | */ |
2420 | function forInRight(object, callback, thisArg) { |
2421 | var pairs = []; |
2422 | |
2423 | forIn(object, function(value, key) { |
2424 | pairs.push(key, value); |
2425 | }); |
2426 | |
2427 | var length = pairs.length; |
2428 | callback = baseCreateCallback(callback, thisArg, 3); |
2429 | while (length--) { |
2430 | if (callback(pairs[length--], pairs[length], object) === false) { |
2431 | break; |
2432 | } |
2433 | } |
2434 | return object; |
2435 | } |
2436 | |
2437 | /** |
2438 | * Iterates over own enumerable properties of an object, executing the callback |
2439 | * for each property. The callback is bound to `thisArg` and invoked with three |
2440 | * arguments; (value, key, object). Callbacks may exit iteration early by |
2441 | * explicitly returning `false`. |
2442 | * |
2443 | * @static |
2444 | * @memberOf _ |
2445 | * @type Function |
2446 | * @category Objects |
2447 | * @param {Object} object The object to iterate over. |
2448 | * @param {Function} [callback=identity] The function called per iteration. |
2449 | * @param {*} [thisArg] The `this` binding of `callback`. |
2450 | * @returns {Object} Returns `object`. |
2451 | * @example |
2452 | * |
2453 | * _.forOwn({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) { |
2454 | * console.log(key); |
2455 | * }); |
2456 | * // => logs '0', '1', and 'length' (property order is not guaranteed across environments) |
2457 | */ |
2458 | var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions); |
2459 | |
2460 | /** |
2461 | * This method is like `_.forOwn` except that it iterates over elements |
2462 | * of a `collection` in the opposite order. |
2463 | * |
2464 | * @static |
2465 | * @memberOf _ |
2466 | * @category Objects |
2467 | * @param {Object} object The object to iterate over. |
2468 | * @param {Function} [callback=identity] The function called per iteration. |
2469 | * @param {*} [thisArg] The `this` binding of `callback`. |
2470 | * @returns {Object} Returns `object`. |
2471 | * @example |
2472 | * |
2473 | * _.forOwnRight({ '0': 'zero', '1': 'one', 'length': 2 }, function(num, key) { |
2474 | * console.log(key); |
2475 | * }); |
2476 | * // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length' |
2477 | */ |
2478 | function forOwnRight(object, callback, thisArg) { |
2479 | var props = keys(object), |
2480 | length = props.length; |
2481 | |
2482 | callback = baseCreateCallback(callback, thisArg, 3); |
2483 | while (length--) { |
2484 | var key = props[length]; |
2485 | if (callback(object[key], key, object) === false) { |
2486 | break; |
2487 | } |
2488 | } |
2489 | return object; |
2490 | } |
2491 | |
2492 | /** |
2493 | * Creates a sorted array of property names of all enumerable properties, |
2494 | * own and inherited, of `object` that have function values. |
2495 | * |
2496 | * @static |
2497 | * @memberOf _ |
2498 | * @alias methods |
2499 | * @category Objects |
2500 | * @param {Object} object The object to inspect. |
2501 | * @returns {Array} Returns an array of property names that have function values. |
2502 | * @example |
2503 | * |
2504 | * _.functions(_); |
2505 | * // => ['all', 'any', 'bind', 'bindAll', 'clone', 'compact', 'compose', ...] |
2506 | */ |
2507 | function functions(object) { |
2508 | var result = []; |
2509 | forIn(object, function(value, key) { |
2510 | if (isFunction(value)) { |
2511 | result.push(key); |
2512 | } |
2513 | }); |
2514 | return result.sort(); |
2515 | } |
2516 | |
2517 | /** |
2518 | * Checks if the specified property name exists as a direct property of `object`, |
2519 | * instead of an inherited property. |
2520 | * |
2521 | * @static |
2522 | * @memberOf _ |
2523 | * @category Objects |
2524 | * @param {Object} object The object to inspect. |
2525 | * @param {string} key The name of the property to check. |
2526 | * @returns {boolean} Returns `true` if key is a direct property, else `false`. |
2527 | * @example |
2528 | * |
2529 | * _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); |
2530 | * // => true |
2531 | */ |
2532 | function has(object, key) { |
2533 | return object ? hasOwnProperty.call(object, key) : false; |
2534 | } |
2535 | |
2536 | /** |
2537 | * Creates an object composed of the inverted keys and values of the given object. |
2538 | * |
2539 | * @static |
2540 | * @memberOf _ |
2541 | * @category Objects |
2542 | * @param {Object} object The object to invert. |
2543 | * @returns {Object} Returns the created inverted object. |
2544 | * @example |
2545 | * |
2546 | * _.invert({ 'first': 'fred', 'second': 'barney' }); |
2547 | * // => { 'fred': 'first', 'barney': 'second' } |
2548 | */ |
2549 | function invert(object) { |
2550 | var index = -1, |
2551 | props = keys(object), |
2552 | length = props.length, |
2553 | result = {}; |
2554 | |
2555 | while (++index < length) { |
2556 | var key = props[index]; |
2557 | result[object[key]] = key; |
2558 | } |
2559 | return result; |
2560 | } |
2561 | |
2562 | /** |
2563 | * Checks if `value` is a boolean value. |
2564 | * |
2565 | * @static |
2566 | * @memberOf _ |
2567 | * @category Objects |
2568 | * @param {*} value The value to check. |
2569 | * @returns {boolean} Returns `true` if the `value` is a boolean value, else `false`. |
2570 | * @example |
2571 | * |
2572 | * _.isBoolean(null); |
2573 | * // => false |
2574 | */ |
2575 | function isBoolean(value) { |
2576 | return value === true || value === false || |
2577 | value && typeof value == 'object' && toString.call(value) == boolClass || false; |
2578 | } |
2579 | |
2580 | /** |
2581 | * Checks if `value` is a date. |
2582 | * |
2583 | * @static |
2584 | * @memberOf _ |
2585 | * @category Objects |
2586 | * @param {*} value The value to check. |
2587 | * @returns {boolean} Returns `true` if the `value` is a date, else `false`. |
2588 | * @example |
2589 | * |
2590 | * _.isDate(new Date); |
2591 | * // => true |
2592 | */ |
2593 | function isDate(value) { |
2594 | return value && typeof value == 'object' && toString.call(value) == dateClass || false; |
2595 | } |
2596 | |
2597 | /** |
2598 | * Checks if `value` is a DOM element. |
2599 | * |
2600 | * @static |
2601 | * @memberOf _ |
2602 | * @category Objects |
2603 | * @param {*} value The value to check. |
2604 | * @returns {boolean} Returns `true` if the `value` is a DOM element, else `false`. |
2605 | * @example |
2606 | * |
2607 | * _.isElement(document.body); |
2608 | * // => true |
2609 | */ |
2610 | function isElement(value) { |
2611 | return value && value.nodeType === 1 || false; |
2612 | } |
2613 | |
2614 | /** |
2615 | * Checks if `value` is empty. Arrays, strings, or `arguments` objects with a |
2616 | * length of `0` and objects with no own enumerable properties are considered |
2617 | * "empty". |
2618 | * |
2619 | * @static |
2620 | * @memberOf _ |
2621 | * @category Objects |
2622 | * @param {Array|Object|string} value The value to inspect. |
2623 | * @returns {boolean} Returns `true` if the `value` is empty, else `false`. |
2624 | * @example |
2625 | * |
2626 | * _.isEmpty([1, 2, 3]); |
2627 | * // => false |
2628 | * |
2629 | * _.isEmpty({}); |
2630 | * // => true |
2631 | * |
2632 | * _.isEmpty(''); |
2633 | * // => true |
2634 | */ |
2635 | function isEmpty(value) { |
2636 | var result = true; |
2637 | if (!value) { |
2638 | return result; |
2639 | } |
2640 | var className = toString.call(value), |
2641 | length = value.length; |
2642 | |
2643 | if ((className == arrayClass || className == stringClass || |
2644 | (support.argsClass ? className == argsClass : isArguments(value))) || |
2645 | (className == objectClass && typeof length == 'number' && isFunction(value.splice))) { |
2646 | return !length; |
2647 | } |
2648 | forOwn(value, function() { |
2649 | return (result = false); |
2650 | }); |
2651 | return result; |
2652 | } |
2653 | |
2654 | /** |
2655 | * Performs a deep comparison between two values to determine if they are |
2656 | * equivalent to each other. If a callback is provided it will be executed |
2657 | * to compare values. If the callback returns `undefined` comparisons will |
2658 | * be handled by the method instead. The callback is bound to `thisArg` and |
2659 | * invoked with two arguments; (a, b). |
2660 | * |
2661 | * @static |
2662 | * @memberOf _ |
2663 | * @category Objects |
2664 | * @param {*} a The value to compare. |
2665 | * @param {*} b The other value to compare. |
2666 | * @param {Function} [callback] The function to customize comparing values. |
2667 | * @param {*} [thisArg] The `this` binding of `callback`. |
2668 | * @returns {boolean} Returns `true` if the values are equivalent, else `false`. |
2669 | * @example |
2670 | * |
2671 | * var object = { 'name': 'fred' }; |
2672 | * var copy = { 'name': 'fred' }; |
2673 | * |
2674 | * object == copy; |
2675 | * // => false |
2676 | * |
2677 | * _.isEqual(object, copy); |
2678 | * // => true |
2679 | * |
2680 | * var words = ['hello', 'goodbye']; |
2681 | * var otherWords = ['hi', 'goodbye']; |
2682 | * |
2683 | * _.isEqual(words, otherWords, function(a, b) { |
2684 | * var reGreet = /^(?:hello|hi)$/i, |
2685 | * aGreet = _.isString(a) && reGreet.test(a), |
2686 | * bGreet = _.isString(b) && reGreet.test(b); |
2687 | * |
2688 | * return (aGreet || bGreet) ? (aGreet == bGreet) : undefined; |
2689 | * }); |
2690 | * // => true |
2691 | */ |
2692 | function isEqual(a, b, callback, thisArg) { |
2693 | return baseIsEqual(a, b, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 2)); |
2694 | } |
2695 | |
2696 | /** |
2697 | * Checks if `value` is, or can be coerced to, a finite number. |
2698 | * |
2699 | * Note: This is not the same as native `isFinite` which will return true for |
2700 | * booleans and empty strings. See http://es5.github.io/#x15.1.2.5. |
2701 | * |
2702 | * @static |
2703 | * @memberOf _ |
2704 | * @category Objects |
2705 | * @param {*} value The value to check. |
2706 | * @returns {boolean} Returns `true` if the `value` is finite, else `false`. |
2707 | * @example |
2708 | * |
2709 | * _.isFinite(-101); |
2710 | * // => true |
2711 | * |
2712 | * _.isFinite('10'); |
2713 | * // => true |
2714 | * |
2715 | * _.isFinite(true); |
2716 | * // => false |
2717 | * |
2718 | * _.isFinite(''); |
2719 | * // => false |
2720 | * |
2721 | * _.isFinite(Infinity); |
2722 | * // => false |
2723 | */ |
2724 | function isFinite(value) { |
2725 | return nativeIsFinite(value) && !nativeIsNaN(parseFloat(value)); |
2726 | } |
2727 | |
2728 | /** |
2729 | * Checks if `value` is a function. |
2730 | * |
2731 | * @static |
2732 | * @memberOf _ |
2733 | * @category Objects |
2734 | * @param {*} value The value to check. |
2735 | * @returns {boolean} Returns `true` if the `value` is a function, else `false`. |
2736 | * @example |
2737 | * |
2738 | * _.isFunction(_); |
2739 | * // => true |
2740 | */ |
2741 | function isFunction(value) { |
2742 | return typeof value == 'function'; |
2743 | } |
2744 | // fallback for older versions of Chrome and Safari |
2745 | if (isFunction(/x/)) { |
2746 | isFunction = function(value) { |
2747 | return typeof value == 'function' && toString.call(value) == funcClass; |
2748 | }; |
2749 | } |
2750 | |
2751 | /** |
2752 | * Checks if `value` is the language type of Object. |
2753 | * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) |
2754 | * |
2755 | * @static |
2756 | * @memberOf _ |
2757 | * @category Objects |
2758 | * @param {*} value The value to check. |
2759 | * @returns {boolean} Returns `true` if the `value` is an object, else `false`. |
2760 | * @example |
2761 | * |
2762 | * _.isObject({}); |
2763 | * // => true |
2764 | * |
2765 | * _.isObject([1, 2, 3]); |
2766 | * // => true |
2767 | * |
2768 | * _.isObject(1); |
2769 | * // => false |
2770 | */ |
2771 | function isObject(value) { |
2772 | // check if the value is the ECMAScript language type of Object |
2773 | // http://es5.github.io/#x8 |
2774 | // and avoid a V8 bug |
2775 | // http://code.google.com/p/v8/issues/detail?id=2291 |
2776 | return !!(value && objectTypes[typeof value]); |
2777 | } |
2778 | |
2779 | /** |
2780 | * Checks if `value` is `NaN`. |
2781 | * |
2782 | * Note: This is not the same as native `isNaN` which will return `true` for |
2783 | * `undefined` and other non-numeric values. See http://es5.github.io/#x15.1.2.4. |
2784 | * |
2785 | * @static |
2786 | * @memberOf _ |
2787 | * @category Objects |
2788 | * @param {*} value The value to check. |
2789 | * @returns {boolean} Returns `true` if the `value` is `NaN`, else `false`. |
2790 | * @example |
2791 | * |
2792 | * _.isNaN(NaN); |
2793 | * // => true |
2794 | * |
2795 | * _.isNaN(new Number(NaN)); |
2796 | * // => true |
2797 | * |
2798 | * isNaN(undefined); |
2799 | * // => true |
2800 | * |
2801 | * _.isNaN(undefined); |
2802 | * // => false |
2803 | */ |
2804 | function isNaN(value) { |
2805 | // `NaN` as a primitive is the only value that is not equal to itself |
2806 | // (perform the [[Class]] check first to avoid errors with some host objects in IE) |
2807 | return isNumber(value) && value != +value; |
2808 | } |
2809 | |
2810 | /** |
2811 | * Checks if `value` is `null`. |
2812 | * |
2813 | * @static |
2814 | * @memberOf _ |
2815 | * @category Objects |
2816 | * @param {*} value The value to check. |
2817 | * @returns {boolean} Returns `true` if the `value` is `null`, else `false`. |
2818 | * @example |
2819 | * |
2820 | * _.isNull(null); |
2821 | * // => true |
2822 | * |
2823 | * _.isNull(undefined); |
2824 | * // => false |
2825 | */ |
2826 | function isNull(value) { |
2827 | return value === null; |
2828 | } |
2829 | |
2830 | /** |
2831 | * Checks if `value` is a number. |
2832 | * |
2833 | * Note: `NaN` is considered a number. See http://es5.github.io/#x8.5. |
2834 | * |
2835 | * @static |
2836 | * @memberOf _ |
2837 | * @category Objects |
2838 | * @param {*} value The value to check. |
2839 | * @returns {boolean} Returns `true` if the `value` is a number, else `false`. |
2840 | * @example |
2841 | * |
2842 | * _.isNumber(8.4 * 5); |
2843 | * // => true |
2844 | */ |
2845 | function isNumber(value) { |
2846 | return typeof value == 'number' || |
2847 | value && typeof value == 'object' && toString.call(value) == numberClass || false; |
2848 | } |
2849 | |
2850 | /** |
2851 | * Checks if `value` is an object created by the `Object` constructor. |
2852 | * |
2853 | * @static |
2854 | * @memberOf _ |
2855 | * @category Objects |
2856 | * @param {*} value The value to check. |
2857 | * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. |
2858 | * @example |
2859 | * |
2860 | * function Shape() { |
2861 | * this.x = 0; |
2862 | * this.y = 0; |
2863 | * } |
2864 | * |
2865 | * _.isPlainObject(new Shape); |
2866 | * // => false |
2867 | * |
2868 | * _.isPlainObject([1, 2, 3]); |
2869 | * // => false |
2870 | * |
2871 | * _.isPlainObject({ 'x': 0, 'y': 0 }); |
2872 | * // => true |
2873 | */ |
2874 | var isPlainObject = !getPrototypeOf ? shimIsPlainObject : function(value) { |
2875 | if (!(value && toString.call(value) == objectClass) || (!support.argsClass && isArguments(value))) { |
2876 | return false; |
2877 | } |
2878 | var valueOf = value.valueOf, |
2879 | objProto = isNative(valueOf) && (objProto = getPrototypeOf(valueOf)) && getPrototypeOf(objProto); |
2880 | |
2881 | return objProto |
2882 | ? (value == objProto || getPrototypeOf(value) == objProto) |
2883 | : shimIsPlainObject(value); |
2884 | }; |
2885 | |
2886 | /** |
2887 | * Checks if `value` is a regular expression. |
2888 | * |
2889 | * @static |
2890 | * @memberOf _ |
2891 | * @category Objects |
2892 | * @param {*} value The value to check. |
2893 | * @returns {boolean} Returns `true` if the `value` is a regular expression, else `false`. |
2894 | * @example |
2895 | * |
2896 | * _.isRegExp(/fred/); |
2897 | * // => true |
2898 | */ |
2899 | function isRegExp(value) { |
2900 | return value && objectTypes[typeof value] && toString.call(value) == regexpClass || false; |
2901 | } |
2902 | |
2903 | /** |
2904 | * Checks if `value` is a string. |
2905 | * |
2906 | * @static |
2907 | * @memberOf _ |
2908 | * @category Objects |
2909 | * @param {*} value The value to check. |
2910 | * @returns {boolean} Returns `true` if the `value` is a string, else `false`. |
2911 | * @example |
2912 | * |
2913 | * _.isString('fred'); |
2914 | * // => true |
2915 | */ |
2916 | function isString(value) { |
2917 | return typeof value == 'string' || |
2918 | value && typeof value == 'object' && toString.call(value) == stringClass || false; |
2919 | } |
2920 | |
2921 | /** |
2922 | * Checks if `value` is `undefined`. |
2923 | * |
2924 | * @static |
2925 | * @memberOf _ |
2926 | * @category Objects |
2927 | * @param {*} value The value to check. |
2928 | * @returns {boolean} Returns `true` if the `value` is `undefined`, else `false`. |
2929 | * @example |
2930 | * |
2931 | * _.isUndefined(void 0); |
2932 | * // => true |
2933 | */ |
2934 | function isUndefined(value) { |
2935 | return typeof value == 'undefined'; |
2936 | } |
2937 | |
2938 | /** |
2939 | * Creates an object with the same keys as `object` and values generated by |
2940 | * running each own enumerable property of `object` through the callback. |
2941 | * The callback is bound to `thisArg` and invoked with three arguments; |
2942 | * (value, key, object). |
2943 | * |
2944 | * If a property name is provided for `callback` the created "_.pluck" style |
2945 | * callback will return the property value of the given element. |
2946 | * |
2947 | * If an object is provided for `callback` the created "_.where" style callback |
2948 | * will return `true` for elements that have the properties of the given object, |
2949 | * else `false`. |
2950 | * |
2951 | * @static |
2952 | * @memberOf _ |
2953 | * @category Objects |
2954 | * @param {Object} object The object to iterate over. |
2955 | * @param {Function|Object|string} [callback=identity] The function called |
2956 | * per iteration. If a property name or object is provided it will be used |
2957 | * to create a "_.pluck" or "_.where" style callback, respectively. |
2958 | * @param {*} [thisArg] The `this` binding of `callback`. |
2959 | * @returns {Array} Returns a new object with values of the results of each `callback` execution. |
2960 | * @example |
2961 | * |
2962 | * _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; }); |
2963 | * // => { 'a': 3, 'b': 6, 'c': 9 } |
2964 | * |
2965 | * var characters = { |
2966 | * 'fred': { 'name': 'fred', 'age': 40 }, |
2967 | * 'pebbles': { 'name': 'pebbles', 'age': 1 } |
2968 | * }; |
2969 | * |
2970 | * // using "_.pluck" callback shorthand |
2971 | * _.mapValues(characters, 'age'); |
2972 | * // => { 'fred': 40, 'pebbles': 1 } |
2973 | */ |
2974 | function mapValues(object, callback, thisArg) { |
2975 | var result = {}; |
2976 | callback = lodash.createCallback(callback, thisArg, 3); |
2977 | |
2978 | forOwn(object, function(value, key, object) { |
2979 | result[key] = callback(value, key, object); |
2980 | }); |
2981 | return result; |
2982 | } |
2983 | |
2984 | /** |
2985 | * Recursively merges own enumerable properties of the source object(s), that |
2986 | * don't resolve to `undefined` into the destination object. Subsequent sources |
2987 | * will overwrite property assignments of previous sources. If a callback is |
2988 | * provided it will be executed to produce the merged values of the destination |
2989 | * and source properties. If the callback returns `undefined` merging will |
2990 | * be handled by the method instead. The callback is bound to `thisArg` and |
2991 | * invoked with two arguments; (objectValue, sourceValue). |
2992 | * |
2993 | * @static |
2994 | * @memberOf _ |
2995 | * @category Objects |
2996 | * @param {Object} object The destination object. |
2997 | * @param {...Object} [source] The source objects. |
2998 | * @param {Function} [callback] The function to customize merging properties. |
2999 | * @param {*} [thisArg] The `this` binding of `callback`. |
3000 | * @returns {Object} Returns the destination object. |
3001 | * @example |
3002 | * |
3003 | * var names = { |
3004 | * 'characters': [ |
3005 | * { 'name': 'barney' }, |
3006 | * { 'name': 'fred' } |
3007 | * ] |
3008 | * }; |
3009 | * |
3010 | * var ages = { |
3011 | * 'characters': [ |
3012 | * { 'age': 36 }, |
3013 | * { 'age': 40 } |
3014 | * ] |
3015 | * }; |
3016 | * |
3017 | * _.merge(names, ages); |
3018 | * // => { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] } |
3019 | * |
3020 | * var food = { |
3021 | * 'fruits': ['apple'], |
3022 | * 'vegetables': ['beet'] |
3023 | * }; |
3024 | * |
3025 | * var otherFood = { |
3026 | * 'fruits': ['banana'], |
3027 | * 'vegetables': ['carrot'] |
3028 | * }; |
3029 | * |
3030 | * _.merge(food, otherFood, function(a, b) { |
3031 | * return _.isArray(a) ? a.concat(b) : undefined; |
3032 | * }); |
3033 | * // => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] } |
3034 | */ |
3035 | function merge(object) { |
3036 | var args = arguments, |
3037 | length = 2; |
3038 | |
3039 | if (!isObject(object)) { |
3040 | return object; |
3041 | } |
3042 | // allows working with `_.reduce` and `_.reduceRight` without using |
3043 | // their `index` and `collection` arguments |
3044 | if (typeof args[2] != 'number') { |
3045 | length = args.length; |
3046 | } |
3047 | if (length > 3 && typeof args[length - 2] == 'function') { |
3048 | var callback = baseCreateCallback(args[--length - 1], args[length--], 2); |
3049 | } else if (length > 2 && typeof args[length - 1] == 'function') { |
3050 | callback = args[--length]; |
3051 | } |
3052 | var sources = slice(arguments, 1, length), |
3053 | index = -1, |
3054 | stackA = getArray(), |
3055 | stackB = getArray(); |
3056 | |
3057 | while (++index < length) { |
3058 | baseMerge(object, sources[index], callback, stackA, stackB); |
3059 | } |
3060 | releaseArray(stackA); |
3061 | releaseArray(stackB); |
3062 | return object; |
3063 | } |
3064 | |
3065 | /** |
3066 | * Creates a shallow clone of `object` excluding the specified properties. |
3067 | * Property names may be specified as individual arguments or as arrays of |
3068 | * property names. If a callback is provided it will be executed for each |
3069 | * property of `object` omitting the properties the callback returns truey |
3070 | * for. The callback is bound to `thisArg` and invoked with three arguments; |
3071 | * (value, key, object). |
3072 | * |
3073 | * @static |
3074 | * @memberOf _ |
3075 | * @category Objects |
3076 | * @param {Object} object The source object. |
3077 | * @param {Function|...string|string[]} [callback] The properties to omit or the |
3078 | * function called per iteration. |
3079 | * @param {*} [thisArg] The `this` binding of `callback`. |
3080 | * @returns {Object} Returns an object without the omitted properties. |
3081 | * @example |
3082 | * |
3083 | * _.omit({ 'name': 'fred', 'age': 40 }, 'age'); |
3084 | * // => { 'name': 'fred' } |
3085 | * |
3086 | * _.omit({ 'name': 'fred', 'age': 40 }, function(value) { |
3087 | * return typeof value == 'number'; |
3088 | * }); |
3089 | * // => { 'name': 'fred' } |
3090 | */ |
3091 | function omit(object, callback, thisArg) { |
3092 | var result = {}; |
3093 | if (typeof callback != 'function') { |
3094 | var props = []; |
3095 | forIn(object, function(value, key) { |
3096 | props.push(key); |
3097 | }); |
3098 | props = baseDifference(props, baseFlatten(arguments, true, false, 1)); |
3099 | |
3100 | var index = -1, |
3101 | length = props.length; |
3102 | |
3103 | while (++index < length) { |
3104 | var key = props[index]; |
3105 | result[key] = object[key]; |
3106 | } |
3107 | } else { |
3108 | callback = lodash.createCallback(callback, thisArg, 3); |
3109 | forIn(object, function(value, key, object) { |
3110 | if (!callback(value, key, object)) { |
3111 | result[key] = value; |
3112 | } |
3113 | }); |
3114 | } |
3115 | return result; |
3116 | } |
3117 | |
3118 | /** |
3119 | * Creates a two dimensional array of an object's key-value pairs, |
3120 | * i.e. `[[key1, value1], [key2, value2]]`. |
3121 | * |
3122 | * @static |
3123 | * @memberOf _ |
3124 | * @category Objects |
3125 | * @param {Object} object The object to inspect. |
3126 | * @returns {Array} Returns new array of key-value pairs. |
3127 | * @example |
3128 | * |
3129 | * _.pairs({ 'barney': 36, 'fred': 40 }); |
3130 | * // => [['barney', 36], ['fred', 40]] (property order is not guaranteed across environments) |
3131 | */ |
3132 | function pairs(object) { |
3133 | var index = -1, |
3134 | props = keys(object), |
3135 | length = props.length, |
3136 | result = Array(length); |
3137 | |
3138 | while (++index < length) { |
3139 | var key = props[index]; |
3140 | result[index] = [key, object[key]]; |
3141 | } |
3142 | return result; |
3143 | } |
3144 | |
3145 | /** |
3146 | * Creates a shallow clone of `object` composed of the specified properties. |
3147 | * Property names may be specified as individual arguments or as arrays of |
3148 | * property names. If a callback is provided it will be executed for each |
3149 | * property of `object` picking the properties the callback returns truey |
3150 | * for. The callback is bound to `thisArg` and invoked with three arguments; |
3151 | * (value, key, object). |
3152 | * |
3153 | * @static |
3154 | * @memberOf _ |
3155 | * @category Objects |
3156 | * @param {Object} object The source object. |
3157 | * @param {Function|...string|string[]} [callback] The function called per |
3158 | * iteration or property names to pick, specified as individual property |
3159 | * names or arrays of property names. |
3160 | * @param {*} [thisArg] The `this` binding of `callback`. |
3161 | * @returns {Object} Returns an object composed of the picked properties. |
3162 | * @example |
3163 | * |
3164 | * _.pick({ 'name': 'fred', '_userid': 'fred1' }, 'name'); |
3165 | * // => { 'name': 'fred' } |
3166 | * |
3167 | * _.pick({ 'name': 'fred', '_userid': 'fred1' }, function(value, key) { |
3168 | * return key.charAt(0) != '_'; |
3169 | * }); |
3170 | * // => { 'name': 'fred' } |
3171 | */ |
3172 | function pick(object, callback, thisArg) { |
3173 | var result = {}; |
3174 | if (typeof callback != 'function') { |
3175 | var index = -1, |
3176 | props = baseFlatten(arguments, true, false, 1), |
3177 | length = isObject(object) ? props.length : 0; |
3178 | |
3179 | while (++index < length) { |
3180 | var key = props[index]; |
3181 | if (key in object) { |
3182 | result[key] = object[key]; |
3183 | } |
3184 | } |
3185 | } else { |
3186 | callback = lodash.createCallback(callback, thisArg, 3); |
3187 | forIn(object, function(value, key, object) { |
3188 | if (callback(value, key, object)) { |
3189 | result[key] = value; |
3190 | } |
3191 | }); |
3192 | } |
3193 | return result; |
3194 | } |
3195 | |
3196 | /** |
3197 | * An alternative to `_.reduce` this method transforms `object` to a new |
3198 | * `accumulator` object which is the result of running each of its own |
3199 | * enumerable properties through a callback, with each callback execution |
3200 | * potentially mutating the `accumulator` object. The callback is bound to |
3201 | * `thisArg` and invoked with four arguments; (accumulator, value, key, object). |
3202 | * Callbacks may exit iteration early by explicitly returning `false`. |
3203 | * |
3204 | * @static |
3205 | * @memberOf _ |
3206 | * @category Objects |
3207 | * @param {Array|Object} object The object to iterate over. |
3208 | * @param {Function} [callback=identity] The function called per iteration. |
3209 | * @param {*} [accumulator] The custom accumulator value. |
3210 | * @param {*} [thisArg] The `this` binding of `callback`. |
3211 | * @returns {*} Returns the accumulated value. |
3212 | * @example |
3213 | * |
3214 | * var squares = _.transform([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(result, num) { |
3215 | * num *= num; |
3216 | * if (num % 2) { |
3217 | * return result.push(num) < 3; |
3218 | * } |
3219 | * }); |
3220 | * // => [1, 9, 25] |
3221 | * |
3222 | * var mapped = _.transform({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { |
3223 | * result[key] = num * 3; |
3224 | * }); |
3225 | * // => { 'a': 3, 'b': 6, 'c': 9 } |
3226 | */ |
3227 | function transform(object, callback, accumulator, thisArg) { |
3228 | var isArr = isArray(object); |
3229 | if (accumulator == null) { |
3230 | if (isArr) { |
3231 | accumulator = []; |
3232 | } else { |
3233 | var ctor = object && object.constructor, |
3234 | proto = ctor && ctor.prototype; |
3235 | |
3236 | accumulator = baseCreate(proto); |
3237 | } |
3238 | } |
3239 | if (callback) { |
3240 | callback = lodash.createCallback(callback, thisArg, 4); |
3241 | (isArr ? baseEach : forOwn)(object, function(value, index, object) { |
3242 | return callback(accumulator, value, index, object); |
3243 | }); |
3244 | } |
3245 | return accumulator; |
3246 | } |
3247 | |
3248 | /** |
3249 | * Creates an array composed of the own enumerable property values of `object`. |
3250 | * |
3251 | * @static |
3252 | * @memberOf _ |
3253 | * @category Objects |
3254 | * @param {Object} object The object to inspect. |
3255 | * @returns {Array} Returns an array of property values. |
3256 | * @example |
3257 | * |
3258 | * _.values({ 'one': 1, 'two': 2, 'three': 3 }); |
3259 | * // => [1, 2, 3] (property order is not guaranteed across environments) |
3260 | */ |
3261 | function values(object) { |
3262 | var index = -1, |
3263 | props = keys(object), |
3264 | length = props.length, |
3265 | result = Array(length); |
3266 | |
3267 | while (++index < length) { |
3268 | result[index] = object[props[index]]; |
3269 | } |
3270 | return result; |
3271 | } |
3272 | |
3273 | /*--------------------------------------------------------------------------*/ |
3274 | |
3275 | /** |
3276 | * Creates an array of elements from the specified indexes, or keys, of the |
3277 | * `collection`. Indexes may be specified as individual arguments or as arrays |
3278 | * of indexes. |
3279 | * |
3280 | * @static |
3281 | * @memberOf _ |
3282 | * @category Collections |
3283 | * @param {Array|Object|string} collection The collection to iterate over. |
3284 | * @param {...(number|number[]|string|string[])} [index] The indexes of `collection` |
3285 | * to retrieve, specified as individual indexes or arrays of indexes. |
3286 | * @returns {Array} Returns a new array of elements corresponding to the |
3287 | * provided indexes. |
3288 | * @example |
3289 | * |
3290 | * _.at(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]); |
3291 | * // => ['a', 'c', 'e'] |
3292 | * |
3293 | * _.at(['fred', 'barney', 'pebbles'], 0, 2); |
3294 | * // => ['fred', 'pebbles'] |
3295 | */ |
3296 | function at(collection) { |
3297 | var args = arguments, |
3298 | index = -1, |
3299 | props = baseFlatten(args, true, false, 1), |
3300 | length = (args[2] && args[2][args[1]] === collection) ? 1 : props.length, |
3301 | result = Array(length); |
3302 | |
3303 | if (support.unindexedChars && isString(collection)) { |
3304 | collection = collection.split(''); |
3305 | } |
3306 | while(++index < length) { |
3307 | result[index] = collection[props[index]]; |
3308 | } |
3309 | return result; |
3310 | } |
3311 | |
3312 | /** |
3313 | * Checks if a given value is present in a collection using strict equality |
3314 | * for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the |
3315 | * offset from the end of the collection. |
3316 | * |
3317 | * @static |
3318 | * @memberOf _ |
3319 | * @alias include |
3320 | * @category Collections |
3321 | * @param {Array|Object|string} collection The collection to iterate over. |
3322 | * @param {*} target The value to check for. |
3323 | * @param {number} [fromIndex=0] The index to search from. |
3324 | * @returns {boolean} Returns `true` if the `target` element is found, else `false`. |
3325 | * @example |
3326 | * |
3327 | * _.contains([1, 2, 3], 1); |
3328 | * // => true |
3329 | * |
3330 | * _.contains([1, 2, 3], 1, 2); |
3331 | * // => false |
3332 | * |
3333 | * _.contains({ 'name': 'fred', 'age': 40 }, 'fred'); |
3334 | * // => true |
3335 | * |
3336 | * _.contains('pebbles', 'eb'); |
3337 | * // => true |
3338 | */ |
3339 | function contains(collection, target, fromIndex) { |
3340 | var index = -1, |
3341 | indexOf = getIndexOf(), |
3342 | length = collection ? collection.length : 0, |
3343 | result = false; |
3344 | |
3345 | fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0; |
3346 | if (isArray(collection)) { |
3347 | result = indexOf(collection, target, fromIndex) > -1; |
3348 | } else if (typeof length == 'number') { |
3349 | result = (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex)) > -1; |
3350 | } else { |
3351 | baseEach(collection, function(value) { |
3352 | if (++index >= fromIndex) { |
3353 | return !(result = value === target); |
3354 | } |
3355 | }); |
3356 | } |
3357 | return result; |
3358 | } |
3359 | |
3360 | /** |
3361 | * Creates an object composed of keys generated from the results of running |
3362 | * each element of `collection` through the callback. The corresponding value |
3363 | * of each key is the number of times the key was returned by the callback. |
3364 | * The callback is bound to `thisArg` and invoked with three arguments; |
3365 | * (value, index|key, collection). |
3366 | * |
3367 | * If a property name is provided for `callback` the created "_.pluck" style |
3368 | * callback will return the property value of the given element. |
3369 | * |
3370 | * If an object is provided for `callback` the created "_.where" style callback |
3371 | * will return `true` for elements that have the properties of the given object, |
3372 | * else `false`. |
3373 | * |
3374 | * @static |
3375 | * @memberOf _ |
3376 | * @category Collections |
3377 | * @param {Array|Object|string} collection The collection to iterate over. |
3378 | * @param {Function|Object|string} [callback=identity] The function called |
3379 | * per iteration. If a property name or object is provided it will be used |
3380 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3381 | * @param {*} [thisArg] The `this` binding of `callback`. |
3382 | * @returns {Object} Returns the composed aggregate object. |
3383 | * @example |
3384 | * |
3385 | * _.countBy([4.3, 6.1, 6.4], function(num) { return Math.floor(num); }); |
3386 | * // => { '4': 1, '6': 2 } |
3387 | * |
3388 | * _.countBy([4.3, 6.1, 6.4], function(num) { return this.floor(num); }, Math); |
3389 | * // => { '4': 1, '6': 2 } |
3390 | * |
3391 | * _.countBy(['one', 'two', 'three'], 'length'); |
3392 | * // => { '3': 2, '5': 1 } |
3393 | */ |
3394 | var countBy = createAggregator(function(result, value, key) { |
3395 | (hasOwnProperty.call(result, key) ? result[key]++ : result[key] = 1); |
3396 | }); |
3397 | |
3398 | /** |
3399 | * Checks if the given callback returns truey value for **all** elements of |
3400 | * a collection. The callback is bound to `thisArg` and invoked with three |
3401 | * arguments; (value, index|key, collection). |
3402 | * |
3403 | * If a property name is provided for `callback` the created "_.pluck" style |
3404 | * callback will return the property value of the given element. |
3405 | * |
3406 | * If an object is provided for `callback` the created "_.where" style callback |
3407 | * will return `true` for elements that have the properties of the given object, |
3408 | * else `false`. |
3409 | * |
3410 | * @static |
3411 | * @memberOf _ |
3412 | * @alias all |
3413 | * @category Collections |
3414 | * @param {Array|Object|string} collection The collection to iterate over. |
3415 | * @param {Function|Object|string} [callback=identity] The function called |
3416 | * per iteration. If a property name or object is provided it will be used |
3417 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3418 | * @param {*} [thisArg] The `this` binding of `callback`. |
3419 | * @returns {boolean} Returns `true` if all elements passed the callback check, |
3420 | * else `false`. |
3421 | * @example |
3422 | * |
3423 | * _.every([true, 1, null, 'yes']); |
3424 | * // => false |
3425 | * |
3426 | * var characters = [ |
3427 | * { 'name': 'barney', 'age': 36 }, |
3428 | * { 'name': 'fred', 'age': 40 } |
3429 | * ]; |
3430 | * |
3431 | * // using "_.pluck" callback shorthand |
3432 | * _.every(characters, 'age'); |
3433 | * // => true |
3434 | * |
3435 | * // using "_.where" callback shorthand |
3436 | * _.every(characters, { 'age': 36 }); |
3437 | * // => false |
3438 | */ |
3439 | function every(collection, callback, thisArg) { |
3440 | var result = true; |
3441 | callback = lodash.createCallback(callback, thisArg, 3); |
3442 | |
3443 | if (isArray(collection)) { |
3444 | var index = -1, |
3445 | length = collection.length; |
3446 | |
3447 | while (++index < length) { |
3448 | if (!(result = !!callback(collection[index], index, collection))) { |
3449 | break; |
3450 | } |
3451 | } |
3452 | } else { |
3453 | baseEach(collection, function(value, index, collection) { |
3454 | return (result = !!callback(value, index, collection)); |
3455 | }); |
3456 | } |
3457 | return result; |
3458 | } |
3459 | |
3460 | /** |
3461 | * Iterates over elements of a collection, returning an array of all elements |
3462 | * the callback returns truey for. The callback is bound to `thisArg` and |
3463 | * invoked with three arguments; (value, index|key, collection). |
3464 | * |
3465 | * If a property name is provided for `callback` the created "_.pluck" style |
3466 | * callback will return the property value of the given element. |
3467 | * |
3468 | * If an object is provided for `callback` the created "_.where" style callback |
3469 | * will return `true` for elements that have the properties of the given object, |
3470 | * else `false`. |
3471 | * |
3472 | * @static |
3473 | * @memberOf _ |
3474 | * @alias select |
3475 | * @category Collections |
3476 | * @param {Array|Object|string} collection The collection to iterate over. |
3477 | * @param {Function|Object|string} [callback=identity] The function called |
3478 | * per iteration. If a property name or object is provided it will be used |
3479 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3480 | * @param {*} [thisArg] The `this` binding of `callback`. |
3481 | * @returns {Array} Returns a new array of elements that passed the callback check. |
3482 | * @example |
3483 | * |
3484 | * var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); |
3485 | * // => [2, 4, 6] |
3486 | * |
3487 | * var characters = [ |
3488 | * { 'name': 'barney', 'age': 36, 'blocked': false }, |
3489 | * { 'name': 'fred', 'age': 40, 'blocked': true } |
3490 | * ]; |
3491 | * |
3492 | * // using "_.pluck" callback shorthand |
3493 | * _.filter(characters, 'blocked'); |
3494 | * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }] |
3495 | * |
3496 | * // using "_.where" callback shorthand |
3497 | * _.filter(characters, { 'age': 36 }); |
3498 | * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }] |
3499 | */ |
3500 | function filter(collection, callback, thisArg) { |
3501 | var result = []; |
3502 | callback = lodash.createCallback(callback, thisArg, 3); |
3503 | |
3504 | if (isArray(collection)) { |
3505 | var index = -1, |
3506 | length = collection.length; |
3507 | |
3508 | while (++index < length) { |
3509 | var value = collection[index]; |
3510 | if (callback(value, index, collection)) { |
3511 | result.push(value); |
3512 | } |
3513 | } |
3514 | } else { |
3515 | baseEach(collection, function(value, index, collection) { |
3516 | if (callback(value, index, collection)) { |
3517 | result.push(value); |
3518 | } |
3519 | }); |
3520 | } |
3521 | return result; |
3522 | } |
3523 | |
3524 | /** |
3525 | * Iterates over elements of a collection, returning the first element that |
3526 | * the callback returns truey for. The callback is bound to `thisArg` and |
3527 | * invoked with three arguments; (value, index|key, collection). |
3528 | * |
3529 | * If a property name is provided for `callback` the created "_.pluck" style |
3530 | * callback will return the property value of the given element. |
3531 | * |
3532 | * If an object is provided for `callback` the created "_.where" style callback |
3533 | * will return `true` for elements that have the properties of the given object, |
3534 | * else `false`. |
3535 | * |
3536 | * @static |
3537 | * @memberOf _ |
3538 | * @alias detect, findWhere |
3539 | * @category Collections |
3540 | * @param {Array|Object|string} collection The collection to iterate over. |
3541 | * @param {Function|Object|string} [callback=identity] The function called |
3542 | * per iteration. If a property name or object is provided it will be used |
3543 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3544 | * @param {*} [thisArg] The `this` binding of `callback`. |
3545 | * @returns {*} Returns the found element, else `undefined`. |
3546 | * @example |
3547 | * |
3548 | * var characters = [ |
3549 | * { 'name': 'barney', 'age': 36, 'blocked': false }, |
3550 | * { 'name': 'fred', 'age': 40, 'blocked': true }, |
3551 | * { 'name': 'pebbles', 'age': 1, 'blocked': false } |
3552 | * ]; |
3553 | * |
3554 | * _.find(characters, function(chr) { |
3555 | * return chr.age < 40; |
3556 | * }); |
3557 | * // => { 'name': 'barney', 'age': 36, 'blocked': false } |
3558 | * |
3559 | * // using "_.where" callback shorthand |
3560 | * _.find(characters, { 'age': 1 }); |
3561 | * // => { 'name': 'pebbles', 'age': 1, 'blocked': false } |
3562 | * |
3563 | * // using "_.pluck" callback shorthand |
3564 | * _.find(characters, 'blocked'); |
3565 | * // => { 'name': 'fred', 'age': 40, 'blocked': true } |
3566 | */ |
3567 | function find(collection, callback, thisArg) { |
3568 | callback = lodash.createCallback(callback, thisArg, 3); |
3569 | |
3570 | if (isArray(collection)) { |
3571 | var index = -1, |
3572 | length = collection.length; |
3573 | |
3574 | while (++index < length) { |
3575 | var value = collection[index]; |
3576 | if (callback(value, index, collection)) { |
3577 | return value; |
3578 | } |
3579 | } |
3580 | } else { |
3581 | var result; |
3582 | baseEach(collection, function(value, index, collection) { |
3583 | if (callback(value, index, collection)) { |
3584 | result = value; |
3585 | return false; |
3586 | } |
3587 | }); |
3588 | return result; |
3589 | } |
3590 | } |
3591 | |
3592 | /** |
3593 | * This method is like `_.find` except that it iterates over elements |
3594 | * of a `collection` from right to left. |
3595 | * |
3596 | * @static |
3597 | * @memberOf _ |
3598 | * @category Collections |
3599 | * @param {Array|Object|string} collection The collection to iterate over. |
3600 | * @param {Function|Object|string} [callback=identity] The function called |
3601 | * per iteration. If a property name or object is provided it will be used |
3602 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3603 | * @param {*} [thisArg] The `this` binding of `callback`. |
3604 | * @returns {*} Returns the found element, else `undefined`. |
3605 | * @example |
3606 | * |
3607 | * _.findLast([1, 2, 3, 4], function(num) { |
3608 | * return num % 2 == 1; |
3609 | * }); |
3610 | * // => 3 |
3611 | */ |
3612 | function findLast(collection, callback, thisArg) { |
3613 | var result; |
3614 | callback = lodash.createCallback(callback, thisArg, 3); |
3615 | forEachRight(collection, function(value, index, collection) { |
3616 | if (callback(value, index, collection)) { |
3617 | result = value; |
3618 | return false; |
3619 | } |
3620 | }); |
3621 | return result; |
3622 | } |
3623 | |
3624 | /** |
3625 | * Iterates over elements of a collection, executing the callback for each |
3626 | * element. The callback is bound to `thisArg` and invoked with three arguments; |
3627 | * (value, index|key, collection). Callbacks may exit iteration early by |
3628 | * explicitly returning `false`. |
3629 | * |
3630 | * Note: As with other "Collections" methods, objects with a `length` property |
3631 | * are iterated like arrays. To avoid this behavior `_.forIn` or `_.forOwn` |
3632 | * may be used for object iteration. |
3633 | * |
3634 | * @static |
3635 | * @memberOf _ |
3636 | * @alias each |
3637 | * @category Collections |
3638 | * @param {Array|Object|string} collection The collection to iterate over. |
3639 | * @param {Function} [callback=identity] The function called per iteration. |
3640 | * @param {*} [thisArg] The `this` binding of `callback`. |
3641 | * @returns {Array|Object|string} Returns `collection`. |
3642 | * @example |
3643 | * |
3644 | * _([1, 2, 3]).forEach(function(num) { console.log(num); }).join(','); |
3645 | * // => logs each number and returns '1,2,3' |
3646 | * |
3647 | * _.forEach({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { console.log(num); }); |
3648 | * // => logs each number and returns the object (property order is not guaranteed across environments) |
3649 | */ |
3650 | function forEach(collection, callback, thisArg) { |
3651 | if (callback && typeof thisArg == 'undefined' && isArray(collection)) { |
3652 | var index = -1, |
3653 | length = collection.length; |
3654 | |
3655 | while (++index < length) { |
3656 | if (callback(collection[index], index, collection) === false) { |
3657 | break; |
3658 | } |
3659 | } |
3660 | } else { |
3661 | baseEach(collection, callback, thisArg); |
3662 | } |
3663 | return collection; |
3664 | } |
3665 | |
3666 | /** |
3667 | * This method is like `_.forEach` except that it iterates over elements |
3668 | * of a `collection` from right to left. |
3669 | * |
3670 | * @static |
3671 | * @memberOf _ |
3672 | * @alias eachRight |
3673 | * @category Collections |
3674 | * @param {Array|Object|string} collection The collection to iterate over. |
3675 | * @param {Function} [callback=identity] The function called per iteration. |
3676 | * @param {*} [thisArg] The `this` binding of `callback`. |
3677 | * @returns {Array|Object|string} Returns `collection`. |
3678 | * @example |
3679 | * |
3680 | * _([1, 2, 3]).forEachRight(function(num) { console.log(num); }).join(','); |
3681 | * // => logs each number from right to left and returns '3,2,1' |
3682 | */ |
3683 | function forEachRight(collection, callback, thisArg) { |
3684 | var iterable = collection, |
3685 | length = collection ? collection.length : 0; |
3686 | |
3687 | callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3); |
3688 | if (isArray(collection)) { |
3689 | while (length--) { |
3690 | if (callback(collection[length], length, collection) === false) { |
3691 | break; |
3692 | } |
3693 | } |
3694 | } else { |
3695 | if (typeof length != 'number') { |
3696 | var props = keys(collection); |
3697 | length = props.length; |
3698 | } else if (support.unindexedChars && isString(collection)) { |
3699 | iterable = collection.split(''); |
3700 | } |
3701 | baseEach(collection, function(value, key, collection) { |
3702 | key = props ? props[--length] : --length; |
3703 | return callback(iterable[key], key, collection); |
3704 | }); |
3705 | } |
3706 | return collection; |
3707 | } |
3708 | |
3709 | /** |
3710 | * Creates an object composed of keys generated from the results of running |
3711 | * each element of a collection through the callback. The corresponding value |
3712 | * of each key is an array of the elements responsible for generating the key. |
3713 | * The callback is bound to `thisArg` and invoked with three arguments; |
3714 | * (value, index|key, collection). |
3715 | * |
3716 | * If a property name is provided for `callback` the created "_.pluck" style |
3717 | * callback will return the property value of the given element. |
3718 | * |
3719 | * If an object is provided for `callback` the created "_.where" style callback |
3720 | * will return `true` for elements that have the properties of the given object, |
3721 | * else `false` |
3722 | * |
3723 | * @static |
3724 | * @memberOf _ |
3725 | * @category Collections |
3726 | * @param {Array|Object|string} collection The collection to iterate over. |
3727 | * @param {Function|Object|string} [callback=identity] The function called |
3728 | * per iteration. If a property name or object is provided it will be used |
3729 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3730 | * @param {*} [thisArg] The `this` binding of `callback`. |
3731 | * @returns {Object} Returns the composed aggregate object. |
3732 | * @example |
3733 | * |
3734 | * _.groupBy([4.2, 6.1, 6.4], function(num) { return Math.floor(num); }); |
3735 | * // => { '4': [4.2], '6': [6.1, 6.4] } |
3736 | * |
3737 | * _.groupBy([4.2, 6.1, 6.4], function(num) { return this.floor(num); }, Math); |
3738 | * // => { '4': [4.2], '6': [6.1, 6.4] } |
3739 | * |
3740 | * // using "_.pluck" callback shorthand |
3741 | * _.groupBy(['one', 'two', 'three'], 'length'); |
3742 | * // => { '3': ['one', 'two'], '5': ['three'] } |
3743 | */ |
3744 | var groupBy = createAggregator(function(result, value, key) { |
3745 | (hasOwnProperty.call(result, key) ? result[key] : result[key] = []).push(value); |
3746 | }); |
3747 | |
3748 | /** |
3749 | * Creates an object composed of keys generated from the results of running |
3750 | * each element of the collection through the given callback. The corresponding |
3751 | * value of each key is the last element responsible for generating the key. |
3752 | * The callback is bound to `thisArg` and invoked with three arguments; |
3753 | * (value, index|key, collection). |
3754 | * |
3755 | * If a property name is provided for `callback` the created "_.pluck" style |
3756 | * callback will return the property value of the given element. |
3757 | * |
3758 | * If an object is provided for `callback` the created "_.where" style callback |
3759 | * will return `true` for elements that have the properties of the given object, |
3760 | * else `false`. |
3761 | * |
3762 | * @static |
3763 | * @memberOf _ |
3764 | * @category Collections |
3765 | * @param {Array|Object|string} collection The collection to iterate over. |
3766 | * @param {Function|Object|string} [callback=identity] The function called |
3767 | * per iteration. If a property name or object is provided it will be used |
3768 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3769 | * @param {*} [thisArg] The `this` binding of `callback`. |
3770 | * @returns {Object} Returns the composed aggregate object. |
3771 | * @example |
3772 | * |
3773 | * var keys = [ |
3774 | * { 'dir': 'left', 'code': 97 }, |
3775 | * { 'dir': 'right', 'code': 100 } |
3776 | * ]; |
3777 | * |
3778 | * _.indexBy(keys, 'dir'); |
3779 | * // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } } |
3780 | * |
3781 | * _.indexBy(keys, function(key) { return String.fromCharCode(key.code); }); |
3782 | * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } |
3783 | * |
3784 | * _.indexBy(characters, function(key) { this.fromCharCode(key.code); }, String); |
3785 | * // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } } |
3786 | */ |
3787 | var indexBy = createAggregator(function(result, value, key) { |
3788 | result[key] = value; |
3789 | }); |
3790 | |
3791 | /** |
3792 | * Invokes the method named by `methodName` on each element in the `collection` |
3793 | * returning an array of the results of each invoked method. Additional arguments |
3794 | * will be provided to each invoked method. If `methodName` is a function it |
3795 | * will be invoked for, and `this` bound to, each element in the `collection`. |
3796 | * |
3797 | * @static |
3798 | * @memberOf _ |
3799 | * @category Collections |
3800 | * @param {Array|Object|string} collection The collection to iterate over. |
3801 | * @param {Function|string} methodName The name of the method to invoke or |
3802 | * the function invoked per iteration. |
3803 | * @param {...*} [arg] Arguments to invoke the method with. |
3804 | * @returns {Array} Returns a new array of the results of each invoked method. |
3805 | * @example |
3806 | * |
3807 | * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); |
3808 | * // => [[1, 5, 7], [1, 2, 3]] |
3809 | * |
3810 | * _.invoke([123, 456], String.prototype.split, ''); |
3811 | * // => [['1', '2', '3'], ['4', '5', '6']] |
3812 | */ |
3813 | function invoke(collection, methodName) { |
3814 | var args = slice(arguments, 2), |
3815 | index = -1, |
3816 | isFunc = typeof methodName == 'function', |
3817 | length = collection ? collection.length : 0, |
3818 | result = Array(typeof length == 'number' ? length : 0); |
3819 | |
3820 | forEach(collection, function(value) { |
3821 | result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args); |
3822 | }); |
3823 | return result; |
3824 | } |
3825 | |
3826 | /** |
3827 | * Creates an array of values by running each element in the collection |
3828 | * through the callback. The callback is bound to `thisArg` and invoked with |
3829 | * three arguments; (value, index|key, collection). |
3830 | * |
3831 | * If a property name is provided for `callback` the created "_.pluck" style |
3832 | * callback will return the property value of the given element. |
3833 | * |
3834 | * If an object is provided for `callback` the created "_.where" style callback |
3835 | * will return `true` for elements that have the properties of the given object, |
3836 | * else `false`. |
3837 | * |
3838 | * @static |
3839 | * @memberOf _ |
3840 | * @alias collect |
3841 | * @category Collections |
3842 | * @param {Array|Object|string} collection The collection to iterate over. |
3843 | * @param {Function|Object|string} [callback=identity] The function called |
3844 | * per iteration. If a property name or object is provided it will be used |
3845 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3846 | * @param {*} [thisArg] The `this` binding of `callback`. |
3847 | * @returns {Array} Returns a new array of the results of each `callback` execution. |
3848 | * @example |
3849 | * |
3850 | * _.map([1, 2, 3], function(num) { return num * 3; }); |
3851 | * // => [3, 6, 9] |
3852 | * |
3853 | * _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); |
3854 | * // => [3, 6, 9] (property order is not guaranteed across environments) |
3855 | * |
3856 | * var characters = [ |
3857 | * { 'name': 'barney', 'age': 36 }, |
3858 | * { 'name': 'fred', 'age': 40 } |
3859 | * ]; |
3860 | * |
3861 | * // using "_.pluck" callback shorthand |
3862 | * _.map(characters, 'name'); |
3863 | * // => ['barney', 'fred'] |
3864 | */ |
3865 | function map(collection, callback, thisArg) { |
3866 | var index = -1, |
3867 | length = collection ? collection.length : 0, |
3868 | result = Array(typeof length == 'number' ? length : 0); |
3869 | |
3870 | callback = lodash.createCallback(callback, thisArg, 3); |
3871 | if (isArray(collection)) { |
3872 | while (++index < length) { |
3873 | result[index] = callback(collection[index], index, collection); |
3874 | } |
3875 | } else { |
3876 | baseEach(collection, function(value, key, collection) { |
3877 | result[++index] = callback(value, key, collection); |
3878 | }); |
3879 | } |
3880 | return result; |
3881 | } |
3882 | |
3883 | /** |
3884 | * Retrieves the maximum value of a collection. If the collection is empty or |
3885 | * falsey `-Infinity` is returned. If a callback is provided it will be executed |
3886 | * for each value in the collection to generate the criterion by which the value |
3887 | * is ranked. The callback is bound to `thisArg` and invoked with three |
3888 | * arguments; (value, index, collection). |
3889 | * |
3890 | * If a property name is provided for `callback` the created "_.pluck" style |
3891 | * callback will return the property value of the given element. |
3892 | * |
3893 | * If an object is provided for `callback` the created "_.where" style callback |
3894 | * will return `true` for elements that have the properties of the given object, |
3895 | * else `false`. |
3896 | * |
3897 | * @static |
3898 | * @memberOf _ |
3899 | * @category Collections |
3900 | * @param {Array|Object|string} collection The collection to iterate over. |
3901 | * @param {Function|Object|string} [callback=identity] The function called |
3902 | * per iteration. If a property name or object is provided it will be used |
3903 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3904 | * @param {*} [thisArg] The `this` binding of `callback`. |
3905 | * @returns {*} Returns the maximum value. |
3906 | * @example |
3907 | * |
3908 | * _.max([4, 2, 8, 6]); |
3909 | * // => 8 |
3910 | * |
3911 | * var characters = [ |
3912 | * { 'name': 'barney', 'age': 36 }, |
3913 | * { 'name': 'fred', 'age': 40 } |
3914 | * ]; |
3915 | * |
3916 | * _.max(characters, function(chr) { return chr.age; }); |
3917 | * // => { 'name': 'fred', 'age': 40 }; |
3918 | * |
3919 | * // using "_.pluck" callback shorthand |
3920 | * _.max(characters, 'age'); |
3921 | * // => { 'name': 'fred', 'age': 40 }; |
3922 | */ |
3923 | function max(collection, callback, thisArg) { |
3924 | var computed = -Infinity, |
3925 | result = computed; |
3926 | |
3927 | // allows working with functions like `_.map` without using |
3928 | // their `index` argument as a callback |
3929 | if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { |
3930 | callback = null; |
3931 | } |
3932 | if (callback == null && isArray(collection)) { |
3933 | var index = -1, |
3934 | length = collection.length; |
3935 | |
3936 | while (++index < length) { |
3937 | var value = collection[index]; |
3938 | if (value > result) { |
3939 | result = value; |
3940 | } |
3941 | } |
3942 | } else { |
3943 | callback = (callback == null && isString(collection)) |
3944 | ? charAtCallback |
3945 | : lodash.createCallback(callback, thisArg, 3); |
3946 | |
3947 | baseEach(collection, function(value, index, collection) { |
3948 | var current = callback(value, index, collection); |
3949 | if (current > computed) { |
3950 | computed = current; |
3951 | result = value; |
3952 | } |
3953 | }); |
3954 | } |
3955 | return result; |
3956 | } |
3957 | |
3958 | /** |
3959 | * Retrieves the minimum value of a collection. If the collection is empty or |
3960 | * falsey `Infinity` is returned. If a callback is provided it will be executed |
3961 | * for each value in the collection to generate the criterion by which the value |
3962 | * is ranked. The callback is bound to `thisArg` and invoked with three |
3963 | * arguments; (value, index, collection). |
3964 | * |
3965 | * If a property name is provided for `callback` the created "_.pluck" style |
3966 | * callback will return the property value of the given element. |
3967 | * |
3968 | * If an object is provided for `callback` the created "_.where" style callback |
3969 | * will return `true` for elements that have the properties of the given object, |
3970 | * else `false`. |
3971 | * |
3972 | * @static |
3973 | * @memberOf _ |
3974 | * @category Collections |
3975 | * @param {Array|Object|string} collection The collection to iterate over. |
3976 | * @param {Function|Object|string} [callback=identity] The function called |
3977 | * per iteration. If a property name or object is provided it will be used |
3978 | * to create a "_.pluck" or "_.where" style callback, respectively. |
3979 | * @param {*} [thisArg] The `this` binding of `callback`. |
3980 | * @returns {*} Returns the minimum value. |
3981 | * @example |
3982 | * |
3983 | * _.min([4, 2, 8, 6]); |
3984 | * // => 2 |
3985 | * |
3986 | * var characters = [ |
3987 | * { 'name': 'barney', 'age': 36 }, |
3988 | * { 'name': 'fred', 'age': 40 } |
3989 | * ]; |
3990 | * |
3991 | * _.min(characters, function(chr) { return chr.age; }); |
3992 | * // => { 'name': 'barney', 'age': 36 }; |
3993 | * |
3994 | * // using "_.pluck" callback shorthand |
3995 | * _.min(characters, 'age'); |
3996 | * // => { 'name': 'barney', 'age': 36 }; |
3997 | */ |
3998 | function min(collection, callback, thisArg) { |
3999 | var computed = Infinity, |
4000 | result = computed; |
4001 | |
4002 | // allows working with functions like `_.map` without using |
4003 | // their `index` argument as a callback |
4004 | if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { |
4005 | callback = null; |
4006 | } |
4007 | if (callback == null && isArray(collection)) { |
4008 | var index = -1, |
4009 | length = collection.length; |
4010 | |
4011 | while (++index < length) { |
4012 | var value = collection[index]; |
4013 | if (value < result) { |
4014 | result = value; |
4015 | } |
4016 | } |
4017 | } else { |
4018 | callback = (callback == null && isString(collection)) |
4019 | ? charAtCallback |
4020 | : lodash.createCallback(callback, thisArg, 3); |
4021 | |
4022 | baseEach(collection, function(value, index, collection) { |
4023 | var current = callback(value, index, collection); |
4024 | if (current < computed) { |
4025 | computed = current; |
4026 | result = value; |
4027 | } |
4028 | }); |
4029 | } |
4030 | return result; |
4031 | } |
4032 | |
4033 | /** |
4034 | * Retrieves the value of a specified property from all elements in the collection. |
4035 | * |
4036 | * @static |
4037 | * @memberOf _ |
4038 | * @type Function |
4039 | * @category Collections |
4040 | * @param {Array|Object|string} collection The collection to iterate over. |
4041 | * @param {string} property The name of the property to pluck. |
4042 | * @returns {Array} Returns a new array of property values. |
4043 | * @example |
4044 | * |
4045 | * var characters = [ |
4046 | * { 'name': 'barney', 'age': 36 }, |
4047 | * { 'name': 'fred', 'age': 40 } |
4048 | * ]; |
4049 | * |
4050 | * _.pluck(characters, 'name'); |
4051 | * // => ['barney', 'fred'] |
4052 | */ |
4053 | var pluck = map; |
4054 | |
4055 | /** |
4056 | * Reduces a collection to a value which is the accumulated result of running |
4057 | * each element in the collection through the callback, where each successive |
4058 | * callback execution consumes the return value of the previous execution. If |
4059 | * `accumulator` is not provided the first element of the collection will be |
4060 | * used as the initial `accumulator` value. The callback is bound to `thisArg` |
4061 | * and invoked with four arguments; (accumulator, value, index|key, collection). |
4062 | * |
4063 | * @static |
4064 | * @memberOf _ |
4065 | * @alias foldl, inject |
4066 | * @category Collections |
4067 | * @param {Array|Object|string} collection The collection to iterate over. |
4068 | * @param {Function} [callback=identity] The function called per iteration. |
4069 | * @param {*} [accumulator] Initial value of the accumulator. |
4070 | * @param {*} [thisArg] The `this` binding of `callback`. |
4071 | * @returns {*} Returns the accumulated value. |
4072 | * @example |
4073 | * |
4074 | * var sum = _.reduce([1, 2, 3], function(sum, num) { |
4075 | * return sum + num; |
4076 | * }); |
4077 | * // => 6 |
4078 | * |
4079 | * var mapped = _.reduce({ 'a': 1, 'b': 2, 'c': 3 }, function(result, num, key) { |
4080 | * result[key] = num * 3; |
4081 | * return result; |
4082 | * }, {}); |
4083 | * // => { 'a': 3, 'b': 6, 'c': 9 } |
4084 | */ |
4085 | function reduce(collection, callback, accumulator, thisArg) { |
4086 | var noaccum = arguments.length < 3; |
4087 | callback = lodash.createCallback(callback, thisArg, 4); |
4088 | |
4089 | if (isArray(collection)) { |
4090 | var index = -1, |
4091 | length = collection.length; |
4092 | |
4093 | if (noaccum) { |
4094 | accumulator = collection[++index]; |
4095 | } |
4096 | while (++index < length) { |
4097 | accumulator = callback(accumulator, collection[index], index, collection); |
4098 | } |
4099 | } else { |
4100 | baseEach(collection, function(value, index, collection) { |
4101 | accumulator = noaccum |
4102 | ? (noaccum = false, value) |
4103 | : callback(accumulator, value, index, collection) |
4104 | }); |
4105 | } |
4106 | return accumulator; |
4107 | } |
4108 | |
4109 | /** |
4110 | * This method is like `_.reduce` except that it iterates over elements |
4111 | * of a `collection` from right to left. |
4112 | * |
4113 | * @static |
4114 | * @memberOf _ |
4115 | * @alias foldr |
4116 | * @category Collections |
4117 | * @param {Array|Object|string} collection The collection to iterate over. |
4118 | * @param {Function} [callback=identity] The function called per iteration. |
4119 | * @param {*} [accumulator] Initial value of the accumulator. |
4120 | * @param {*} [thisArg] The `this` binding of `callback`. |
4121 | * @returns {*} Returns the accumulated value. |
4122 | * @example |
4123 | * |
4124 | * var list = [[0, 1], [2, 3], [4, 5]]; |
4125 | * var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); |
4126 | * // => [4, 5, 2, 3, 0, 1] |
4127 | */ |
4128 | function reduceRight(collection, callback, accumulator, thisArg) { |
4129 | var noaccum = arguments.length < 3; |
4130 | callback = lodash.createCallback(callback, thisArg, 4); |
4131 | forEachRight(collection, function(value, index, collection) { |
4132 | accumulator = noaccum |
4133 | ? (noaccum = false, value) |
4134 | : callback(accumulator, value, index, collection); |
4135 | }); |
4136 | return accumulator; |
4137 | } |
4138 | |
4139 | /** |
4140 | * The opposite of `_.filter` this method returns the elements of a |
4141 | * collection that the callback does **not** return truey for. |
4142 | * |
4143 | * If a property name is provided for `callback` the created "_.pluck" style |
4144 | * callback will return the property value of the given element. |
4145 | * |
4146 | * If an object is provided for `callback` the created "_.where" style callback |
4147 | * will return `true` for elements that have the properties of the given object, |
4148 | * else `false`. |
4149 | * |
4150 | * @static |
4151 | * @memberOf _ |
4152 | * @category Collections |
4153 | * @param {Array|Object|string} collection The collection to iterate over. |
4154 | * @param {Function|Object|string} [callback=identity] The function called |
4155 | * per iteration. If a property name or object is provided it will be used |
4156 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4157 | * @param {*} [thisArg] The `this` binding of `callback`. |
4158 | * @returns {Array} Returns a new array of elements that failed the callback check. |
4159 | * @example |
4160 | * |
4161 | * var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); |
4162 | * // => [1, 3, 5] |
4163 | * |
4164 | * var characters = [ |
4165 | * { 'name': 'barney', 'age': 36, 'blocked': false }, |
4166 | * { 'name': 'fred', 'age': 40, 'blocked': true } |
4167 | * ]; |
4168 | * |
4169 | * // using "_.pluck" callback shorthand |
4170 | * _.reject(characters, 'blocked'); |
4171 | * // => [{ 'name': 'barney', 'age': 36, 'blocked': false }] |
4172 | * |
4173 | * // using "_.where" callback shorthand |
4174 | * _.reject(characters, { 'age': 36 }); |
4175 | * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }] |
4176 | */ |
4177 | function reject(collection, callback, thisArg) { |
4178 | callback = lodash.createCallback(callback, thisArg, 3); |
4179 | return filter(collection, function(value, index, collection) { |
4180 | return !callback(value, index, collection); |
4181 | }); |
4182 | } |
4183 | |
4184 | /** |
4185 | * Retrieves a random element or `n` random elements from a collection. |
4186 | * |
4187 | * @static |
4188 | * @memberOf _ |
4189 | * @category Collections |
4190 | * @param {Array|Object|string} collection The collection to sample. |
4191 | * @param {number} [n] The number of elements to sample. |
4192 | * @param- {Object} [guard] Allows working with functions like `_.map` |
4193 | * without using their `index` arguments as `n`. |
4194 | * @returns {Array} Returns the random sample(s) of `collection`. |
4195 | * @example |
4196 | * |
4197 | * _.sample([1, 2, 3, 4]); |
4198 | * // => 2 |
4199 | * |
4200 | * _.sample([1, 2, 3, 4], 2); |
4201 | * // => [3, 1] |
4202 | */ |
4203 | function sample(collection, n, guard) { |
4204 | if (collection && typeof collection.length != 'number') { |
4205 | collection = values(collection); |
4206 | } else if (support.unindexedChars && isString(collection)) { |
4207 | collection = collection.split(''); |
4208 | } |
4209 | if (n == null || guard) { |
4210 | return collection ? collection[baseRandom(0, collection.length - 1)] : undefined; |
4211 | } |
4212 | var result = shuffle(collection); |
4213 | result.length = nativeMin(nativeMax(0, n), result.length); |
4214 | return result; |
4215 | } |
4216 | |
4217 | /** |
4218 | * Creates an array of shuffled values, using a version of the Fisher-Yates |
4219 | * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. |
4220 | * |
4221 | * @static |
4222 | * @memberOf _ |
4223 | * @category Collections |
4224 | * @param {Array|Object|string} collection The collection to shuffle. |
4225 | * @returns {Array} Returns a new shuffled collection. |
4226 | * @example |
4227 | * |
4228 | * _.shuffle([1, 2, 3, 4, 5, 6]); |
4229 | * // => [4, 1, 6, 3, 5, 2] |
4230 | */ |
4231 | function shuffle(collection) { |
4232 | var index = -1, |
4233 | length = collection ? collection.length : 0, |
4234 | result = Array(typeof length == 'number' ? length : 0); |
4235 | |
4236 | forEach(collection, function(value) { |
4237 | var rand = baseRandom(0, ++index); |
4238 | result[index] = result[rand]; |
4239 | result[rand] = value; |
4240 | }); |
4241 | return result; |
4242 | } |
4243 | |
4244 | /** |
4245 | * Gets the size of the `collection` by returning `collection.length` for arrays |
4246 | * and array-like objects or the number of own enumerable properties for objects. |
4247 | * |
4248 | * @static |
4249 | * @memberOf _ |
4250 | * @category Collections |
4251 | * @param {Array|Object|string} collection The collection to inspect. |
4252 | * @returns {number} Returns `collection.length` or number of own enumerable properties. |
4253 | * @example |
4254 | * |
4255 | * _.size([1, 2]); |
4256 | * // => 2 |
4257 | * |
4258 | * _.size({ 'one': 1, 'two': 2, 'three': 3 }); |
4259 | * // => 3 |
4260 | * |
4261 | * _.size('pebbles'); |
4262 | * // => 7 |
4263 | */ |
4264 | function size(collection) { |
4265 | var length = collection ? collection.length : 0; |
4266 | return typeof length == 'number' ? length : keys(collection).length; |
4267 | } |
4268 | |
4269 | /** |
4270 | * Checks if the callback returns a truey value for **any** element of a |
4271 | * collection. The function returns as soon as it finds a passing value and |
4272 | * does not iterate over the entire collection. The callback is bound to |
4273 | * `thisArg` and invoked with three arguments; (value, index|key, collection). |
4274 | * |
4275 | * If a property name is provided for `callback` the created "_.pluck" style |
4276 | * callback will return the property value of the given element. |
4277 | * |
4278 | * If an object is provided for `callback` the created "_.where" style callback |
4279 | * will return `true` for elements that have the properties of the given object, |
4280 | * else `false`. |
4281 | * |
4282 | * @static |
4283 | * @memberOf _ |
4284 | * @alias any |
4285 | * @category Collections |
4286 | * @param {Array|Object|string} collection The collection to iterate over. |
4287 | * @param {Function|Object|string} [callback=identity] The function called |
4288 | * per iteration. If a property name or object is provided it will be used |
4289 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4290 | * @param {*} [thisArg] The `this` binding of `callback`. |
4291 | * @returns {boolean} Returns `true` if any element passed the callback check, |
4292 | * else `false`. |
4293 | * @example |
4294 | * |
4295 | * _.some([null, 0, 'yes', false], Boolean); |
4296 | * // => true |
4297 | * |
4298 | * var characters = [ |
4299 | * { 'name': 'barney', 'age': 36, 'blocked': false }, |
4300 | * { 'name': 'fred', 'age': 40, 'blocked': true } |
4301 | * ]; |
4302 | * |
4303 | * // using "_.pluck" callback shorthand |
4304 | * _.some(characters, 'blocked'); |
4305 | * // => true |
4306 | * |
4307 | * // using "_.where" callback shorthand |
4308 | * _.some(characters, { 'age': 1 }); |
4309 | * // => false |
4310 | */ |
4311 | function some(collection, callback, thisArg) { |
4312 | var result; |
4313 | callback = lodash.createCallback(callback, thisArg, 3); |
4314 | |
4315 | if (isArray(collection)) { |
4316 | var index = -1, |
4317 | length = collection.length; |
4318 | |
4319 | while (++index < length) { |
4320 | if ((result = callback(collection[index], index, collection))) { |
4321 | break; |
4322 | } |
4323 | } |
4324 | } else { |
4325 | baseEach(collection, function(value, index, collection) { |
4326 | return !(result = callback(value, index, collection)); |
4327 | }); |
4328 | } |
4329 | return !!result; |
4330 | } |
4331 | |
4332 | /** |
4333 | * Creates an array of elements, sorted in ascending order by the results of |
4334 | * running each element in a collection through the callback. This method |
4335 | * performs a stable sort, that is, it will preserve the original sort order |
4336 | * of equal elements. The callback is bound to `thisArg` and invoked with |
4337 | * three arguments; (value, index|key, collection). |
4338 | * |
4339 | * If a property name is provided for `callback` the created "_.pluck" style |
4340 | * callback will return the property value of the given element. |
4341 | * |
4342 | * If an array of property names is provided for `callback` the collection |
4343 | * will be sorted by each property value. |
4344 | * |
4345 | * If an object is provided for `callback` the created "_.where" style callback |
4346 | * will return `true` for elements that have the properties of the given object, |
4347 | * else `false`. |
4348 | * |
4349 | * @static |
4350 | * @memberOf _ |
4351 | * @category Collections |
4352 | * @param {Array|Object|string} collection The collection to iterate over. |
4353 | * @param {Array|Function|Object|string} [callback=identity] The function called |
4354 | * per iteration. If a property name or object is provided it will be used |
4355 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4356 | * @param {*} [thisArg] The `this` binding of `callback`. |
4357 | * @returns {Array} Returns a new array of sorted elements. |
4358 | * @example |
4359 | * |
4360 | * _.sortBy([1, 2, 3], function(num) { return Math.sin(num); }); |
4361 | * // => [3, 1, 2] |
4362 | * |
4363 | * _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math); |
4364 | * // => [3, 1, 2] |
4365 | * |
4366 | * var characters = [ |
4367 | * { 'name': 'barney', 'age': 36 }, |
4368 | * { 'name': 'fred', 'age': 40 }, |
4369 | * { 'name': 'barney', 'age': 26 }, |
4370 | * { 'name': 'fred', 'age': 30 } |
4371 | * ]; |
4372 | * |
4373 | * // using "_.pluck" callback shorthand |
4374 | * _.map(_.sortBy(characters, 'age'), _.values); |
4375 | * // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]] |
4376 | * |
4377 | * // sorting by multiple properties |
4378 | * _.map(_.sortBy(characters, ['name', 'age']), _.values); |
4379 | * // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]] |
4380 | */ |
4381 | function sortBy(collection, callback, thisArg) { |
4382 | var index = -1, |
4383 | isArr = isArray(callback), |
4384 | length = collection ? collection.length : 0, |
4385 | result = Array(typeof length == 'number' ? length : 0); |
4386 | |
4387 | if (!isArr) { |
4388 | callback = lodash.createCallback(callback, thisArg, 3); |
4389 | } |
4390 | forEach(collection, function(value, key, collection) { |
4391 | var object = result[++index] = getObject(); |
4392 | if (isArr) { |
4393 | object.criteria = map(callback, function(key) { return value[key]; }); |
4394 | } else { |
4395 | (object.criteria = getArray())[0] = callback(value, key, collection); |
4396 | } |
4397 | object.index = index; |
4398 | object.value = value; |
4399 | }); |
4400 | |
4401 | length = result.length; |
4402 | result.sort(compareAscending); |
4403 | while (length--) { |
4404 | var object = result[length]; |
4405 | result[length] = object.value; |
4406 | if (!isArr) { |
4407 | releaseArray(object.criteria); |
4408 | } |
4409 | releaseObject(object); |
4410 | } |
4411 | return result; |
4412 | } |
4413 | |
4414 | /** |
4415 | * Converts the `collection` to an array. |
4416 | * |
4417 | * @static |
4418 | * @memberOf _ |
4419 | * @category Collections |
4420 | * @param {Array|Object|string} collection The collection to convert. |
4421 | * @returns {Array} Returns the new converted array. |
4422 | * @example |
4423 | * |
4424 | * (function() { return _.toArray(arguments).slice(1); })(1, 2, 3, 4); |
4425 | * // => [2, 3, 4] |
4426 | */ |
4427 | function toArray(collection) { |
4428 | if (collection && typeof collection.length == 'number') { |
4429 | return (support.unindexedChars && isString(collection)) |
4430 | ? collection.split('') |
4431 | : slice(collection); |
4432 | } |
4433 | return values(collection); |
4434 | } |
4435 | |
4436 | /** |
4437 | * Performs a deep comparison of each element in a `collection` to the given |
4438 | * `properties` object, returning an array of all elements that have equivalent |
4439 | * property values. |
4440 | * |
4441 | * @static |
4442 | * @memberOf _ |
4443 | * @type Function |
4444 | * @category Collections |
4445 | * @param {Array|Object|string} collection The collection to iterate over. |
4446 | * @param {Object} props The object of property values to filter by. |
4447 | * @returns {Array} Returns a new array of elements that have the given properties. |
4448 | * @example |
4449 | * |
4450 | * var characters = [ |
4451 | * { 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }, |
4452 | * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] } |
4453 | * ]; |
4454 | * |
4455 | * _.where(characters, { 'age': 36 }); |
4456 | * // => [{ 'name': 'barney', 'age': 36, 'pets': ['hoppy'] }] |
4457 | * |
4458 | * _.where(characters, { 'pets': ['dino'] }); |
4459 | * // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }] |
4460 | */ |
4461 | var where = filter; |
4462 | |
4463 | /*--------------------------------------------------------------------------*/ |
4464 | |
4465 | /** |
4466 | * Creates an array with all falsey values removed. The values `false`, `null`, |
4467 | * `0`, `""`, `undefined`, and `NaN` are all falsey. |
4468 | * |
4469 | * @static |
4470 | * @memberOf _ |
4471 | * @category Arrays |
4472 | * @param {Array} array The array to compact. |
4473 | * @returns {Array} Returns a new array of filtered values. |
4474 | * @example |
4475 | * |
4476 | * _.compact([0, 1, false, 2, '', 3]); |
4477 | * // => [1, 2, 3] |
4478 | */ |
4479 | function compact(array) { |
4480 | var index = -1, |
4481 | length = array ? array.length : 0, |
4482 | result = []; |
4483 | |
4484 | while (++index < length) { |
4485 | var value = array[index]; |
4486 | if (value) { |
4487 | result.push(value); |
4488 | } |
4489 | } |
4490 | return result; |
4491 | } |
4492 | |
4493 | /** |
4494 | * Creates an array excluding all values of the provided arrays using strict |
4495 | * equality for comparisons, i.e. `===`. |
4496 | * |
4497 | * @static |
4498 | * @memberOf _ |
4499 | * @category Arrays |
4500 | * @param {Array} array The array to process. |
4501 | * @param {...Array} [values] The arrays of values to exclude. |
4502 | * @returns {Array} Returns a new array of filtered values. |
4503 | * @example |
4504 | * |
4505 | * _.difference([1, 2, 3, 4, 5], [5, 2, 10]); |
4506 | * // => [1, 3, 4] |
4507 | */ |
4508 | function difference(array) { |
4509 | return baseDifference(array, baseFlatten(arguments, true, true, 1)); |
4510 | } |
4511 | |
4512 | /** |
4513 | * This method is like `_.find` except that it returns the index of the first |
4514 | * element that passes the callback check, instead of the element itself. |
4515 | * |
4516 | * If a property name is provided for `callback` the created "_.pluck" style |
4517 | * callback will return the property value of the given element. |
4518 | * |
4519 | * If an object is provided for `callback` the created "_.where" style callback |
4520 | * will return `true` for elements that have the properties of the given object, |
4521 | * else `false`. |
4522 | * |
4523 | * @static |
4524 | * @memberOf _ |
4525 | * @category Arrays |
4526 | * @param {Array} array The array to search. |
4527 | * @param {Function|Object|string} [callback=identity] The function called |
4528 | * per iteration. If a property name or object is provided it will be used |
4529 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4530 | * @param {*} [thisArg] The `this` binding of `callback`. |
4531 | * @returns {number} Returns the index of the found element, else `-1`. |
4532 | * @example |
4533 | * |
4534 | * var characters = [ |
4535 | * { 'name': 'barney', 'age': 36, 'blocked': false }, |
4536 | * { 'name': 'fred', 'age': 40, 'blocked': true }, |
4537 | * { 'name': 'pebbles', 'age': 1, 'blocked': false } |
4538 | * ]; |
4539 | * |
4540 | * _.findIndex(characters, function(chr) { |
4541 | * return chr.age < 20; |
4542 | * }); |
4543 | * // => 2 |
4544 | * |
4545 | * // using "_.where" callback shorthand |
4546 | * _.findIndex(characters, { 'age': 36 }); |
4547 | * // => 0 |
4548 | * |
4549 | * // using "_.pluck" callback shorthand |
4550 | * _.findIndex(characters, 'blocked'); |
4551 | * // => 1 |
4552 | */ |
4553 | function findIndex(array, callback, thisArg) { |
4554 | var index = -1, |
4555 | length = array ? array.length : 0; |
4556 | |
4557 | callback = lodash.createCallback(callback, thisArg, 3); |
4558 | while (++index < length) { |
4559 | if (callback(array[index], index, array)) { |
4560 | return index; |
4561 | } |
4562 | } |
4563 | return -1; |
4564 | } |
4565 | |
4566 | /** |
4567 | * This method is like `_.findIndex` except that it iterates over elements |
4568 | * of a `collection` from right to left. |
4569 | * |
4570 | * If a property name is provided for `callback` the created "_.pluck" style |
4571 | * callback will return the property value of the given element. |
4572 | * |
4573 | * If an object is provided for `callback` the created "_.where" style callback |
4574 | * will return `true` for elements that have the properties of the given object, |
4575 | * else `false`. |
4576 | * |
4577 | * @static |
4578 | * @memberOf _ |
4579 | * @category Arrays |
4580 | * @param {Array} array The array to search. |
4581 | * @param {Function|Object|string} [callback=identity] The function called |
4582 | * per iteration. If a property name or object is provided it will be used |
4583 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4584 | * @param {*} [thisArg] The `this` binding of `callback`. |
4585 | * @returns {number} Returns the index of the found element, else `-1`. |
4586 | * @example |
4587 | * |
4588 | * var characters = [ |
4589 | * { 'name': 'barney', 'age': 36, 'blocked': true }, |
4590 | * { 'name': 'fred', 'age': 40, 'blocked': false }, |
4591 | * { 'name': 'pebbles', 'age': 1, 'blocked': true } |
4592 | * ]; |
4593 | * |
4594 | * _.findLastIndex(characters, function(chr) { |
4595 | * return chr.age > 30; |
4596 | * }); |
4597 | * // => 1 |
4598 | * |
4599 | * // using "_.where" callback shorthand |
4600 | * _.findLastIndex(characters, { 'age': 36 }); |
4601 | * // => 0 |
4602 | * |
4603 | * // using "_.pluck" callback shorthand |
4604 | * _.findLastIndex(characters, 'blocked'); |
4605 | * // => 2 |
4606 | */ |
4607 | function findLastIndex(array, callback, thisArg) { |
4608 | var length = array ? array.length : 0; |
4609 | callback = lodash.createCallback(callback, thisArg, 3); |
4610 | while (length--) { |
4611 | if (callback(array[length], length, array)) { |
4612 | return length; |
4613 | } |
4614 | } |
4615 | return -1; |
4616 | } |
4617 | |
4618 | /** |
4619 | * Gets the first element or first `n` elements of an array. If a callback |
4620 | * is provided elements at the beginning of the array are returned as long |
4621 | * as the callback returns truey. The callback is bound to `thisArg` and |
4622 | * invoked with three arguments; (value, index, array). |
4623 | * |
4624 | * If a property name is provided for `callback` the created "_.pluck" style |
4625 | * callback will return the property value of the given element. |
4626 | * |
4627 | * If an object is provided for `callback` the created "_.where" style callback |
4628 | * will return `true` for elements that have the properties of the given object, |
4629 | * else `false`. |
4630 | * |
4631 | * @static |
4632 | * @memberOf _ |
4633 | * @alias head, take |
4634 | * @category Arrays |
4635 | * @param {Array} array The array to query. |
4636 | * @param {Function|Object|number|string} [callback] The function called |
4637 | * per element or the number of elements to return. If a property name or |
4638 | * object is provided it will be used to create a "_.pluck" or "_.where" |
4639 | * style callback, respectively. |
4640 | * @param {*} [thisArg] The `this` binding of `callback`. |
4641 | * @returns {*} Returns the first element(s) of `array`. |
4642 | * @example |
4643 | * |
4644 | * _.first([1, 2, 3]); |
4645 | * // => 1 |
4646 | * |
4647 | * _.first([1, 2, 3], 2); |
4648 | * // => [1, 2] |
4649 | * |
4650 | * _.first([1, 2, 3], function(num) { |
4651 | * return num < 3; |
4652 | * }); |
4653 | * // => [1, 2] |
4654 | * |
4655 | * var characters = [ |
4656 | * { 'name': 'barney', 'blocked': true, 'employer': 'slate' }, |
4657 | * { 'name': 'fred', 'blocked': false, 'employer': 'slate' }, |
4658 | * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' } |
4659 | * ]; |
4660 | * |
4661 | * // using "_.pluck" callback shorthand |
4662 | * _.first(characters, 'blocked'); |
4663 | * // => [{ 'name': 'barney', 'blocked': true, 'employer': 'slate' }] |
4664 | * |
4665 | * // using "_.where" callback shorthand |
4666 | * _.pluck(_.first(characters, { 'employer': 'slate' }), 'name'); |
4667 | * // => ['barney', 'fred'] |
4668 | */ |
4669 | function first(array, callback, thisArg) { |
4670 | var n = 0, |
4671 | length = array ? array.length : 0; |
4672 | |
4673 | if (typeof callback != 'number' && callback != null) { |
4674 | var index = -1; |
4675 | callback = lodash.createCallback(callback, thisArg, 3); |
4676 | while (++index < length && callback(array[index], index, array)) { |
4677 | n++; |
4678 | } |
4679 | } else { |
4680 | n = callback; |
4681 | if (n == null || thisArg) { |
4682 | return array ? array[0] : undefined; |
4683 | } |
4684 | } |
4685 | return slice(array, 0, nativeMin(nativeMax(0, n), length)); |
4686 | } |
4687 | |
4688 | /** |
4689 | * Flattens a nested array (the nesting can be to any depth). If `isShallow` |
4690 | * is truey, the array will only be flattened a single level. If a callback |
4691 | * is provided each element of the array is passed through the callback before |
4692 | * flattening. The callback is bound to `thisArg` and invoked with three |
4693 | * arguments; (value, index, array). |
4694 | * |
4695 | * If a property name is provided for `callback` the created "_.pluck" style |
4696 | * callback will return the property value of the given element. |
4697 | * |
4698 | * If an object is provided for `callback` the created "_.where" style callback |
4699 | * will return `true` for elements that have the properties of the given object, |
4700 | * else `false`. |
4701 | * |
4702 | * @static |
4703 | * @memberOf _ |
4704 | * @category Arrays |
4705 | * @param {Array} array The array to flatten. |
4706 | * @param {boolean} [isShallow=false] A flag to restrict flattening to a single level. |
4707 | * @param {Function|Object|string} [callback=identity] The function called |
4708 | * per iteration. If a property name or object is provided it will be used |
4709 | * to create a "_.pluck" or "_.where" style callback, respectively. |
4710 | * @param {*} [thisArg] The `this` binding of `callback`. |
4711 | * @returns {Array} Returns a new flattened array. |
4712 | * @example |
4713 | * |
4714 | * _.flatten([1, [2], [3, [[4]]]]); |
4715 | * // => [1, 2, 3, 4]; |
4716 | * |
4717 | * _.flatten([1, [2], [3, [[4]]]], true); |
4718 | * // => [1, 2, 3, [[4]]]; |
4719 | * |
4720 | * var characters = [ |
4721 | * { 'name': 'barney', 'age': 30, 'pets': ['hoppy'] }, |
4722 | * { 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] } |
4723 | * ]; |
4724 | * |
4725 | * // using "_.pluck" callback shorthand |
4726 | * _.flatten(characters, 'pets'); |
4727 | * // => ['hoppy', 'baby puss', 'dino'] |
4728 | */ |
4729 | function flatten(array, isShallow, callback, thisArg) { |
4730 | // juggle arguments |
4731 | if (typeof isShallow != 'boolean' && isShallow != null) { |
4732 | thisArg = callback; |
4733 | callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow; |
4734 | isShallow = false; |
4735 | } |
4736 | if (callback != null) { |
4737 | array = map(array, callback, thisArg); |
4738 | } |
4739 | return baseFlatten(array, isShallow); |
4740 | } |
4741 | |
4742 | /** |
4743 | * Gets the index at which the first occurrence of `value` is found using |
4744 | * strict equality for comparisons, i.e. `===`. If the array is already sorted |
4745 | * providing `true` for `fromIndex` will run a faster binary search. |
4746 | * |
4747 | * @static |
4748 | * @memberOf _ |
4749 | * @category Arrays |
4750 | * @param {Array} array The array to search. |
4751 | * @param {*} value The value to search for. |
4752 | * @param {boolean|number} [fromIndex=0] The index to search from or `true` |
4753 | * to perform a binary search on a sorted array. |
4754 | * @returns {number} Returns the index of the matched value or `-1`. |
4755 | * @example |
4756 | * |
4757 | * _.indexOf([1, 2, 3, 1, 2, 3], 2); |
4758 | * // => 1 |
4759 | * |
4760 | * _.indexOf([1, 2, 3, 1, 2, 3], 2, 3); |
4761 | * // => 4 |
4762 | * |
4763 | * _.indexOf([1, 1, 2, 2, 3, 3], 2, true); |
4764 | * // => 2 |
4765 | */ |
4766 | function indexOf(array, value, fromIndex) { |
4767 | if (typeof fromIndex == 'number') { |
4768 | var length = array ? array.length : 0; |
4769 | fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); |
4770 | } else if (fromIndex) { |
4771 | var index = sortedIndex(array, value); |
4772 | return array[index] === value ? index : -1; |
4773 | } |
4774 | return baseIndexOf(array, value, fromIndex); |
4775 | } |
4776 | |
4777 | /** |
4778 | * Gets all but the last element or last `n` elements of an array. If a |
4779 | * callback is provided elements at the end of the array are excluded from |
4780 | * the result as long as the callback returns truey. The callback is bound |
4781 | * to `thisArg` and invoked with three arguments; (value, index, array). |
4782 | * |
4783 | * If a property name is provided for `callback` the created "_.pluck" style |
4784 | * callback will return the property value of the given element. |
4785 | * |
4786 | * If an object is provided for `callback` the created "_.where" style callback |
4787 | * will return `true` for elements that have the properties of the given object, |
4788 | * else `false`. |
4789 | * |
4790 | * @static |
4791 | * @memberOf _ |
4792 | * @category Arrays |
4793 | * @param {Array} array The array to query. |
4794 | * @param {Function|Object|number|string} [callback=1] The function called |
4795 | * per element or the number of elements to exclude. If a property name or |
4796 | * object is provided it will be used to create a "_.pluck" or "_.where" |
4797 | * style callback, respectively. |
4798 | * @param {*} [thisArg] The `this` binding of `callback`. |
4799 | * @returns {Array} Returns a slice of `array`. |
4800 | * @example |
4801 | * |
4802 | * _.initial([1, 2, 3]); |
4803 | * // => [1, 2] |
4804 | * |
4805 | * _.initial([1, 2, 3], 2); |
4806 | * // => [1] |
4807 | * |
4808 | * _.initial([1, 2, 3], function(num) { |
4809 | * return num > 1; |
4810 | * }); |
4811 | * // => [1] |
4812 | * |
4813 | * var characters = [ |
4814 | * { 'name': 'barney', 'blocked': false, 'employer': 'slate' }, |
4815 | * { 'name': 'fred', 'blocked': true, 'employer': 'slate' }, |
4816 | * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' } |
4817 | * ]; |
4818 | * |
4819 | * // using "_.pluck" callback shorthand |
4820 | * _.initial(characters, 'blocked'); |
4821 | * // => [{ 'name': 'barney', 'blocked': false, 'employer': 'slate' }] |
4822 | * |
4823 | * // using "_.where" callback shorthand |
4824 | * _.pluck(_.initial(characters, { 'employer': 'na' }), 'name'); |
4825 | * // => ['barney', 'fred'] |
4826 | */ |
4827 | function initial(array, callback, thisArg) { |
4828 | var n = 0, |
4829 | length = array ? array.length : 0; |
4830 | |
4831 | if (typeof callback != 'number' && callback != null) { |
4832 | var index = length; |
4833 | callback = lodash.createCallback(callback, thisArg, 3); |
4834 | while (index-- && callback(array[index], index, array)) { |
4835 | n++; |
4836 | } |
4837 | } else { |
4838 | n = (callback == null || thisArg) ? 1 : callback || n; |
4839 | } |
4840 | return slice(array, 0, nativeMin(nativeMax(0, length - n), length)); |
4841 | } |
4842 | |
4843 | /** |
4844 | * Creates an array of unique values present in all provided arrays using |
4845 | * strict equality for comparisons, i.e. `===`. |
4846 | * |
4847 | * @static |
4848 | * @memberOf _ |
4849 | * @category Arrays |
4850 | * @param {...Array} [array] The arrays to inspect. |
4851 | * @returns {Array} Returns an array of shared values. |
4852 | * @example |
4853 | * |
4854 | * _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]); |
4855 | * // => [1, 2] |
4856 | */ |
4857 | function intersection() { |
4858 | var args = [], |
4859 | argsIndex = -1, |
4860 | argsLength = arguments.length, |
4861 | caches = getArray(), |
4862 | indexOf = getIndexOf(), |
4863 | trustIndexOf = indexOf === baseIndexOf, |
4864 | seen = getArray(); |
4865 | |
4866 | while (++argsIndex < argsLength) { |
4867 | var value = arguments[argsIndex]; |
4868 | if (isArray(value) || isArguments(value)) { |
4869 | args.push(value); |
4870 | caches.push(trustIndexOf && value.length >= largeArraySize && |
4871 | createCache(argsIndex ? args[argsIndex] : seen)); |
4872 | } |
4873 | } |
4874 | var array = args[0], |
4875 | index = -1, |
4876 | length = array ? array.length : 0, |
4877 | result = []; |
4878 | |
4879 | outer: |
4880 | while (++index < length) { |
4881 | var cache = caches[0]; |
4882 | value = array[index]; |
4883 | |
4884 | if ((cache ? cacheIndexOf(cache, value) : indexOf(seen, value)) < 0) { |
4885 | argsIndex = argsLength; |
4886 | (cache || seen).push(value); |
4887 | while (--argsIndex) { |
4888 | cache = caches[argsIndex]; |
4889 | if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) { |
4890 | continue outer; |
4891 | } |
4892 | } |
4893 | result.push(value); |
4894 | } |
4895 | } |
4896 | while (argsLength--) { |
4897 | cache = caches[argsLength]; |
4898 | if (cache) { |
4899 | releaseObject(cache); |
4900 | } |
4901 | } |
4902 | releaseArray(caches); |
4903 | releaseArray(seen); |
4904 | return result; |
4905 | } |
4906 | |
4907 | /** |
4908 | * Gets the last element or last `n` elements of an array. If a callback is |
4909 | * provided elements at the end of the array are returned as long as the |
4910 | * callback returns truey. The callback is bound to `thisArg` and invoked |
4911 | * with three arguments; (value, index, array). |
4912 | * |
4913 | * If a property name is provided for `callback` the created "_.pluck" style |
4914 | * callback will return the property value of the given element. |
4915 | * |
4916 | * If an object is provided for `callback` the created "_.where" style callback |
4917 | * will return `true` for elements that have the properties of the given object, |
4918 | * else `false`. |
4919 | * |
4920 | * @static |
4921 | * @memberOf _ |
4922 | * @category Arrays |
4923 | * @param {Array} array The array to query. |
4924 | * @param {Function|Object|number|string} [callback] The function called |
4925 | * per element or the number of elements to return. If a property name or |
4926 | * object is provided it will be used to create a "_.pluck" or "_.where" |
4927 | * style callback, respectively. |
4928 | * @param {*} [thisArg] The `this` binding of `callback`. |
4929 | * @returns {*} Returns the last element(s) of `array`. |
4930 | * @example |
4931 | * |
4932 | * _.last([1, 2, 3]); |
4933 | * // => 3 |
4934 | * |
4935 | * _.last([1, 2, 3], 2); |
4936 | * // => [2, 3] |
4937 | * |
4938 | * _.last([1, 2, 3], function(num) { |
4939 | * return num > 1; |
4940 | * }); |
4941 | * // => [2, 3] |
4942 | * |
4943 | * var characters = [ |
4944 | * { 'name': 'barney', 'blocked': false, 'employer': 'slate' }, |
4945 | * { 'name': 'fred', 'blocked': true, 'employer': 'slate' }, |
4946 | * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' } |
4947 | * ]; |
4948 | * |
4949 | * // using "_.pluck" callback shorthand |
4950 | * _.pluck(_.last(characters, 'blocked'), 'name'); |
4951 | * // => ['fred', 'pebbles'] |
4952 | * |
4953 | * // using "_.where" callback shorthand |
4954 | * _.last(characters, { 'employer': 'na' }); |
4955 | * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }] |
4956 | */ |
4957 | function last(array, callback, thisArg) { |
4958 | var n = 0, |
4959 | length = array ? array.length : 0; |
4960 | |
4961 | if (typeof callback != 'number' && callback != null) { |
4962 | var index = length; |
4963 | callback = lodash.createCallback(callback, thisArg, 3); |
4964 | while (index-- && callback(array[index], index, array)) { |
4965 | n++; |
4966 | } |
4967 | } else { |
4968 | n = callback; |
4969 | if (n == null || thisArg) { |
4970 | return array ? array[length - 1] : undefined; |
4971 | } |
4972 | } |
4973 | return slice(array, nativeMax(0, length - n)); |
4974 | } |
4975 | |
4976 | /** |
4977 | * Gets the index at which the last occurrence of `value` is found using strict |
4978 | * equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used |
4979 | * as the offset from the end of the collection. |
4980 | * |
4981 | * If a property name is provided for `callback` the created "_.pluck" style |
4982 | * callback will return the property value of the given element. |
4983 | * |
4984 | * If an object is provided for `callback` the created "_.where" style callback |
4985 | * will return `true` for elements that have the properties of the given object, |
4986 | * else `false`. |
4987 | * |
4988 | * @static |
4989 | * @memberOf _ |
4990 | * @category Arrays |
4991 | * @param {Array} array The array to search. |
4992 | * @param {*} value The value to search for. |
4993 | * @param {number} [fromIndex=array.length-1] The index to search from. |
4994 | * @returns {number} Returns the index of the matched value or `-1`. |
4995 | * @example |
4996 | * |
4997 | * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2); |
4998 | * // => 4 |
4999 | * |
5000 | * _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3); |
5001 | * // => 1 |
5002 | */ |
5003 | function lastIndexOf(array, value, fromIndex) { |
5004 | var index = array ? array.length : 0; |
5005 | if (typeof fromIndex == 'number') { |
5006 | index = (fromIndex < 0 ? nativeMax(0, index + fromIndex) : nativeMin(fromIndex, index - 1)) + 1; |
5007 | } |
5008 | while (index--) { |
5009 | if (array[index] === value) { |
5010 | return index; |
5011 | } |
5012 | } |
5013 | return -1; |
5014 | } |
5015 | |
5016 | /** |
5017 | * Removes all provided values from the given array using strict equality for |
5018 | * comparisons, i.e. `===`. |
5019 | * |
5020 | * @static |
5021 | * @memberOf _ |
5022 | * @category Arrays |
5023 | * @param {Array} array The array to modify. |
5024 | * @param {...*} [value] The values to remove. |
5025 | * @returns {Array} Returns `array`. |
5026 | * @example |
5027 | * |
5028 | * var array = [1, 2, 3, 1, 2, 3]; |
5029 | * _.pull(array, 2, 3); |
5030 | * console.log(array); |
5031 | * // => [1, 1] |
5032 | */ |
5033 | function pull(array) { |
5034 | var args = arguments, |
5035 | argsIndex = 0, |
5036 | argsLength = args.length, |
5037 | length = array ? array.length : 0; |
5038 | |
5039 | while (++argsIndex < argsLength) { |
5040 | var index = -1, |
5041 | value = args[argsIndex]; |
5042 | while (++index < length) { |
5043 | if (array[index] === value) { |
5044 | splice.call(array, index--, 1); |
5045 | length--; |
5046 | } |
5047 | } |
5048 | } |
5049 | return array; |
5050 | } |
5051 | |
5052 | /** |
5053 | * Creates an array of numbers (positive and/or negative) progressing from |
5054 | * `start` up to but not including `end`. If `start` is less than `stop` a |
5055 | * zero-length range is created unless a negative `step` is specified. |
5056 | * |
5057 | * @static |
5058 | * @memberOf _ |
5059 | * @category Arrays |
5060 | * @param {number} [start=0] The start of the range. |
5061 | * @param {number} end The end of the range. |
5062 | * @param {number} [step=1] The value to increment or decrement by. |
5063 | * @returns {Array} Returns a new range array. |
5064 | * @example |
5065 | * |
5066 | * _.range(4); |
5067 | * // => [0, 1, 2, 3] |
5068 | * |
5069 | * _.range(1, 5); |
5070 | * // => [1, 2, 3, 4] |
5071 | * |
5072 | * _.range(0, 20, 5); |
5073 | * // => [0, 5, 10, 15] |
5074 | * |
5075 | * _.range(0, -4, -1); |
5076 | * // => [0, -1, -2, -3] |
5077 | * |
5078 | * _.range(1, 4, 0); |
5079 | * // => [1, 1, 1] |
5080 | * |
5081 | * _.range(0); |
5082 | * // => [] |
5083 | */ |
5084 | function range(start, end, step) { |
5085 | start = +start || 0; |
5086 | step = typeof step == 'number' ? step : (+step || 1); |
5087 | |
5088 | if (end == null) { |
5089 | end = start; |
5090 | start = 0; |
5091 | } |
5092 | // use `Array(length)` so engines like Chakra and V8 avoid slower modes |
5093 | // http://youtu.be/XAqIpGU8ZZk#t=17m25s |
5094 | var index = -1, |
5095 | length = nativeMax(0, ceil((end - start) / (step || 1))), |
5096 | result = Array(length); |
5097 | |
5098 | while (++index < length) { |
5099 | result[index] = start; |
5100 | start += step; |
5101 | } |
5102 | return result; |
5103 | } |
5104 | |
5105 | /** |
5106 | * Removes all elements from an array that the callback returns truey for |
5107 | * and returns an array of removed elements. The callback is bound to `thisArg` |
5108 | * and invoked with three arguments; (value, index, array). |
5109 | * |
5110 | * If a property name is provided for `callback` the created "_.pluck" style |
5111 | * callback will return the property value of the given element. |
5112 | * |
5113 | * If an object is provided for `callback` the created "_.where" style callback |
5114 | * will return `true` for elements that have the properties of the given object, |
5115 | * else `false`. |
5116 | * |
5117 | * @static |
5118 | * @memberOf _ |
5119 | * @category Arrays |
5120 | * @param {Array} array The array to modify. |
5121 | * @param {Function|Object|string} [callback=identity] The function called |
5122 | * per iteration. If a property name or object is provided it will be used |
5123 | * to create a "_.pluck" or "_.where" style callback, respectively. |
5124 | * @param {*} [thisArg] The `this` binding of `callback`. |
5125 | * @returns {Array} Returns a new array of removed elements. |
5126 | * @example |
5127 | * |
5128 | * var array = [1, 2, 3, 4, 5, 6]; |
5129 | * var evens = _.remove(array, function(num) { return num % 2 == 0; }); |
5130 | * |
5131 | * console.log(array); |
5132 | * // => [1, 3, 5] |
5133 | * |
5134 | * console.log(evens); |
5135 | * // => [2, 4, 6] |
5136 | */ |
5137 | function remove(array, callback, thisArg) { |
5138 | var index = -1, |
5139 | length = array ? array.length : 0, |
5140 | result = []; |
5141 | |
5142 | callback = lodash.createCallback(callback, thisArg, 3); |
5143 | while (++index < length) { |
5144 | var value = array[index]; |
5145 | if (callback(value, index, array)) { |
5146 | result.push(value); |
5147 | splice.call(array, index--, 1); |
5148 | length--; |
5149 | } |
5150 | } |
5151 | return result; |
5152 | } |
5153 | |
5154 | /** |
5155 | * The opposite of `_.initial` this method gets all but the first element or |
5156 | * first `n` elements of an array. If a callback function is provided elements |
5157 | * at the beginning of the array are excluded from the result as long as the |
5158 | * callback returns truey. The callback is bound to `thisArg` and invoked |
5159 | * with three arguments; (value, index, array). |
5160 | * |
5161 | * If a property name is provided for `callback` the created "_.pluck" style |
5162 | * callback will return the property value of the given element. |
5163 | * |
5164 | * If an object is provided for `callback` the created "_.where" style callback |
5165 | * will return `true` for elements that have the properties of the given object, |
5166 | * else `false`. |
5167 | * |
5168 | * @static |
5169 | * @memberOf _ |
5170 | * @alias drop, tail |
5171 | * @category Arrays |
5172 | * @param {Array} array The array to query. |
5173 | * @param {Function|Object|number|string} [callback=1] The function called |
5174 | * per element or the number of elements to exclude. If a property name or |
5175 | * object is provided it will be used to create a "_.pluck" or "_.where" |
5176 | * style callback, respectively. |
5177 | * @param {*} [thisArg] The `this` binding of `callback`. |
5178 | * @returns {Array} Returns a slice of `array`. |
5179 | * @example |
5180 | * |
5181 | * _.rest([1, 2, 3]); |
5182 | * // => [2, 3] |
5183 | * |
5184 | * _.rest([1, 2, 3], 2); |
5185 | * // => [3] |
5186 | * |
5187 | * _.rest([1, 2, 3], function(num) { |
5188 | * return num < 3; |
5189 | * }); |
5190 | * // => [3] |
5191 | * |
5192 | * var characters = [ |
5193 | * { 'name': 'barney', 'blocked': true, 'employer': 'slate' }, |
5194 | * { 'name': 'fred', 'blocked': false, 'employer': 'slate' }, |
5195 | * { 'name': 'pebbles', 'blocked': true, 'employer': 'na' } |
5196 | * ]; |
5197 | * |
5198 | * // using "_.pluck" callback shorthand |
5199 | * _.pluck(_.rest(characters, 'blocked'), 'name'); |
5200 | * // => ['fred', 'pebbles'] |
5201 | * |
5202 | * // using "_.where" callback shorthand |
5203 | * _.rest(characters, { 'employer': 'slate' }); |
5204 | * // => [{ 'name': 'pebbles', 'blocked': true, 'employer': 'na' }] |
5205 | */ |
5206 | function rest(array, callback, thisArg) { |
5207 | if (typeof callback != 'number' && callback != null) { |
5208 | var n = 0, |
5209 | index = -1, |
5210 | length = array ? array.length : 0; |
5211 | |
5212 | callback = lodash.createCallback(callback, thisArg, 3); |
5213 | while (++index < length && callback(array[index], index, array)) { |
5214 | n++; |
5215 | } |
5216 | } else { |
5217 | n = (callback == null || thisArg) ? 1 : nativeMax(0, callback); |
5218 | } |
5219 | return slice(array, n); |
5220 | } |
5221 | |
5222 | /** |
5223 | * Uses a binary search to determine the smallest index at which a value |
5224 | * should be inserted into a given sorted array in order to maintain the sort |
5225 | * order of the array. If a callback is provided it will be executed for |
5226 | * `value` and each element of `array` to compute their sort ranking. The |
5227 | * callback is bound to `thisArg` and invoked with one argument; (value). |
5228 | * |
5229 | * If a property name is provided for `callback` the created "_.pluck" style |
5230 | * callback will return the property value of the given element. |
5231 | * |
5232 | * If an object is provided for `callback` the created "_.where" style callback |
5233 | * will return `true` for elements that have the properties of the given object, |
5234 | * else `false`. |
5235 | * |
5236 | * @static |
5237 | * @memberOf _ |
5238 | * @category Arrays |
5239 | * @param {Array} array The array to inspect. |
5240 | * @param {*} value The value to evaluate. |
5241 | * @param {Function|Object|string} [callback=identity] The function called |
5242 | * per iteration. If a property name or object is provided it will be used |
5243 | * to create a "_.pluck" or "_.where" style callback, respectively. |
5244 | * @param {*} [thisArg] The `this` binding of `callback`. |
5245 | * @returns {number} Returns the index at which `value` should be inserted |
5246 | * into `array`. |
5247 | * @example |
5248 | * |
5249 | * _.sortedIndex([20, 30, 50], 40); |
5250 | * // => 2 |
5251 | * |
5252 | * // using "_.pluck" callback shorthand |
5253 | * _.sortedIndex([{ 'x': 20 }, { 'x': 30 }, { 'x': 50 }], { 'x': 40 }, 'x'); |
5254 | * // => 2 |
5255 | * |
5256 | * var dict = { |
5257 | * 'wordToNumber': { 'twenty': 20, 'thirty': 30, 'fourty': 40, 'fifty': 50 } |
5258 | * }; |
5259 | * |
5260 | * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) { |
5261 | * return dict.wordToNumber[word]; |
5262 | * }); |
5263 | * // => 2 |
5264 | * |
5265 | * _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) { |
5266 | * return this.wordToNumber[word]; |
5267 | * }, dict); |
5268 | * // => 2 |
5269 | */ |
5270 | function sortedIndex(array, value, callback, thisArg) { |
5271 | var low = 0, |
5272 | high = array ? array.length : low; |
5273 | |
5274 | // explicitly reference `identity` for better inlining in Firefox |
5275 | callback = callback ? lodash.createCallback(callback, thisArg, 1) : identity; |
5276 | value = callback(value); |
5277 | |
5278 | while (low < high) { |
5279 | var mid = (low + high) >>> 1; |
5280 | (callback(array[mid]) < value) |
5281 | ? low = mid + 1 |
5282 | : high = mid; |
5283 | } |
5284 | return low; |
5285 | } |
5286 | |
5287 | /** |
5288 | * Creates an array of unique values, in order, of the provided arrays using |
5289 | * strict equality for comparisons, i.e. `===`. |
5290 | * |
5291 | * @static |
5292 | * @memberOf _ |
5293 | * @category Arrays |
5294 | * @param {...Array} [array] The arrays to inspect. |
5295 | * @returns {Array} Returns an array of combined values. |
5296 | * @example |
5297 | * |
5298 | * _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]); |
5299 | * // => [1, 2, 3, 5, 4] |
5300 | */ |
5301 | function union() { |
5302 | return baseUniq(baseFlatten(arguments, true, true)); |
5303 | } |
5304 | |
5305 | /** |
5306 | * Creates a duplicate-value-free version of an array using strict equality |
5307 | * for comparisons, i.e. `===`. If the array is sorted, providing |
5308 | * `true` for `isSorted` will use a faster algorithm. If a callback is provided |
5309 | * each element of `array` is passed through the callback before uniqueness |
5310 | * is computed. The callback is bound to `thisArg` and invoked with three |
5311 | * arguments; (value, index, array). |
5312 | * |
5313 | * If a property name is provided for `callback` the created "_.pluck" style |
5314 | * callback will return the property value of the given element. |
5315 | * |
5316 | * If an object is provided for `callback` the created "_.where" style callback |
5317 | * will return `true` for elements that have the properties of the given object, |
5318 | * else `false`. |
5319 | * |
5320 | * @static |
5321 | * @memberOf _ |
5322 | * @alias unique |
5323 | * @category Arrays |
5324 | * @param {Array} array The array to process. |
5325 | * @param {boolean} [isSorted=false] A flag to indicate that `array` is sorted. |
5326 | * @param {Function|Object|string} [callback=identity] The function called |
5327 | * per iteration. If a property name or object is provided it will be used |
5328 | * to create a "_.pluck" or "_.where" style callback, respectively. |
5329 | * @param {*} [thisArg] The `this` binding of `callback`. |
5330 | * @returns {Array} Returns a duplicate-value-free array. |
5331 | * @example |
5332 | * |
5333 | * _.uniq([1, 2, 1, 3, 1]); |
5334 | * // => [1, 2, 3] |
5335 | * |
5336 | * _.uniq([1, 1, 2, 2, 3], true); |
5337 | * // => [1, 2, 3] |
5338 | * |
5339 | * _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); }); |
5340 | * // => ['A', 'b', 'C'] |
5341 | * |
5342 | * _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math); |
5343 | * // => [1, 2.5, 3] |
5344 | * |
5345 | * // using "_.pluck" callback shorthand |
5346 | * _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); |
5347 | * // => [{ 'x': 1 }, { 'x': 2 }] |
5348 | */ |
5349 | function uniq(array, isSorted, callback, thisArg) { |
5350 | // juggle arguments |
5351 | if (typeof isSorted != 'boolean' && isSorted != null) { |
5352 | thisArg = callback; |
5353 | callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted; |
5354 | isSorted = false; |
5355 | } |
5356 | if (callback != null) { |
5357 | callback = lodash.createCallback(callback, thisArg, 3); |
5358 | } |
5359 | return baseUniq(array, isSorted, callback); |
5360 | } |
5361 | |
5362 | /** |
5363 | * Creates an array excluding all provided values using strict equality for |
5364 | * comparisons, i.e. `===`. |
5365 | * |
5366 | * @static |
5367 | * @memberOf _ |
5368 | * @category Arrays |
5369 | * @param {Array} array The array to filter. |
5370 | * @param {...*} [value] The values to exclude. |
5371 | * @returns {Array} Returns a new array of filtered values. |
5372 | * @example |
5373 | * |
5374 | * _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); |
5375 | * // => [2, 3, 4] |
5376 | */ |
5377 | function without(array) { |
5378 | return baseDifference(array, slice(arguments, 1)); |
5379 | } |
5380 | |
5381 | /** |
5382 | * Creates an array that is the symmetric difference of the provided arrays. |
5383 | * See http://en.wikipedia.org/wiki/Symmetric_difference. |
5384 | * |
5385 | * @static |
5386 | * @memberOf _ |
5387 | * @category Arrays |
5388 | * @param {...Array} [array] The arrays to inspect. |
5389 | * @returns {Array} Returns an array of values. |
5390 | * @example |
5391 | * |
5392 | * _.xor([1, 2, 3], [5, 2, 1, 4]); |
5393 | * // => [3, 5, 4] |
5394 | * |
5395 | * _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]); |
5396 | * // => [1, 4, 5] |
5397 | */ |
5398 | function xor() { |
5399 | var index = -1, |
5400 | length = arguments.length; |
5401 | |
5402 | while (++index < length) { |
5403 | var array = arguments[index]; |
5404 | if (isArray(array) || isArguments(array)) { |
5405 | var result = result |
5406 | ? baseUniq(baseDifference(result, array).concat(baseDifference(array, result))) |
5407 | : array; |
5408 | } |
5409 | } |
5410 | return result || []; |
5411 | } |
5412 | |
5413 | /** |
5414 | * Creates an array of grouped elements, the first of which contains the first |
5415 | * elements of the given arrays, the second of which contains the second |
5416 | * elements of the given arrays, and so on. |
5417 | * |
5418 | * @static |
5419 | * @memberOf _ |
5420 | * @alias unzip |
5421 | * @category Arrays |
5422 | * @param {...Array} [array] Arrays to process. |
5423 | * @returns {Array} Returns a new array of grouped elements. |
5424 | * @example |
5425 | * |
5426 | * _.zip(['fred', 'barney'], [30, 40], [true, false]); |
5427 | * // => [['fred', 30, true], ['barney', 40, false]] |
5428 | */ |
5429 | function zip() { |
5430 | var array = arguments.length > 1 ? arguments : arguments[0], |
5431 | index = -1, |
5432 | length = array ? max(pluck(array, 'length')) : 0, |
5433 | result = Array(length < 0 ? 0 : length); |
5434 | |
5435 | while (++index < length) { |
5436 | result[index] = pluck(array, index); |
5437 | } |
5438 | return result; |
5439 | } |
5440 | |
5441 | /** |
5442 | * Creates an object composed from arrays of `keys` and `values`. Provide |
5443 | * either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]` |
5444 | * or two arrays, one of `keys` and one of corresponding `values`. |
5445 | * |
5446 | * @static |
5447 | * @memberOf _ |
5448 | * @alias object |
5449 | * @category Arrays |
5450 | * @param {Array} keys The array of keys. |
5451 | * @param {Array} [values=[]] The array of values. |
5452 | * @returns {Object} Returns an object composed of the given keys and |
5453 | * corresponding values. |
5454 | * @example |
5455 | * |
5456 | * _.zipObject(['fred', 'barney'], [30, 40]); |
5457 | * // => { 'fred': 30, 'barney': 40 } |
5458 | */ |
5459 | function zipObject(keys, values) { |
5460 | var index = -1, |
5461 | length = keys ? keys.length : 0, |
5462 | result = {}; |
5463 | |
5464 | if (!values && length && !isArray(keys[0])) { |
5465 | values = []; |
5466 | } |
5467 | while (++index < length) { |
5468 | var key = keys[index]; |
5469 | if (values) { |
5470 | result[key] = values[index]; |
5471 | } else if (key) { |
5472 | result[key[0]] = key[1]; |
5473 | } |
5474 | } |
5475 | return result; |
5476 | } |
5477 | |
5478 | /*--------------------------------------------------------------------------*/ |
5479 | |
5480 | /** |
5481 | * Creates a function that executes `func`, with the `this` binding and |
5482 | * arguments of the created function, only after being called `n` times. |
5483 | * |
5484 | * @static |
5485 | * @memberOf _ |
5486 | * @category Functions |
5487 | * @param {number} n The number of times the function must be called before |
5488 | * `func` is executed. |
5489 | * @param {Function} func The function to restrict. |
5490 | * @returns {Function} Returns the new restricted function. |
5491 | * @example |
5492 | * |
5493 | * var saves = ['profile', 'settings']; |
5494 | * |
5495 | * var done = _.after(saves.length, function() { |
5496 | * console.log('Done saving!'); |
5497 | * }); |
5498 | * |
5499 | * _.forEach(saves, function(type) { |
5500 | * asyncSave({ 'type': type, 'complete': done }); |
5501 | * }); |
5502 | * // => logs 'Done saving!', after all saves have completed |
5503 | */ |
5504 | function after(n, func) { |
5505 | if (!isFunction(func)) { |
5506 | throw new TypeError; |
5507 | } |
5508 | return function() { |
5509 | if (--n < 1) { |
5510 | return func.apply(this, arguments); |
5511 | } |
5512 | }; |
5513 | } |
5514 | |
5515 | /** |
5516 | * Creates a function that, when called, invokes `func` with the `this` |
5517 | * binding of `thisArg` and prepends any additional `bind` arguments to those |
5518 | * provided to the bound function. |
5519 | * |
5520 | * @static |
5521 | * @memberOf _ |
5522 | * @category Functions |
5523 | * @param {Function} func The function to bind. |
5524 | * @param {*} [thisArg] The `this` binding of `func`. |
5525 | * @param {...*} [arg] Arguments to be partially applied. |
5526 | * @returns {Function} Returns the new bound function. |
5527 | * @example |
5528 | * |
5529 | * var func = function(greeting) { |
5530 | * return greeting + ' ' + this.name; |
5531 | * }; |
5532 | * |
5533 | * func = _.bind(func, { 'name': 'fred' }, 'hi'); |
5534 | * func(); |
5535 | * // => 'hi fred' |
5536 | */ |
5537 | function bind(func, thisArg) { |
5538 | return arguments.length > 2 |
5539 | ? createWrapper(func, 17, slice(arguments, 2), null, thisArg) |
5540 | : createWrapper(func, 1, null, null, thisArg); |
5541 | } |
5542 | |
5543 | /** |
5544 | * Binds methods of an object to the object itself, overwriting the existing |
5545 | * method. Method names may be specified as individual arguments or as arrays |
5546 | * of method names. If no method names are provided all the function properties |
5547 | * of `object` will be bound. |
5548 | * |
5549 | * @static |
5550 | * @memberOf _ |
5551 | * @category Functions |
5552 | * @param {Object} object The object to bind and assign the bound methods to. |
5553 | * @param {...string} [methodName] The object method names to |
5554 | * bind, specified as individual method names or arrays of method names. |
5555 | * @returns {Object} Returns `object`. |
5556 | * @example |
5557 | * |
5558 | * var view = { |
5559 | * 'label': 'docs', |
5560 | * 'onClick': function() { console.log('clicked ' + this.label); } |
5561 | * }; |
5562 | * |
5563 | * _.bindAll(view); |
5564 | * jQuery('#docs').on('click', view.onClick); |
5565 | * // => logs 'clicked docs', when the button is clicked |
5566 | */ |
5567 | function bindAll(object) { |
5568 | var funcs = arguments.length > 1 ? baseFlatten(arguments, true, false, 1) : functions(object), |
5569 | index = -1, |
5570 | length = funcs.length; |
5571 | |
5572 | while (++index < length) { |
5573 | var key = funcs[index]; |
5574 | object[key] = createWrapper(object[key], 1, null, null, object); |
5575 | } |
5576 | return object; |
5577 | } |
5578 | |
5579 | /** |
5580 | * Creates a function that, when called, invokes the method at `object[key]` |
5581 | * and prepends any additional `bindKey` arguments to those provided to the bound |
5582 | * function. This method differs from `_.bind` by allowing bound functions to |
5583 | * reference methods that will be redefined or don't yet exist. |
5584 | * See http://michaux.ca/articles/lazy-function-definition-pattern. |
5585 | * |
5586 | * @static |
5587 | * @memberOf _ |
5588 | * @category Functions |
5589 | * @param {Object} object The object the method belongs to. |
5590 | * @param {string} key The key of the method. |
5591 | * @param {...*} [arg] Arguments to be partially applied. |
5592 | * @returns {Function} Returns the new bound function. |
5593 | * @example |
5594 | * |
5595 | * var object = { |
5596 | * 'name': 'fred', |
5597 | * 'greet': function(greeting) { |
5598 | * return greeting + ' ' + this.name; |
5599 | * } |
5600 | * }; |
5601 | * |
5602 | * var func = _.bindKey(object, 'greet', 'hi'); |
5603 | * func(); |
5604 | * // => 'hi fred' |
5605 | * |
5606 | * object.greet = function(greeting) { |
5607 | * return greeting + 'ya ' + this.name + '!'; |
5608 | * }; |
5609 | * |
5610 | * func(); |
5611 | * // => 'hiya fred!' |
5612 | */ |
5613 | function bindKey(object, key) { |
5614 | return arguments.length > 2 |
5615 | ? createWrapper(key, 19, slice(arguments, 2), null, object) |
5616 | : createWrapper(key, 3, null, null, object); |
5617 | } |
5618 | |
5619 | /** |
5620 | * Creates a function that is the composition of the provided functions, |
5621 | * where each function consumes the return value of the function that follows. |
5622 | * For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`. |
5623 | * Each function is executed with the `this` binding of the composed function. |
5624 | * |
5625 | * @static |
5626 | * @memberOf _ |
5627 | * @category Functions |
5628 | * @param {...Function} [func] Functions to compose. |
5629 | * @returns {Function} Returns the new composed function. |
5630 | * @example |
5631 | * |
5632 | * var realNameMap = { |
5633 | * 'pebbles': 'penelope' |
5634 | * }; |
5635 | * |
5636 | * var format = function(name) { |
5637 | * name = realNameMap[name.toLowerCase()] || name; |
5638 | * return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase(); |
5639 | * }; |
5640 | * |
5641 | * var greet = function(formatted) { |
5642 | * return 'Hiya ' + formatted + '!'; |
5643 | * }; |
5644 | * |
5645 | * var welcome = _.compose(greet, format); |
5646 | * welcome('pebbles'); |
5647 | * // => 'Hiya Penelope!' |
5648 | */ |
5649 | function compose() { |
5650 | var funcs = arguments, |
5651 | length = funcs.length; |
5652 | |
5653 | while (length--) { |
5654 | if (!isFunction(funcs[length])) { |
5655 | throw new TypeError; |
5656 | } |
5657 | } |
5658 | return function() { |
5659 | var args = arguments, |
5660 | length = funcs.length; |
5661 | |
5662 | while (length--) { |
5663 | args = [funcs[length].apply(this, args)]; |
5664 | } |
5665 | return args[0]; |
5666 | }; |
5667 | } |
5668 | |
5669 | /** |
5670 | * Creates a function which accepts one or more arguments of `func` that when |
5671 | * invoked either executes `func` returning its result, if all `func` arguments |
5672 | * have been provided, or returns a function that accepts one or more of the |
5673 | * remaining `func` arguments, and so on. The arity of `func` can be specified |
5674 | * if `func.length` is not sufficient. |
5675 | * |
5676 | * @static |
5677 | * @memberOf _ |
5678 | * @category Functions |
5679 | * @param {Function} func The function to curry. |
5680 | * @param {number} [arity=func.length] The arity of `func`. |
5681 | * @returns {Function} Returns the new curried function. |
5682 | * @example |
5683 | * |
5684 | * var curried = _.curry(function(a, b, c) { |
5685 | * console.log(a + b + c); |
5686 | * }); |
5687 | * |
5688 | * curried(1)(2)(3); |
5689 | * // => 6 |
5690 | * |
5691 | * curried(1, 2)(3); |
5692 | * // => 6 |
5693 | * |
5694 | * curried(1, 2, 3); |
5695 | * // => 6 |
5696 | */ |
5697 | function curry(func, arity) { |
5698 | arity = typeof arity == 'number' ? arity : (+arity || func.length); |
5699 | return createWrapper(func, 4, null, null, null, arity); |
5700 | } |
5701 | |
5702 | /** |
5703 | * Creates a function that will delay the execution of `func` until after |
5704 | * `wait` milliseconds have elapsed since the last time it was invoked. |
5705 | * Provide an options object to indicate that `func` should be invoked on |
5706 | * the leading and/or trailing edge of the `wait` timeout. Subsequent calls |
5707 | * to the debounced function will return the result of the last `func` call. |
5708 | * |
5709 | * Note: If `leading` and `trailing` options are `true` `func` will be called |
5710 | * on the trailing edge of the timeout only if the the debounced function is |
5711 | * invoked more than once during the `wait` timeout. |
5712 | * |
5713 | * @static |
5714 | * @memberOf _ |
5715 | * @category Functions |
5716 | * @param {Function} func The function to debounce. |
5717 | * @param {number} wait The number of milliseconds to delay. |
5718 | * @param {Object} [options] The options object. |
5719 | * @param {boolean} [options.leading=false] Specify execution on the leading edge of the timeout. |
5720 | * @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's called. |
5721 | * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout. |
5722 | * @returns {Function} Returns the new debounced function. |
5723 | * @example |
5724 | * |
5725 | * // avoid costly calculations while the window size is in flux |
5726 | * var lazyLayout = _.debounce(calculateLayout, 150); |
5727 | * jQuery(window).on('resize', lazyLayout); |
5728 | * |
5729 | * // execute `sendMail` when the click event is fired, debouncing subsequent calls |
5730 | * jQuery('#postbox').on('click', _.debounce(sendMail, 300, { |
5731 | * 'leading': true, |
5732 | * 'trailing': false |
5733 | * }); |
5734 | * |
5735 | * // ensure `batchLog` is executed once after 1 second of debounced calls |
5736 | * var source = new EventSource('/stream'); |
5737 | * source.addEventListener('message', _.debounce(batchLog, 250, { |
5738 | * 'maxWait': 1000 |
5739 | * }, false); |
5740 | */ |
5741 | function debounce(func, wait, options) { |
5742 | var args, |
5743 | maxTimeoutId, |
5744 | result, |
5745 | stamp, |
5746 | thisArg, |
5747 | timeoutId, |
5748 | trailingCall, |
5749 | lastCalled = 0, |
5750 | maxWait = false, |
5751 | trailing = true; |
5752 | |
5753 | if (!isFunction(func)) { |
5754 | throw new TypeError; |
5755 | } |
5756 | wait = nativeMax(0, wait) || 0; |
5757 | if (options === true) { |
5758 | var leading = true; |
5759 | trailing = false; |
5760 | } else if (isObject(options)) { |
5761 | leading = options.leading; |
5762 | maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0); |
5763 | trailing = 'trailing' in options ? options.trailing : trailing; |
5764 | } |
5765 | var delayed = function() { |
5766 | var remaining = wait - (now() - stamp); |
5767 | if (remaining <= 0) { |
5768 | if (maxTimeoutId) { |
5769 | clearTimeout(maxTimeoutId); |
5770 | } |
5771 | var isCalled = trailingCall; |
5772 | maxTimeoutId = timeoutId = trailingCall = undefined; |
5773 | if (isCalled) { |
5774 | lastCalled = now(); |
5775 | result = func.apply(thisArg, args); |
5776 | if (!timeoutId && !maxTimeoutId) { |
5777 | args = thisArg = null; |
5778 | } |
5779 | } |
5780 | } else { |
5781 | timeoutId = setTimeout(delayed, remaining); |
5782 | } |
5783 | }; |
5784 | |
5785 | var maxDelayed = function() { |
5786 | if (timeoutId) { |
5787 | clearTimeout(timeoutId); |
5788 | } |
5789 | maxTimeoutId = timeoutId = trailingCall = undefined; |
5790 | if (trailing || (maxWait !== wait)) { |
5791 | lastCalled = now(); |
5792 | result = func.apply(thisArg, args); |
5793 | if (!timeoutId && !maxTimeoutId) { |
5794 | args = thisArg = null; |
5795 | } |
5796 | } |
5797 | }; |
5798 | |
5799 | return function() { |
5800 | args = arguments; |
5801 | stamp = now(); |
5802 | thisArg = this; |
5803 | trailingCall = trailing && (timeoutId || !leading); |
5804 | |
5805 | if (maxWait === false) { |
5806 | var leadingCall = leading && !timeoutId; |
5807 | } else { |
5808 | if (!maxTimeoutId && !leading) { |
5809 | lastCalled = stamp; |
5810 | } |
5811 | var remaining = maxWait - (stamp - lastCalled), |
5812 | isCalled = remaining <= 0; |
5813 | |
5814 | if (isCalled) { |
5815 | if (maxTimeoutId) { |
5816 | maxTimeoutId = clearTimeout(maxTimeoutId); |
5817 | } |
5818 | lastCalled = stamp; |
5819 | result = func.apply(thisArg, args); |
5820 | } |
5821 | else if (!maxTimeoutId) { |
5822 | maxTimeoutId = setTimeout(maxDelayed, remaining); |
5823 | } |
5824 | } |
5825 | if (isCalled && timeoutId) { |
5826 | timeoutId = clearTimeout(timeoutId); |
5827 | } |
5828 | else if (!timeoutId && wait !== maxWait) { |
5829 | timeoutId = setTimeout(delayed, wait); |
5830 | } |
5831 | if (leadingCall) { |
5832 | isCalled = true; |
5833 | result = func.apply(thisArg, args); |
5834 | } |
5835 | if (isCalled && !timeoutId && !maxTimeoutId) { |
5836 | args = thisArg = null; |
5837 | } |
5838 | return result; |
5839 | }; |
5840 | } |
5841 | |
5842 | /** |
5843 | * Defers executing the `func` function until the current call stack has cleared. |
5844 | * Additional arguments will be provided to `func` when it is invoked. |
5845 | * |
5846 | * @static |
5847 | * @memberOf _ |
5848 | * @category Functions |
5849 | * @param {Function} func The function to defer. |
5850 | * @param {...*} [arg] Arguments to invoke the function with. |
5851 | * @returns {number} Returns the timer id. |
5852 | * @example |
5853 | * |
5854 | * _.defer(function(text) { console.log(text); }, 'deferred'); |
5855 | * // logs 'deferred' after one or more milliseconds |
5856 | */ |
5857 | function defer(func) { |
5858 | if (!isFunction(func)) { |
5859 | throw new TypeError; |
5860 | } |
5861 | var args = slice(arguments, 1); |
5862 | return setTimeout(function() { func.apply(undefined, args); }, 1); |
5863 | } |
5864 | |
5865 | /** |
5866 | * Executes the `func` function after `wait` milliseconds. Additional arguments |
5867 | * will be provided to `func` when it is invoked. |
5868 | * |
5869 | * @static |
5870 | * @memberOf _ |
5871 | * @category Functions |
5872 | * @param {Function} func The function to delay. |
5873 | * @param {number} wait The number of milliseconds to delay execution. |
5874 | * @param {...*} [arg] Arguments to invoke the function with. |
5875 | * @returns {number} Returns the timer id. |
5876 | * @example |
5877 | * |
5878 | * _.delay(function(text) { console.log(text); }, 1000, 'later'); |
5879 | * // => logs 'later' after one second |
5880 | */ |
5881 | function delay(func, wait) { |
5882 | if (!isFunction(func)) { |
5883 | throw new TypeError; |
5884 | } |
5885 | var args = slice(arguments, 2); |
5886 | return setTimeout(function() { func.apply(undefined, args); }, wait); |
5887 | } |
5888 | |
5889 | /** |
5890 | * Creates a function that memoizes the result of `func`. If `resolver` is |
5891 | * provided it will be used to determine the cache key for storing the result |
5892 | * based on the arguments provided to the memoized function. By default, the |
5893 | * first argument provided to the memoized function is used as the cache key. |
5894 | * The `func` is executed with the `this` binding of the memoized function. |
5895 | * The result cache is exposed as the `cache` property on the memoized function. |
5896 | * |
5897 | * @static |
5898 | * @memberOf _ |
5899 | * @category Functions |
5900 | * @param {Function} func The function to have its output memoized. |
5901 | * @param {Function} [resolver] A function used to resolve the cache key. |
5902 | * @returns {Function} Returns the new memoizing function. |
5903 | * @example |
5904 | * |
5905 | * var fibonacci = _.memoize(function(n) { |
5906 | * return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); |
5907 | * }); |
5908 | * |
5909 | * fibonacci(9) |
5910 | * // => 34 |
5911 | * |
5912 | * var data = { |
5913 | * 'fred': { 'name': 'fred', 'age': 40 }, |
5914 | * 'pebbles': { 'name': 'pebbles', 'age': 1 } |
5915 | * }; |
5916 | * |
5917 | * // modifying the result cache |
5918 | * var get = _.memoize(function(name) { return data[name]; }, _.identity); |
5919 | * get('pebbles'); |
5920 | * // => { 'name': 'pebbles', 'age': 1 } |
5921 | * |
5922 | * get.cache.pebbles.name = 'penelope'; |
5923 | * get('pebbles'); |
5924 | * // => { 'name': 'penelope', 'age': 1 } |
5925 | */ |
5926 | function memoize(func, resolver) { |
5927 | if (!isFunction(func)) { |
5928 | throw new TypeError; |
5929 | } |
5930 | var memoized = function() { |
5931 | var cache = memoized.cache, |
5932 | key = resolver ? resolver.apply(this, arguments) : keyPrefix + arguments[0]; |
5933 | |
5934 | return hasOwnProperty.call(cache, key) |
5935 | ? cache[key] |
5936 | : (cache[key] = func.apply(this, arguments)); |
5937 | } |
5938 | memoized.cache = {}; |
5939 | return memoized; |
5940 | } |
5941 | |
5942 | /** |
5943 | * Creates a function that is restricted to execute `func` once. Repeat calls to |
5944 | * the function will return the value of the first call. The `func` is executed |
5945 | * with the `this` binding of the created function. |
5946 | * |
5947 | * @static |
5948 | * @memberOf _ |
5949 | * @category Functions |
5950 | * @param {Function} func The function to restrict. |
5951 | * @returns {Function} Returns the new restricted function. |
5952 | * @example |
5953 | * |
5954 | * var initialize = _.once(createApplication); |
5955 | * initialize(); |
5956 | * initialize(); |
5957 | * // `initialize` executes `createApplication` once |
5958 | */ |
5959 | function once(func) { |
5960 | var ran, |
5961 | result; |
5962 | |
5963 | if (!isFunction(func)) { |
5964 | throw new TypeError; |
5965 | } |
5966 | return function() { |
5967 | if (ran) { |
5968 | return result; |
5969 | } |
5970 | ran = true; |
5971 | result = func.apply(this, arguments); |
5972 | |
5973 | // clear the `func` variable so the function may be garbage collected |
5974 | func = null; |
5975 | return result; |
5976 | }; |
5977 | } |
5978 | |
5979 | /** |
5980 | * Creates a function that, when called, invokes `func` with any additional |
5981 | * `partial` arguments prepended to those provided to the new function. This |
5982 | * method is similar to `_.bind` except it does **not** alter the `this` binding. |
5983 | * |
5984 | * @static |
5985 | * @memberOf _ |
5986 | * @category Functions |
5987 | * @param {Function} func The function to partially apply arguments to. |
5988 | * @param {...*} [arg] Arguments to be partially applied. |
5989 | * @returns {Function} Returns the new partially applied function. |
5990 | * @example |
5991 | * |
5992 | * var greet = function(greeting, name) { return greeting + ' ' + name; }; |
5993 | * var hi = _.partial(greet, 'hi'); |
5994 | * hi('fred'); |
5995 | * // => 'hi fred' |
5996 | */ |
5997 | function partial(func) { |
5998 | return createWrapper(func, 16, slice(arguments, 1)); |
5999 | } |
6000 | |
6001 | /** |
6002 | * This method is like `_.partial` except that `partial` arguments are |
6003 | * appended to those provided to the new function. |
6004 | * |
6005 | * @static |
6006 | * @memberOf _ |
6007 | * @category Functions |
6008 | * @param {Function} func The function to partially apply arguments to. |
6009 | * @param {...*} [arg] Arguments to be partially applied. |
6010 | * @returns {Function} Returns the new partially applied function. |
6011 | * @example |
6012 | * |
6013 | * var defaultsDeep = _.partialRight(_.merge, _.defaults); |
6014 | * |
6015 | * var options = { |
6016 | * 'variable': 'data', |
6017 | * 'imports': { 'jq': $ } |
6018 | * }; |
6019 | * |
6020 | * defaultsDeep(options, _.templateSettings); |
6021 | * |
6022 | * options.variable |
6023 | * // => 'data' |
6024 | * |
6025 | * options.imports |
6026 | * // => { '_': _, 'jq': $ } |
6027 | */ |
6028 | function partialRight(func) { |
6029 | return createWrapper(func, 32, null, slice(arguments, 1)); |
6030 | } |
6031 | |
6032 | /** |
6033 | * Creates a function that, when executed, will only call the `func` function |
6034 | * at most once per every `wait` milliseconds. Provide an options object to |
6035 | * indicate that `func` should be invoked on the leading and/or trailing edge |
6036 | * of the `wait` timeout. Subsequent calls to the throttled function will |
6037 | * return the result of the last `func` call. |
6038 | * |
6039 | * Note: If `leading` and `trailing` options are `true` `func` will be called |
6040 | * on the trailing edge of the timeout only if the the throttled function is |
6041 | * invoked more than once during the `wait` timeout. |
6042 | * |
6043 | * @static |
6044 | * @memberOf _ |
6045 | * @category Functions |
6046 | * @param {Function} func The function to throttle. |
6047 | * @param {number} wait The number of milliseconds to throttle executions to. |
6048 | * @param {Object} [options] The options object. |
6049 | * @param {boolean} [options.leading=true] Specify execution on the leading edge of the timeout. |
6050 | * @param {boolean} [options.trailing=true] Specify execution on the trailing edge of the timeout. |
6051 | * @returns {Function} Returns the new throttled function. |
6052 | * @example |
6053 | * |
6054 | * // avoid excessively updating the position while scrolling |
6055 | * var throttled = _.throttle(updatePosition, 100); |
6056 | * jQuery(window).on('scroll', throttled); |
6057 | * |
6058 | * // execute `renewToken` when the click event is fired, but not more than once every 5 minutes |
6059 | * jQuery('.interactive').on('click', _.throttle(renewToken, 300000, { |
6060 | * 'trailing': false |
6061 | * })); |
6062 | */ |
6063 | function throttle(func, wait, options) { |
6064 | var leading = true, |
6065 | trailing = true; |
6066 | |
6067 | if (!isFunction(func)) { |
6068 | throw new TypeError; |
6069 | } |
6070 | if (options === false) { |
6071 | leading = false; |
6072 | } else if (isObject(options)) { |
6073 | leading = 'leading' in options ? options.leading : leading; |
6074 | trailing = 'trailing' in options ? options.trailing : trailing; |
6075 | } |
6076 | debounceOptions.leading = leading; |
6077 | debounceOptions.maxWait = wait; |
6078 | debounceOptions.trailing = trailing; |
6079 | |
6080 | return debounce(func, wait, debounceOptions); |
6081 | } |
6082 | |
6083 | /** |
6084 | * Creates a function that provides `value` to the wrapper function as its |
6085 | * first argument. Additional arguments provided to the function are appended |
6086 | * to those provided to the wrapper function. The wrapper is executed with |
6087 | * the `this` binding of the created function. |
6088 | * |
6089 | * @static |
6090 | * @memberOf _ |
6091 | * @category Functions |
6092 | * @param {*} value The value to wrap. |
6093 | * @param {Function} wrapper The wrapper function. |
6094 | * @returns {Function} Returns the new function. |
6095 | * @example |
6096 | * |
6097 | * var p = _.wrap(_.escape, function(func, text) { |
6098 | * return '<p>' + func(text) + '</p>'; |
6099 | * }); |
6100 | * |
6101 | * p('Fred, Wilma, & Pebbles'); |
6102 | * // => '<p>Fred, Wilma, & Pebbles</p>' |
6103 | */ |
6104 | function wrap(value, wrapper) { |
6105 | return createWrapper(wrapper, 16, [value]); |
6106 | } |
6107 | |
6108 | /*--------------------------------------------------------------------------*/ |
6109 | |
6110 | /** |
6111 | * Creates a function that returns `value`. |
6112 | * |
6113 | * @static |
6114 | * @memberOf _ |
6115 | * @category Utilities |
6116 | * @param {*} value The value to return from the new function. |
6117 | * @returns {Function} Returns the new function. |
6118 | * @example |
6119 | * |
6120 | * var object = { 'name': 'fred' }; |
6121 | * var getter = _.constant(object); |
6122 | * getter() === object; |
6123 | * // => true |
6124 | */ |
6125 | function constant(value) { |
6126 | return function() { |
6127 | return value; |
6128 | }; |
6129 | } |
6130 | |
6131 | /** |
6132 | * Produces a callback bound to an optional `thisArg`. If `func` is a property |
6133 | * name the created callback will return the property value for a given element. |
6134 | * If `func` is an object the created callback will return `true` for elements |
6135 | * that contain the equivalent object properties, otherwise it will return `false`. |
6136 | * |
6137 | * @static |
6138 | * @memberOf _ |
6139 | * @category Utilities |
6140 | * @param {*} [func=identity] The value to convert to a callback. |
6141 | * @param {*} [thisArg] The `this` binding of the created callback. |
6142 | * @param {number} [argCount] The number of arguments the callback accepts. |
6143 | * @returns {Function} Returns a callback function. |
6144 | * @example |
6145 | * |
6146 | * var characters = [ |
6147 | * { 'name': 'barney', 'age': 36 }, |
6148 | * { 'name': 'fred', 'age': 40 } |
6149 | * ]; |
6150 | * |
6151 | * // wrap to create custom callback shorthands |
6152 | * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { |
6153 | * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); |
6154 | * return !match ? func(callback, thisArg) : function(object) { |
6155 | * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; |
6156 | * }; |
6157 | * }); |
6158 | * |
6159 | * _.filter(characters, 'age__gt38'); |
6160 | * // => [{ 'name': 'fred', 'age': 40 }] |
6161 | */ |
6162 | function createCallback(func, thisArg, argCount) { |
6163 | var type = typeof func; |
6164 | if (func == null || type == 'function') { |
6165 | return baseCreateCallback(func, thisArg, argCount); |
6166 | } |
6167 | // handle "_.pluck" style callback shorthands |
6168 | if (type != 'object') { |
6169 | return property(func); |
6170 | } |
6171 | var props = keys(func), |
6172 | key = props[0], |
6173 | a = func[key]; |
6174 | |
6175 | // handle "_.where" style callback shorthands |
6176 | if (props.length == 1 && a === a && !isObject(a)) { |
6177 | // fast path the common case of providing an object with a single |
6178 | // property containing a primitive value |
6179 | return function(object) { |
6180 | var b = object[key]; |
6181 | return a === b && (a !== 0 || (1 / a == 1 / b)); |
6182 | }; |
6183 | } |
6184 | return function(object) { |
6185 | var length = props.length, |
6186 | result = false; |
6187 | |
6188 | while (length--) { |
6189 | if (!(result = baseIsEqual(object[props[length]], func[props[length]], null, true))) { |
6190 | break; |
6191 | } |
6192 | } |
6193 | return result; |
6194 | }; |
6195 | } |
6196 | |
6197 | /** |
6198 | * Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their |
6199 | * corresponding HTML entities. |
6200 | * |
6201 | * @static |
6202 | * @memberOf _ |
6203 | * @category Utilities |
6204 | * @param {string} string The string to escape. |
6205 | * @returns {string} Returns the escaped string. |
6206 | * @example |
6207 | * |
6208 | * _.escape('Fred, Wilma, & Pebbles'); |
6209 | * // => 'Fred, Wilma, & Pebbles' |
6210 | */ |
6211 | function escape(string) { |
6212 | return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar); |
6213 | } |
6214 | |
6215 | /** |
6216 | * This method returns the first argument provided to it. |
6217 | * |
6218 | * @static |
6219 | * @memberOf _ |
6220 | * @category Utilities |
6221 | * @param {*} value Any value. |
6222 | * @returns {*} Returns `value`. |
6223 | * @example |
6224 | * |
6225 | * var object = { 'name': 'fred' }; |
6226 | * _.identity(object) === object; |
6227 | * // => true |
6228 | */ |
6229 | function identity(value) { |
6230 | return value; |
6231 | } |
6232 | |
6233 | /** |
6234 | * Adds function properties of a source object to the destination object. |
6235 | * If `object` is a function methods will be added to its prototype as well. |
6236 | * |
6237 | * @static |
6238 | * @memberOf _ |
6239 | * @category Utilities |
6240 | * @param {Function|Object} [object=lodash] object The destination object. |
6241 | * @param {Object} source The object of functions to add. |
6242 | * @param {Object} [options] The options object. |
6243 | * @param {boolean} [options.chain=true] Specify whether the functions added are chainable. |
6244 | * @example |
6245 | * |
6246 | * function capitalize(string) { |
6247 | * return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase(); |
6248 | * } |
6249 | * |
6250 | * _.mixin({ 'capitalize': capitalize }); |
6251 | * _.capitalize('fred'); |
6252 | * // => 'Fred' |
6253 | * |
6254 | * _('fred').capitalize().value(); |
6255 | * // => 'Fred' |
6256 | * |
6257 | * _.mixin({ 'capitalize': capitalize }, { 'chain': false }); |
6258 | * _('fred').capitalize(); |
6259 | * // => 'Fred' |
6260 | */ |
6261 | function mixin(object, source, options) { |
6262 | var chain = true, |
6263 | methodNames = source && functions(source); |
6264 | |
6265 | if (!source || (!options && !methodNames.length)) { |
6266 | if (options == null) { |
6267 | options = source; |
6268 | } |
6269 | ctor = lodashWrapper; |
6270 | source = object; |
6271 | object = lodash; |
6272 | methodNames = functions(source); |
6273 | } |
6274 | if (options === false) { |
6275 | chain = false; |
6276 | } else if (isObject(options) && 'chain' in options) { |
6277 | chain = options.chain; |
6278 | } |
6279 | var ctor = object, |
6280 | isFunc = isFunction(ctor); |
6281 | |
6282 | forEach(methodNames, function(methodName) { |
6283 | var func = object[methodName] = source[methodName]; |
6284 | if (isFunc) { |
6285 | ctor.prototype[methodName] = function() { |
6286 | var chainAll = this.__chain__, |
6287 | value = this.__wrapped__, |
6288 | args = [value]; |
6289 | |
6290 | push.apply(args, arguments); |
6291 | var result = func.apply(object, args); |
6292 | if (chain || chainAll) { |
6293 | if (value === result && isObject(result)) { |
6294 | return this; |
6295 | } |
6296 | result = new ctor(result); |
6297 | result.__chain__ = chainAll; |
6298 | } |
6299 | return result; |
6300 | }; |
6301 | } |
6302 | }); |
6303 | } |
6304 | |
6305 | /** |
6306 | * Reverts the '_' variable to its previous value and returns a reference to |
6307 | * the `lodash` function. |
6308 | * |
6309 | * @static |
6310 | * @memberOf _ |
6311 | * @category Utilities |
6312 | * @returns {Function} Returns the `lodash` function. |
6313 | * @example |
6314 | * |
6315 | * var lodash = _.noConflict(); |
6316 | */ |
6317 | function noConflict() { |
6318 | context._ = oldDash; |
6319 | return this; |
6320 | } |
6321 | |
6322 | /** |
6323 | * A no-operation function. |
6324 | * |
6325 | * @static |
6326 | * @memberOf _ |
6327 | * @category Utilities |
6328 | * @example |
6329 | * |
6330 | * var object = { 'name': 'fred' }; |
6331 | * _.noop(object) === undefined; |
6332 | * // => true |
6333 | */ |
6334 | function noop() { |
6335 | // no operation performed |
6336 | } |
6337 | |
6338 | /** |
6339 | * Gets the number of milliseconds that have elapsed since the Unix epoch |
6340 | * (1 January 1970 00:00:00 UTC). |
6341 | * |
6342 | * @static |
6343 | * @memberOf _ |
6344 | * @category Utilities |
6345 | * @example |
6346 | * |
6347 | * var stamp = _.now(); |
6348 | * _.defer(function() { console.log(_.now() - stamp); }); |
6349 | * // => logs the number of milliseconds it took for the deferred function to be called |
6350 | */ |
6351 | var now = isNative(now = Date.now) && now || function() { |
6352 | return new Date().getTime(); |
6353 | }; |
6354 | |
6355 | /** |
6356 | * Converts the given value into an integer of the specified radix. |
6357 | * If `radix` is `undefined` or `0` a `radix` of `10` is used unless the |
6358 | * `value` is a hexadecimal, in which case a `radix` of `16` is used. |
6359 | * |
6360 | * Note: This method avoids differences in native ES3 and ES5 `parseInt` |
6361 | * implementations. See http://es5.github.io/#E. |
6362 | * |
6363 | * @static |
6364 | * @memberOf _ |
6365 | * @category Utilities |
6366 | * @param {string} value The value to parse. |
6367 | * @param {number} [radix] The radix used to interpret the value to parse. |
6368 | * @returns {number} Returns the new integer value. |
6369 | * @example |
6370 | * |
6371 | * _.parseInt('08'); |
6372 | * // => 8 |
6373 | */ |
6374 | var parseInt = nativeParseInt(whitespace + '08') == 8 ? nativeParseInt : function(value, radix) { |
6375 | // Firefox < 21 and Opera < 15 follow the ES3 specified implementation of `parseInt` |
6376 | return nativeParseInt(isString(value) ? value.replace(reLeadingSpacesAndZeros, '') : value, radix || 0); |
6377 | }; |
6378 | |
6379 | /** |
6380 | * Creates a "_.pluck" style function, which returns the `key` value of a |
6381 | * given object. |
6382 | * |
6383 | * @static |
6384 | * @memberOf _ |
6385 | * @category Utilities |
6386 | * @param {string} key The name of the property to retrieve. |
6387 | * @returns {Function} Returns the new function. |
6388 | * @example |
6389 | * |
6390 | * var characters = [ |
6391 | * { 'name': 'fred', 'age': 40 }, |
6392 | * { 'name': 'barney', 'age': 36 } |
6393 | * ]; |
6394 | * |
6395 | * var getName = _.property('name'); |
6396 | * |
6397 | * _.map(characters, getName); |
6398 | * // => ['barney', 'fred'] |
6399 | * |
6400 | * _.sortBy(characters, getName); |
6401 | * // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] |
6402 | */ |
6403 | function property(key) { |
6404 | return function(object) { |
6405 | return object[key]; |
6406 | }; |
6407 | } |
6408 | |
6409 | /** |
6410 | * Produces a random number between `min` and `max` (inclusive). If only one |
6411 | * argument is provided a number between `0` and the given number will be |
6412 | * returned. If `floating` is truey or either `min` or `max` are floats a |
6413 | * floating-point number will be returned instead of an integer. |
6414 | * |
6415 | * @static |
6416 | * @memberOf _ |
6417 | * @category Utilities |
6418 | * @param {number} [min=0] The minimum possible value. |
6419 | * @param {number} [max=1] The maximum possible value. |
6420 | * @param {boolean} [floating=false] Specify returning a floating-point number. |
6421 | * @returns {number} Returns a random number. |
6422 | * @example |
6423 | * |
6424 | * _.random(0, 5); |
6425 | * // => an integer between 0 and 5 |
6426 | * |
6427 | * _.random(5); |
6428 | * // => also an integer between 0 and 5 |
6429 | * |
6430 | * _.random(5, true); |
6431 | * // => a floating-point number between 0 and 5 |
6432 | * |
6433 | * _.random(1.2, 5.2); |
6434 | * // => a floating-point number between 1.2 and 5.2 |
6435 | */ |
6436 | function random(min, max, floating) { |
6437 | var noMin = min == null, |
6438 | noMax = max == null; |
6439 | |
6440 | if (floating == null) { |
6441 | if (typeof min == 'boolean' && noMax) { |
6442 | floating = min; |
6443 | min = 1; |
6444 | } |
6445 | else if (!noMax && typeof max == 'boolean') { |
6446 | floating = max; |
6447 | noMax = true; |
6448 | } |
6449 | } |
6450 | if (noMin && noMax) { |
6451 | max = 1; |
6452 | } |
6453 | min = +min || 0; |
6454 | if (noMax) { |
6455 | max = min; |
6456 | min = 0; |
6457 | } else { |
6458 | max = +max || 0; |
6459 | } |
6460 | if (floating || min % 1 || max % 1) { |
6461 | var rand = nativeRandom(); |
6462 | return nativeMin(min + (rand * (max - min + parseFloat('1e-' + ((rand +'').length - 1)))), max); |
6463 | } |
6464 | return baseRandom(min, max); |
6465 | } |
6466 | |
6467 | /** |
6468 | * Resolves the value of property `key` on `object`. If `key` is a function |
6469 | * it will be invoked with the `this` binding of `object` and its result returned, |
6470 | * else the property value is returned. If `object` is falsey then `undefined` |
6471 | * is returned. |
6472 | * |
6473 | * @static |
6474 | * @memberOf _ |
6475 | * @category Utilities |
6476 | * @param {Object} object The object to inspect. |
6477 | * @param {string} key The name of the property to resolve. |
6478 | * @returns {*} Returns the resolved value. |
6479 | * @example |
6480 | * |
6481 | * var object = { |
6482 | * 'cheese': 'crumpets', |
6483 | * 'stuff': function() { |
6484 | * return 'nonsense'; |
6485 | * } |
6486 | * }; |
6487 | * |
6488 | * _.result(object, 'cheese'); |
6489 | * // => 'crumpets' |
6490 | * |
6491 | * _.result(object, 'stuff'); |
6492 | * // => 'nonsense' |
6493 | */ |
6494 | function result(object, key) { |
6495 | if (object) { |
6496 | var value = object[key]; |
6497 | return isFunction(value) ? object[key]() : value; |
6498 | } |
6499 | } |
6500 | |
6501 | /** |
6502 | * A micro-templating method that handles arbitrary delimiters, preserves |
6503 | * whitespace, and correctly escapes quotes within interpolated code. |
6504 | * |
6505 | * Note: In the development build, `_.template` utilizes sourceURLs for easier |
6506 | * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl |
6507 | * |
6508 | * For more information on precompiling templates see: |
6509 | * http://lodash.com/custom-builds |
6510 | * |
6511 | * For more information on Chrome extension sandboxes see: |
6512 | * http://developer.chrome.com/stable/extensions/sandboxingEval.html |
6513 | * |
6514 | * @static |
6515 | * @memberOf _ |
6516 | * @category Utilities |
6517 | * @param {string} text The template text. |
6518 | * @param {Object} data The data object used to populate the text. |
6519 | * @param {Object} [options] The options object. |
6520 | * @param {RegExp} [options.escape] The "escape" delimiter. |
6521 | * @param {RegExp} [options.evaluate] The "evaluate" delimiter. |
6522 | * @param {Object} [options.imports] An object to import into the template as local variables. |
6523 | * @param {RegExp} [options.interpolate] The "interpolate" delimiter. |
6524 | * @param {string} [sourceURL] The sourceURL of the template's compiled source. |
6525 | * @param {string} [variable] The data object variable name. |
6526 | * @returns {Function|string} Returns a compiled function when no `data` object |
6527 | * is given, else it returns the interpolated text. |
6528 | * @example |
6529 | * |
6530 | * // using the "interpolate" delimiter to create a compiled template |
6531 | * var compiled = _.template('hello <%= name %>'); |
6532 | * compiled({ 'name': 'fred' }); |
6533 | * // => 'hello fred' |
6534 | * |
6535 | * // using the "escape" delimiter to escape HTML in data property values |
6536 | * _.template('<b><%- value %></b>', { 'value': '<script>' }); |
6537 | * // => '<b><script></b>' |
6538 | * |
6539 | * // using the "evaluate" delimiter to generate HTML |
6540 | * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>'; |
6541 | * _.template(list, { 'people': ['fred', 'barney'] }); |
6542 | * // => '<li>fred</li><li>barney</li>' |
6543 | * |
6544 | * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter |
6545 | * _.template('hello ${ name }', { 'name': 'pebbles' }); |
6546 | * // => 'hello pebbles' |
6547 | * |
6548 | * // using the internal `print` function in "evaluate" delimiters |
6549 | * _.template('<% print("hello " + name); %>!', { 'name': 'barney' }); |
6550 | * // => 'hello barney!' |
6551 | * |
6552 | * // using a custom template delimiters |
6553 | * _.templateSettings = { |
6554 | * 'interpolate': /{{([\s\S]+?)}}/g |
6555 | * }; |
6556 | * |
6557 | * _.template('hello {{ name }}!', { 'name': 'mustache' }); |
6558 | * // => 'hello mustache!' |
6559 | * |
6560 | * // using the `imports` option to import jQuery |
6561 | * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>'; |
6562 | * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } }); |
6563 | * // => '<li>fred</li><li>barney</li>' |
6564 | * |
6565 | * // using the `sourceURL` option to specify a custom sourceURL for the template |
6566 | * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' }); |
6567 | * compiled(data); |
6568 | * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector |
6569 | * |
6570 | * // using the `variable` option to ensure a with-statement isn't used in the compiled template |
6571 | * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' }); |
6572 | * compiled.source; |
6573 | * // => function(data) { |
6574 | * var __t, __p = '', __e = _.escape; |
6575 | * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!'; |
6576 | * return __p; |
6577 | * } |
6578 | * |
6579 | * // using the `source` property to inline compiled templates for meaningful |
6580 | * // line numbers in error messages and a stack trace |
6581 | * fs.writeFileSync(path.join(cwd, 'jst.js'), '\ |
6582 | * var JST = {\ |
6583 | * "main": ' + _.template(mainText).source + '\ |
6584 | * };\ |
6585 | * '); |
6586 | */ |
6587 | function template(text, data, options) { |
6588 | // based on John Resig's `tmpl` implementation |
6589 | // http://ejohn.org/blog/javascript-micro-templating/ |
6590 | // and Laura Doktorova's doT.js |
6591 | // https://github.com/olado/doT |
6592 | var settings = lodash.templateSettings; |
6593 | text = String(text || ''); |
6594 | |
6595 | // avoid missing dependencies when `iteratorTemplate` is not defined |
6596 | options = iteratorTemplate ? defaults({}, options, settings) : settings; |
6597 | |
6598 | var imports = iteratorTemplate && defaults({}, options.imports, settings.imports), |
6599 | importsKeys = iteratorTemplate ? keys(imports) : ['_'], |
6600 | importsValues = iteratorTemplate ? values(imports) : [lodash]; |
6601 | |
6602 | var isEvaluating, |
6603 | index = 0, |
6604 | interpolate = options.interpolate || reNoMatch, |
6605 | source = "__p += '"; |
6606 | |
6607 | // compile the regexp to match each delimiter |
6608 | var reDelimiters = RegExp( |
6609 | (options.escape || reNoMatch).source + '|' + |
6610 | interpolate.source + '|' + |
6611 | (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' + |
6612 | (options.evaluate || reNoMatch).source + '|$' |
6613 | , 'g'); |
6614 | |
6615 | text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) { |
6616 | interpolateValue || (interpolateValue = esTemplateValue); |
6617 | |
6618 | // escape characters that cannot be included in string literals |
6619 | source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar); |
6620 | |
6621 | // replace delimiters with snippets |
6622 | if (escapeValue) { |
6623 | source += "' +\n__e(" + escapeValue + ") +\n'"; |
6624 | } |
6625 | if (evaluateValue) { |
6626 | isEvaluating = true; |
6627 | source += "';\n" + evaluateValue + ";\n__p += '"; |
6628 | } |
6629 | if (interpolateValue) { |
6630 | source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'"; |
6631 | } |
6632 | index = offset + match.length; |
6633 | |
6634 | // the JS engine embedded in Adobe products requires returning the `match` |
6635 | // string in order to produce the correct `offset` value |
6636 | return match; |
6637 | }); |
6638 | |
6639 | source += "';\n"; |
6640 | |
6641 | // if `variable` is not specified, wrap a with-statement around the generated |
6642 | // code to add the data object to the top of the scope chain |
6643 | var variable = options.variable, |
6644 | hasVariable = variable; |
6645 | |
6646 | if (!hasVariable) { |
6647 | variable = 'obj'; |
6648 | source = 'with (' + variable + ') {\n' + source + '\n}\n'; |
6649 | } |
6650 | // cleanup code by stripping empty strings |
6651 | source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source) |
6652 | .replace(reEmptyStringMiddle, '$1') |
6653 | .replace(reEmptyStringTrailing, '$1;'); |
6654 | |
6655 | // frame code as the function body |
6656 | source = 'function(' + variable + ') {\n' + |
6657 | (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') + |
6658 | "var __t, __p = '', __e = _.escape" + |
6659 | (isEvaluating |
6660 | ? ', __j = Array.prototype.join;\n' + |
6661 | "function print() { __p += __j.call(arguments, '') }\n" |
6662 | : ';\n' |
6663 | ) + |
6664 | source + |
6665 | 'return __p\n}'; |
6666 | |
6667 | // Use a sourceURL for easier debugging. |
6668 | // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl |
6669 | var sourceURL = '\n/*\n//# sourceURL=' + (options.sourceURL || '/lodash/template/source[' + (templateCounter++) + ']') + '\n*/'; |
6670 | |
6671 | try { |
6672 | var result = Function(importsKeys, 'return ' + source + sourceURL).apply(undefined, importsValues); |
6673 | } catch(e) { |
6674 | e.source = source; |
6675 | throw e; |
6676 | } |
6677 | if (data) { |
6678 | return result(data); |
6679 | } |
6680 | // provide the compiled function's source by its `toString` method, in |
6681 | // supported environments, or the `source` property as a convenience for |
6682 | // inlining compiled templates during the build process |
6683 | result.source = source; |
6684 | return result; |
6685 | } |
6686 | |
6687 | /** |
6688 | * Executes the callback `n` times, returning an array of the results |
6689 | * of each callback execution. The callback is bound to `thisArg` and invoked |
6690 | * with one argument; (index). |
6691 | * |
6692 | * @static |
6693 | * @memberOf _ |
6694 | * @category Utilities |
6695 | * @param {number} n The number of times to execute the callback. |
6696 | * @param {Function} callback The function called per iteration. |
6697 | * @param {*} [thisArg] The `this` binding of `callback`. |
6698 | * @returns {Array} Returns an array of the results of each `callback` execution. |
6699 | * @example |
6700 | * |
6701 | * var diceRolls = _.times(3, _.partial(_.random, 1, 6)); |
6702 | * // => [3, 6, 4] |
6703 | * |
6704 | * _.times(3, function(n) { mage.castSpell(n); }); |
6705 | * // => calls `mage.castSpell(n)` three times, passing `n` of `0`, `1`, and `2` respectively |
6706 | * |
6707 | * _.times(3, function(n) { this.cast(n); }, mage); |
6708 | * // => also calls `mage.castSpell(n)` three times |
6709 | */ |
6710 | function times(n, callback, thisArg) { |
6711 | n = (n = +n) > -1 ? n : 0; |
6712 | var index = -1, |
6713 | result = Array(n); |
6714 | |
6715 | callback = baseCreateCallback(callback, thisArg, 1); |
6716 | while (++index < n) { |
6717 | result[index] = callback(index); |
6718 | } |
6719 | return result; |
6720 | } |
6721 | |
6722 | /** |
6723 | * The inverse of `_.escape` this method converts the HTML entities |
6724 | * `&`, `<`, `>`, `"`, and `'` in `string` to their |
6725 | * corresponding characters. |
6726 | * |
6727 | * @static |
6728 | * @memberOf _ |
6729 | * @category Utilities |
6730 | * @param {string} string The string to unescape. |
6731 | * @returns {string} Returns the unescaped string. |
6732 | * @example |
6733 | * |
6734 | * _.unescape('Fred, Barney & Pebbles'); |
6735 | * // => 'Fred, Barney & Pebbles' |
6736 | */ |
6737 | function unescape(string) { |
6738 | return string == null ? '' : String(string).replace(reEscapedHtml, unescapeHtmlChar); |
6739 | } |
6740 | |
6741 | /** |
6742 | * Generates a unique ID. If `prefix` is provided the ID will be appended to it. |
6743 | * |
6744 | * @static |
6745 | * @memberOf _ |
6746 | * @category Utilities |
6747 | * @param {string} [prefix] The value to prefix the ID with. |
6748 | * @returns {string} Returns the unique ID. |
6749 | * @example |
6750 | * |
6751 | * _.uniqueId('contact_'); |
6752 | * // => 'contact_104' |
6753 | * |
6754 | * _.uniqueId(); |
6755 | * // => '105' |
6756 | */ |
6757 | function uniqueId(prefix) { |
6758 | var id = ++idCounter; |
6759 | return String(prefix == null ? '' : prefix) + id; |
6760 | } |
6761 | |
6762 | /*--------------------------------------------------------------------------*/ |
6763 | |
6764 | /** |
6765 | * Creates a `lodash` object that wraps the given value with explicit |
6766 | * method chaining enabled. |
6767 | * |
6768 | * @static |
6769 | * @memberOf _ |
6770 | * @category Chaining |
6771 | * @param {*} value The value to wrap. |
6772 | * @returns {Object} Returns the wrapper object. |
6773 | * @example |
6774 | * |
6775 | * var characters = [ |
6776 | * { 'name': 'barney', 'age': 36 }, |
6777 | * { 'name': 'fred', 'age': 40 }, |
6778 | * { 'name': 'pebbles', 'age': 1 } |
6779 | * ]; |
6780 | * |
6781 | * var youngest = _.chain(characters) |
6782 | * .sortBy('age') |
6783 | * .map(function(chr) { return chr.name + ' is ' + chr.age; }) |
6784 | * .first() |
6785 | * .value(); |
6786 | * // => 'pebbles is 1' |
6787 | */ |
6788 | function chain(value) { |
6789 | value = new lodashWrapper(value); |
6790 | value.__chain__ = true; |
6791 | return value; |
6792 | } |
6793 | |
6794 | /** |
6795 | * Invokes `interceptor` with the `value` as the first argument and then |
6796 | * returns `value`. The purpose of this method is to "tap into" a method |
6797 | * chain in order to perform operations on intermediate results within |
6798 | * the chain. |
6799 | * |
6800 | * @static |
6801 | * @memberOf _ |
6802 | * @category Chaining |
6803 | * @param {*} value The value to provide to `interceptor`. |
6804 | * @param {Function} interceptor The function to invoke. |
6805 | * @returns {*} Returns `value`. |
6806 | * @example |
6807 | * |
6808 | * _([1, 2, 3, 4]) |
6809 | * .tap(function(array) { array.pop(); }) |
6810 | * .reverse() |
6811 | * .value(); |
6812 | * // => [3, 2, 1] |
6813 | */ |
6814 | function tap(value, interceptor) { |
6815 | interceptor(value); |
6816 | return value; |
6817 | } |
6818 | |
6819 | /** |
6820 | * Enables explicit method chaining on the wrapper object. |
6821 | * |
6822 | * @name chain |
6823 | * @memberOf _ |
6824 | * @category Chaining |
6825 | * @returns {*} Returns the wrapper object. |
6826 | * @example |
6827 | * |
6828 | * var characters = [ |
6829 | * { 'name': 'barney', 'age': 36 }, |
6830 | * { 'name': 'fred', 'age': 40 } |
6831 | * ]; |
6832 | * |
6833 | * // without explicit chaining |
6834 | * _(characters).first(); |
6835 | * // => { 'name': 'barney', 'age': 36 } |
6836 | * |
6837 | * // with explicit chaining |
6838 | * _(characters).chain() |
6839 | * .first() |
6840 | * .pick('age') |
6841 | * .value(); |
6842 | * // => { 'age': 36 } |
6843 | */ |
6844 | function wrapperChain() { |
6845 | this.__chain__ = true; |
6846 | return this; |
6847 | } |
6848 | |
6849 | /** |
6850 | * Produces the `toString` result of the wrapped value. |
6851 | * |
6852 | * @name toString |
6853 | * @memberOf _ |
6854 | * @category Chaining |
6855 | * @returns {string} Returns the string result. |
6856 | * @example |
6857 | * |
6858 | * _([1, 2, 3]).toString(); |
6859 | * // => '1,2,3' |
6860 | */ |
6861 | function wrapperToString() { |
6862 | return String(this.__wrapped__); |
6863 | } |
6864 | |
6865 | /** |
6866 | * Extracts the wrapped value. |
6867 | * |
6868 | * @name valueOf |
6869 | * @memberOf _ |
6870 | * @alias value |
6871 | * @category Chaining |
6872 | * @returns {*} Returns the wrapped value. |
6873 | * @example |
6874 | * |
6875 | * _([1, 2, 3]).valueOf(); |
6876 | * // => [1, 2, 3] |
6877 | */ |
6878 | function wrapperValueOf() { |
6879 | return this.__wrapped__; |
6880 | } |
6881 | |
6882 | /*--------------------------------------------------------------------------*/ |
6883 | |
6884 | // add functions that return wrapped values when chaining |
6885 | lodash.after = after; |
6886 | lodash.assign = assign; |
6887 | lodash.at = at; |
6888 | lodash.bind = bind; |
6889 | lodash.bindAll = bindAll; |
6890 | lodash.bindKey = bindKey; |
6891 | lodash.chain = chain; |
6892 | lodash.compact = compact; |
6893 | lodash.compose = compose; |
6894 | lodash.constant = constant; |
6895 | lodash.countBy = countBy; |
6896 | lodash.create = create; |
6897 | lodash.createCallback = createCallback; |
6898 | lodash.curry = curry; |
6899 | lodash.debounce = debounce; |
6900 | lodash.defaults = defaults; |
6901 | lodash.defer = defer; |
6902 | lodash.delay = delay; |
6903 | lodash.difference = difference; |
6904 | lodash.filter = filter; |
6905 | lodash.flatten = flatten; |
6906 | lodash.forEach = forEach; |
6907 | lodash.forEachRight = forEachRight; |
6908 | lodash.forIn = forIn; |
6909 | lodash.forInRight = forInRight; |
6910 | lodash.forOwn = forOwn; |
6911 | lodash.forOwnRight = forOwnRight; |
6912 | lodash.functions = functions; |
6913 | lodash.groupBy = groupBy; |
6914 | lodash.indexBy = indexBy; |
6915 | lodash.initial = initial; |
6916 | lodash.intersection = intersection; |
6917 | lodash.invert = invert; |
6918 | lodash.invoke = invoke; |
6919 | lodash.keys = keys; |
6920 | lodash.map = map; |
6921 | lodash.mapValues = mapValues; |
6922 | lodash.max = max; |
6923 | lodash.memoize = memoize; |
6924 | lodash.merge = merge; |
6925 | lodash.min = min; |
6926 | lodash.omit = omit; |
6927 | lodash.once = once; |
6928 | lodash.pairs = pairs; |
6929 | lodash.partial = partial; |
6930 | lodash.partialRight = partialRight; |
6931 | lodash.pick = pick; |
6932 | lodash.pluck = pluck; |
6933 | lodash.property = property; |
6934 | lodash.pull = pull; |
6935 | lodash.range = range; |
6936 | lodash.reject = reject; |
6937 | lodash.remove = remove; |
6938 | lodash.rest = rest; |
6939 | lodash.shuffle = shuffle; |
6940 | lodash.sortBy = sortBy; |
6941 | lodash.tap = tap; |
6942 | lodash.throttle = throttle; |
6943 | lodash.times = times; |
6944 | lodash.toArray = toArray; |
6945 | lodash.transform = transform; |
6946 | lodash.union = union; |
6947 | lodash.uniq = uniq; |
6948 | lodash.values = values; |
6949 | lodash.where = where; |
6950 | lodash.without = without; |
6951 | lodash.wrap = wrap; |
6952 | lodash.xor = xor; |
6953 | lodash.zip = zip; |
6954 | lodash.zipObject = zipObject; |
6955 | |
6956 | // add aliases |
6957 | lodash.collect = map; |
6958 | lodash.drop = rest; |
6959 | lodash.each = forEach; |
6960 | lodash.eachRight = forEachRight; |
6961 | lodash.extend = assign; |
6962 | lodash.methods = functions; |
6963 | lodash.object = zipObject; |
6964 | lodash.select = filter; |
6965 | lodash.tail = rest; |
6966 | lodash.unique = uniq; |
6967 | lodash.unzip = zip; |
6968 | |
6969 | // add functions to `lodash.prototype` |
6970 | mixin(lodash); |
6971 | |
6972 | /*--------------------------------------------------------------------------*/ |
6973 | |
6974 | // add functions that return unwrapped values when chaining |
6975 | lodash.clone = clone; |
6976 | lodash.cloneDeep = cloneDeep; |
6977 | lodash.contains = contains; |
6978 | lodash.escape = escape; |
6979 | lodash.every = every; |
6980 | lodash.find = find; |
6981 | lodash.findIndex = findIndex; |
6982 | lodash.findKey = findKey; |
6983 | lodash.findLast = findLast; |
6984 | lodash.findLastIndex = findLastIndex; |
6985 | lodash.findLastKey = findLastKey; |
6986 | lodash.has = has; |
6987 | lodash.identity = identity; |
6988 | lodash.indexOf = indexOf; |
6989 | lodash.isArguments = isArguments; |
6990 | lodash.isArray = isArray; |
6991 | lodash.isBoolean = isBoolean; |
6992 | lodash.isDate = isDate; |
6993 | lodash.isElement = isElement; |
6994 | lodash.isEmpty = isEmpty; |
6995 | lodash.isEqual = isEqual; |
6996 | lodash.isFinite = isFinite; |
6997 | lodash.isFunction = isFunction; |
6998 | lodash.isNaN = isNaN; |
6999 | lodash.isNull = isNull; |
7000 | lodash.isNumber = isNumber; |
7001 | lodash.isObject = isObject; |
7002 | lodash.isPlainObject = isPlainObject; |
7003 | lodash.isRegExp = isRegExp; |
7004 | lodash.isString = isString; |
7005 | lodash.isUndefined = isUndefined; |
7006 | lodash.lastIndexOf = lastIndexOf; |
7007 | lodash.mixin = mixin; |
7008 | lodash.noConflict = noConflict; |
7009 | lodash.noop = noop; |
7010 | lodash.now = now; |
7011 | lodash.parseInt = parseInt; |
7012 | lodash.random = random; |
7013 | lodash.reduce = reduce; |
7014 | lodash.reduceRight = reduceRight; |
7015 | lodash.result = result; |
7016 | lodash.runInContext = runInContext; |
7017 | lodash.size = size; |
7018 | lodash.some = some; |
7019 | lodash.sortedIndex = sortedIndex; |
7020 | lodash.template = template; |
7021 | lodash.unescape = unescape; |
7022 | lodash.uniqueId = uniqueId; |
7023 | |
7024 | // add aliases |
7025 | lodash.all = every; |
7026 | lodash.any = some; |
7027 | lodash.detect = find; |
7028 | lodash.findWhere = find; |
7029 | lodash.foldl = reduce; |
7030 | lodash.foldr = reduceRight; |
7031 | lodash.include = contains; |
7032 | lodash.inject = reduce; |
7033 | |
7034 | mixin(function() { |
7035 | var source = {} |
7036 | forOwn(lodash, function(func, methodName) { |
7037 | if (!lodash.prototype[methodName]) { |
7038 | source[methodName] = func; |
7039 | } |
7040 | }); |
7041 | return source; |
7042 | }(), false); |
7043 | |
7044 | /*--------------------------------------------------------------------------*/ |
7045 | |
7046 | // add functions capable of returning wrapped and unwrapped values when chaining |
7047 | lodash.first = first; |
7048 | lodash.last = last; |
7049 | lodash.sample = sample; |
7050 | |
7051 | // add aliases |
7052 | lodash.take = first; |
7053 | lodash.head = first; |
7054 | |
7055 | forOwn(lodash, function(func, methodName) { |
7056 | var callbackable = methodName !== 'sample'; |
7057 | if (!lodash.prototype[methodName]) { |
7058 | lodash.prototype[methodName]= function(n, guard) { |
7059 | var chainAll = this.__chain__, |
7060 | result = func(this.__wrapped__, n, guard); |
7061 | |
7062 | return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function'))) |
7063 | ? result |
7064 | : new lodashWrapper(result, chainAll); |
7065 | }; |
7066 | } |
7067 | }); |
7068 | |
7069 | /*--------------------------------------------------------------------------*/ |
7070 | |
7071 | /** |
7072 | * The semantic version number. |
7073 | * |
7074 | * @static |
7075 | * @memberOf _ |
7076 | * @type string |
7077 | */ |
7078 | lodash.VERSION = '2.4.1'; |
7079 | |
7080 | // add "Chaining" functions to the wrapper |
7081 | lodash.prototype.chain = wrapperChain; |
7082 | lodash.prototype.toString = wrapperToString; |
7083 | lodash.prototype.value = wrapperValueOf; |
7084 | lodash.prototype.valueOf = wrapperValueOf; |
7085 | |
7086 | // add `Array` functions that return unwrapped values |
7087 | baseEach(['join', 'pop', 'shift'], function(methodName) { |
7088 | var func = arrayRef[methodName]; |
7089 | lodash.prototype[methodName] = function() { |
7090 | var chainAll = this.__chain__, |
7091 | result = func.apply(this.__wrapped__, arguments); |
7092 | |
7093 | return chainAll |
7094 | ? new lodashWrapper(result, chainAll) |
7095 | : result; |
7096 | }; |
7097 | }); |
7098 | |
7099 | // add `Array` functions that return the existing wrapped value |
7100 | baseEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) { |
7101 | var func = arrayRef[methodName]; |
7102 | lodash.prototype[methodName] = function() { |
7103 | func.apply(this.__wrapped__, arguments); |
7104 | return this; |
7105 | }; |
7106 | }); |
7107 | |
7108 | // add `Array` functions that return new wrapped values |
7109 | baseEach(['concat', 'slice', 'splice'], function(methodName) { |
7110 | var func = arrayRef[methodName]; |
7111 | lodash.prototype[methodName] = function() { |
7112 | return new lodashWrapper(func.apply(this.__wrapped__, arguments), this.__chain__); |
7113 | }; |
7114 | }); |
7115 | |
7116 | // avoid array-like object bugs with `Array#shift` and `Array#splice` |
7117 | // in IE < 9, Firefox < 10, Narwhal, and RingoJS |
7118 | if (!support.spliceObjects) { |
7119 | baseEach(['pop', 'shift', 'splice'], function(methodName) { |
7120 | var func = arrayRef[methodName], |
7121 | isSplice = methodName == 'splice'; |
7122 | |
7123 | lodash.prototype[methodName] = function() { |
7124 | var chainAll = this.__chain__, |
7125 | value = this.__wrapped__, |
7126 | result = func.apply(value, arguments); |
7127 | |
7128 | if (value.length === 0) { |
7129 | delete value[0]; |
7130 | } |
7131 | return (chainAll || isSplice) |
7132 | ? new lodashWrapper(result, chainAll) |
7133 | : result; |
7134 | }; |
7135 | }); |
7136 | } |
7137 | |
7138 | // add pseudo private property to be used and removed during the build process |
7139 | lodash._baseEach = baseEach; |
7140 | lodash._iteratorTemplate = iteratorTemplate; |
7141 | lodash._shimKeys = shimKeys; |
7142 | |
7143 | return lodash; |
7144 | } |
7145 | |
7146 | /*--------------------------------------------------------------------------*/ |
7147 | |
7148 | // expose Lo-Dash |
7149 | var _ = runInContext(); |
7150 | |
7151 | // some AMD build optimizers like r.js check for condition patterns like the following: |
7152 | if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { |
7153 | // Expose Lo-Dash to the global object even when an AMD loader is present in |
7154 | // case Lo-Dash is loaded with a RequireJS shim config. |
7155 | // See http://requirejs.org/docs/api.html#config-shim |
7156 | root._ = _; |
7157 | |
7158 | // define as an anonymous module so, through path mapping, it can be |
7159 | // referenced as the "underscore" module |
7160 | define(function() { |
7161 | return _; |
7162 | }); |
7163 | } |
7164 | // check for `exports` after `define` in case a build optimizer adds an `exports` object |
7165 | else if (freeExports && freeModule) { |
7166 | // in Node.js or RingoJS |
7167 | if (moduleExports) { |
7168 | (freeModule.exports = _)._ = _; |
7169 | } |
7170 | // in Narwhal or Rhino -require |
7171 | else { |
7172 | freeExports._ = _; |
7173 | } |
7174 | } |
7175 | else { |
7176 | // in a browser or Rhino |
7177 | root._ = _; |
7178 | } |
7179 | }.call(this)); |