All files / utils chunkEqual.ts

100% Statements 9/9
100% Branches 7/7
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                                13x 13x 13x 5x 5x 8x     13x    
import { chunk } from 'lodash-uni'
 
/**
 * 类似 `chunk`,但当每组条目数量不一致时会在最后一组添加填充值以达到每组条目数量一致。
 *
 * @param array 数组
 * @param size 分组大小
 * @param filler 填充值
 * @param consistent 是否保持一致性,默认 false,设为 true 则当数组长度小于分组大小时也填充数组使其长度达到分组大小
 */
export function chunkEqual<T>(
  array: T[],
  size: number,
  filler: (index: number) => T,
  consistent = false,
): T[][] {
  const len = array.length
  const remain = len % size
  if (remain !== 0 && (consistent ? true : size < array.length)) {
    array = array.slice()
    for (let i = 0, n = size - remain; i < n; i++) {
      array.push(filler(len + i))
    }
  }
  return chunk(array, size)
}