diff --git a/src/methods.js b/src/methods.js index b0bf528..065e6ea 100644 --- a/src/methods.js +++ b/src/methods.js @@ -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); diff --git a/test/methods/group.js b/test/methods/group.js index 91ff8eb..255210a 100644 --- a/test/methods/group.js +++ b/test/methods/group.js @@ -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]] } + ] + ); + }); + }); });