-
Notifications
You must be signed in to change notification settings - Fork 22
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
156 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,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, | ||
], | ||
] | ||
`; |
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,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() | ||
}) | ||
}) |
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,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) | ||
} |
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