Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/chunk/__snapshots__/chunk.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
]
`;
34 changes: 34 additions & 0 deletions src/chunk/chunk.spec.ts
Original file line number Diff line number Diff line change
@@ -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<number>(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<number>(2)([1, 2, 3, 4, 5])).toEqual([[1, 2], [3, 4], [5]]);
});

test('[1, 2, 3, 4] |> chunk() === [[1], [2], [3], [4]]', () => {
expect(chunk<number>()([1, 2, 3, 4])).toEqual([[1], [2], [3], [4]]);
});

test('[1, 2, 3, 4] |> chunk(5) === [[1, 2, 3, 4]]', () => {
expect(chunk<number>(5)([1, 2, 3, 4])).toEqual([[1, 2, 3, 4]]);
});

test('[1, 2, 3, 4] |> chunk(0) === []', () => {
expect(chunk<number>(0)([1, 2, 3, 4])).toEqual([]);
});

test('[1, 2, 3, 4] |> chunk(-1) === []', () => {
expect(chunk<number>(-1)([1, 2, 3, 4])).toEqual([]);
});
});
61 changes: 61 additions & 0 deletions src/chunk/chunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @module array=>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<number>(2)([1, 2, 3, 4]) // [[1, 2], [3, 4]]
* chunk<number>(2)([1, 2, 3, 4, 5]) // [[1, 2], [3, 4], [5]]
* ```
* @example Using the chain
* ```
* chain([1, 2, 3, 4])
* .chain(chunk<number>(2))
* .value() // [[1, 2], [3, 4]]
* ```
* @example default size is 1
* ```
* chunk<number>()([1, 2, 3, 4]) // [[1], [2], [3], [4]]
* ```
* @example when size is superior to input length
* ```
* chunk<number>(5)([1, 2, 3, 4]) // [[1, 2, 3, 4]]
* ```
* @example when size is 0
* ```
* chunk<number>(0)([1, 2, 3, 4]) // []
* ```
* @example when size is less than 0
* ```
* chunk<number>(-1)([1, 2, 3, 4]) // []
* ```
*/
export function chunk<T>(
size = 1
): (element: ReadonlyArray<T>) => ReadonlyArray<ReadonlyArray<T>> {
if (size <= 0) {
return () => [];
}

return (array: ReadonlyArray<T>): ReadonlyArray<ReadonlyArray<T>> => {
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;
};
}
1 change: 1 addition & 0 deletions src/chunk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './chunk';
1 change: 1 addition & 0 deletions src/taninsam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down