1
0
Fork 0

Merge pull request #86 from algolia/fix/85

Consider the key's lowercase form while grouping
This commit is contained in:
Sylvain Utard 2016-03-24 17:14:18 +01:00
commit 89173e67ba
2 changed files with 23 additions and 0 deletions

View file

@ -35,6 +35,8 @@ let utils = {
},
/*
* Group all objects of a collection by the value of the specified attribute
* If the attribute is a string, use the lowercase form.
*
* eg.
* groupBy([
* {name: 'Tim', category: 'dev'},
@ -69,6 +71,9 @@ let utils = {
throw new Error(`[groupBy]: Object has no key ${property}`);
}
let key = item[property];
if (typeof key === 'string') {
key = key.toLowerCase();
}
if (!newCollection[key]) {
newCollection[key] = [];
}

View file

@ -121,6 +121,24 @@ describe('utils', () => {
]
});
});
it('group by key considering lowercase forms', () => {
// Given
let input = [
{name: 'Tim', category: 'devs'},
{name: 'Vincent', category: 'DeVs'}
];
// When
let actual = utils.groupBy(input, 'category');
// Expect
expect(actual).toEqual({
devs: [
{name: 'Tim', category: 'devs'},
{name: 'Vincent', category: 'DeVs'}
]
});
});
it('throw an error if key does not exist', () => {
// Given
let input = [