UNPKG

flat-zip

Version:

Concats arrays but does so like a zipper instead of appending them. Like lodash.zip + lodash.flat, plus a length limit

23 lines (22 loc) 719 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** Concats arrays but does so like a zipper instead of appending them. Like lodash.zip + lodash.flat, plus a total limit */ function flatZip(table, limit = Infinity) { if (limit <= 0) { return []; } const maxColumns = Math.max(...table.map(row => row.length)); const zipped = []; for (let col = 0; col < maxColumns; col++) { for (const row of table) { if (row.length > col) { zipped.push(row[col]); if (zipped.length === limit) { return zipped; } } } } return zipped; } exports.flatZip = flatZip;