-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import chunked from './chunked'; | ||
|
||
describe('chunked', () => { | ||
it.each([ | ||
{ input: [1], size: 0 }, | ||
{ input: [1, 2], size: 3 }, | ||
])('chunks default', ({ input, size }) => { | ||
const result = chunked(input, size); | ||
|
||
expect(result).toEqual([input]); | ||
}); | ||
|
||
it.each([{ size: 0 }, { size: -1 }])('chunks empty input', ({ size }) => { | ||
const result = chunked([], size); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it.each([{ size: -3 }, { size: 0 }])( | ||
'chunks zero or bellow size', | ||
({ size }) => { | ||
const input = [1, 2, 3, 4]; | ||
|
||
const result = chunked(input, size); | ||
|
||
expect(result).toEqual([input]); | ||
} | ||
); | ||
|
||
it('chunks in to equal pieces', () => { | ||
const result = chunked([1, 2, 3, 4], 2); | ||
|
||
expect(result).toEqual([ | ||
[1, 2], | ||
[3, 4], | ||
]); | ||
}); | ||
|
||
it('chunks in to uneven pieces', () => { | ||
const result = chunked([1, 2, 3, 4], 3); | ||
|
||
expect(result).toEqual([[1, 2, 3], [4]]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function chunked<Element>( | ||
array: Element[], | ||
chunkSize: number | ||
): Element[][] { | ||
let buffer: Element[] = []; | ||
const chunks: Element[][] = []; | ||
array.forEach((item, index) => { | ||
buffer.push(item); | ||
if (index % chunkSize !== chunkSize - 1) return; | ||
|
||
chunks.push(buffer); | ||
buffer = []; | ||
}); | ||
|
||
if (buffer.length > 0) { | ||
chunks.push(buffer); | ||
} | ||
|
||
return chunks; | ||
} | ||
|
||
export default chunked; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './compactMap'; | ||
export * from './uniques'; | ||
export * from './zip'; | ||
export * from './chunked'; |