css-grid-template-parser
Version:
A simple CSS Grid template parser
82 lines (81 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Matching area
* @param areas
* @param row
* @param column
* @returns
*/
function matchingArea(areas, row, column) {
return function (area) {
return areas[area].row.start <= row + 1 &&
areas[area].row.end > row + 1 &&
areas[area].column.start <= column + 1 &&
areas[area].column.end > column + 1;
};
}
/**
* Gets columns
* @param areas
* @param grid
* @param row
* @param [current]
* @param [cols]
* @returns columns
*/
function getColumns(areas, grid, row, current, cols) {
if (current === void 0) { current = 0; }
if (cols === void 0) { cols = ''; }
var area = areas.find(matchingArea(grid.areas, row, current));
cols += typeof area === 'string' ? area : '.';
if (current < grid.width - 1) {
return getColumns(areas, grid, row, current + 1, cols + " ");
}
return cols;
}
/**
* Gets rows
* @param areas
* @param grid
* @param [current]
* @param [rows]
* @returns rows
*/
function getRows(areas, grid, current, rows) {
if (current === void 0) { current = 0; }
if (rows === void 0) { rows = ''; }
rows += "\"" + getColumns(areas, grid, current) + "\"";
if (current < grid.height - 1) {
return getRows(areas, grid, current + 1, rows + "\n");
}
return rows;
}
/**
*
* @param grid
* @example
* const areas = template({
width: 5,
height: 4,
areas: {
a: {
column: { start: 1, end: 4, span: 3 },
row: { start: 1, end: 3, span: 2 },
},
b: {
column: { start: 3, end: 6, span: 3 },
row: { start: 3, end: 5, span: 2 },
},
},
})
// → `"a a a . ."
// "a a a . ."
// ". . b b b"
// ". . b b b"`
*/
function template(grid) {
return getRows(Object.keys(grid.areas), grid);
}
exports.default = template;
//# sourceMappingURL=template.js.map