Skip to content

Commit

Permalink
feat(colums): support multine cells
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 20, 2018
1 parent 67526f3 commit 585fc59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
46 changes: 33 additions & 13 deletions columns.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
"use strict";

var from = require("es5-ext/array/from")
var generate = require("es5-ext/array/generate")
, from = require("es5-ext/array/from")
, iterable = require("es5-ext/iterable/validate-object")
, isValue = require("es5-ext/object/is-value")
, stringifiable = require("es5-ext/object/validate-stringifiable")
, repeat = require("es5-ext/string/#/repeat")
, getStrippedLength = require("./get-stripped-length");

module.exports = function (rows/*, options*/) {
var options = Object(arguments[1]), colsMeta = [], colsOptions = options.columns || [];
return (
from(iterable(rows), function (row) {
return from(iterable(row), function (str, index) {
var col = colsMeta[index], strLength;
if (!col) col = colsMeta[index] = { width: 0 };
str = stringifiable(str);
strLength = getStrippedLength(str);
if (strLength > col.width) col.width = strLength;
return { str: str, length: strLength };
var push = Array.prototype.push;

module.exports = function (inputRows/*, options*/) {
var options = Object(arguments[1])
, colsMeta = []
, colsOptions = options.columns || []
, rows = [];

from(iterable(inputRows), function (row) {
var rowRows = [[]];
from(iterable(row), function (cellStr, columnIndex) {
var cellRows = stringifiable(cellStr).split("\n");
while (cellRows.length > rowRows.length) rowRows.push(generate(columnIndex, ""));
cellRows.forEach(function (cellRow, rowRowIndex) {
rowRows[rowRowIndex][columnIndex] = cellRow;
});
})
});
push.apply(rows, rowRows);
});

return (
rows
.map(function (row) {
return from(iterable(row), function (str, index) {
var col = colsMeta[index], strLength;
if (!col) col = colsMeta[index] = { width: 0 };
str = stringifiable(str);
strLength = getStrippedLength(str);
if (strLength > col.width) col.width = strLength;
return { str: str, length: strLength };
});
})
.map(function (row) {
return row
.map(function (item, index) {
Expand Down
7 changes: 7 additions & 0 deletions test/columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ module.exports = function (t, a) {

a(t([["ID"], [1], [1, 23], [1, 23, 456]]), "ID\n1 \n1 | 23\n1 | 23 | 456\n", "Force columns");

a(
t([["ONE", "TWO", "THREE"], ["ON", "DWA\nTRZY", "DWA\nTRZY\nCZTERY"], ["HOPLA", "B", "C"]]),
"ONE | TWO | THREE \nON | DWA | DWA \n | TRZY | TRZY \n" +
" | | CZTERY\nHOPLA | B | C \n",
"Rows"
);

a(t([["ID"], ["", ""], [123, 123]]), "ID \n | \n123 | 123\n", "Empty cells");
};

0 comments on commit 585fc59

Please sign in to comment.