mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Merge pull request #2725 from spenceralger/organizeBy
[mixin] added _.organizeBy
This commit is contained in:
commit
e156a968cc
3 changed files with 90 additions and 0 deletions
|
@ -207,6 +207,46 @@ define(function (require) {
|
|||
|
||||
// place the obj at it's new index
|
||||
objs.splice(targetI, 0, objs.splice(origI, 1)[0]);
|
||||
},
|
||||
|
||||
/**
|
||||
* Like _.groupBy, but allows specifying multiple groups for a
|
||||
* single object.
|
||||
*
|
||||
* _.organizeBy([{ a: [1, 2, 3] }, { b: true, a: [1, 4] }], 'a')
|
||||
* // Object {1: Array[2], 2: Array[1], 3: Array[1], 4: Array[1]}
|
||||
*
|
||||
* _.groupBy([{ a: [1, 2, 3] }, { b: true, a: [1, 4] }], 'a')
|
||||
* // Object {'1,2,3': Array[1], '1,4': Array[1]}
|
||||
*
|
||||
* @param {array} collection - the list of values to organize
|
||||
* @param {Function} callback - either a property name, or a callback.
|
||||
* @return {object}
|
||||
*/
|
||||
organizeBy: function (collection, callback) {
|
||||
var buckets = {};
|
||||
var prop = typeof callback === 'function' ? false : callback;
|
||||
|
||||
function add(key, obj) {
|
||||
if (!buckets[key]) buckets[key] = [];
|
||||
buckets[key].push(obj);
|
||||
}
|
||||
|
||||
_.each(collection, function (obj) {
|
||||
var keys = prop === false ? callback(obj) : obj[prop];
|
||||
|
||||
if (!_.isArray(keys)) {
|
||||
add(keys, obj);
|
||||
return;
|
||||
}
|
||||
|
||||
var length = keys.length;
|
||||
while (length-- > 0) {
|
||||
add(keys[length], obj);
|
||||
}
|
||||
});
|
||||
|
||||
return buckets;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
|
49
test/unit/specs/utils/mixins/_organize_by.js
Normal file
49
test/unit/specs/utils/mixins/_organize_by.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
define(function (require) {
|
||||
return ['_.organize', function () {
|
||||
var _ = require('lodash');
|
||||
|
||||
it('it works', function () {
|
||||
var col = [
|
||||
{
|
||||
name: 'one',
|
||||
roles: ['user', 'admin', 'owner']
|
||||
},
|
||||
{
|
||||
name: 'two',
|
||||
roles: ['user']
|
||||
},
|
||||
{
|
||||
name: 'three',
|
||||
roles: ['user']
|
||||
},
|
||||
{
|
||||
name: 'four',
|
||||
roles: ['user', 'admin']
|
||||
}
|
||||
];
|
||||
|
||||
var resp = _.organizeBy(col, 'roles');
|
||||
expect(resp).to.have.property('user');
|
||||
expect(resp.user).to.have.length(4);
|
||||
|
||||
expect(resp).to.have.property('admin');
|
||||
expect(resp.admin).to.have.length(2);
|
||||
|
||||
expect(resp).to.have.property('owner');
|
||||
expect(resp.owner).to.have.length(1);
|
||||
});
|
||||
|
||||
it('behaves just like groupBy in normal scenarios', function () {
|
||||
var col = [
|
||||
{ name: 'one' },
|
||||
{ name: 'two' },
|
||||
{ name: 'three' },
|
||||
{ name: 'four' }
|
||||
];
|
||||
|
||||
var orgs = _.organizeBy(col, 'name');
|
||||
var groups = _.groupBy(col, 'name');
|
||||
expect(orgs).to.eql(groups);
|
||||
});
|
||||
}];
|
||||
});
|
|
@ -1,6 +1,7 @@
|
|||
define(function (require) {
|
||||
describe('lodash mixins', function () {
|
||||
run(require('specs/utils/mixins/_move'));
|
||||
run(require('specs/utils/mixins/_organize_by'));
|
||||
function run(m) { describe(m[0], m[1]); }
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue