-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@noaignite/utils': minor | ||
--- | ||
|
||
add chunk helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters