You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
519 B
24 lines
519 B
'use strict';
|
|
|
|
var nestedValue = require('../helpers/nestedValue');
|
|
|
|
var _require = require('../helpers/is'),
|
|
isFunction = _require.isFunction;
|
|
|
|
module.exports = function keyBy(key) {
|
|
var collection = {};
|
|
|
|
if (isFunction(key)) {
|
|
this.items.forEach(function (item) {
|
|
collection[key(item)] = item;
|
|
});
|
|
} else {
|
|
this.items.forEach(function (item) {
|
|
var keyValue = nestedValue(item, key);
|
|
|
|
collection[keyValue || ''] = item;
|
|
});
|
|
}
|
|
|
|
return new this.constructor(collection);
|
|
}; |