-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: create utils function to chunk array * fix: do not parallel sync for externalResources pb of heavy resources * bump: 2.3.3-rc.0 * chore: type callBack functions * refactor: typing and error handling on chunk function * test: add tests on chunk function --------- Co-authored-by: Quentin Ruhier <[email protected]>
- Loading branch information
1 parent
68eb561
commit bc22110
Showing
9 changed files
with
202 additions
and
26 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
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,50 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { chunk } from './array' | ||
|
||
describe('chunk function', () => { | ||
it('splits an array into chunks of the specified size', () => { | ||
const array = [1, 2, 3, 4, 5] | ||
const result = chunk(array, 2) | ||
expect(result).toEqual([[1, 2], [3, 4], [5]]) | ||
}) | ||
|
||
it('returns an empty array if input array is empty', () => { | ||
const result = chunk([], 3) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it('throws an error if chunkSize is not strictly positive', () => { | ||
const array = [1, 2, 3] | ||
expect(() => chunk(array, 0)).toThrow( | ||
'chunkSize must be a strictly positive integer', | ||
) | ||
expect(() => chunk(array, -2)).toThrow( | ||
'chunkSize must be a strictly positive integer', | ||
) | ||
expect(() => chunk(array, 0.5)).toThrow( | ||
'chunkSize must be a strictly positive integer', | ||
) | ||
}) | ||
|
||
it('handles chunkSize larger than the array length', () => { | ||
const array = [1, 2, 3] | ||
const result = chunk(array, 10) | ||
expect(result).toEqual([[1, 2, 3]]) | ||
}) | ||
|
||
it('works with arrays of objects', () => { | ||
const array = [{ id: 1 }, { id: 2 }, { id: 3 }] | ||
const result = chunk(array, 2) | ||
expect(result).toEqual([[{ id: 1 }, { id: 2 }], [{ id: 3 }]]) | ||
}) | ||
|
||
it('works with mixed-type arrays', () => { | ||
const array = [1, 'a', true, null] | ||
const result = chunk(array, 2) | ||
expect(result).toEqual([ | ||
[1, 'a'], | ||
[true, null], | ||
]) | ||
}) | ||
}) |
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,12 @@ | ||
export const chunk = <T>(array: T[], chunkSize: number) => { | ||
if (chunkSize <= 0 || !Number.isInteger(chunkSize)) { | ||
throw new Error('chunkSize must be a strictly positive integer') | ||
} | ||
|
||
const res: T[][] = [] | ||
for (let i = 0; i < array.length; i += chunkSize) { | ||
const chunk = array.slice(i, i + chunkSize) | ||
res.push(chunk) | ||
} | ||
return res | ||
} |
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
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
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
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
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
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