From e4705b34bdbe947dbb636a6c51653843269d058d Mon Sep 17 00:00:00 2001 From: Sylvain UTARD Date: Thu, 24 Mar 2016 13:45:06 +0100 Subject: [PATCH] chore(groupBy): consider lowercase forms of keys Fix #85 --- src/lib/utils.js | 5 +++++ test/utils-test.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib/utils.js b/src/lib/utils.js index e4e94d34..c94b889e 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -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] = []; } diff --git a/test/utils-test.js b/test/utils-test.js index 7665c574..cee8b6b8 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -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 = [