This commit is contained in:
2018-09-27 13:30:45 +08:00
parent fb2bb7b59e
commit 1148ae79d6
5 changed files with 88 additions and 72 deletions

31
scripts/csv.js Normal file
View File

@ -0,0 +1,31 @@
class CSV {
constructor(data) {
this._data = data || [];
}
row(index) {
return this._data[index];
}
column(index) {
return new [...Array(this._data.length).keys].map(
i => this._data[i, index]
);
}
get data() {
return this._data;
}
toString(rowsCount) {
let data = rowsCount > 0 ? this._data.slice(0, rowsCount) : this._data;
return data.slice().reduce(
(csv, lineCells) => {
let line = lineCells.reduce(
(lineText, cell, idx) => {
cell = '"' + cell.trim().replace(/"/g, '""') + '"';
return lineText + cell + (idx == lineCells.length - 1 ? "" : ",")
}, "");
return csv + line + "\n";
},
""
);
}
}