Skip to content

Commit 326462b

Browse files
feat: add array chunk algorithm
1 parent 5c39e87 commit 326462b

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎Data-Structures/Array/Chunk.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @function chunk
3+
* @description Splits an array into smaller arrays of a specified size.
4+
* The last chunk may be smaller if the array doesn't divide evenly.
5+
* @param {Array} arr - The input array to chunk
6+
* @param {number} size - The size of each chunk
7+
* @returns {Array} - An array of chunks
8+
* @see https://en.wikipedia.org/wiki/Chunking_(computing)
9+
* @example chunk([1, 2, 3, 4, 5], 2) => [[1, 2], [3, 4], [5]]
10+
* @example chunk([1, 2, 3], 3) => [[1, 2, 3]]
11+
*/
12+
const chunk = (arr, size) => {
13+
if (!Array.isArray(arr)) {
14+
throw new TypeError('First argument must be an array')
15+
}
16+
if (!Number.isInteger(size) || size < 1) {
17+
throw new TypeError('Size must be a positive integer')
18+
}
19+
const result = []
20+
for (let i = 0; i < arr.length; i += size) {
21+
result.push(arr.slice(i, i + size))
22+
}
23+
return result
24+
}
25+
26+
export { chunk }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { chunk } from '../Chunk.js'
2+
3+
describe('chunk', () => {
4+
test('chunk([1,2,3,4,5], 2) returns [[1,2],[3,4],[5]]', () => {
5+
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]])
6+
})
7+
8+
test('chunk([1,2,3], 3) returns [[1,2,3]]', () => {
9+
expect(chunk([1, 2, 3], 3)).toEqual([[1, 2, 3]])
10+
})
11+
12+
test('chunk([1,2,3,4], 2) returns [[1,2],[3,4]]', () => {
13+
expect(chunk([1, 2, 3, 4], 2)).toEqual([
14+
[1, 2],
15+
[3, 4]
16+
])
17+
})
18+
19+
test('chunk with size 1 returns array of single elements', () => {
20+
expect(chunk([1, 2, 3], 1)).toEqual([[1], [2], [3]])
21+
})
22+
23+
test('chunk empty array returns empty array', () => {
24+
expect(chunk([], 2)).toEqual([])
25+
})
26+
27+
test('chunk with size larger than array returns one chunk', () => {
28+
expect(chunk([1, 2, 3], 10)).toEqual([[1, 2, 3]])
29+
})
30+
31+
test('chunk does not mutate original array', () => {
32+
const arr = [1, 2, 3, 4]
33+
chunk(arr, 2)
34+
expect(arr).toEqual([1, 2, 3, 4])
35+
})
36+
37+
test('chunk works with strings', () => {
38+
expect(chunk(['a', 'b', 'c', 'd'], 2)).toEqual([
39+
['a', 'b'],
40+
['c', 'd']
41+
])
42+
})
43+
44+
test('throws TypeError for non-array input', () => {
45+
expect(() => chunk('123', 2)).toThrow(TypeError)
46+
})
47+
48+
test('throws TypeError for size 0', () => {
49+
expect(() => chunk([1, 2, 3], 0)).toThrow(TypeError)
50+
})
51+
52+
test('throws TypeError for negative size', () => {
53+
expect(() => chunk([1, 2, 3], -1)).toThrow(TypeError)
54+
})
55+
56+
test('throws TypeError for float size', () => {
57+
expect(() => chunk([1, 2, 3], 1.5)).toThrow(TypeError)
58+
})
59+
})

0 commit comments

Comments
 (0)