Skip to content

Commit e78e5b1

Browse files
committed
--update: added Array chunk generation problem
1 parent cf77bef commit e78e5b1

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

src/array-chunk/array-chunk.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const {
2+
arrayChunk, errFirstArgument, errSecondArguemnt, chunkUsingSlice,
3+
} = require('.');
4+
5+
describe('Chunk of Arrays', () => {
6+
describe('Using normal itteration', () => {
7+
it('Should throw an error for invalid `array` argument', () => {
8+
expect(() => arrayChunk({ array: 'hello', size: 3 })).toThrow(errFirstArgument);
9+
});
10+
11+
it('Should throw an error for invalid `size` value', () => {
12+
expect(() => arrayChunk({ array: [1, 2, 3, 4, 5], size: 'A' })).toThrow(errSecondArguemnt);
13+
});
14+
15+
it('Should return 5 chunks of size 2 of array with 10 elements', () => {
16+
expect(
17+
arrayChunk({
18+
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
19+
size: 2,
20+
}).length,
21+
).toEqual(5);
22+
});
23+
24+
it('Should return the given array as chunk if the chunk size >= given array', () => {
25+
const array = [1, 2, 3, 4, 5];
26+
expect(
27+
arrayChunk({
28+
array,
29+
size: 8,
30+
}),
31+
).toEqual([array]);
32+
});
33+
34+
it('Should return [[1,2], [3,4], [5]] for [1, 2, 3, 4, 5] with chunk size of 2', () => {
35+
const array = [1, 2, 3, 4, 5];
36+
const output = [[1, 2], [3, 4], [5]];
37+
38+
expect(
39+
arrayChunk({
40+
array,
41+
size: 2,
42+
}),
43+
).toEqual(output);
44+
});
45+
});
46+
47+
describe('Using Array.slice()', () => {
48+
it('Should throw an error for invalid `array` argument', () => {
49+
expect(() => chunkUsingSlice({ array: 'hello', size: 3 })).toThrow(errFirstArgument);
50+
});
51+
52+
it('Should throw an error for invalid `size` value', () => {
53+
expect(() => chunkUsingSlice({ array: [1, 2, 3, 4, 5], size: 'A' })).toThrow(
54+
errSecondArguemnt,
55+
);
56+
});
57+
58+
it('Should return 5 chunks of size 2 of array with 10 elements', () => {
59+
expect(
60+
chunkUsingSlice({
61+
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
62+
size: 2,
63+
}).length,
64+
).toEqual(5);
65+
});
66+
67+
it('Should return the given array as chunk if the chunk size >= given array', () => {
68+
const array = [1, 2, 3, 4, 5];
69+
expect(
70+
chunkUsingSlice({
71+
array,
72+
size: 8,
73+
}),
74+
).toEqual([array]);
75+
});
76+
77+
it('Should return [[1,2], [3,4], [5]] for [1, 2, 3, 4, 5] with chunk size of 2', () => {
78+
const array = [1, 2, 3, 4, 5];
79+
const output = [[1, 2], [3, 4], [5]];
80+
81+
expect(
82+
chunkUsingSlice({
83+
array,
84+
size: 2,
85+
}),
86+
).toEqual(output);
87+
});
88+
});
89+
});

src/array-chunk/index.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const errFirstArgument = 'Invalid Argument: Expected an array as first argument';
2+
const errSecondArguemnt = 'Invalid Argument: Expected a positive number as second argument';
3+
4+
function validateArguments(array, size) {
5+
if (!Array.isArray(array)) {
6+
throw new Error(errFirstArgument);
7+
}
8+
9+
if (typeof size !== 'number' || size < 0) {
10+
throw new Error(errSecondArguemnt);
11+
}
12+
13+
if (size > array.length) {
14+
return [array];
15+
}
16+
return 0;
17+
}
18+
19+
function arrayChunk({ array, size }) {
20+
validateArguments(array, size);
21+
22+
const result = [];
23+
24+
for (let i = 0; i < array.length; i += 1) {
25+
const lastChunk = result[result.length - 1];
26+
27+
if (!lastChunk || lastChunk.length === size) {
28+
result.push([array[i]]);
29+
} else {
30+
lastChunk.push(array[i]);
31+
}
32+
}
33+
34+
return result;
35+
}
36+
37+
function chunkUsingSlice({ array, size }) {
38+
validateArguments(array, size);
39+
40+
let index = 0;
41+
const result = [];
42+
43+
while (index < array.length) {
44+
result.push(array.slice(index, index + size));
45+
index += size;
46+
}
47+
48+
return result;
49+
}
50+
51+
module.exports = {
52+
errFirstArgument,
53+
errSecondArguemnt,
54+
arrayChunk,
55+
chunkUsingSlice,
56+
};

0 commit comments

Comments
 (0)