@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
125 lines • 4.22 kB
JavaScript
var UtilCsvState = /** @class */ (function () {
function UtilCsvState(lines) {
this.lines = lines;
this.linesLength = lines.length;
this.cursor = -1;
this.line = "";
this.lineLength = 0;
}
UtilCsvState.prototype.next = function () {
var newCursor = this.cursor + 1;
if (newCursor < this.linesLength) {
this.cursor = newCursor;
var nextLine = this.lines[newCursor];
this.line = nextLine;
this.lineLength = nextLine.length;
return true;
}
return false;
};
return UtilCsvState;
}());
export { UtilCsvState };
var UtilCsv = /** @class */ (function () {
function UtilCsv() {
}
UtilCsv.parseLine = function (state) {
var result = [];
var icomma = -1;
var line = state.line;
var length = state.lineLength;
for (var i = 0; i < length; ++i) {
if (line.charAt(i) === '"') {
// Find the corresponding double quote
for (var j = i + 1; j < length; ++j) {
var k = line.indexOf('"', j);
if (k < 0) {
// Not found.
if (state.next()) {
// Consumes the next line
line += "\n" + state.line;
length += 1 + state.lineLength;
// And try again
j -= 1;
continue;
}
else {
// Skip to the end.
j = length + 1;
result.push(this.unescape(line.substring(i + 1, j - 1)));
i = j;
}
}
else {
j = k + 1;
// If the next character is a double quote, then continues.
if (j < length && line.charAt(j) === '"') {
continue;
}
// The next character isn't a double quote
result.push(this.unescape(line.substring(i + 1, j - 1)));
i = j;
}
break;
}
}
else {
if (icomma < i) {
icomma = line.indexOf(",", i);
if (icomma < 0) {
icomma = length;
}
}
result.push(line.substring(i, icomma));
i = icomma;
}
}
return result;
};
UtilCsv.parse = function (str) {
var lines = str.split(/(?:\r\n?|\n)/g);
var result = [];
var state = new UtilCsvState(lines);
while (state.next()) {
var row = this.parseLine(state);
if (0 < row.length) {
result.push(row);
}
}
return result;
};
UtilCsv.stringifyLine = function (line) {
var result = "";
var delimiter = "";
for (var i = 0, imax = line.length; i < imax; ++i) {
result += delimiter + this.escape(String(line[i]));
delimiter = ",";
}
return result;
};
UtilCsv.stringify = function (lines) {
var result = "";
var delimiter = "";
for (var i = 0, imax = lines.length; i < imax; ++i) {
result += delimiter + this.stringifyLine(lines[i]);
delimiter = "\n";
}
return result;
};
UtilCsv.escape = function (target) {
var result = target.replace(/"/g, '""');
if (result.length !== target.length ||
0 <= target.indexOf(",") ||
0 <= target.indexOf("\n") ||
0 <= target.indexOf("\r")) {
return "\"".concat(result, "\"");
}
return target;
};
UtilCsv.unescape = function (target) {
return target.replace(/""/g, '"');
};
return UtilCsv;
}());
export { UtilCsv };
//# sourceMappingURL=util-csv.js.map