Reduce objectTypes and indexTypes deps.

This commit is contained in:
John-David Dalton
2014-01-12 11:54:27 -08:00
parent bf2f0c38ed
commit a499fdbb13
7 changed files with 424 additions and 297 deletions

116
dist/lodash.js vendored
View File

@@ -144,24 +144,10 @@
''': "'"
};
/** Used to determine if values are an indexes or keys */
var indexTypes = {
'boolean': false,
'function': false,
'object': false,
'number': true,
'string': true,
'undefined': false
};
/** Used to determine if values are of the language type Object */
var objectTypes = {
'boolean': false,
'function': true,
'object': true,
'number': false,
'string': false,
'undefined': false
'object': true
};
/** Used to escape characters for inclusion in compiled string literals */
@@ -1285,8 +1271,8 @@
// exit early for unlike primitive values
if (a === a &&
!(a && objectTypes[type]) &&
!(b && objectTypes[otherType])) {
!(a && (type == 'function' || type == 'object')) &&
!(b && (otherType == 'function' || otherType == 'object'))) {
return false;
}
// exit early for `null` and `undefined` avoiding ES3's Function#call behavior
@@ -1766,7 +1752,7 @@
*/
var shimKeys = function(object) {
var result = [];
if (!(object && objectTypes[typeof object])) {
if (!isObject(object)) {
return result;
}
for (var key in object) {
@@ -2048,11 +2034,19 @@
* // => ['hoppy', 'baby puss', 'dino']
*/
function flatten(array, isShallow, callback, thisArg) {
var type = typeof isShallow;
// juggle arguments
if (typeof isShallow != 'boolean' && isShallow != null) {
if (type != 'boolean' && isShallow != null) {
thisArg = callback;
callback = (indexTypes[typeof isShallow] && thisArg && thisArg[isShallow] === array) ? null : isShallow;
callback = isShallow;
isShallow = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) {
callback = null;
}
}
if (callback != null) {
array = map(array, callback, thisArg);
@@ -2727,11 +2721,19 @@
* // => [{ 'x': 1 }, { 'x': 2 }]
*/
function uniq(array, isSorted, callback, thisArg) {
var type = typeof isSorted;
// juggle arguments
if (typeof isSorted != 'boolean' && isSorted != null) {
if (type != 'boolean' && isSorted != null) {
thisArg = callback;
callback = (indexTypes[typeof isSorted] && thisArg && thisArg[isSorted] === array) ? null : isSorted;
callback = isSorted;
isSorted = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) {
callback = null;
}
}
if (callback != null) {
callback = lodash.createCallback(callback, thisArg, 3);
@@ -3007,9 +3009,15 @@
var args = arguments,
index = -1,
props = baseFlatten(args, true, false, 1),
length = (indexTypes[typeof guard] && args[2] && args[2][guard] === collection) ? 1 : props.length,
result = Array(length);
length = props.length,
type = typeof guard;
// allows working with functions like `_.map` without using
// their `index` arguments
if ((type == 'number' || type == 'string') && args[2] && args[2][guard] === collection) {
length = 1;
}
var result = Array(length);
while(++index < length) {
result[index] = collection[props[index]];
}
@@ -3637,11 +3645,12 @@
*/
function max(collection, callback, thisArg) {
var computed = -Infinity,
result = computed;
result = computed,
type = typeof callback;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if (indexTypes[typeof callback] && thisArg && thisArg[callback] === collection) {
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) {
callback = null;
}
if (callback == null && isArray(collection)) {
@@ -3712,11 +3721,12 @@
*/
function min(collection, callback, thisArg) {
var computed = Infinity,
result = computed;
result = computed,
type = typeof callback;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if (indexTypes[typeof callback] && thisArg && thisArg[callback] === collection) {
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) {
callback = null;
}
if (callback == null && isArray(collection)) {
@@ -4845,8 +4855,14 @@
function assign(object, source, guard) {
var args = arguments,
argsIndex = 0,
argsLength = indexTypes[typeof guard] && args[3] && args[3][guard] === source ? 2 : args.length;
argsLength = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
argsLength = 2;
}
// juggle arguments
if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {
var callback = baseCreateCallback(args[--argsLength - 1], args[argsLength--], 2);
@@ -4910,11 +4926,19 @@
* // => 0
*/
function clone(value, isDeep, callback, thisArg) {
var type = typeof isDeep;
// juggle arguments
if (typeof isDeep != 'boolean' && isDeep != null) {
if (type != 'boolean' && isDeep != null) {
thisArg = callback;
callback = (indexTypes[typeof isDeep] && thisArg && thisArg[isDeep] === value) ? null : isDeep;
callback = isDeep;
isDeep = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === value) {
callback = null;
}
}
return baseClone(value, isDeep, typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1));
}
@@ -5011,8 +5035,8 @@
* @category Objects
* @param {Object} object The destination object.
* @param {...Object} [source] The source objects.
* @param- {Object} [guard] Allows working with `_.reduce` without using its
* `key` and `object` arguments as sources.
* @param- {Object} [guard] Allows working with functions like `_.reduce`
* without using their `key` and `object` arguments as sources.
* @returns {Object} Returns the destination object.
* @example
*
@@ -5023,8 +5047,14 @@
function defaults(object, source, guard) {
var args = arguments,
argsIndex = 0,
argsLength = indexTypes[typeof guard] && args[3] && args[3][guard] === source ? 2 : args.length;
argsLength = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
argsLength = 2;
}
while (++argsIndex < argsLength) {
source = args[argsIndex];
if (isObject(source)) {
@@ -5182,7 +5212,7 @@
*/
var forIn = function(object, callback, thisArg) {
var result = object;
if (!(object && objectTypes[typeof object])) {
if (!isObject(object)) {
return result;
}
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
@@ -5637,7 +5667,8 @@
// http://es5.github.io/#x8
// and avoid a V8 bug
// http://code.google.com/p/v8/issues/detail?id=2291
return !!(value && objectTypes[typeof value]);
var type = typeof value;
return value && (type == 'function' || type == 'object') || false;
}
/**
@@ -5709,8 +5740,9 @@
* // => true
*/
function isNumber(value) {
return typeof value == 'number' ||
value && typeof value == 'object' && toString.call(value) == numberClass || false;
var type = typeof value;
return type == 'number' ||
value && type == 'object' && toString.call(value) == numberClass || false;
}
/**
@@ -5923,8 +5955,14 @@
return object;
}
var args = arguments,
length = indexTypes[typeof guard] && args[3] && args[3][guard] === source ? 2 : args.length;
length = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
length = 2;
}
// juggle arguments
if (length > 3 && typeof args[length - 2] == 'function') {
var callback = baseCreateCallback(args[--length - 1], args[length--], 2);