Skip to content

Commit

Permalink
Add support for TypedArrays for group() method
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed May 18, 2024
1 parent 80ddb57 commit 3fd533c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ export default Object.freeze({
valueGetter = self;
}

if (!Array.isArray(current)) {
if (!isArray(current)) {
current = [current];
}

for (const item of current) {
const keys = keyGetter(item);

if (Array.isArray(keys)) {
if (isArray(keys)) {
if (keys.length > 0) {
const value = valueGetter(item);

Expand Down
32 changes: 32 additions & 0 deletions test/methods/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,36 @@ describe('group()', () => {
]
);
});

describe('should work with TypedArrays', () => {
it('group TypedArray elements', () => {
assert.deepEqual(
query('group(=>$ % 2 ? "odd" : "even")')(new Uint8Array([1, 2, 3, 4, 5])),
[
{ key: 'odd', value: [1, 3, 5] },
{ key: 'even', value: [2, 4] }
]
);
});

it('group by TypedArray', () => {
const data = [
{ id: 1, tags: new Uint8Array([1, 2]) },
{ id: 2, tags: new Uint8Array([2, 3]) },
{ id: 3, tags: new Uint8Array([3, 4]) },
{ id: 4, tags: new Uint8Array([]) }
];

assert.deepEqual(
query('group(=>tags)')(data),
[
{ key: 1, value: [data[0]] },
{ key: 2, value: [data[0], data[1]] },
{ key: 3, value: [data[1], data[2]] },
{ key: 4, value: [data[2]] },
{ key: undefined, value: [data[3]] }
]
);
});
});
});

0 comments on commit 3fd533c

Please sign in to comment.