diff --git a/README.md b/README.md index 0d8ef0c5..bb8b3cce 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,30 @@ Checks if given arguments are all `Arrays`. ``` +#### chunk + +Splits the given array into array of chunks of up to the given length. + +##### Type signature + + +```typescript +(count: number) => (xs: any[]) => any[] +``` + + +##### Examples + + +```javascript +chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']] +``` + +```javascript +chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']] +``` + + #### difference ##### Type signature diff --git a/array/README.md b/array/README.md index 28e53f64..ca751df4 100644 --- a/array/README.md +++ b/array/README.md @@ -41,6 +41,30 @@ Checks if given arguments are all `Arrays`. ``` +# chunk + +Splits the given array into array of chunks of up to the given length. + +## Type signature + + +```typescript +(count: number) => (xs: any[]) => any[] +``` + + +## Examples + + +```javascript +chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']] +``` + +```javascript +chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']] +``` + + # difference ## Type signature diff --git a/array/chunk.js b/array/chunk.js new file mode 100644 index 00000000..8e1e1754 --- /dev/null +++ b/array/chunk.js @@ -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 []; +}; diff --git a/array/chunk.json b/array/chunk.json new file mode 100644 index 00000000..c0502f44 --- /dev/null +++ b/array/chunk.json @@ -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."] +} diff --git a/array/chunk.md b/array/chunk.md new file mode 100644 index 00000000..33a205d0 --- /dev/null +++ b/array/chunk.md @@ -0,0 +1,23 @@ +# chunk + +Splits the given array into array of chunks of up to the given length. + +## Type signature + + +```typescript +(count: number) => (xs: any[]) => any[] +``` + + +## Examples + + +```javascript +chunk(2)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b'], ['c', 'd']] +``` + +```javascript +chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']] +``` + diff --git a/array/chunk.test.ts b/array/chunk.test.ts new file mode 100644 index 00000000..851c43d4 --- /dev/null +++ b/array/chunk.test.ts @@ -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([]); + }); +}); diff --git a/array/chunk.ts b/array/chunk.ts new file mode 100644 index 00000000..8778728e --- /dev/null +++ b/array/chunk.ts @@ -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 []; +}; diff --git a/array/index.js b/array/index.js index e75718ec..94dc08b1 100644 --- a/array/index.js +++ b/array/index.js @@ -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"; @@ -42,6 +43,7 @@ import zipWith from "./zipWith.js"; export { any, are, + chunk, difference, differs, duplicates, @@ -85,6 +87,7 @@ export { export default { any, are, + chunk, difference, differs, duplicates, diff --git a/array/index.ts b/array/index.ts index d1322252..8d1b5e4a 100644 --- a/array/index.ts +++ b/array/index.ts @@ -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"; @@ -42,6 +43,7 @@ import zipWith from "./zipWith"; export { any, are, + chunk, difference, differs, duplicates, @@ -85,6 +87,7 @@ export { export default { any, are, + chunk, difference, differs, duplicates,