Skip to content

Commit

Permalink
feat(utils): 新增 chunkEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 29, 2021
1 parent 98d388d commit eb6f101
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/utils/__snapshots__/chunkEqual.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`chunkEqual 表现正常 1`] = `Array []`;

exports[`chunkEqual 表现正常 2`] = `
Array [
Array [
1,
2,
],
]
`;

exports[`chunkEqual 表现正常 3`] = `
Array [
Array [
1,
2,
3,
],
]
`;

exports[`chunkEqual 表现正常 4`] = `
Array [
Array [
1,
2,
3,
],
Array [
4,
100,
100,
],
]
`;

exports[`chunkEqual 表现正常 5`] = `
Array [
Array [
1,
2,
3,
],
Array [
4,
5,
5,
],
]
`;

exports[`chunkEqual 表现正常 6`] = `
Array [
Array [
1,
],
Array [
2,
],
Array [
3,
],
Array [
4,
],
Array [
5,
],
]
`;

exports[`chunkEqual 表现正常 7`] = `
Array [
Array [
1,
2,
],
Array [
3,
4,
],
Array [
5,
5,
],
]
`;

exports[`chunkEqual 表现正常 8`] = `
Array [
Array [
1,
2,
3,
4,
5,
],
]
`;

exports[`chunkEqual 表现正常 9`] = `
Array [
Array [
1,
2,
3,
4,
],
Array [
5,
5,
6,
7,
],
]
`;
15 changes: 15 additions & 0 deletions src/utils/chunkEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { chunkEqual } from './chunkEqual'

describe('chunkEqual', () => {
test('表现正常', () => {
expect(chunkEqual([], 3, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2], 3, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3], 3, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4], 3, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5], 3, i => i)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5], 1, i => i)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5], 2, i => i)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5], 8, i => i)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5], 4, i => i)).toMatchSnapshot()
})
})
22 changes: 22 additions & 0 deletions src/utils/chunkEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { chunk } from 'lodash-uni'

/**
* 类似 `chunk`,但当每组条目数量不一致时会在最后一组添加填充值以达到每组条目数量一致。
*
* @param array 数组
* @param size 分组大小
* @param filler 填充值
*/
export function chunkEqual<T>(
array: T[],
size: number,
filler: (index: number) => T,
): T[][] {
if (1 < size && size < array.length) {
array = array.slice()
for (let i = 0, l = array.length, n = size - (l % size); i < n; i++) {
array.push(filler(l + i))
}
}
return chunk(array, size)
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from 'lodash-uni'
export * from './base64'
export * from './bindEvent'
export * from './chooseFile'
export * from './chunkEqual'
export * from './cloneDeepFast'
export * from './constantCase'
export * from './copyTextToClipboard'
Expand Down

0 comments on commit eb6f101

Please sign in to comment.