Skip to content

Commit

Permalink
feat(utils): add chunk helper
Browse files Browse the repository at this point in the history
  • Loading branch information
maeertin committed Nov 2, 2024
1 parent 18d82e6 commit 7bec3e0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-feet-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noaignite/utils': minor
---

add chunk helper
39 changes: 39 additions & 0 deletions packages/utils/src/chunk.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
28 changes: 28 additions & 0 deletions packages/utils/src/chunk.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 7bec3e0

Please sign in to comment.