Merge pull request #86 from algolia/fix/85
Consider the key's lowercase form while grouping
This commit is contained in:
commit
89173e67ba
2 changed files with 23 additions and 0 deletions
|
|
@ -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] = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = [
|
||||
|
|
|
|||
Loading…
Reference in a new issue