Rename _.invoke to _.invokeMap.

This commit is contained in:
John-David Dalton
2015-11-24 14:10:11 -08:00
parent 9e334b5ece
commit 44abe46da4
6 changed files with 73 additions and 54 deletions

View File

@@ -1495,7 +1495,7 @@
* `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, `flip`, `flow`,
* `flowRight`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`,
* `forOwnRight`, `fromPairs`, `functions`, `functionsIn`, `groupBy`, `initial`,
* `intersection`, `intersectionBy`, `intersectionWith`, invert`, `invoke`,
* `intersection`, `intersectionBy`, `intersectionWith`, invert`, `invokeMap`,
* `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`,
* `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, `method`,
* `methodOf`, `mixin`, `modArgs`, `modArgsSet', `negate`, `nthArg`, `omit`,
@@ -1517,20 +1517,20 @@
* `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
* `findLast`, `findLastIndex`, `findLastKey`, `floor`, `get`, `gt`, `gte`,
* `has`, `hasIn`, `head`, `identity`, `includes`, `indexOf`, `inRange`,
* `isArguments`, `isArray`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`,
* `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`, `isError`,
* `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMatch`, `isMatchWith`,
* `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`,
* `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`,
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`,
* `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`,
* `noConflict`, `noop`, `now`, `pad`, `padEnd`, `padStart`, `parseInt`,
* `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`,
* `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
* `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`,
* `startsWith`, `subtract`, `sum`, sumBy`, `template`, `times`, `toLower`,
* `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, toString`, `toUpper`,
* `trim`, `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`,
* `invokePath`, `isArguments`, `isArray`, `isArrayLike`, `isArrayLikeObject`,
* `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`,
* `isError`, `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMatch`,
* `isMatchWith`, `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`,
* `isObject`, `isObjectLike`, `isPlainObject`, `isRegExp`, `isSafeInteger`,
* `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`, `last`,
* `lastIndexOf`, `lowerCase`, `lowerFirst`, `lt`, `lte`, `max`, `maxBy`,
* `mean`, `min`, `minBy`, `noConflict`, `noop`, `now`, `pad`, `padEnd`,
* `padStart`, `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`,
* `result`, `round`, `runInContext`, `sample`, `shift`, `size`, `snakeCase`,
* `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`,
* `startCase`, `startsWith`, `subtract`, `sum`, sumBy`, `template`, `times`,
* `toLower`, `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, toString`,
* `toUpper`, `trim`, `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`,
* `upperCase`, `upperFirst`, `value`, and `words`
*
* @name _
@@ -7485,13 +7485,13 @@
* @returns {Array} Returns the array of results.
* @example
*
* _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
* _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
* // => [[1, 5, 7], [1, 2, 3]]
*
* _.invoke([123, 456], String.prototype.split, '');
* _.invokeMap([123, 456], String.prototype.split, '');
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
var invoke = rest(function(collection, path, args) {
var invokeMap = rest(function(collection, path, args) {
var index = -1,
isFunc = typeof path == 'function',
isProp = isKey(path),
@@ -12925,7 +12925,7 @@
* _.map(objects, _.method('a.b.c'));
* // => [2, 1]
*
* _.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
* _.invokeMap(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
* // => [1, 2]
*/
var method = rest(function(path, args) {
@@ -13725,8 +13725,7 @@
lodash.intersectionBy = intersectionBy;
lodash.intersectionWith = intersectionWith;
lodash.invert = invert;
lodash.invoke = invoke;
lodash.invokePath = invokePath;
lodash.invokeMap = invokeMap;
lodash.iteratee = iteratee;
lodash.keyBy = keyBy;
lodash.keys = keys;
@@ -13866,6 +13865,7 @@
lodash.includes = includes;
lodash.indexOf = indexOf;
lodash.inRange = inRange;
lodash.invokePath = invokePath;
lodash.isArguments = isArguments;
lodash.isArray = isArray;
lodash.isArrayLike = isArrayLike;

View File

@@ -1180,42 +1180,42 @@
/*--------------------------------------------------------------------------*/
suites.push(
Benchmark.Suite('`_.invoke` iterating an array')
Benchmark.Suite('`_.invokeMap` iterating an array')
.add(buildName, '\
lodash.invoke(numbers, "toFixed")'
lodash.invokeMap(numbers, "toFixed")'
)
.add(otherName, '\
_.invoke(numbers, "toFixed")'
_.invokeMap(numbers, "toFixed")'
)
);
suites.push(
Benchmark.Suite('`_.invoke` with arguments iterating an array')
Benchmark.Suite('`_.invokeMap` with arguments iterating an array')
.add(buildName, '\
lodash.invoke(numbers, "toFixed", 1)'
lodash.invokeMap(numbers, "toFixed", 1)'
)
.add(otherName, '\
_.invoke(numbers, "toFixed", 1)'
_.invokeMap(numbers, "toFixed", 1)'
)
);
suites.push(
Benchmark.Suite('`_.invoke` with a function for `path` iterating an array')
Benchmark.Suite('`_.invokeMap` with a function for `path` iterating an array')
.add(buildName, '\
lodash.invoke(numbers, Number.prototype.toFixed, 1)'
lodash.invokeMap(numbers, Number.prototype.toFixed, 1)'
)
.add(otherName, '\
_.invoke(numbers, Number.prototype.toFixed, 1)'
_.invokeMap(numbers, Number.prototype.toFixed, 1)'
)
);
suites.push(
Benchmark.Suite('`_.invoke` iterating an object')
Benchmark.Suite('`_.invokeMap` iterating an object')
.add(buildName, '\
lodash.invoke(object, "toFixed", 1)'
lodash.invokeMap(object, "toFixed", 1)'
)
.add(otherName, '\
_.invoke(object, "toFixed", 1)'
_.invokeMap(object, "toFixed", 1)'
)
);

View File

@@ -28,12 +28,30 @@
QUnit.config.hidepassed = true;
var mixinPrereqs = (function() {
var aliasToReal = {
'indexBy': 'keyBy',
'invoke': 'invokeMap'
};
var keyMap = {
'rest': 'tail'
};
var lodash = _.noConflict();
return function(_) {
lodash.defaultsDeep(_, { 'templateSettings': lodash.templateSettings });
lodash.mixin(_ , { 'indexBy': lodash.keyBy, 'rest': lodash.tail });
lodash.mixin(_, lodash.pick(lodash, lodash.difference(lodash.functions(lodash), lodash.functions(_))));
lodash.forOwn(keyMap, function(realName, otherName) {
_[otherName] = lodash[realName];
_.prototype[otherName] = lodash.prototype[realName];
});
lodash.forOwn(aliasToReal, function(realName, alias) {
_[alias] = _[realName];
_.prototype[alias] = _.prototype[realName];
});
};
}());

View File

@@ -290,7 +290,7 @@ function logThrobber() {
* @returns {Array} Returns the new converted array.
*/
function optionToArray(name, string) {
return _.compact(_.invoke((optionToValue(name, string) || '').split(/, */), 'trim'));
return _.compact(_.invokeMap((optionToValue(name, string) || '').split(/, */), 'trim'));
}
/**
@@ -714,7 +714,7 @@ function Tunnel(properties) {
total = all.length,
tunnel = this;
_.invoke(all, 'on', 'complete', function() {
_.invokeMap(all, 'on', 'complete', function() {
_.pull(active, this);
if (success) {
success = !this.failed;
@@ -726,7 +726,7 @@ function Tunnel(properties) {
tunnel.dequeue();
});
_.invoke(all, 'on', 'restart', function() {
_.invokeMap(all, 'on', 'restart', function() {
if (!_.includes(restarted, this)) {
restarted.push(this);
}
@@ -774,7 +774,7 @@ Tunnel.prototype.restart = function(callback) {
all = jobs.all;
var reset = _.after(all.length, _.bind(this.stop, this, onGenericRestart)),
stop = _.after(active.length, _.partial(_.invoke, all, 'reset', reset));
stop = _.after(active.length, _.partial(_.invokeMap, all, 'reset', reset));
if (_.isEmpty(active)) {
_.defer(stop);
@@ -782,7 +782,7 @@ Tunnel.prototype.restart = function(callback) {
if (_.isEmpty(all)) {
_.defer(reset);
}
_.invoke(active, 'stop', function() {
_.invokeMap(active, 'stop', function() {
_.pull(active, this);
stop();
});
@@ -870,7 +870,7 @@ Tunnel.prototype.stop = function(callback) {
if (_.isEmpty(active)) {
_.defer(stop);
}
_.invoke(active, 'stop', function() {
_.invokeMap(active, 'stop', function() {
_.pull(active, this);
stop();
});

View File

@@ -7063,14 +7063,14 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.invoke');
QUnit.module('lodash.invokeMap');
(function() {
QUnit.test('should invoke a methods on each element of `collection`', function(assert) {
assert.expect(1);
var array = ['a', 'b', 'c'],
actual = _.invoke(array, 'toUpperCase');
actual = _.invokeMap(array, 'toUpperCase');
assert.deepEqual(actual, ['A', 'B', 'C']);
});
@@ -7079,7 +7079,7 @@
assert.expect(1);
var array = [function() { return slice.call(arguments); }],
actual = _.invoke(array, 'call', null, 'a', 'b', 'c');
actual = _.invokeMap(array, 'call', null, 'a', 'b', 'c');
assert.deepEqual(actual, [['a', 'b', 'c']]);
});
@@ -7089,7 +7089,7 @@
var array = ['a', 'b', 'c'];
var actual = _.invoke(array, function(left, right) {
var actual = _.invokeMap(array, function(left, right) {
return left + this.toUpperCase() + right;
}, '(', ')');
@@ -7100,7 +7100,7 @@
assert.expect(1);
var object = { 'a': 1, 'b': 2, 'c': 3 },
actual = _.invoke(object, 'toFixed', 1);
actual = _.invokeMap(object, 'toFixed', 1);
assert.deepEqual(actual, ['1.0', '2.0', '3.0']);
});
@@ -7108,7 +7108,7 @@
QUnit.test('should treat number values for `collection` as empty', function(assert) {
assert.expect(1);
assert.deepEqual(_.invoke(1), []);
assert.deepEqual(_.invokeMap(1), []);
});
QUnit.test('should not error on nullish elements', function(assert) {
@@ -7117,10 +7117,10 @@
var array = ['a', null, undefined, 'd'];
try {
var actual = _.invoke(array, 'toUpperCase');
var actual = _.invokeMap(array, 'toUpperCase');
} catch (e) {}
assert.deepEqual(_.invoke(array, 'toUpperCase'), ['A', undefined, undefined, 'D']);
assert.deepEqual(_.invokeMap(array, 'toUpperCase'), ['A', undefined, undefined, 'D']);
});
QUnit.test('should not error on elements with missing properties', function(assert) {
@@ -7133,7 +7133,7 @@
var expected = lodashStable.times(objects.length - 1, lodashStable.constant(undefined)).concat(1);
try {
var actual = _.invoke(objects, 'a');
var actual = _.invokeMap(objects, 'a');
} catch (e) {}
assert.deepEqual(actual, expected);
@@ -7145,12 +7145,11 @@
var object = { 'a': { 'b': function() { return this.c; }, 'c': 1 } };
lodashStable.each(['a.b', ['a', 'b']], function(path) {
assert.deepEqual(_.invoke([object], path), [1]);
assert.deepEqual(_.invokeMap([object], path), [1]);
});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.invokePath');
@@ -9849,7 +9848,8 @@
assert.expect(1);
if (realm.object) {
var props = _.invoke(typedArrays, 'toLowerCase');
var invoke = lodashStable.invokeMap || lodashStable.invoke,
props = invoke(typedArrays, 'toLowerCase');
var expected = lodashStable.map(props, function(key) {
return key in realm;
@@ -22670,7 +22670,7 @@
'functions',
'initial',
'intersection',
'invoke',
'invokeMap',
'keys',
'map',
'pull',
@@ -22746,7 +22746,7 @@
func = _[methodName];
switch (methodName) {
case 'invoke':
case 'invokeMap':
actual = func(array, 'toFixed');
break;
case 'sample':

View File

@@ -344,6 +344,7 @@
'include': 'includes',
'indexBy': 'keyBy',
'inject': 'reduce',
'invoke': 'invokeMap',
'mapObject': 'mapValues',
'matcher': 'matches',
'methods': 'functions',