Finish group-by util

Move function to util, add tests.
This commit is contained in:
2019-04-27 18:41:26 +01:00
parent 283ec1d48a
commit 185d7c58c2
3 changed files with 71 additions and 24 deletions
+27
View File
@@ -0,0 +1,27 @@
//
// Code from https://github.com/HeroicEric/ember-group-by (MIT licensed)
//
import { A } from '@ember/array';
import { get } from '@ember/object';
import { isPresent } from '@ember/utils';
export default function groupBy (collection, property) {
let groups = A();
let items = collection;
if (items) {
items.forEach(function(item) {
let value = get(item, property);
let group = groups.findBy('value', value);
if (isPresent(group)) {
get(group, 'items').push(item);
} else {
group = { property: property, value: value, items: [item] };
groups.push(group);
}
});
}
return groups;
}