1234567891011121314151617181920212223242526272829 |
- 'use strict';
-
- var nestedValue = require('../helpers/nestedValue');
-
- module.exports = function groupBy(key) {
- var _this = this;
-
- var collection = {};
-
- this.items.forEach(function (item, index) {
- var resolvedKey = void 0;
-
- if (typeof key === 'function') {
- resolvedKey = key(item, index);
- } else if (nestedValue(item, key) || nestedValue(item, key) === 0) {
- resolvedKey = nestedValue(item, key);
- } else {
- resolvedKey = '';
- }
-
- if (collection[resolvedKey] === undefined) {
- collection[resolvedKey] = new _this.constructor([]);
- }
-
- collection[resolvedKey].push(item);
- });
-
- return new this.constructor(collection);
- };
|