Skip to content

Commit

Permalink
Fixed group() method to not omit elements with an empty array as key
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed May 18, 2024
1 parent c7afead commit 80ddb57
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`)
Expand Down
10 changes: 8 additions & 2 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
38 changes: 32 additions & 6 deletions test/methods/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]] }
]
);
});
});

0 comments on commit 80ddb57

Please sign in to comment.