Skip to content

Commit 74b3a33

Browse files
kborucinskikrystianborucinski
authored andcommitted
Add array/chunk method (#42)
* Add chunk method * Add description * Update after code review * Update after code review Co-authored-by: krystianborucinski <krystian.borucinski@sandstream.pl>
1 parent 0a56a3b commit 74b3a33

File tree

9 files changed

+150
-0
lines changed

9 files changed

+150
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ Checks if given arguments are all `Arrays`.
5858
```
5959
<!-- prettier-ignore-end -->
6060

61+
#### chunk
62+
63+
Splits the given array into array of chunks of up to the given length.
64+
65+
##### Type signature
66+
67+
<!-- prettier-ignore-start -->
68+
```typescript
69+
(count: number) => (xs: any[]) => any[]
70+
```
71+
<!-- prettier-ignore-end -->
72+
73+
##### Examples
74+
75+
<!-- prettier-ignore-start -->
76+
```javascript
77+
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
78+
```
79+
80+
```javascript
81+
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
82+
```
83+
<!-- prettier-ignore-end -->
84+
6185
#### difference
6286

6387
##### Type signature

array/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ Checks if given arguments are all `Arrays`.
4141
```
4242
<!-- prettier-ignore-end -->
4343

44+
# chunk
45+
46+
Splits the given array into array of chunks of up to the given length.
47+
48+
## Type signature
49+
50+
<!-- prettier-ignore-start -->
51+
```typescript
52+
(count: number) => (xs: any[]) => any[]
53+
```
54+
<!-- prettier-ignore-end -->
55+
56+
## Examples
57+
58+
<!-- prettier-ignore-start -->
59+
```javascript
60+
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
61+
```
62+
63+
```javascript
64+
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
65+
```
66+
<!-- prettier-ignore-end -->
67+
4468
# difference
4569

4670
## Type signature

array/chunk.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import range from "./range.js";
2+
3+
export default count => xs => {
4+
if (count > 0) {
5+
const chunks = Math.ceil(xs.length / count);
6+
7+
return chunks > 0
8+
? range(chunks).map(i => xs.slice(i * count, (i + 1) * count))
9+
: xs;
10+
}
11+
12+
return [];
13+
};

array/chunk.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "chunk",
3+
"description": "Splits the given array into array of chunks of up to the given length.",
4+
"signature": "(count: number) => (xs: any[]) => any[]",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]"
9+
},
10+
{
11+
"language": "javascript",
12+
"content": "chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]"
13+
}
14+
],
15+
"questions": ["TODO: List questions that may this function answer."]
16+
}

array/chunk.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# chunk
2+
3+
Splits the given array into array of chunks of up to the given length.
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(count: number) => (xs: any[]) => any[]
10+
```
11+
<!-- prettier-ignore-end -->
12+
13+
## Examples
14+
15+
<!-- prettier-ignore-start -->
16+
```javascript
17+
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
18+
```
19+
20+
```javascript
21+
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
22+
```
23+
<!-- prettier-ignore-end -->

array/chunk.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import chunk from "./chunk.ts";
4+
5+
describe("chunk", () => {
6+
it("should return an array of elements split into groups of the given length", () => {
7+
expect(chunk(1)(["a", "b", "c", "d"])).toEqual([
8+
["a"],
9+
["b"],
10+
["c"],
11+
["d"]
12+
]);
13+
expect(chunk(2)(["a", "b", "c", "d"])).toEqual([
14+
["a", "b"],
15+
["c", "d"]
16+
]);
17+
expect(chunk(4)(["a", "b", "c", "d"])).toEqual([["a", "b", "c", "d"]]);
18+
});
19+
20+
it("fills the final chunk with the remaining elements when the given array can't be split evenly", () => {
21+
expect(chunk(3)(["a", "b", "c", "d"])).toEqual([["a", "b", "c"], ["d"]]);
22+
});
23+
24+
it("should return an empty array when the chunk size is equal zero", () => {
25+
expect(chunk(0)(["a", "b", "c", "d"])).toEqual([]);
26+
});
27+
28+
it("should return an empty array when the given array is empty", () => {
29+
expect(chunk(2)([])).toEqual([]);
30+
});
31+
});

array/chunk.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import range from "./range";
2+
3+
export default (count: number) => (xs: any[]) => {
4+
if (count > 0) {
5+
const chunks = Math.ceil(xs.length / count);
6+
7+
return chunks > 0
8+
? range(chunks).map(i => xs.slice(i * count, (i + 1) * count))
9+
: xs;
10+
}
11+
12+
return [];
13+
};

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import any from "./any.js";
22
import are from "./are.js";
3+
import chunk from "./chunk.js";
34
import difference from "./difference.js";
45
import differs from "./differs.js";
56
import duplicates from "./duplicates.js";
@@ -45,6 +46,7 @@ import zipWith from "./zipWith.js";
4546
export {
4647
any,
4748
are,
49+
chunk,
4850
difference,
4951
differs,
5052
duplicates,
@@ -91,6 +93,7 @@ export {
9193
export default {
9294
any,
9395
are,
96+
chunk,
9497
difference,
9598
differs,
9699
duplicates,

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import any from "./any";
22
import are from "./are";
3+
import chunk from "./chunk";
34
import difference from "./difference";
45
import differs from "./differs";
56
import duplicates from "./duplicates";
@@ -45,6 +46,7 @@ import zipWith from "./zipWith";
4546
export {
4647
any,
4748
are,
49+
chunk,
4850
difference,
4951
differs,
5052
duplicates,
@@ -91,6 +93,7 @@ export {
9193
export default {
9294
any,
9395
are,
96+
chunk,
9497
difference,
9598
differs,
9699
duplicates,

0 commit comments

Comments
 (0)