@ztimson/utils 0.23.16 → 0.23.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +18 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +18 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -690,16 +690,16 @@ ${opts.message || this.desc}`;
|
|
|
690
690
|
function fromCsv(csv, hasHeaders = true) {
|
|
691
691
|
function parseLine(line) {
|
|
692
692
|
const columns = [];
|
|
693
|
-
let current = "",
|
|
693
|
+
let current = "", inQuotes2 = false;
|
|
694
694
|
for (let i = 0; i < line.length; i++) {
|
|
695
695
|
const char = line[i];
|
|
696
696
|
const nextChar = line[i + 1];
|
|
697
697
|
if (char === '"') {
|
|
698
|
-
if (
|
|
698
|
+
if (inQuotes2 && nextChar === '"') {
|
|
699
699
|
current += '"';
|
|
700
700
|
i++;
|
|
701
|
-
} else
|
|
702
|
-
} else if (char === "," && !
|
|
701
|
+
} else inQuotes2 = !inQuotes2;
|
|
702
|
+
} else if (char === "," && !inQuotes2) {
|
|
703
703
|
columns.push(current);
|
|
704
704
|
current = "";
|
|
705
705
|
} else current += char;
|
|
@@ -707,12 +707,21 @@ ${opts.message || this.desc}`;
|
|
|
707
707
|
columns.push(current);
|
|
708
708
|
return columns.map((col) => col.replace(/^"|"$/g, "").replace(/""/g, '"'));
|
|
709
709
|
}
|
|
710
|
-
const
|
|
711
|
-
let
|
|
710
|
+
const rows2 = [];
|
|
711
|
+
let currentRow = "", inQuotes = false;
|
|
712
|
+
for (const char of csv) {
|
|
713
|
+
if (char === '"') inQuotes = !inQuotes;
|
|
714
|
+
if (char === "\n" && !inQuotes) {
|
|
715
|
+
rows2.push(currentRow);
|
|
716
|
+
currentRow = "";
|
|
717
|
+
} else currentRow += char;
|
|
718
|
+
}
|
|
719
|
+
if (currentRow) rows2.push(currentRow);
|
|
720
|
+
let headers = hasHeaders ? rows2.splice(0, 1)[0] : null;
|
|
712
721
|
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g);
|
|
713
|
-
return
|
|
722
|
+
return rows2.map((r2) => {
|
|
714
723
|
const props = parseLine(r2);
|
|
715
|
-
const h = headers || Array(props.length).fill(null).map((
|
|
724
|
+
const h = headers || Array(props.length).fill(null).map((_, i) => {
|
|
716
725
|
let letter = "";
|
|
717
726
|
const first = i / 26;
|
|
718
727
|
if (first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
|
|
@@ -733,7 +742,7 @@ ${opts.message || this.desc}`;
|
|
|
733
742
|
...t.map((row) => headers.map((h) => {
|
|
734
743
|
const value2 = dotNotation(row, h);
|
|
735
744
|
if (value2 == null) return "";
|
|
736
|
-
if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll("
|
|
745
|
+
if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll('"', '""')}"`;
|
|
737
746
|
if (typeof value2 == "string" && /[\n"]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
|
|
738
747
|
return value2;
|
|
739
748
|
}).join(","))
|