Merged master into esm branch

This commit is contained in:
n1474335 2018-05-14 11:58:01 +00:00
commit a98d37e61c
21 changed files with 2206 additions and 2342 deletions

View file

@ -524,36 +524,43 @@ class Utils {
* Parses CSV data and returns it as a two dimensional array or strings.
*
* @param {string} data
* @param {string[]} [cellDelims=[","]]
* @param {string[]} [lineDelims=["\n", "\r"]]
* @returns {string[][]}
*
* @example
* // returns [["head1", "head2"], ["data1", "data2"]]
* Utils.parseCSV("head1,head2\ndata1,data2");
*/
static parseCSV(data) {
static parseCSV(data, cellDelims=[","], lineDelims=["\n", "\r"]) {
let b,
ignoreNext = false,
next,
renderNext = false,
inString = false,
cell = "",
line = [];
const lines = [];
// Remove BOM, often present in Excel CSV files
if (data.length && data[0] === "\uFEFF") data = data.substr(1);
for (let i = 0; i < data.length; i++) {
b = data[i];
if (ignoreNext) {
next = data[i+1] || "";
if (renderNext) {
cell += b;
ignoreNext = false;
renderNext = false;
} else if (b === "\\") {
cell += b;
ignoreNext = true;
renderNext = true;
} else if (b === "\"" && !inString) {
inString = true;
} else if (b === "\"" && inString) {
inString = false;
} else if (b === "," && !inString) {
if (next === "\"") renderNext = true;
else inString = false;
} else if (!inString && cellDelims.indexOf(b) >= 0) {
line.push(cell);
cell = "";
} else if ((b === "\n" || b === "\r") && !inString) {
} else if (!inString && lineDelims.indexOf(b) >= 0) {
line.push(cell);
cell = "";
lines.push(line);