From 80ddb5746860c5615c84670659ee03aa93e68bc9 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Sat, 18 May 2024 20:30:04 +0200 Subject: [PATCH] Fixed group() method to not omit elements with an empty array as key --- CHANGELOG.md | 3 ++- src/methods.js | 10 ++++++++-- test/methods/group.js | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee0470d..c257952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ - Added support for TypedArray in bracket notation, filtering, mapping, matching, spread operator, +/- operators - Changed `is array` assertion to return `true` for TypedArray values -- Fixed `lastIndexOf()` behavior when `fromIndex` is zero; previously, the search incorrectly started from the end of an array instead of the zero position +- Fixed `lastIndexOf()` method behavior when `fromIndex` is zero; previously, the search incorrectly started from the end of an array instead of the zero position +- Fixed `group()` method to include elements with an empty array as a key in the group with `key: undefined`; previously, such elements were omitted from the result - Modified the behavior of slice notation: - When the value is a string, the result remains a string (was an array of chars) - When `from` is greater than `to` and `step` is not specified, `step` defaults to `-1` (i.e. `$[3:1]` is the same as `$[1:3:-1]`) diff --git a/src/methods.js b/src/methods.js index 26adc16..b0bf528 100644 --- a/src/methods.js +++ b/src/methods.js @@ -161,8 +161,14 @@ export default Object.freeze({ const keys = keyGetter(item); if (Array.isArray(keys)) { - for (const key of keys) { - addToMapSet(map, key, valueGetter(item)); + if (keys.length > 0) { + const value = valueGetter(item); + + for (const key of keys) { + addToMapSet(map, key, value); + } + } else { + addToMapSet(map, undefined, valueGetter(item)); } } else { addToMapSet(map, keys, valueGetter(item)); diff --git a/test/methods/group.js b/test/methods/group.js index e81ef0f..91ff8eb 100644 --- a/test/methods/group.js +++ b/test/methods/group.js @@ -42,13 +42,39 @@ describe('group()', () => { }); it('should group by an element when key value is an array', () => { + const data = [ + { id: 1, tags: ['foo', 'bar'] }, + { id: 2, tags: ['baz'] }, + { id: 3, tags: ['bar', 'baz'] }, + { id: 4, tags: ['bar'] } + ]; + assert.deepEqual( - query('.group(=>refs.type)')(data), - ['svg', 'css', 'js'] - .map(type => ({ - key: type, - value: data.filter(item => item.refs.find(ref => ref.type === type)) - })) + query('group(=>tags)')(data), + [ + { key: 'foo', value: [data[0]] }, + { key: 'bar', value: [data[0], data[2], data[3]] }, + { key: 'baz', value: [data[1], data[2]] } + ] + ); + }); + + it('should not loose elements with empty array', () => { + const data = [ + { id: 1, tags: ['foo', 'bar'] }, + { id: 2, tags: ['baz'] }, + { id: 3, tags: ['bar', 'baz'] }, + { id: 4, tags: [] } + ]; + + assert.deepEqual( + query('group(=>tags)')(data), + [ + { key: 'foo', value: [data[0]] }, + { key: 'bar', value: [data[0], data[2]] }, + { key: 'baz', value: [data[1], data[2]] }, + { key: undefined, value: [data[3]] } + ] ); }); });