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.

23 lines
519 B

4 years ago
  1. 'use strict';
  2. var nestedValue = require('../helpers/nestedValue');
  3. var _require = require('../helpers/is'),
  4. isFunction = _require.isFunction;
  5. module.exports = function keyBy(key) {
  6. var collection = {};
  7. if (isFunction(key)) {
  8. this.items.forEach(function (item) {
  9. collection[key(item)] = item;
  10. });
  11. } else {
  12. this.items.forEach(function (item) {
  13. var keyValue = nestedValue(item, key);
  14. collection[keyValue || ''] = item;
  15. });
  16. }
  17. return new this.constructor(collection);
  18. };