mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Rename _.indexBy to _.keyBy to avoid confusion with _.sortedIndexBy forms.
This commit is contained in:
68
lodash.js
68
lodash.js
@@ -805,8 +805,8 @@
|
|||||||
* `defer`, `delay`, `difference`, `drop`, `dropRight`, `dropRightWhile`,
|
* `defer`, `delay`, `difference`, `drop`, `dropRight`, `dropRightWhile`,
|
||||||
* `dropWhile`, `extend`, `extendWith`, `fill`, `filter`, `flatten`,
|
* `dropWhile`, `extend`, `extendWith`, `fill`, `filter`, `flatten`,
|
||||||
* `flattenDeep`, `flow`, `flowRight`, `forEach`, `forEachRight`, `forIn`,
|
* `flattenDeep`, `flow`, `flowRight`, `forEach`, `forEachRight`, `forIn`,
|
||||||
* `forInRight`, `forOwn`, `forOwnRight`, `functions`, `groupBy`, `indexBy`,
|
* `forInRight`, `forOwn`, `forOwnRight`, `functions`, `groupBy`, `initial`,
|
||||||
* `initial`, `intersection`, `invert`, `invoke`, `iteratee`, `keys`, `keysIn`,
|
* `intersection`, `invert`, `invoke`, `iteratee`, `keyBy`, `keys`, `keysIn`,
|
||||||
* `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`, `memoize`,
|
* `map`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`, `memoize`,
|
||||||
* `merge`, `mergeWith` `method`, `methodOf`, `mixin`, `modArgs`, `negate`,
|
* `merge`, `mergeWith` `method`, `methodOf`, `mixin`, `modArgs`, `negate`,
|
||||||
* `omit`, `omitBy`, `once`, `pairs`, `partial`, `partialRight`, `partition`,
|
* `omit`, `omitBy`, `once`, `pairs`, `partial`, `partialRight`, `partition`,
|
||||||
@@ -6197,37 +6197,6 @@
|
|||||||
: (!!length && getIndexOf(collection, target, fromIndex) > -1);
|
: (!!length && getIndexOf(collection, target, fromIndex) > -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an object composed of keys generated from the results of running
|
|
||||||
* each element of `collection` through `iteratee`. The corresponding value
|
|
||||||
* of each key is the last element responsible for generating the key. The
|
|
||||||
* iteratee is invoked with one argument: (value).
|
|
||||||
*
|
|
||||||
* @static
|
|
||||||
* @memberOf _
|
|
||||||
* @category Collection
|
|
||||||
* @param {Array|Object} collection The collection to iterate over.
|
|
||||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
|
||||||
* @returns {Object} Returns the composed aggregate object.
|
|
||||||
* @example
|
|
||||||
*
|
|
||||||
* var keyData = [
|
|
||||||
* { 'dir': 'left', 'code': 97 },
|
|
||||||
* { 'dir': 'right', 'code': 100 }
|
|
||||||
* ];
|
|
||||||
*
|
|
||||||
* _.indexBy(keyData, 'dir');
|
|
||||||
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
|
||||||
*
|
|
||||||
* _.indexBy(keyData, function(o) {
|
|
||||||
* return String.fromCharCode(o.code);
|
|
||||||
* });
|
|
||||||
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
|
||||||
*/
|
|
||||||
var indexBy = createAggregator(function(result, value, key) {
|
|
||||||
result[key] = value;
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the method at `path` of each element in `collection`, returning
|
* Invokes the method at `path` of each element in `collection`, returning
|
||||||
* an array of the results of each invoked method. Any additional arguments
|
* an array of the results of each invoked method. Any additional arguments
|
||||||
@@ -6263,6 +6232,37 @@
|
|||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an object composed of keys generated from the results of running
|
||||||
|
* each element of `collection` through `iteratee`. The corresponding value
|
||||||
|
* of each key is the last element responsible for generating the key. The
|
||||||
|
* iteratee is invoked with one argument: (value).
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Collection
|
||||||
|
* @param {Array|Object} collection The collection to iterate over.
|
||||||
|
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
||||||
|
* @returns {Object} Returns the composed aggregate object.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* var keyData = [
|
||||||
|
* { 'dir': 'left', 'code': 97 },
|
||||||
|
* { 'dir': 'right', 'code': 100 }
|
||||||
|
* ];
|
||||||
|
*
|
||||||
|
* _.keyBy(keyData, 'dir');
|
||||||
|
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
||||||
|
*
|
||||||
|
* _.keyBy(keyData, function(o) {
|
||||||
|
* return String.fromCharCode(o.code);
|
||||||
|
* });
|
||||||
|
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
||||||
|
*/
|
||||||
|
var keyBy = createAggregator(function(result, value, key) {
|
||||||
|
result[key] = value;
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an array of values by running each element in `collection` through
|
* Creates an array of values by running each element in `collection` through
|
||||||
* `iteratee`. The iteratee is invoked with three arguments:
|
* `iteratee`. The iteratee is invoked with three arguments:
|
||||||
@@ -11455,12 +11455,12 @@
|
|||||||
lodash.flowRight = flowRight;
|
lodash.flowRight = flowRight;
|
||||||
lodash.functions = functions;
|
lodash.functions = functions;
|
||||||
lodash.groupBy = groupBy;
|
lodash.groupBy = groupBy;
|
||||||
lodash.indexBy = indexBy;
|
|
||||||
lodash.initial = initial;
|
lodash.initial = initial;
|
||||||
lodash.intersection = intersection;
|
lodash.intersection = intersection;
|
||||||
lodash.invert = invert;
|
lodash.invert = invert;
|
||||||
lodash.invoke = invoke;
|
lodash.invoke = invoke;
|
||||||
lodash.iteratee = iteratee;
|
lodash.iteratee = iteratee;
|
||||||
|
lodash.keyBy = keyBy;
|
||||||
lodash.keys = keys;
|
lodash.keys = keys;
|
||||||
lodash.keysIn = keysIn;
|
lodash.keysIn = keysIn;
|
||||||
lodash.map = map;
|
lodash.map = map;
|
||||||
|
|||||||
36
perf/perf.js
36
perf/perf.js
@@ -1117,42 +1117,6 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
suites.push(
|
|
||||||
Benchmark.Suite('`_.indexBy` with `callback` iterating an array')
|
|
||||||
.add(buildName, '\
|
|
||||||
lodash.indexBy(numbers, function(num) { return num >> 1; })'
|
|
||||||
)
|
|
||||||
.add(otherName, '\
|
|
||||||
_.indexBy(numbers, function(num) { return num >> 1; })'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
suites.push(
|
|
||||||
Benchmark.Suite('`_.indexBy` with `property` name iterating an array')
|
|
||||||
.add(buildName, {
|
|
||||||
'fn': 'lodash.indexBy(words, "length")',
|
|
||||||
'teardown': 'function countBy(){}'
|
|
||||||
})
|
|
||||||
.add(otherName, {
|
|
||||||
'fn': '_.indexBy(words, "length")',
|
|
||||||
'teardown': 'function countBy(){}'
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
suites.push(
|
|
||||||
Benchmark.Suite('`_.indexBy` with `callback` iterating an object')
|
|
||||||
.add(buildName, {
|
|
||||||
'fn': 'lodash.indexBy(wordToNumber, function(num) { return num >> 1; })',
|
|
||||||
'teardown': 'function countBy(){}'
|
|
||||||
})
|
|
||||||
.add(otherName, {
|
|
||||||
'fn': '_.indexBy(wordToNumber, function(num) { return num >> 1; })',
|
|
||||||
'teardown': 'function countBy(){}'
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
suites.push(
|
suites.push(
|
||||||
Benchmark.Suite('`_.indexOf`')
|
Benchmark.Suite('`_.indexOf`')
|
||||||
.add(buildName, {
|
.add(buildName, {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
|
|
||||||
return function(_) {
|
return function(_) {
|
||||||
lodash.defaultsDeep(_, { 'templateSettings': lodash.templateSettings });
|
lodash.defaultsDeep(_, { 'templateSettings': lodash.templateSettings });
|
||||||
|
lodash.mixin(_ , { 'indexBy': lodash.keyBy });
|
||||||
lodash.mixin(_, lodash.pick(lodash, lodash.difference(lodash.functions(lodash), lodash.functions(_))));
|
lodash.mixin(_, lodash.pick(lodash, lodash.difference(lodash.functions(lodash), lodash.functions(_))));
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|||||||
144
test/test.js
144
test/test.js
@@ -4635,7 +4635,7 @@
|
|||||||
'forOwn',
|
'forOwn',
|
||||||
'forOwnRight',
|
'forOwnRight',
|
||||||
'groupBy',
|
'groupBy',
|
||||||
'indexBy',
|
'keyBy',
|
||||||
'map',
|
'map',
|
||||||
'mapKeys',
|
'mapKeys',
|
||||||
'mapValues',
|
'mapValues',
|
||||||
@@ -4665,7 +4665,7 @@
|
|||||||
'forEach',
|
'forEach',
|
||||||
'forEachRight',
|
'forEachRight',
|
||||||
'groupBy',
|
'groupBy',
|
||||||
'indexBy',
|
'keyBy',
|
||||||
'map',
|
'map',
|
||||||
'partition',
|
'partition',
|
||||||
'reduce',
|
'reduce',
|
||||||
@@ -5605,74 +5605,6 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.indexBy');
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
test('should use `_.identity` when `iteratee` is nullish', 1, function() {
|
|
||||||
var array = [4, 6, 6],
|
|
||||||
values = [, null, undefined],
|
|
||||||
expected = _.map(values, _.constant({ '4': 4, '6': 6 }));
|
|
||||||
|
|
||||||
var actual = _.map(values, function(value, index) {
|
|
||||||
return index ? _.indexBy(array, value) : _.indexBy(array);
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual, expected);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with a "_.property" style `iteratee`', 1, function() {
|
|
||||||
var actual = _.indexBy(['one', 'two', 'three'], 'length');
|
|
||||||
deepEqual(actual, { '3': 'two', '5': 'three' });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should only add values to own, not inherited, properties', 2, function() {
|
|
||||||
var actual = _.indexBy([4.2, 6.1, 6.4], function(num) {
|
|
||||||
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor';
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual.constructor, 4.2);
|
|
||||||
deepEqual(actual.hasOwnProperty, 6.4);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with a number for `iteratee`', 2, function() {
|
|
||||||
var array = [
|
|
||||||
[1, 'a'],
|
|
||||||
[2, 'a'],
|
|
||||||
[2, 'b']
|
|
||||||
];
|
|
||||||
|
|
||||||
deepEqual(_.indexBy(array, 0), { '1': [1, 'a'], '2': [2, 'b'] });
|
|
||||||
deepEqual(_.indexBy(array, 1), { 'a': [2, 'a'], 'b': [2, 'b'] });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work with an object for `collection`', 1, function() {
|
|
||||||
var actual = _.indexBy({ 'a': 4.2, 'b': 6.1, 'c': 6.4 }, function(num) {
|
|
||||||
return Math.floor(num);
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual, { '4': 4.2, '6': 6.4 });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should work in a lazy chain sequence', 1, function() {
|
|
||||||
if (!isNpm) {
|
|
||||||
var array = _.range(LARGE_ARRAY_SIZE).concat(
|
|
||||||
_.range(Math.floor(LARGE_ARRAY_SIZE / 2), LARGE_ARRAY_SIZE),
|
|
||||||
_.range(Math.floor(LARGE_ARRAY_SIZE / 1.5), LARGE_ARRAY_SIZE)
|
|
||||||
);
|
|
||||||
|
|
||||||
var predicate = function(value) { return value > 2; },
|
|
||||||
actual = _(array).indexBy().map(square).filter(predicate).take().value();
|
|
||||||
|
|
||||||
deepEqual(actual, _.take(_.filter(_.map(_.indexBy(array), square), predicate)));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
skipTest();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}());
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
QUnit.module('lodash.indexOf');
|
QUnit.module('lodash.indexOf');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -8466,10 +8398,10 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.indexBy` should use `_.iteratee` internally', 1, function() {
|
test('`_.keyBy` should use `_.iteratee` internally', 1, function() {
|
||||||
if (!isModularize) {
|
if (!isModularize) {
|
||||||
_.iteratee = getLength;
|
_.iteratee = getLength;
|
||||||
deepEqual(_.indexBy(array), { '3': 'two', '5': 'three' });
|
deepEqual(_.keyBy(array), { '3': 'two', '5': 'three' });
|
||||||
_.iteratee = iteratee;
|
_.iteratee = iteratee;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -8705,6 +8637,74 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.keyBy');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('should use `_.identity` when `iteratee` is nullish', 1, function() {
|
||||||
|
var array = [4, 6, 6],
|
||||||
|
values = [, null, undefined],
|
||||||
|
expected = _.map(values, _.constant({ '4': 4, '6': 6 }));
|
||||||
|
|
||||||
|
var actual = _.map(values, function(value, index) {
|
||||||
|
return index ? _.keyBy(array, value) : _.keyBy(array);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with a "_.property" style `iteratee`', 1, function() {
|
||||||
|
var actual = _.keyBy(['one', 'two', 'three'], 'length');
|
||||||
|
deepEqual(actual, { '3': 'two', '5': 'three' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should only add values to own, not inherited, properties', 2, function() {
|
||||||
|
var actual = _.keyBy([4.2, 6.1, 6.4], function(num) {
|
||||||
|
return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor';
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual.constructor, 4.2);
|
||||||
|
deepEqual(actual.hasOwnProperty, 6.4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with a number for `iteratee`', 2, function() {
|
||||||
|
var array = [
|
||||||
|
[1, 'a'],
|
||||||
|
[2, 'a'],
|
||||||
|
[2, 'b']
|
||||||
|
];
|
||||||
|
|
||||||
|
deepEqual(_.keyBy(array, 0), { '1': [1, 'a'], '2': [2, 'b'] });
|
||||||
|
deepEqual(_.keyBy(array, 1), { 'a': [2, 'a'], 'b': [2, 'b'] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with an object for `collection`', 1, function() {
|
||||||
|
var actual = _.keyBy({ 'a': 4.2, 'b': 6.1, 'c': 6.4 }, function(num) {
|
||||||
|
return Math.floor(num);
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, { '4': 4.2, '6': 6.4 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work in a lazy chain sequence', 1, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var array = _.range(LARGE_ARRAY_SIZE).concat(
|
||||||
|
_.range(Math.floor(LARGE_ARRAY_SIZE / 2), LARGE_ARRAY_SIZE),
|
||||||
|
_.range(Math.floor(LARGE_ARRAY_SIZE / 1.5), LARGE_ARRAY_SIZE)
|
||||||
|
);
|
||||||
|
|
||||||
|
var predicate = function(value) { return value > 2; },
|
||||||
|
actual = _(array).keyBy().map(square).filter(predicate).take().value();
|
||||||
|
|
||||||
|
deepEqual(actual, _.take(_.filter(_.map(_.keyBy(array), square), predicate)));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('keys methods');
|
QUnit.module('keys methods');
|
||||||
|
|
||||||
_.each(['keys', 'keysIn'], function(methodName) {
|
_.each(['keys', 'keysIn'], function(methodName) {
|
||||||
|
|||||||
@@ -185,6 +185,8 @@
|
|||||||
'Lookup falsy iterator'
|
'Lookup falsy iterator'
|
||||||
],
|
],
|
||||||
'partition': [
|
'partition': [
|
||||||
|
'can reference the array index',
|
||||||
|
'Died on test #8',
|
||||||
'partition takes a context argument',
|
'partition takes a context argument',
|
||||||
'function(a){[code]}'
|
'function(a){[code]}'
|
||||||
],
|
],
|
||||||
@@ -317,6 +319,7 @@
|
|||||||
'extendOwn': 'assign',
|
'extendOwn': 'assign',
|
||||||
'findWhere': 'find',
|
'findWhere': 'find',
|
||||||
'include': 'includes',
|
'include': 'includes',
|
||||||
|
'indexBy': 'keyBy',
|
||||||
'inject': 'reduce',
|
'inject': 'reduce',
|
||||||
'mapObject': 'mapValues',
|
'mapObject': 'mapValues',
|
||||||
'matcher': 'matches',
|
'matcher': 'matches',
|
||||||
|
|||||||
Reference in New Issue
Block a user