to-grid
Version:
generate an array of elements split into groups in specified layout
28 lines • 939 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var Layout;
(function (Layout) {
Layout[Layout["N"] = 0] = "N";
Layout[Layout["Z"] = 1] = "Z";
})(Layout = exports.Layout || (exports.Layout = {}));
function toGrid(arr, numOfColumns, layout) {
if (layout === void 0) { layout = Layout.Z; }
var result = [];
var numOfRows = Math.ceil(arr.length / numOfColumns);
arr.forEach(function (it, index) {
var rowIndex, columnIndex;
if (layout === Layout.Z) {
rowIndex = Math.floor(index / numOfColumns);
columnIndex = index % numOfColumns;
}
else {
rowIndex = index % numOfRows;
columnIndex = Math.floor(index / numOfRows);
}
result[rowIndex] = result[rowIndex] || [];
result[rowIndex][columnIndex] = it;
});
return result;
}
exports.default = toGrid;
//# sourceMappingURL=index.js.map