Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ Checks if given arguments are all `Arrays`.
```
<!-- prettier-ignore-end -->

#### chunk

Splits the given array into array of chunks of up to the given length.

##### Type signature

<!-- prettier-ignore-start -->
```typescript
(count: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

##### Examples

<!-- prettier-ignore-start -->
```javascript
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
```

```javascript
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
```
<!-- prettier-ignore-end -->

#### difference

##### Type signature
Expand Down
24 changes: 24 additions & 0 deletions array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ Checks if given arguments are all `Arrays`.
```
<!-- prettier-ignore-end -->

# chunk

Splits the given array into array of chunks of up to the given length.

## Type signature

<!-- prettier-ignore-start -->
```typescript
(count: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
```

```javascript
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
```
<!-- prettier-ignore-end -->

# difference

## Type signature
Expand Down
13 changes: 13 additions & 0 deletions array/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import range from "./range.js";

export default count => xs => {
if (count > 0) {
const chunks = Math.ceil(xs.length / count);

return chunks > 0
? range(chunks).map(i => xs.slice(i * count, (i + 1) * count))
: xs;
}

return [];
};
16 changes: 16 additions & 0 deletions array/chunk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "chunk",
"description": "Splits the given array into array of chunks of up to the given length.",
"signature": "(count: number) => (xs: any[]) => any[]",
"examples": [
{
"language": "javascript",
"content": "chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]"
},
{
"language": "javascript",
"content": "chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]"
}
],
"questions": ["TODO: List questions that may this function answer."]
}
23 changes: 23 additions & 0 deletions array/chunk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# chunk

Splits the given array into array of chunks of up to the given length.

## Type signature

<!-- prettier-ignore-start -->
```typescript
(count: number) => (xs: any[]) => any[]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']]
```

```javascript
chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]
```
<!-- prettier-ignore-end -->
31 changes: 31 additions & 0 deletions array/chunk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env jest */
// @ts-ignore ambiguous import
import chunk from "./chunk.ts";

describe("chunk", () => {
it("should return an array of elements split into groups of the given length", () => {
expect(chunk(1)(["a", "b", "c", "d"])).toEqual([
["a"],
["b"],
["c"],
["d"]
]);
expect(chunk(2)(["a", "b", "c", "d"])).toEqual([
["a", "b"],
["c", "d"]
]);
expect(chunk(4)(["a", "b", "c", "d"])).toEqual([["a", "b", "c", "d"]]);
});

it("fills the final chunk with the remaining elements when the given array can't be split evenly", () => {
expect(chunk(3)(["a", "b", "c", "d"])).toEqual([["a", "b", "c"], ["d"]]);
});

it("should return an empty array when the chunk size is equal zero", () => {
expect(chunk(0)(["a", "b", "c", "d"])).toEqual([]);
});

it("should return an empty array when the given array is empty", () => {
expect(chunk(2)([])).toEqual([]);
});
});
13 changes: 13 additions & 0 deletions array/chunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import range from "./range";

export default (count: number) => (xs: any[]) => {
if (count > 0) {
const chunks = Math.ceil(xs.length / count);

return chunks > 0
? range(chunks).map(i => xs.slice(i * count, (i + 1) * count))
: xs;
}

return [];
};
3 changes: 3 additions & 0 deletions array/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import any from "./any.js";
import are from "./are.js";
import chunk from "./chunk.js";
import difference from "./difference.js";
import differs from "./differs.js";
import duplicates from "./duplicates.js";
Expand Down Expand Up @@ -42,6 +43,7 @@ import zipWith from "./zipWith.js";
export {
any,
are,
chunk,
difference,
differs,
duplicates,
Expand Down Expand Up @@ -85,6 +87,7 @@ export {
export default {
any,
are,
chunk,
difference,
differs,
duplicates,
Expand Down
3 changes: 3 additions & 0 deletions array/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import any from "./any";
import are from "./are";
import chunk from "./chunk";
import difference from "./difference";
import differs from "./differs";
import duplicates from "./duplicates";
Expand Down Expand Up @@ -42,6 +43,7 @@ import zipWith from "./zipWith";
export {
any,
are,
chunk,
difference,
differs,
duplicates,
Expand Down Expand Up @@ -85,6 +87,7 @@ export {
export default {
any,
are,
chunk,
difference,
differs,
duplicates,
Expand Down