Rename _.createCallback to _.callback.

This commit is contained in:
John-David Dalton
2014-06-17 01:07:04 -07:00
parent 20202b793b
commit 0d2db28908
2 changed files with 55 additions and 56 deletions

105
lodash.js
View File

@@ -735,13 +735,13 @@
* implicitly or explicitly included in the build. * implicitly or explicitly included in the build.
* *
* The chainable wrapper functions are: * The chainable wrapper functions are:
* `after`, `assign`, `at`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`, * `after`, `assign`, `at`, `bind`, `bindAll`, `bindKey`, `callback`, `chain`,
* `compose`, `concat`, `constant`, `countBy`, `create`, `createCallback`, * `compact`, `compose`, `concat`, `constant`, `countBy`, `create`, `curry`,
* `curry`, `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, * `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`, `flatten`,
* `flatten`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
* `forOwnRight`, `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
* `invert`, `invoke`, `keys`, `map`, `mapValues`, `matches`, `max`, `memoize`, * `invoke`, `keys`, `map`, `mapValues`, `matches`, `max`, `memoize`, `merge`,
* `merge`, `min`, `mixin`, `noop`, `object`, `omit`, `once`, `pairs`, `partial`, * `min`, `mixin`, `noop`, `object`, `omit`, `once`, `pairs`, `partial`,
* `partialRight`, `pick`, `pluck`, `property`, `pull`, `pullAt`, `push`, * `partialRight`, `pick`, `pluck`, `property`, `pull`, `pullAt`, `push`,
* `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
* `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`, * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
@@ -1331,7 +1331,7 @@
} }
/** /**
* The base implementation of `_.createCallback` without support for creating * The base implementation of `_.callback` without support for creating
* "_.pluck" and "_.where" style callbacks. * "_.pluck" and "_.where" style callbacks.
* *
* @private * @private
@@ -1340,7 +1340,7 @@
* @param {number} [argCount] The number of arguments the callback accepts. * @param {number} [argCount] The number of arguments the callback accepts.
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function baseCreateCallback(func, thisArg, argCount) { function baseCallback(func, thisArg, argCount) {
if (typeof func != 'function') { if (typeof func != 'function') {
return identity; return identity;
} }
@@ -2272,7 +2272,7 @@
function createAggregator(setter, initializer) { function createAggregator(setter, initializer) {
return function(collection, callback, thisArg) { return function(collection, callback, thisArg) {
var result = initializer ? initializer() : {}; var result = initializer ? initializer() : {};
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.callback(callback, thisArg, 3);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -2314,7 +2314,7 @@
} }
// juggle arguments // juggle arguments
if (length > 3 && typeof args[length - 2] == 'function') { if (length > 3 && typeof args[length - 2] == 'function') {
var callback = baseCreateCallback(args[--length - 1], args[length--], 5); var callback = baseCallback(args[--length - 1], args[length--], 5);
} else if (length > 2 && typeof args[length - 1] == 'function') { } else if (length > 2 && typeof args[length - 1] == 'function') {
callback = args[--length]; callback = args[--length];
} }
@@ -2912,7 +2912,7 @@
var index = -1, var index = -1,
length = array ? array.length : 0; length = array ? array.length : 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (++index < length) { while (++index < length) {
if (predicate(array[index], index, array)) { if (predicate(array[index], index, array)) {
return index; return index;
@@ -2965,7 +2965,7 @@
function findLastIndex(array, predicate, thisArg) { function findLastIndex(array, predicate, thisArg) {
var length = array ? array.length : 0; var length = array ? array.length : 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (length--) { while (length--) {
if (predicate(array[length], length, array)) { if (predicate(array[length], length, array)) {
return length; return length;
@@ -3000,7 +3000,7 @@
length = array ? array.length : 0, length = array ? array.length : 0,
n = 0; n = 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (++index < length && predicate(array[index], index, array)) { while (++index < length && predicate(array[index], index, array)) {
n++; n++;
} }
@@ -3116,7 +3116,7 @@
var index = length, var index = length,
n = 0; n = 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (index-- && predicate(array[index], index, array)) { while (index-- && predicate(array[index], index, array)) {
n++; n++;
} }
@@ -3207,7 +3207,7 @@
var index = length, var index = length,
n = 0; n = 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (index-- && predicate(array[index], index, array)) { while (index-- && predicate(array[index], index, array)) {
n++; n++;
} }
@@ -3361,7 +3361,7 @@
length = array ? array.length : 0, length = array ? array.length : 0,
result = []; result = [];
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (++index < length) { while (++index < length) {
var value = array[index]; var value = array[index];
if (predicate(value, index, array)) { if (predicate(value, index, array)) {
@@ -3396,7 +3396,7 @@
length = array ? array.length : 0, length = array ? array.length : 0,
n = 0; n = 0;
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
while (++index < length && predicate(array[index], index, array)) { while (++index < length && predicate(array[index], index, array)) {
n++; n++;
} }
@@ -3502,7 +3502,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 ? lodash.createCallback(callback, thisArg, 1) : identity; callback = callback ? lodash.callback(callback, thisArg, 1) : identity;
value = callback(value); value = callback(value);
while (low < high) { while (low < high) {
@@ -3731,7 +3731,7 @@
} }
} }
if (callback != null) { if (callback != null) {
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.callback(callback, thisArg, 3);
} }
return baseUniq(array, isSorted, callback); return baseUniq(array, isSorted, callback);
} }
@@ -4137,7 +4137,7 @@
var result = true; var result = true;
if (typeof predicate != 'function' || typeof thisArg != 'undefined') { if (typeof predicate != 'function' || typeof thisArg != 'undefined') {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
} }
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -4199,7 +4199,7 @@
*/ */
function filter(collection, predicate, thisArg) { function filter(collection, predicate, thisArg) {
var result = []; var result = [];
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -4269,7 +4269,7 @@
var index = findIndex(collection, predicate, thisArg); var index = findIndex(collection, predicate, thisArg);
return index > -1 ? collection[index] : undefined; return index > -1 ? collection[index] : undefined;
} }
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
return baseFind(collection, predicate, baseEach); return baseFind(collection, predicate, baseEach);
} }
@@ -4292,7 +4292,7 @@
* // => 3 * // => 3
*/ */
function findLast(collection, predicate, thisArg) { function findLast(collection, predicate, thisArg) {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
return baseFind(collection, predicate, baseEachRight); return baseFind(collection, predicate, baseEachRight);
} }
@@ -4353,7 +4353,7 @@
function forEach(collection, callback, thisArg) { function forEach(collection, callback, thisArg) {
return (typeof callback == 'function' && typeof thisArg == 'undefined' && isArray(collection)) return (typeof callback == 'function' && typeof thisArg == 'undefined' && isArray(collection))
? arrayEach(collection, callback) ? arrayEach(collection, callback)
: baseEach(collection, baseCreateCallback(callback, thisArg, 3)); : baseEach(collection, baseCallback(callback, thisArg, 3));
} }
/** /**
@@ -4376,7 +4376,7 @@
function forEachRight(collection, callback, thisArg) { function forEachRight(collection, callback, thisArg) {
return (typeof callback == 'function' && typeof thisArg == 'undefined' && isArray(collection)) return (typeof callback == 'function' && typeof thisArg == 'undefined' && isArray(collection))
? arrayEachRight(collection, callback) ? arrayEachRight(collection, callback)
: baseEachRight(collection, baseCreateCallback(callback, thisArg, 3)); : baseEachRight(collection, baseCallback(callback, thisArg, 3));
} }
/** /**
@@ -4531,7 +4531,7 @@
* // => ['barney', 'fred'] * // => ['barney', 'fred']
*/ */
function map(collection, callback, thisArg) { function map(collection, callback, thisArg) {
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.callback(callback, thisArg, 3);
if (isArray(collection)) { if (isArray(collection)) {
return arrayMap(collection, callback, thisArg); return arrayMap(collection, callback, thisArg);
@@ -4610,7 +4610,7 @@
} else { } else {
callback = (callback == null && isString(collection)) callback = (callback == null && isString(collection))
? charAtCallback ? charAtCallback
: lodash.createCallback(callback, thisArg, 3); : lodash.callback(callback, thisArg, 3);
baseEach(collection, function(value, index, collection) { baseEach(collection, function(value, index, collection) {
var current = callback(value, index, collection); var current = callback(value, index, collection);
@@ -4688,7 +4688,7 @@
} else { } else {
callback = (callback == null && isString(collection)) callback = (callback == null && isString(collection))
? charAtCallback ? charAtCallback
: lodash.createCallback(callback, thisArg, 3); : lodash.callback(callback, thisArg, 3);
baseEach(collection, function(value, index, collection) { baseEach(collection, function(value, index, collection) {
var current = callback(value, index, collection); var current = callback(value, index, collection);
@@ -4802,7 +4802,7 @@
*/ */
function reduce(collection, callback, accumulator, thisArg) { function reduce(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3; var noaccum = arguments.length < 3;
callback = lodash.createCallback(callback, thisArg, 4); callback = lodash.callback(callback, thisArg, 4);
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -4845,7 +4845,7 @@
*/ */
function reduceRight(collection, callback, accumulator, thisArg) { function reduceRight(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3; var noaccum = arguments.length < 3;
callback = lodash.createCallback(callback, thisArg, 4); callback = lodash.callback(callback, thisArg, 4);
baseEachRight(collection, function(value, index, collection) { baseEachRight(collection, function(value, index, collection) {
accumulator = noaccum accumulator = noaccum
@@ -4894,7 +4894,7 @@
* // => [{ 'name': 'fred', 'age': 40, 'blocked': true }] * // => [{ 'name': 'fred', 'age': 40, 'blocked': true }]
*/ */
function reject(collection, predicate, thisArg) { function reject(collection, predicate, thisArg) {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
return filter(collection, negate(predicate)); return filter(collection, negate(predicate));
} }
@@ -5032,7 +5032,7 @@
var result; var result;
if (typeof predicate != 'function' || typeof thisArg != 'undefined') { if (typeof predicate != 'function' || typeof thisArg != 'undefined') {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
} }
if (isArray(collection)) { if (isArray(collection)) {
var index = -1, var index = -1,
@@ -5108,7 +5108,7 @@
result = Array(length < 0 ? 0 : length >>> 0); result = Array(length < 0 ? 0 : length >>> 0);
if (!multi) { if (!multi) {
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.callback(callback, thisArg, 3);
} }
baseEach(collection, function(value, key, collection) { baseEach(collection, function(value, key, collection) {
if (multi) { if (multi) {
@@ -6020,7 +6020,7 @@
callback = null; callback = null;
} }
} }
callback = typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1); callback = typeof callback == 'function' && baseCallback(callback, thisArg, 1);
return baseClone(value, isDeep, callback); return baseClone(value, isDeep, callback);
} }
@@ -6067,7 +6067,7 @@
* // => false * // => false
*/ */
function cloneDeep(value, callback, thisArg) { function cloneDeep(value, callback, thisArg) {
callback = typeof callback == 'function' && baseCreateCallback(callback, thisArg, 1); callback = typeof callback == 'function' && baseCallback(callback, thisArg, 1);
return baseClone(value, true, callback); return baseClone(value, true, callback);
} }
@@ -6178,7 +6178,7 @@
* // => 'fred' * // => 'fred'
*/ */
function findKey(object, predicate, thisArg) { function findKey(object, predicate, thisArg) {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
return baseFind(object, predicate, baseForOwn, true); return baseFind(object, predicate, baseForOwn, true);
} }
@@ -6224,7 +6224,7 @@
* // => 'pebbles' * // => 'pebbles'
*/ */
function findLastKey(object, predicate, thisArg) { function findLastKey(object, predicate, thisArg) {
predicate = lodash.createCallback(predicate, thisArg, 3); predicate = lodash.callback(predicate, thisArg, 3);
return baseFind(object, predicate, baseForOwnRight, true); return baseFind(object, predicate, baseForOwnRight, true);
} }
@@ -6257,7 +6257,7 @@
*/ */
function forIn(object, callback, thisArg) { function forIn(object, callback, thisArg) {
if (typeof callback != 'function' || typeof thisArg != 'undefined') { if (typeof callback != 'function' || typeof thisArg != 'undefined') {
callback = baseCreateCallback(callback, thisArg, 3); callback = baseCallback(callback, thisArg, 3);
} }
return baseFor(object, callback, keysIn); return baseFor(object, callback, keysIn);
} }
@@ -6288,7 +6288,7 @@
* // => logs 'z', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'z' * // => logs 'z', 'y', and 'x' assuming `_.forIn ` logs 'x', 'y', and 'z'
*/ */
function forInRight(object, callback, thisArg) { function forInRight(object, callback, thisArg) {
callback = baseCreateCallback(callback, thisArg, 3); callback = baseCallback(callback, thisArg, 3);
return baseForRight(object, callback, keysIn); return baseForRight(object, callback, keysIn);
} }
@@ -6314,7 +6314,7 @@
*/ */
function forOwn(object, callback, thisArg) { function forOwn(object, callback, thisArg) {
if (typeof callback != 'function' || typeof thisArg != 'undefined') { if (typeof callback != 'function' || typeof thisArg != 'undefined') {
callback = baseCreateCallback(callback, thisArg, 3); callback = baseCallback(callback, thisArg, 3);
} }
return baseForOwn(object, callback); return baseForOwn(object, callback);
} }
@@ -6338,7 +6338,7 @@
* // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length' * // => logs 'length', '1', and '0' assuming `_.forOwn` logs '0', '1', and 'length'
*/ */
function forOwnRight(object, callback, thisArg) { function forOwnRight(object, callback, thisArg) {
callback = baseCreateCallback(callback, thisArg, 3); callback = baseCallback(callback, thisArg, 3);
return baseForRight(object, callback, keys); return baseForRight(object, callback, keys);
} }
@@ -6632,7 +6632,7 @@
* // => true * // => true
*/ */
function isEqual(value, other, callback, thisArg) { function isEqual(value, other, callback, thisArg) {
callback = typeof callback == 'function' && baseCreateCallback(callback, thisArg, 3); callback = typeof callback == 'function' && baseCallback(callback, thisArg, 3);
if (!callback) { if (!callback) {
// exit early for identical values // exit early for identical values
@@ -7090,7 +7090,7 @@
*/ */
function mapValues(object, callback, thisArg) { function mapValues(object, callback, thisArg) {
var result = {}; var result = {};
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.callback(callback, thisArg, 3);
baseForOwn(object, function(value, key, object) { baseForOwn(object, function(value, key, object) {
result[key] = callback(value, key, object); result[key] = callback(value, key, object);
@@ -7183,7 +7183,7 @@
return {}; return {};
} }
if (typeof predicate == 'function') { if (typeof predicate == 'function') {
return basePick(object, negate(lodash.createCallback(predicate, thisArg, 3))); return basePick(object, negate(lodash.callback(predicate, thisArg, 3)));
} }
var omitProps = baseFlatten(arguments, false, false, 1); var omitProps = baseFlatten(arguments, false, false, 1);
return basePick(object, baseDifference(keysIn(object), arrayMap(omitProps, String))); return basePick(object, baseDifference(keysIn(object), arrayMap(omitProps, String)));
@@ -7248,7 +7248,7 @@
return {}; return {};
} }
return basePick(object, typeof predicate == 'function' return basePick(object, typeof predicate == 'function'
? lodash.createCallback(predicate, thisArg, 3) ? lodash.callback(predicate, thisArg, 3)
: baseFlatten(arguments, false, false, 1)); : baseFlatten(arguments, false, false, 1));
} }
@@ -7297,7 +7297,7 @@
} }
} }
if (callback) { if (callback) {
callback = lodash.createCallback(callback, thisArg, 4); callback = lodash.callback(callback, thisArg, 4);
(isArr ? arrayEach : baseForOwn)(object, function(value, index, object) { (isArr ? arrayEach : baseForOwn)(object, function(value, index, object) {
return callback(accumulator, value, index, object); return callback(accumulator, value, index, object);
}); });
@@ -8106,7 +8106,7 @@
* ]; * ];
* *
* // wrap to create custom callback shorthands * // wrap to create custom callback shorthands
* _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { * _.callback = _.wrap(_.callback, function(func, callback, thisArg) {
* var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback);
* return !match ? func(callback, thisArg) : function(object) { * return !match ? func(callback, thisArg) : function(object) {
* return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3];
@@ -8116,7 +8116,7 @@
* _.filter(characters, 'age__gt38'); * _.filter(characters, 'age__gt38');
* // => [{ 'name': 'fred', 'age': 40 }] * // => [{ 'name': 'fred', 'age': 40 }]
*/ */
function createCallback(func, thisArg, argCount) { function callback(func, thisArg, argCount) {
var type = typeof func, var type = typeof func,
isFunc = type == 'function'; isFunc = type == 'function';
@@ -8124,7 +8124,7 @@
return func; return func;
} }
if (isFunc || func == null) { if (isFunc || func == null) {
return baseCreateCallback(func, thisArg, argCount); return baseCallback(func, thisArg, argCount);
} }
// handle "_.pluck" and "_.where" style callback shorthands // handle "_.pluck" and "_.where" style callback shorthands
return type == 'object' ? matches(func) : property(func); return type == 'object' ? matches(func) : property(func);
@@ -8577,7 +8577,7 @@
*/ */
function times(n, callback, thisArg) { function times(n, callback, thisArg) {
n = n < 0 ? 0 : n >>> 0; n = n < 0 ? 0 : n >>> 0;
callback = baseCreateCallback(callback, thisArg, 1); callback = baseCallback(callback, thisArg, 1);
var index = -1, var index = -1,
result = Array(n); result = Array(n);
@@ -8618,6 +8618,7 @@
lodash.bind = bind; lodash.bind = bind;
lodash.bindAll = bindAll; lodash.bindAll = bindAll;
lodash.bindKey = bindKey; lodash.bindKey = bindKey;
lodash.callback = callback;
lodash.chain = chain; lodash.chain = chain;
lodash.chunk = chunk; lodash.chunk = chunk;
lodash.compact = compact; lodash.compact = compact;
@@ -8625,7 +8626,6 @@
lodash.constant = constant; lodash.constant = constant;
lodash.countBy = countBy; lodash.countBy = countBy;
lodash.create = create; lodash.create = create;
lodash.createCallback = createCallback;
lodash.curry = curry; lodash.curry = curry;
lodash.debounce = debounce; lodash.debounce = debounce;
lodash.defaults = defaults; lodash.defaults = defaults;
@@ -8695,7 +8695,6 @@
lodash.zipObject = zipObject; lodash.zipObject = zipObject;
// add aliases // add aliases
lodash.callback = createCallback;
lodash.collect = map; lodash.collect = map;
lodash.each = forEach; lodash.each = forEach;
lodash.eachRight = forEachRight; lodash.eachRight = forEachRight;

View File

@@ -3012,8 +3012,8 @@
}); });
test('should perform a shallow flatten when used as a callback for `_.map`', 1, function() { test('should perform a shallow flatten when used as a callback for `_.map`', 1, function() {
var array = [[['a']], [['b']]]; var array = [[[['a']]], [[['b']]]];
deepEqual(_.map(array, _.flatten), [['a'], ['b']]); deepEqual(_.map(array, _.flatten), [[['a']], [['b']]]);
}); });
test('should treat sparse arrays as dense', 4, function() { test('should treat sparse arrays as dense', 4, function() {
@@ -10945,7 +10945,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey); var acceptFalsey = _.difference(allMethods, rejectFalsey);
test('should accept falsey arguments', 189, function() { test('should accept falsey arguments', 188, function() {
var emptyArrays = _.map(falsey, _.constant([])), var emptyArrays = _.map(falsey, _.constant([])),
isExposed = '_' in root, isExposed = '_' in root,
oldDash = root._; oldDash = root._;