Skip to content

Commit

Permalink
fix(chunkEqual): 修复临界值错误
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Sep 29, 2021
1 parent 706fa4a commit 1a08efe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/utils/__snapshots__/chunkEqual.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,48 @@ Array [
],
]
`;

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

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

exports[`chunkEqual 表现正常 12`] = `
Array [
Array [
1,
2,
],
Array [
3,
4,
],
Array [
5,
6,
],
]
`;
3 changes: 3 additions & 0 deletions src/utils/chunkEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ describe('chunkEqual', () => {
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()
expect(chunkEqual([1, 2, 3, 4], 2, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5, 6], 3, () => 100)).toMatchSnapshot()
expect(chunkEqual([1, 2, 3, 4, 5, 6], 2, () => 100)).toMatchSnapshot()
})
})
8 changes: 5 additions & 3 deletions src/utils/chunkEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export function chunkEqual<T>(
size: number,
filler: (index: number) => T,
): T[][] {
if (1 < size && size < array.length) {
const len = array.length
const remain = len % size
if (remain !== 0 && 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))
for (let i = 0, n = size - remain; i < n; i++) {
array.push(filler(len + i))
}
}
return chunk(array, size)
Expand Down

0 comments on commit 1a08efe

Please sign in to comment.