-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor chunk function to handle edge cases
- Loading branch information
Showing
2 changed files
with
48 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,45 @@ | ||
import { chunk } from './util'; | ||
|
||
describe('chunk', () => { | ||
it('should split an array into chunks of the specified size', () => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
const size = 3; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); | ||
}); | ||
|
||
it('should handle arrays that do not divide evenly by the chunk size', () => { | ||
const arr = [1, 2, 3, 4, 5, 6, 7]; | ||
const size = 3; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([[1, 2, 3], [4, 5, 6], [7]]); | ||
}); | ||
|
||
it('should return an empty array when given an empty array', () => { | ||
const arr: number[] = []; | ||
const size = 3; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should handle chunk sizes larger than the array length', () => { | ||
const arr = [1, 2, 3]; | ||
const size = 5; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([[1, 2, 3]]); | ||
}); | ||
|
||
it('should handle chunk size of 1', () => { | ||
const arr = [1, 2, 3]; | ||
const size = 1; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([[1], [2], [3]]); | ||
}); | ||
|
||
it('should handle chunk size of 0', () => { | ||
const arr = [1, 2, 3]; | ||
const size = 0; | ||
const result = chunk(arr, size); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
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