From 32d4f9295b015c94091638f19587f474568ba617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Tue, 7 Jan 2020 11:53:33 +0100 Subject: [PATCH 1/4] Add chunk method --- README.md | 22 ++++++++++++++++++++++ array/README.md | 22 ++++++++++++++++++++++ array/chunk.js | 6 ++++++ array/chunk.json | 16 ++++++++++++++++ array/chunk.md | 21 +++++++++++++++++++++ array/chunk.test.ts | 31 +++++++++++++++++++++++++++++++ array/chunk.ts | 6 ++++++ array/index.js | 3 +++ array/index.ts | 3 +++ 9 files changed, 130 insertions(+) create mode 100644 array/chunk.js create mode 100644 array/chunk.json create mode 100644 array/chunk.md create mode 100644 array/chunk.test.ts create mode 100644 array/chunk.ts diff --git a/README.md b/README.md index 0d8ef0c5..2fb968ab 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,28 @@ Checks if given arguments are all `Arrays`. ``` +#### chunk + +##### Type signature + + +```typescript +(size: 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..73058615 100644 --- a/array/README.md +++ b/array/README.md @@ -41,6 +41,28 @@ Checks if given arguments are all `Arrays`. ``` +# chunk + +## Type signature + + +```typescript +(size: 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..da4370e3 --- /dev/null +++ b/array/chunk.js @@ -0,0 +1,6 @@ +export default size => xs => + xs.reduce( + (acc, _, index) => + index % size ? acc : [...acc, xs.slice(index, index + size)], + [] + ); diff --git a/array/chunk.json b/array/chunk.json new file mode 100644 index 00000000..f07e1df3 --- /dev/null +++ b/array/chunk.json @@ -0,0 +1,16 @@ +{ + "name": "chunk", + "description": "TODO: Fill short description here.", + "signature": "(size: 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 answers."] +} diff --git a/array/chunk.md b/array/chunk.md new file mode 100644 index 00000000..8481fe86 --- /dev/null +++ b/array/chunk.md @@ -0,0 +1,21 @@ +# chunk + +## Type signature + + +```typescript +(size: 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..bb20a848 --- /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("returns a list of tuples if chunk size is equal 2 and number of elements in the array is even", () => { + expect(chunk(2)(["a", "b", "c", "d"])).toEqual([ + ["a", "b"], + ["c", "d"] + ]); + }); + + it("returns a list of n-element arrays if number of elements in the array is not a multiple of chunk size then the last array contains remaining elements from source array", () => { + expect(chunk(3)(["a", "b", "c", "d"])).toEqual([["a", "b", "c"], ["d"]]); + expect(chunk(4)(["a", "b", "c", "d"])).toEqual([["a", "b", "c", "d"]]); + expect(chunk(1)(["a", "b", "c", "d"])).toEqual([ + ["a"], + ["b"], + ["c"], + ["d"] + ]); + }); + + it("returns a list with empty arrays if chunk size is equal zero", () => { + expect(chunk(0)(["a", "b", "c", "d"])).toEqual([[], [], [], []]); + }); + + it("returns a empty list if source array is empty", () => { + expect(chunk(2)([])).toEqual([]); + }); +}); diff --git a/array/chunk.ts b/array/chunk.ts new file mode 100644 index 00000000..32a46cc8 --- /dev/null +++ b/array/chunk.ts @@ -0,0 +1,6 @@ +export default (size: number) => (xs: any[]) => + xs.reduce( + (acc, _, index) => + index % size ? acc : [...acc, xs.slice(index, index + size)], + [] + ); 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, From 610d86cb4ed6fd6bc76aa846328324318b488255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Tue, 7 Jan 2020 12:09:29 +0100 Subject: [PATCH 2/4] Add description --- README.md | 2 ++ array/README.md | 2 ++ array/chunk.json | 2 +- array/chunk.md | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fb968ab..9bdf77a5 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Checks if given arguments are all `Arrays`. #### chunk +Returns the new array of chunks. + ##### Type signature diff --git a/array/README.md b/array/README.md index 73058615..24770f26 100644 --- a/array/README.md +++ b/array/README.md @@ -43,6 +43,8 @@ Checks if given arguments are all `Arrays`. # chunk +Returns the new array of chunks. + ## Type signature diff --git a/array/chunk.json b/array/chunk.json index f07e1df3..05ee4e38 100644 --- a/array/chunk.json +++ b/array/chunk.json @@ -1,6 +1,6 @@ { "name": "chunk", - "description": "TODO: Fill short description here.", + "description": "Returns the new array of chunks.", "signature": "(size: number) => (xs: any[]) => any", "examples": [ { diff --git a/array/chunk.md b/array/chunk.md index 8481fe86..e509e5b8 100644 --- a/array/chunk.md +++ b/array/chunk.md @@ -1,5 +1,7 @@ # chunk +Returns the new array of chunks. + ## Type signature From 5621a723e3f07d30f0acfac36d157dfa0960001d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 15:18:13 +0100 Subject: [PATCH 3/4] Update after code review --- README.md | 4 ++-- array/README.md | 4 ++-- array/chunk.js | 19 +++++++++++++------ array/chunk.json | 6 +++--- array/chunk.md | 4 ++-- array/chunk.test.ts | 24 ++++++++++++------------ array/chunk.ts | 19 +++++++++++++------ 7 files changed, 47 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 9bdf77a5..1f12f826 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,13 @@ Checks if given arguments are all `Arrays`. #### chunk -Returns the new array of chunks. +Creates an array of elements split into groups the length of size. ##### Type signature ```typescript -(size: number) => (xs: any[]) => any +(count: number) => (xs: any[]) => any[] ``` diff --git a/array/README.md b/array/README.md index 24770f26..11d39e52 100644 --- a/array/README.md +++ b/array/README.md @@ -43,13 +43,13 @@ Checks if given arguments are all `Arrays`. # chunk -Returns the new array of chunks. +Creates an array of elements split into groups the length of size. ## Type signature ```typescript -(size: number) => (xs: any[]) => any +(count: number) => (xs: any[]) => any[] ``` diff --git a/array/chunk.js b/array/chunk.js index da4370e3..8e1e1754 100644 --- a/array/chunk.js +++ b/array/chunk.js @@ -1,6 +1,13 @@ -export default size => xs => - xs.reduce( - (acc, _, index) => - index % size ? acc : [...acc, xs.slice(index, index + size)], - [] - ); +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 index 05ee4e38..e8d0db0e 100644 --- a/array/chunk.json +++ b/array/chunk.json @@ -1,7 +1,7 @@ { "name": "chunk", - "description": "Returns the new array of chunks.", - "signature": "(size: number) => (xs: any[]) => any", + "description": "Creates an array of elements split into groups the length of size.", + "signature": "(count: number) => (xs: any[]) => any[]", "examples": [ { "language": "javascript", @@ -12,5 +12,5 @@ "content": "chunk(3)(['a', 'b', 'c', 'd']); // ⇒ [['a', 'b', 'c'], ['d']]" } ], - "questions": ["TODO: List questions that may this function answers."] + "questions": ["TODO: List questions that may this function answer."] } diff --git a/array/chunk.md b/array/chunk.md index e509e5b8..f5a55d82 100644 --- a/array/chunk.md +++ b/array/chunk.md @@ -1,12 +1,12 @@ # chunk -Returns the new array of chunks. +Creates an array of elements split into groups the length of size. ## Type signature ```typescript -(size: number) => (xs: any[]) => any +(count: number) => (xs: any[]) => any[] ``` diff --git a/array/chunk.test.ts b/array/chunk.test.ts index bb20a848..9c7c0e0a 100644 --- a/array/chunk.test.ts +++ b/array/chunk.test.ts @@ -3,29 +3,29 @@ import chunk from "./chunk.ts"; describe("chunk", () => { - it("returns a list of tuples if chunk size is equal 2 and number of elements in the array is even", () => { + it("should return an array of elements split into groups the length of size", () => { + 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("returns a list of n-element arrays if number of elements in the array is not a multiple of chunk size then the last array contains remaining elements from source array", () => { + it("should return an array of triples when array can't be split evenly, the final chunk will be the remaining elements", () => { expect(chunk(3)(["a", "b", "c", "d"])).toEqual([["a", "b", "c"], ["d"]]); - expect(chunk(4)(["a", "b", "c", "d"])).toEqual([["a", "b", "c", "d"]]); - expect(chunk(1)(["a", "b", "c", "d"])).toEqual([ - ["a"], - ["b"], - ["c"], - ["d"] - ]); }); - it("returns a list with empty arrays if chunk size is equal zero", () => { - expect(chunk(0)(["a", "b", "c", "d"])).toEqual([[], [], [], []]); + it("should return an empty array when the chunk size is equal zero", () => { + expect(chunk(0)(["a", "b", "c", "d"])).toEqual([]); }); - it("returns a empty list if source array is empty", () => { + 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 index 32a46cc8..8778728e 100644 --- a/array/chunk.ts +++ b/array/chunk.ts @@ -1,6 +1,13 @@ -export default (size: number) => (xs: any[]) => - xs.reduce( - (acc, _, index) => - index % size ? acc : [...acc, xs.slice(index, index + size)], - [] - ); +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 []; +}; From 38cc18399184a390849e47a25c8b7d1368f6443e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Boruci=C5=84ski?= Date: Thu, 9 Jan 2020 17:09:57 +0100 Subject: [PATCH 4/4] Update after code review --- README.md | 2 +- array/README.md | 2 +- array/chunk.json | 2 +- array/chunk.md | 2 +- array/chunk.test.ts | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1f12f826..bb8b3cce 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Checks if given arguments are all `Arrays`. #### chunk -Creates an array of elements split into groups the length of size. +Splits the given array into array of chunks of up to the given length. ##### Type signature diff --git a/array/README.md b/array/README.md index 11d39e52..ca751df4 100644 --- a/array/README.md +++ b/array/README.md @@ -43,7 +43,7 @@ Checks if given arguments are all `Arrays`. # chunk -Creates an array of elements split into groups the length of size. +Splits the given array into array of chunks of up to the given length. ## Type signature diff --git a/array/chunk.json b/array/chunk.json index e8d0db0e..c0502f44 100644 --- a/array/chunk.json +++ b/array/chunk.json @@ -1,6 +1,6 @@ { "name": "chunk", - "description": "Creates an array of elements split into groups the length of size.", + "description": "Splits the given array into array of chunks of up to the given length.", "signature": "(count: number) => (xs: any[]) => any[]", "examples": [ { diff --git a/array/chunk.md b/array/chunk.md index f5a55d82..33a205d0 100644 --- a/array/chunk.md +++ b/array/chunk.md @@ -1,6 +1,6 @@ # chunk -Creates an array of elements split into groups the length of size. +Splits the given array into array of chunks of up to the given length. ## Type signature diff --git a/array/chunk.test.ts b/array/chunk.test.ts index 9c7c0e0a..851c43d4 100644 --- a/array/chunk.test.ts +++ b/array/chunk.test.ts @@ -3,7 +3,7 @@ import chunk from "./chunk.ts"; describe("chunk", () => { - it("should return an array of elements split into groups the length of size", () => { + it("should return an array of elements split into groups of the given length", () => { expect(chunk(1)(["a", "b", "c", "d"])).toEqual([ ["a"], ["b"], @@ -17,7 +17,7 @@ describe("chunk", () => { expect(chunk(4)(["a", "b", "c", "d"])).toEqual([["a", "b", "c", "d"]]); }); - it("should return an array of triples when array can't be split evenly, the final chunk will be the remaining elements", () => { + 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"]]); });