Skip to content

Commit

Permalink
feat: chunk function
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Nov 9, 2020
1 parent 50d5feb commit 8c6ceb7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import toInteger from './toInteger';
* If `array` can't be split evenly, the final chunk will be the remaining
* elements.
*
* @since 3.0.0
* @since 5.18.0
* @category Array
* @param {Array} array The array to process.
* @param {number} [size=1] The length of each chunk
* @returns {Array} Returns the new array of chunks.
* @param array The array to process.
* @param size The length of each chunk, default is 1
* @returns Returns the new array of chunks.
* @example
*
* ```js
* chunk(['a', 'b', 'c', 'd'], 2)
* // => [['a', 'b'], ['c', 'd']]
*
* chunk(['a', 'b', 'c', 'd'], 3)
* // => [['a', 'b', 'c'], ['d']]
* ```
*/
function chunk(array, size = 1) {
export function chunk<T>(array: Array<T>, size = 1): Array<Array<T>> {
size = Math.max(toInteger(size), 0);
const length = array == null ? 0 : array.length;
if (!length || size < 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/concurrency/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { AsyncFunction } from '../types';
* @since 5.15.0
* @category Async
*
* @param runner
* @param timeout
* @param runner async runner please, otherwise the `timeout` is not meaningful
* @param timeout timeout threshold in milliseconds
*
* @throws {TimeoutError}
*
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { cacheIt } from './cacheIt';
import { cacheProvider } from './cacheProvider';
import { capitalize } from './capitalize';
import { ceil } from './ceil';
import { chunk } from './chunk';
import { clone } from './clone';
import { cloneDeep } from './cloneDeep';
import { concat } from './concat';
Expand Down Expand Up @@ -151,7 +152,7 @@ import { trimEnd } from './trimEnd';
import { trimPrefix } from './trimPrefix';
import { trimStart } from './trimStart';
import { trimSuffix } from './trimSuffix';
import unionBy from './unionBy';
import { unionBy } from './unionBy';
import { uniq } from './uniq';
import uniqBy from './uniqBy';
import uniqueId from './uniqueId';
Expand Down Expand Up @@ -195,7 +196,7 @@ export {
wrap, unWrap, createTimeoutPromise, TimeoutError,
retry, concurrency, fallback, logic, isSubClass,
cacheIt, cacheProvider, distance, closest, isInstance,
values
values, chunk
};


Expand Down Expand Up @@ -229,7 +230,7 @@ export default {
wrap, unWrap, createTimeoutPromise, TimeoutError,
retry, concurrency, fallback, logic, isSubClass,
cacheIt, cacheProvider, distance, closest, isInstance,
values
values, chunk

};

2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export type Class<T = any> = new (...args: any[]) => T
/**
* async function
*/
export type AsyncFunction = (...args: any[]) => Promise<any>
export type AsyncFunction<P extends any[] = any[], R = any> = (...args: P) => Promise<R>

/**
* return type of function
Expand Down
5 changes: 3 additions & 2 deletions test/chunk.test.js → test/chunk.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as assert from 'assert';
import { falsey, stubArray } from './utils';
import chunk from '../src/chunk';
import map from '../src/map';
import { isUndefined } from '../src/isUndefined';
import map from '../src/map';
import reject from '../src/reject';
import { falsey, stubArray } from './utils';

describe('chunk', () => {
const array = [0, 1, 2, 3, 4, 5];
Expand All @@ -21,6 +21,7 @@ describe('chunk', () => {
it('should treat falsey `size` values, except `undefined`, as `0`', () => {
const expected = map(falsey, (value) => value === undefined ? [[0], [1], [2], [3], [4], [5]] : []);

// @ts-ignore
const actual = map(falsey, (size, index) => index ? chunk(array, size) : chunk(array));

assert.deepStrictEqual(actual, expected);
Expand Down

0 comments on commit 8c6ceb7

Please sign in to comment.