mirror of
https://github.com/frappe/books.git
synced 2025-01-22 22:58:28 +00:00
fix: use \n splitter
- fallback behaviour for when - file is not spec conformant
This commit is contained in:
parent
233798ae3c
commit
2db6dba73e
@ -8,9 +8,9 @@ function unwrapDq(item: string): string {
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitCsvBlock(text: string): string[] {
|
function splitCsvBlock(text: string, splitter: string = '\r\n'): string[] {
|
||||||
if (!text.endsWith('\r\n')) {
|
if (!text.endsWith(splitter)) {
|
||||||
text += '\r\n';
|
text += splitter;
|
||||||
}
|
}
|
||||||
const lines = [];
|
const lines = [];
|
||||||
let line = '';
|
let line = '';
|
||||||
@ -26,10 +26,15 @@ function splitCsvBlock(text: string): string[] {
|
|||||||
inDq = !inDq;
|
inDq = !inDq;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!inDq && c === '\r' && text[i + 1] === '\n') {
|
const isEnd = [...splitter]
|
||||||
|
.slice(1)
|
||||||
|
.map((s, j) => text[i + j + 1] === s)
|
||||||
|
.every(Boolean);
|
||||||
|
|
||||||
|
if (!inDq && c === splitter[0] && isEnd) {
|
||||||
lines.push(line);
|
lines.push(line);
|
||||||
line = '';
|
line = '';
|
||||||
i = i + 1;
|
i = i + splitter.length - 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +80,9 @@ function splitCsvLine(line: string): string[] {
|
|||||||
|
|
||||||
export function parseCSV(text: string): string[][] {
|
export function parseCSV(text: string): string[][] {
|
||||||
// Works on RFC 4180
|
// Works on RFC 4180
|
||||||
const rows = splitCsvBlock(text);
|
let rows = splitCsvBlock(text);
|
||||||
|
if (rows.length === 1) {
|
||||||
|
rows = splitCsvBlock(text, '\n');
|
||||||
|
}
|
||||||
return rows.map(splitCsvLine);
|
return rows.map(splitCsvLine);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user