diff --git a/src/chunk/__snapshots__/chunk.spec.ts.snap b/src/chunk/__snapshots__/chunk.spec.ts.snap new file mode 100644 index 000000000..a6a6c96cb --- /dev/null +++ b/src/chunk/__snapshots__/chunk.spec.ts.snap @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`chunk function is a pure function 1`] = ` +Array [ + 1, + 2, + 3, + 4, + 5, +] +`; diff --git a/src/chunk/chunk.spec.ts b/src/chunk/chunk.spec.ts new file mode 100644 index 000000000..898bd6ce0 --- /dev/null +++ b/src/chunk/chunk.spec.ts @@ -0,0 +1,34 @@ +import { chunk } from './chunk'; + +describe('chunk function', () => { + const input = [1, 2, 3, 4, 5]; + + test('is a pure function', () => { + chunk(2)(input); + expect(input).toMatchSnapshot(); + }); + + test('[1, 2, 3, 4] |> chunk(2) === [[1, 2], [3, 4]]', () => { + expect(chunk(2)([1, 2, 3, 4])).toEqual([[1, 2], [3, 4]]); + }); + + test('[1, 2, 3, 4, 5] |> chunk(2) === [[1, 2], [3, 4], [5]]', () => { + expect(chunk(2)([1, 2, 3, 4, 5])).toEqual([[1, 2], [3, 4], [5]]); + }); + + test('[1, 2, 3, 4] |> chunk() === [[1], [2], [3], [4]]', () => { + expect(chunk()([1, 2, 3, 4])).toEqual([[1], [2], [3], [4]]); + }); + + test('[1, 2, 3, 4] |> chunk(5) === [[1, 2, 3, 4]]', () => { + expect(chunk(5)([1, 2, 3, 4])).toEqual([[1, 2, 3, 4]]); + }); + + test('[1, 2, 3, 4] |> chunk(0) === []', () => { + expect(chunk(0)([1, 2, 3, 4])).toEqual([]); + }); + + test('[1, 2, 3, 4] |> chunk(-1) === []', () => { + expect(chunk(-1)([1, 2, 3, 4])).toEqual([]); + }); +}); diff --git a/src/chunk/chunk.ts b/src/chunk/chunk.ts new file mode 100644 index 000000000..9d9418667 --- /dev/null +++ b/src/chunk/chunk.ts @@ -0,0 +1,61 @@ +/** + * @module array=>array + */ +/** + * This method chunk an array into multiple array of the same size, + * except the last one + * @param size The number elements by chunk. + * @return the function to apply on the TBD to do something + * @example + * ``` + * chunk(2)([1, 2, 3, 4]) // [[1, 2], [3, 4]] + * chunk(2)([1, 2, 3, 4, 5]) // [[1, 2], [3, 4], [5]] + * ``` + * @example Using the chain + * ``` + * chain([1, 2, 3, 4]) + * .chain(chunk(2)) + * .value() // [[1, 2], [3, 4]] + * ``` + * @example default size is 1 + * ``` + * chunk()([1, 2, 3, 4]) // [[1], [2], [3], [4]] + * ``` + * @example when size is superior to input length + * ``` + * chunk(5)([1, 2, 3, 4]) // [[1, 2, 3, 4]] + * ``` + * @example when size is 0 + * ``` + * chunk(0)([1, 2, 3, 4]) // [] + * ``` + * @example when size is less than 0 + * ``` + * chunk(-1)([1, 2, 3, 4]) // [] + * ``` + */ +export function chunk( + size = 1 +): (element: ReadonlyArray) => ReadonlyArray> { + if (size <= 0) { + return () => []; + } + + return (array: ReadonlyArray): ReadonlyArray> => { + const result: T[][] = []; + let counter = 0; + let accu: T[] = []; + for (let i = 0; i < array.length; i++) { + const element = array[i]; + if (i !== 0 && 0 === counter % size) { + result.push(accu); + accu = []; + } + accu.push(element); + counter++; + } + result.push(accu); + + return result; + }; +} diff --git a/src/chunk/index.ts b/src/chunk/index.ts new file mode 100644 index 000000000..3cd54488b --- /dev/null +++ b/src/chunk/index.ts @@ -0,0 +1 @@ +export * from './chunk'; diff --git a/src/taninsam.ts b/src/taninsam.ts index 9376a67d7..4c43668af 100644 --- a/src/taninsam.ts +++ b/src/taninsam.ts @@ -2,6 +2,7 @@ export * from './array-from-value'; export * from './cast-to'; export * from './chain'; export * from './chain-fn'; +export * from './chunk'; export * from './count-character'; export * from './entries'; export * from './every';