UNPKG

sequency

Version:

Functional sequences for processing iterable data in JavaScript

34 lines 1.09 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.Chunk = void 0; var Chunk = /** @class */ (function () { function Chunk() { } /** * Splits the elements of the sequence into arrays which length is determined by * the given `chunkSize` and returns all chunks as array. * * @param {number} chunkSize * @returns {Array<Array<T>>} */ Chunk.prototype.chunk = function (chunkSize) { if (chunkSize < 1) { throw new Error("chunkSize must be > 0 but is " + chunkSize); } var result = []; var index = 0; for (var item = this.iterator.next(); !item.done; item = this.iterator.next()) { var chunkIndex = Math.floor(index / chunkSize); if (result[chunkIndex] == null) { result[chunkIndex] = [item.value]; } else { result[chunkIndex].push(item.value); } index++; } return result; }; return Chunk; }()); exports.Chunk = Chunk; //# sourceMappingURL=chunk.js.map