Expose createCallback.

Former-commit-id: d0c0b02a68e33a2bf220a1605a6fe62eb4a11a83
This commit is contained in:
John-David Dalton
2013-03-03 16:19:36 -08:00
parent 9961ddc2a8
commit 9638c393bb
4 changed files with 150 additions and 125 deletions

View File

@@ -87,23 +87,24 @@
'compact': [], 'compact': [],
'compose': [], 'compose': [],
'contains': ['indexOf', 'isString'], 'contains': ['indexOf', 'isString'],
'countBy': ['forEach', 'identity', 'isEqual', 'keys'], 'countBy': ['createCallback', 'forEach'],
'createCallback': ['identity', 'isEqual', 'keys'],
'debounce': [], 'debounce': [],
'defaults': ['isArray', 'forEach', 'forOwn'], 'defaults': ['isArray', 'forEach', 'forOwn'],
'defer': ['bind'], 'defer': ['bind'],
'delay': [], 'delay': [],
'difference': ['indexOf'], 'difference': ['indexOf'],
'escape': [], 'escape': [],
'every': ['identity', 'isArray', 'isEqual', 'keys'], 'every': ['createCallback', 'isArray'],
'filter': ['identity', 'isArray', 'isEqual', 'keys'], 'filter': ['createCallback', 'isArray'],
'find': ['forEach', 'identity', 'isEqual', 'keys'], 'find': ['createCallback', 'forEach'],
'first': [], 'first': [],
'flatten': ['isArray'], 'flatten': ['isArray'],
'forEach': ['identity', 'isArguments', 'isArray', 'isString'], 'forEach': ['createCallback', 'isArguments', 'isArray', 'isString'],
'forIn': ['identity', 'isArguments'], 'forIn': ['createCallback', 'isArguments'],
'forOwn': ['identity', 'isArguments'], 'forOwn': ['createCallback', 'isArguments'],
'functions': ['forIn', 'isFunction'], 'functions': ['forIn', 'isFunction'],
'groupBy': ['forEach', 'identity', 'isEqual', 'keys'], 'groupBy': ['createCallback', 'forEach'],
'has': [], 'has': [],
'identity': [], 'identity': [],
'indexOf': ['sortedIndex'], 'indexOf': ['sortedIndex'],
@@ -131,11 +132,11 @@
'keys': ['forOwn', 'isArguments', 'isObject'], 'keys': ['forOwn', 'isArguments', 'isObject'],
'last': [], 'last': [],
'lastIndexOf': [], 'lastIndexOf': [],
'map': ['identity', 'isArray', 'isEqual', 'keys'], 'map': ['createCallback', 'isArray'],
'max': ['isArray', 'isEqual', 'isString', 'keys'], 'max': ['createCallback', 'isArray', 'isString'],
'memoize': [], 'memoize': [],
'merge': ['forEach', 'forOwn', 'isArray', 'isObject', 'isPlainObject'], 'merge': ['forEach', 'forOwn', 'isArray', 'isObject', 'isPlainObject'],
'min': ['isArray', 'isEqual', 'isString', 'keys'], 'min': ['createCallback', 'isArray', 'isString'],
'mixin': ['forEach', 'functions'], 'mixin': ['forEach', 'functions'],
'noConflict': [], 'noConflict': [],
'omit': ['forIn', 'indexOf'], 'omit': ['forIn', 'indexOf'],
@@ -148,17 +149,17 @@
'pluck': ['map'], 'pluck': ['map'],
'random': [], 'random': [],
'range': [], 'range': [],
'reduce': ['identity', 'isArray', 'isEqual', 'keys'], 'reduce': ['createCallback', 'isArray'],
'reduceRight': ['forEach', 'identity', 'isEqual', 'isString', 'keys'], 'reduceRight': ['createCallback', 'forEach', 'isString', 'keys'],
'reject': ['filter', 'identity', 'isEqual', 'keys'], 'reject': ['createCallback', 'filter'],
'rest': [], 'rest': [],
'result': ['isFunction'], 'result': ['isFunction'],
'runInContext': ['defaults', 'pick'], 'runInContext': ['defaults', 'pick'],
'shuffle': ['forEach'], 'shuffle': ['forEach'],
'size': ['keys'], 'size': ['keys'],
'some': ['identity', 'isArray', 'isEqual', 'keys'], 'some': ['createCallback', 'isArray'],
'sortBy': ['forEach', 'identity', 'isEqual', 'keys'], 'sortBy': ['createCallback', 'forEach'],
'sortedIndex': ['identity', 'isEqual', 'keys'], 'sortedIndex': ['createCallback', 'identity'],
'tap': ['value'], 'tap': ['value'],
'template': ['defaults', 'escape', 'keys', 'values'], 'template': ['defaults', 'escape', 'keys', 'values'],
'throttle': [], 'throttle': [],
@@ -166,7 +167,7 @@
'toArray': ['isString', 'values'], 'toArray': ['isString', 'values'],
'unescape': [], 'unescape': [],
'union': ['uniq'], 'union': ['uniq'],
'uniq': ['indexOf', 'isEqual', 'keys'], 'uniq': ['createCallback', 'indexOf'],
'uniqueId': [], 'uniqueId': [],
'value': ['forOwn'], 'value': ['forOwn'],
'values': ['keys'], 'values': ['keys'],
@@ -259,6 +260,7 @@
'at', 'at',
'bindKey', 'bindKey',
'cloneDeep', 'cloneDeep',
'createCallback',
'forIn', 'forIn',
'forOwn', 'forOwn',
'isPlainObject', 'isPlainObject',
@@ -707,17 +709,23 @@
* @private * @private
* @param {Array|String} methodName A single method name or array of * @param {Array|String} methodName A single method name or array of
* dependencies to query. * dependencies to query.
* @param- {Object} [stackA=[]] Internally used track queried methods.
* @returns {Array} Returns an array of method dependencies. * @returns {Array} Returns an array of method dependencies.
*/ */
function getDependencies(methodName) { function getDependencies(methodName, stack) {
var dependencies = Array.isArray(methodName) ? methodName : dependencyMap[methodName]; var dependencies = Array.isArray(methodName) ? methodName : dependencyMap[methodName];
if (!dependencies) { if (!dependencies) {
return []; return [];
} }
stack || (stack = []);
// recursively accumulate the dependencies of the `methodName` function, and // recursively accumulate the dependencies of the `methodName` function, and
// the dependencies of its dependencies, and so on // the dependencies of its dependencies, and so on
return _.uniq(dependencies.reduce(function(result, otherName) { return _.uniq(dependencies.reduce(function(result, otherName) {
result.push.apply(result, getDependencies(otherName).concat(otherName)); if (stack.indexOf(otherName) < 0) {
stack.push(otherName);
result.push.apply(result, getDependencies(otherName, stack).concat(otherName));
}
return result; return result;
}, [])); }, []));
} }
@@ -1598,6 +1606,7 @@
// flags to specify exposing Lo-Dash methods in an Underscore build // flags to specify exposing Lo-Dash methods in an Underscore build
var exposeAssign = !isUnderscore, var exposeAssign = !isUnderscore,
exposeCreateCallback = !isUnderscore,
exposeForIn = !isUnderscore, exposeForIn = !isUnderscore,
exposeForOwn = !isUnderscore, exposeForOwn = !isUnderscore,
exposeIsPlainObject = !isUnderscore, exposeIsPlainObject = !isUnderscore,
@@ -1637,6 +1646,7 @@
if (isUnderscore) { if (isUnderscore) {
var methods = _.without.apply(_, [_.union(includeMethods, plusMethods)].concat(minusMethods)); var methods = _.without.apply(_, [_.union(includeMethods, plusMethods)].concat(minusMethods));
exposeAssign = methods.indexOf('assign') > -1; exposeAssign = methods.indexOf('assign') > -1;
exposeCreateCallback = methods.indexOf('createCallback') > -1;
exposeForIn = methods.indexOf('forIn') > -1; exposeForIn = methods.indexOf('forIn') > -1;
exposeForOwn = methods.indexOf('forOwn') > -1; exposeForOwn = methods.indexOf('forOwn') > -1;
exposeIsPlainObject = methods.indexOf('isPlainObject') > -1; exposeIsPlainObject = methods.indexOf('isPlainObject') > -1;
@@ -1657,25 +1667,13 @@
} }
if (isUnderscore) { if (isUnderscore) {
dependencyMap.contains = _.without(dependencyMap.contains, 'isString'); dependencyMap.contains = _.without(dependencyMap.contains, 'isString');
dependencyMap.countBy = _.without(dependencyMap.countBy, 'isEqual', 'keys');
dependencyMap.every = _.without(dependencyMap.every, 'isEqual', 'keys');
dependencyMap.filter = _.without(dependencyMap.filter, 'isEqual');
dependencyMap.find = _.without(dependencyMap.find, 'isEqual', 'keys');
dependencyMap.groupBy = _.without(dependencyMap.groupBy, 'isEqual', 'keys');
dependencyMap.isEmpty = ['isArray', 'isString']; dependencyMap.isEmpty = ['isArray', 'isString'];
dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments'); dependencyMap.isEqual = _.without(dependencyMap.isEqual, 'forIn', 'isArguments');
dependencyMap.map = _.without(dependencyMap.map, 'isEqual', 'keys'); dependencyMap.max = _.without(dependencyMap.max, 'isString');
dependencyMap.max = _.without(dependencyMap.max, 'isEqual', 'isString', 'keys'); dependencyMap.min = _.without(dependencyMap.min, 'isString');
dependencyMap.min = _.without(dependencyMap.min, 'isEqual', 'isString', 'keys');
dependencyMap.pick = _.without(dependencyMap.pick, 'forIn', 'isObject'); dependencyMap.pick = _.without(dependencyMap.pick, 'forIn', 'isObject');
dependencyMap.reduce = _.without(dependencyMap.reduce, 'isEqual', 'keys'); dependencyMap.reduceRight = _.without(dependencyMap.reduceRight, 'isString');
dependencyMap.reduceRight = _.without(dependencyMap.reduceRight, 'isEqual', 'isString');
dependencyMap.reject = _.without(dependencyMap.reject, 'isEqual', 'keys');
dependencyMap.some = _.without(dependencyMap.some, 'isEqual', 'keys');
dependencyMap.sortBy = _.without(dependencyMap.sortBy, 'isEqual', 'keys');
dependencyMap.sortedIndex = _.without(dependencyMap.sortedIndex, 'isEqual', 'keys');
dependencyMap.template = _.without(dependencyMap.template, 'keys', 'values'); dependencyMap.template = _.without(dependencyMap.template, 'keys', 'values');
dependencyMap.uniq = _.without(dependencyMap.uniq, 'isEqual', 'keys');
dependencyMap.where.push('find', 'isEmpty'); dependencyMap.where.push('find', 'isEmpty');
if (useUnderscoreClone) { if (useUnderscoreClone) {
@@ -2201,7 +2199,7 @@
// remove `_.isEqual` use from `createCallback` // remove `_.isEqual` use from `createCallback`
source = source.replace(matchFunction(source, 'createCallback'), function(match) { source = source.replace(matchFunction(source, 'createCallback'), function(match) {
return match.replace(/isEqual\(([^,]+), *([^,]+)[^)]+\)/, '$1 === $2'); return match.replace(/\bisEqual\(([^,]+), *([^,]+)[^)]+\)/, '$1 === $2');
}); });
// remove conditional `charCodeCallback` use from `_.max` and `_.min` // remove conditional `charCodeCallback` use from `_.max` and `_.min`
@@ -2211,6 +2209,10 @@
}); });
}); });
// replace `lodash.createCallback` references with `createCallback`
if (!exposeCreateCallback) {
source = source.replace(/\blodash\.(createCallback\()\b/g, '$1');
}
// remove unneeded variables // remove unneeded variables
if (useUnderscoreClone) { if (useUnderscoreClone) {
source = removeVar(source, 'cloneableClasses'); source = removeVar(source, 'cloneableClasses');
@@ -2334,6 +2336,9 @@
if (!exposeAssign) { if (!exposeAssign) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.assign *=.+\n/m, ''); modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.assign *=.+\n/m, '');
} }
if (!exposeCreateCallback) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.createCallback *=.+\n/m, '');
}
if (!exposeForIn) { if (!exposeForIn) {
modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.forIn *=.+\n/m, ''); modified = modified.replace(/^(?: *\/\/.*\s*)* *lodash\.forIn *=.+\n/m, '');
} }

View File

@@ -12,7 +12,6 @@
'argsLength', 'argsLength',
'callback', 'callback',
'collection', 'collection',
'createCallback',
'ctor', 'ctor',
'guard', 'guard',
'hasOwnProperty', 'hasOwnProperty',
@@ -22,6 +21,7 @@
'isString', 'isString',
'iterable', 'iterable',
'length', 'length',
'lodash',
'nativeKeys', 'nativeKeys',
'object', 'object',
'objectTypes', 'objectTypes',
@@ -88,6 +88,7 @@
'compose', 'compose',
'contains', 'contains',
'countBy', 'countBy',
'createCallback',
'criteria', 'criteria',
'debounce', 'debounce',
'defaults', 'defaults',
@@ -237,7 +238,10 @@
return "['" + prop.replace(/['\n\r\t]/g, '\\$&') + "']"; return "['" + prop.replace(/['\n\r\t]/g, '\\$&') + "']";
}); });
// remove brackets from `_.escape()` in `_.template` // remove brackets from `lodash.createCallback` in `_.assign`
source = source.replace("' var callback = lodash['createCallback']", "'var callback=lodash.createCallback");
// remove brackets from `_.escape` in `_.template`
source = source.replace(/__e *= *_\['escape']/g, '__e=_.escape'); source = source.replace(/__e *= *_\['escape']/g, '__e=_.escape');
// remove brackets from `collection.indexOf` in `_.contains` // remove brackets from `collection.indexOf` in `_.contains`

182
lodash.js
View File

@@ -513,7 +513,7 @@
/** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */ /** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */
var eachIteratorOptions = { var eachIteratorOptions = {
'args': 'collection, callback, thisArg', 'args': 'collection, callback, thisArg',
'top': "callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg)", 'top': "callback = callback && typeof thisArg == 'undefined' ? callback : lodash.createCallback(callback, thisArg)",
'arrays': "typeof length == 'number'", 'arrays': "typeof length == 'number'",
'loop': 'if (callback(iterable[index], index, collection) === false) return result' 'loop': 'if (callback(iterable[index], index, collection) === false) return result'
}; };
@@ -657,64 +657,6 @@
return bound; return bound;
} }
/**
* Produces a callback bound to an optional `thisArg`. If `func` is a property
* name, the created callback will return the property value for a given element.
* If `func` is an object, the created callback will return `true` for elements
* that contain the equivalent object properties, otherwise it will return `false`.
*
* @private
* @param {Mixed} [func=identity] The value to convert to a callback.
* @param {Mixed} [thisArg] The `this` binding of the created callback.
* @param {Number} [argCount=3] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
*/
function createCallback(func, thisArg, argCount) {
if (func == null) {
return identity;
}
var type = typeof func;
if (type != 'function') {
if (type != 'object') {
return function(object) {
return object[func];
};
}
var props = keys(func);
return function(object) {
var length = props.length,
result = false;
while (length--) {
if (!(result = isEqual(object[props[length]], func[props[length]], indicatorObject))) {
break;
}
}
return result;
};
}
if (typeof thisArg != 'undefined') {
if (argCount === 1) {
return function(value) {
return func.call(thisArg, value);
};
}
if (argCount === 2) {
return function(a, b) {
return func.call(thisArg, a, b);
};
}
if (argCount === 4) {
return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
}
return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
}
return func;
}
/** /**
* Creates compiled iteration functions. * Creates compiled iteration functions.
* *
@@ -757,13 +699,13 @@
// create the function factory // create the function factory
var factory = Function( var factory = Function(
'createCallback, hasOwnProperty, isArguments, isArray, isString, ' + 'hasOwnProperty, isArguments, isArray, isString, lodash, ' +
'objectTypes, nativeKeys', 'objectTypes, nativeKeys',
'return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}' 'return function(' + args + ') {\n' + iteratorTemplate(data) + '\n}'
); );
// return the compiled function // return the compiled function
return factory( return factory(
createCallback, hasOwnProperty, isArguments, isArray, isString, hasOwnProperty, isArguments, isArray, isString, lodash,
objectTypes, nativeKeys objectTypes, nativeKeys
); );
} }
@@ -1137,7 +1079,7 @@
defaultsIteratorOptions.top.replace(';', defaultsIteratorOptions.top.replace(';',
';\n' + ';\n' +
"if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {\n" + "if (argsLength > 3 && typeof args[argsLength - 2] == 'function') {\n" +
' var callback = createCallback(args[--argsLength - 1], args[argsLength--], 2);\n' + ' var callback = lodash.createCallback(args[--argsLength - 1], args[argsLength--], 2);\n' +
"} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {\n" + "} else if (argsLength > 2 && typeof args[argsLength - 1] == 'function') {\n" +
' callback = args[--argsLength];\n' + ' callback = args[--argsLength];\n' +
'}' '}'
@@ -1198,9 +1140,11 @@
deep = false; deep = false;
} }
if (typeof callback == 'function') { if (typeof callback == 'function') {
callback = typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg, 1); callback = (typeof thisArg == 'undefined')
result = callback(result); ? callback
: lodash.createCallback(callback, thisArg, 1);
result = callback(result);
if (typeof result != 'undefined') { if (typeof result != 'undefined') {
return result; return result;
} }
@@ -1541,7 +1485,10 @@
// used to indicate that when comparing objects, `a` has at least the properties of `b` // used to indicate that when comparing objects, `a` has at least the properties of `b`
var whereIndicator = callback === indicatorObject; var whereIndicator = callback === indicatorObject;
if (callback && !whereIndicator) { if (callback && !whereIndicator) {
callback = typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg, 2); callback = (typeof thisArg == 'undefined')
? callback
: lodash.createCallback(callback, thisArg, 2);
var result = callback(a, b); var result = callback(a, b);
if (typeof result != 'undefined') { if (typeof result != 'undefined') {
return !!result; return !!result;
@@ -2006,7 +1953,7 @@
length = args.length; length = args.length;
} }
if (length > 3 && typeof args[length - 2] == 'function') { if (length > 3 && typeof args[length - 2] == 'function') {
callback = createCallback(args[--length - 1], args[length--], 2); callback = lodash.createCallback(args[--length - 1], args[length--], 2);
} else if (length > 2 && typeof args[length - 1] == 'function') { } else if (length > 2 && typeof args[length - 1] == 'function') {
callback = args[--length]; callback = args[--length];
} }
@@ -2096,7 +2043,7 @@
result = {}; result = {};
if (isFunc) { if (isFunc) {
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
} else { } else {
var props = concat.apply(arrayRef, arguments); var props = concat.apply(arrayRef, arguments);
} }
@@ -2198,7 +2145,7 @@
} }
} }
} else { } else {
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
forIn(object, function(value, key, object) { forIn(object, function(value, key, object) {
if (callback(value, key, object)) { if (callback(value, key, object)) {
result[key] = value; result[key] = value;
@@ -2354,7 +2301,7 @@
*/ */
function countBy(collection, callback, thisArg) { function countBy(collection, callback, thisArg) {
var result = {}; var result = {};
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) { forEach(collection, function(value, key, collection) {
key = callback(value, key, collection) + ''; key = callback(value, key, collection) + '';
@@ -2406,7 +2353,7 @@
*/ */
function every(collection, callback, thisArg) { function every(collection, callback, thisArg) {
var result = true; var result = true;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -2467,7 +2414,7 @@
*/ */
function filter(collection, callback, thisArg) { function filter(collection, callback, thisArg) {
var result = []; var result = [];
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -2534,7 +2481,7 @@
*/ */
function find(collection, callback, thisArg) { function find(collection, callback, thisArg) {
var result; var result;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
forEach(collection, function(value, index, collection) { forEach(collection, function(value, index, collection) {
if (callback(value, index, collection)) { if (callback(value, index, collection)) {
@@ -2619,7 +2566,7 @@
*/ */
function groupBy(collection, callback, thisArg) { function groupBy(collection, callback, thisArg) {
var result = {}; var result = {};
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) { forEach(collection, function(value, key, collection) {
key = callback(value, key, collection) + ''; key = callback(value, key, collection) + '';
@@ -2707,7 +2654,7 @@
length = collection ? collection.length : 0, length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0); result = Array(typeof length == 'number' ? length : 0);
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
if (isArray(collection)) { if (isArray(collection)) {
while (++index < length) { while (++index < length) {
result[index] = callback(collection[index], index, collection); result[index] = callback(collection[index], index, collection);
@@ -2776,7 +2723,7 @@
} else { } else {
callback = (!callback && isString(collection)) callback = (!callback && isString(collection))
? charAtCallback ? charAtCallback
: createCallback(callback, thisArg); : lodash.createCallback(callback, thisArg);
each(collection, function(value, index, collection) { each(collection, function(value, index, collection) {
var current = callback(value, index, collection); var current = callback(value, index, collection);
@@ -2845,7 +2792,7 @@
} else { } else {
callback = (!callback && isString(collection)) callback = (!callback && isString(collection))
? charAtCallback ? charAtCallback
: createCallback(callback, thisArg); : lodash.createCallback(callback, thisArg);
each(collection, function(value, index, collection) { each(collection, function(value, index, collection) {
var current = callback(value, index, collection); var current = callback(value, index, collection);
@@ -2912,7 +2859,7 @@
*/ */
function reduce(collection, callback, accumulator, thisArg) { function reduce(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3; var noaccum = arguments.length < 3;
callback = createCallback(callback, thisArg, 4); callback = lodash.createCallback(callback, thisArg, 4);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -2964,7 +2911,7 @@
} else if (noCharByIndex && isString(collection)) { } else if (noCharByIndex && isString(collection)) {
iterable = collection.split(''); iterable = collection.split('');
} }
callback = createCallback(callback, thisArg, 4); callback = lodash.createCallback(callback, thisArg, 4);
forEach(collection, function(value, index, collection) { forEach(collection, function(value, index, collection) {
index = props ? props[--length] : --length; index = props ? props[--length] : --length;
accumulator = noaccum accumulator = noaccum
@@ -3014,7 +2961,7 @@
* // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }] * // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
*/ */
function reject(collection, callback, thisArg) { function reject(collection, callback, thisArg) {
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
return filter(collection, function(value, index, collection) { return filter(collection, function(value, index, collection) {
return !callback(value, index, collection); return !callback(value, index, collection);
}); });
@@ -3116,7 +3063,7 @@
*/ */
function some(collection, callback, thisArg) { function some(collection, callback, thisArg) {
var result; var result;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -3175,7 +3122,7 @@
length = collection ? collection.length : 0, length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0); result = Array(typeof length == 'number' ? length : 0);
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
forEach(collection, function(value, key, collection) { forEach(collection, function(value, key, collection) {
result[++index] = { result[++index] = {
'criteria': callback(value, key, collection), 'criteria': callback(value, key, collection),
@@ -3365,7 +3312,7 @@
if (typeof callback != 'number' && callback != null) { if (typeof callback != 'number' && callback != null) {
var index = -1; var index = -1;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
while (++index < length && callback(array[index], index, array)) { while (++index < length && callback(array[index], index, array)) {
n++; n++;
} }
@@ -3522,7 +3469,7 @@
if (typeof callback != 'number' && callback != null) { if (typeof callback != 'number' && callback != null) {
var index = length; var index = length;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
while (index-- && callback(array[index], index, array)) { while (index-- && callback(array[index], index, array)) {
n++; n++;
} }
@@ -3646,7 +3593,7 @@
if (typeof callback != 'number' && callback != null) { if (typeof callback != 'number' && callback != null) {
var index = length; var index = length;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
while (index-- && callback(array[index], index, array)) { while (index-- && callback(array[index], index, array)) {
n++; n++;
} }
@@ -3806,7 +3753,7 @@
index = -1, index = -1,
length = array ? array.length : 0; length = array ? array.length : 0;
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
while (++index < length && callback(array[index], index, array)) { while (++index < length && callback(array[index], index, array)) {
n++; n++;
} }
@@ -3869,7 +3816,7 @@
high = array ? array.length : low; high = array ? array.length : low;
// explicitly reference `identity` for better inlining in Firefox // explicitly reference `identity` for better inlining in Firefox
callback = callback ? createCallback(callback, thisArg, 1) : identity; callback = callback ? lodash.createCallback(callback, thisArg, 1) : identity;
value = callback(value); value = callback(value);
while (low < high) { while (low < high) {
@@ -3962,7 +3909,7 @@
} }
if (callback != null) { if (callback != null) {
seen = []; seen = [];
callback = createCallback(callback, thisArg); callback = lodash.createCallback(callback, thisArg);
} }
while (++index < length) { while (++index < length) {
var value = array[index], var value = array[index],
@@ -4246,6 +4193,66 @@
}; };
} }
/**
* Produces a callback bound to an optional `thisArg`. If `func` is a property
* name, the created callback will return the property value for a given element.
* If `func` is an object, the created callback will return `true` for elements
* that contain the equivalent object properties, otherwise it will return `false`.
*
* @static
* @memberOf _
* @category Functions
* @param {Mixed} [func=identity] The value to convert to a callback.
* @param {Mixed} [thisArg] The `this` binding of the created callback.
* @param {Number} [argCount=3] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
*/
function createCallback(func, thisArg, argCount) {
if (func == null) {
return identity;
}
var type = typeof func;
if (type != 'function') {
if (type != 'object') {
return function(object) {
return object[func];
};
}
var props = keys(func);
return function(object) {
var length = props.length,
result = false;
while (length--) {
if (!(result = isEqual(object[props[length]], func[props[length]], indicatorObject))) {
break;
}
}
return result;
};
}
if (typeof thisArg != 'undefined') {
if (argCount === 1) {
return function(value) {
return func.call(thisArg, value);
};
}
if (argCount === 2) {
return function(a, b) {
return func.call(thisArg, a, b);
};
}
if (argCount === 4) {
return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
}
return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
}
return func;
}
/** /**
* Creates a function that will delay the execution of `func` until after * Creates a function that will delay the execution of `func` until after
* `wait` milliseconds have elapsed since the last time it was invoked. Pass * `wait` milliseconds have elapsed since the last time it was invoked. Pass
@@ -5020,6 +5027,7 @@
lodash.compact = compact; lodash.compact = compact;
lodash.compose = compose; lodash.compose = compose;
lodash.countBy = countBy; lodash.countBy = countBy;
lodash.createCallback = createCallback;
lodash.debounce = debounce; lodash.debounce = debounce;
lodash.defaults = defaults; lodash.defaults = defaults;
lodash.defer = defer; lodash.defer = defer;

View File

@@ -167,6 +167,7 @@
'bind', 'bind',
'bindAll', 'bindAll',
'bindKey', 'bindKey',
'createCallback',
'compose', 'compose',
'debounce', 'debounce',
'defer', 'defer',
@@ -291,6 +292,7 @@
'at', 'at',
'bindKey', 'bindKey',
'cloneDeep', 'cloneDeep',
'createCallback',
'forIn', 'forIn',
'forOwn', 'forOwn',
'isPlainObject', 'isPlainObject',
@@ -932,6 +934,7 @@
'assign', 'assign',
'at', 'at',
'bindKey', 'bindKey',
'createCallback',
'forIn', 'forIn',
'forOwn', 'forOwn',
'isPlainObject', 'isPlainObject',
@@ -1334,6 +1337,7 @@
context = createContext(), context = createContext(),
isUnderscore = /backbone|underscore/.test(command), isUnderscore = /backbone|underscore/.test(command),
exposeAssign = !isUnderscore, exposeAssign = !isUnderscore,
exposeCreateCallback = !isUnderscore,
exposeZipObject = !isUnderscore; exposeZipObject = !isUnderscore;
try { try {
@@ -1352,6 +1356,7 @@
if (isUnderscore) { if (isUnderscore) {
if (methodNames) { if (methodNames) {
exposeAssign = methodNames.indexOf('assign') > -1; exposeAssign = methodNames.indexOf('assign') > -1;
exposeCreateCallback = methodNames.indexOf('createCallback') > -1;
exposeZipObject = methodNames.indexOf('zipObject') > -1; exposeZipObject = methodNames.indexOf('zipObject') > -1;
} else { } else {
methodNames = underscoreMethods.slice(); methodNames = underscoreMethods.slice();
@@ -1392,6 +1397,9 @@
if (!exposeAssign) { if (!exposeAssign) {
methodNames = _.without(methodNames, 'assign'); methodNames = _.without(methodNames, 'assign');
} }
if (!exposeCreateCallback) {
methodNames = _.without(methodNames, 'createCallback');
}
if (!exposeZipObject) { if (!exposeZipObject) {
methodNames = _.without(methodNames, 'zipObject'); methodNames = _.without(methodNames, 'zipObject');
} }