From 7bec3e0543a7767142354f1235bdc492a1aaee71 Mon Sep 17 00:00:00 2001 From: Martin Barri Date: Mon, 28 Oct 2024 14:56:55 +0100 Subject: [PATCH] feat(utils): add chunk helper --- .changeset/strong-feet-allow.md | 5 ++++ packages/utils/src/chunk.test.ts | 39 ++++++++++++++++++++++++++++++++ packages/utils/src/chunk.ts | 28 +++++++++++++++++++++++ packages/utils/src/index.ts | 1 + 4 files changed, 73 insertions(+) create mode 100644 .changeset/strong-feet-allow.md create mode 100644 packages/utils/src/chunk.test.ts create mode 100644 packages/utils/src/chunk.ts diff --git a/.changeset/strong-feet-allow.md b/.changeset/strong-feet-allow.md new file mode 100644 index 00000000..df8b81e7 --- /dev/null +++ b/.changeset/strong-feet-allow.md @@ -0,0 +1,5 @@ +--- +'@noaignite/utils': minor +--- + +add chunk helper diff --git a/packages/utils/src/chunk.test.ts b/packages/utils/src/chunk.test.ts new file mode 100644 index 00000000..2197e2b1 --- /dev/null +++ b/packages/utils/src/chunk.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest' +import { chunk } from './chunk' + +describe('chunk', () => { + it('is a function and returns an array', () => { + expect(typeof chunk).toEqual('function') + expect(Array.isArray(chunk([], 1))).toEqual(true) + }) + + it('throws an error if passed invalid arguments', () => { + // @ts-expect-error -- Test invalid interface + expect(() => chunk('', 2)).toThrowError(/^Argument `array` should be of type array$/) + // @ts-expect-error -- Test invalid interface + expect(() => chunk({}, 2)).toThrowError(/^Argument `array` should be of type array$/) + // @ts-expect-error -- Test invalid interface + expect(() => chunk([], '')).toThrowError(/^Argument `size` should be of type integer$/) + expect(() => chunk([], 0)).toThrowError(/^Argument `size` should be greater than 0$/) + expect(() => chunk([], -1)).toThrowError(/^Argument `size` should be greater than 0$/) + }) + + it('should turn array into thunks of n size', () => { + const arr = [1, 2, 3, 4, 5, 6] // prettier-ignore + const len1chunks = [[1], [2], [3], [4], [5], [6]] // prettier-ignore + const len2chunks = [[1, 2], [3, 4], [5, 6]] // prettier-ignore + const len3chunks = [[1, 2, 3], [4, 5, 6]] // prettier-ignore + const len4chunks = [[1, 2, 3, 4], [5, 6]] // prettier-ignore + const len5chunks = [[1, 2, 3, 4, 5], [6]] // prettier-ignore + const len6chunks = [[1, 2, 3, 4, 5, 6]] // prettier-ignore + const len7chunks = [[1, 2, 3, 4, 5, 6]] // prettier-ignore + + expect(chunk(arr, 1)).toStrictEqual(len1chunks) + expect(chunk(arr, 2)).toStrictEqual(len2chunks) + expect(chunk(arr, 3)).toStrictEqual(len3chunks) + expect(chunk(arr, 4)).toStrictEqual(len4chunks) + expect(chunk(arr, 5)).toStrictEqual(len5chunks) + expect(chunk(arr, 6)).toStrictEqual(len6chunks) + expect(chunk(arr, 7)).toStrictEqual(len7chunks) + }) +}) diff --git a/packages/utils/src/chunk.ts b/packages/utils/src/chunk.ts new file mode 100644 index 00000000..7ecf3cb4 --- /dev/null +++ b/packages/utils/src/chunk.ts @@ -0,0 +1,28 @@ +import { assert } from './assert' + +/** + * Splits an array into chunks of a specified size. + * + * @param array - The array to be split into chunks. + * @param size - The maximum size of each chunk. Must be a positive integer. + * @returns An array of chunks, where each chunk is an array of elements from the input array. + * @throws Will throw an error if `array` is not an array. + * @throws Will throw an error if `size` is not a positive integer. + * + * @example + * ```ts + * chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]] + * ``` + */ +export function chunk(array: T[], size: number): T[][] { + assert(Array.isArray(array), 'Argument `array` should be of type array') + assert(Number.isInteger(size), 'Argument `size` should be of type integer') + assert(size > 0, 'Argument `size` should be greater than 0') + + const result: T[][] = [] + for (let i = 0; i < array.length; i += size) { + result.push(array.slice(i, i + size)) + } + + return result +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index f55ca8ca..01774ac7 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -2,6 +2,7 @@ export * from './assert' export * from './calculateContrast' export * from './calculateLuminance' export * from './capitalize' +export * from './chunk' export * from './clamp' export * from './colorContrast' export * from './deepmerge'