UNPKG

ut2

Version:

一个现代 JavaScript 实用工具库。[点击查看在线文档]。

22 lines (19 loc) 615 B
import baseSlice from './internals/baseSlice.js'; import { mathCeil } from './internals/native.js'; import isNil from './isNil.js'; import toInteger from './toInteger.js'; function chunk(array, size) { if (size === void 0) { size = 1; } size = toInteger(size); var length = isNil(array) ? 0 : array.length; if (!length || size < 1) { return []; } var result = Array(mathCeil(length / size)); var resIndex = 0, index = 0; while (index < length) { result[resIndex++] = baseSlice(array, index, (index += size)); } return result; } export { chunk as default };