@ztimson/utils 0.23.16 → 0.23.18
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 +22 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +22 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -684,31 +684,41 @@ function validateEmail(email) {
|
|
|
684
684
|
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
|
|
685
685
|
}
|
|
686
686
|
function fromCsv(csv, hasHeaders = true) {
|
|
687
|
+
var _a;
|
|
687
688
|
function parseLine(line) {
|
|
688
689
|
const columns = [];
|
|
689
|
-
let current = "",
|
|
690
|
+
let current = "", inQuotes2 = false;
|
|
690
691
|
for (let i = 0; i < line.length; i++) {
|
|
691
692
|
const char = line[i];
|
|
692
693
|
const nextChar = line[i + 1];
|
|
693
694
|
if (char === '"') {
|
|
694
|
-
if (
|
|
695
|
+
if (inQuotes2 && nextChar === '"') {
|
|
695
696
|
current += '"';
|
|
696
697
|
i++;
|
|
697
|
-
} else
|
|
698
|
-
} else if (char === "," && !
|
|
699
|
-
columns.push(current);
|
|
698
|
+
} else inQuotes2 = !inQuotes2;
|
|
699
|
+
} else if (char === "," && !inQuotes2) {
|
|
700
|
+
columns.push(current.trim());
|
|
700
701
|
current = "";
|
|
701
702
|
} else current += char;
|
|
702
703
|
}
|
|
703
|
-
columns.push(current);
|
|
704
|
+
columns.push(current.trim());
|
|
704
705
|
return columns.map((col) => col.replace(/^"|"$/g, "").replace(/""/g, '"'));
|
|
705
706
|
}
|
|
706
|
-
const
|
|
707
|
-
let
|
|
708
|
-
|
|
709
|
-
|
|
707
|
+
const rows2 = [];
|
|
708
|
+
let currentRow = "", inQuotes = false;
|
|
709
|
+
for (const char of csv.replace(/\r\n/g, "\n")) {
|
|
710
|
+
if (char === '"') inQuotes = !inQuotes;
|
|
711
|
+
if (char === "\n" && !inQuotes) {
|
|
712
|
+
rows2.push(currentRow.trim());
|
|
713
|
+
currentRow = "";
|
|
714
|
+
} else currentRow += char;
|
|
715
|
+
}
|
|
716
|
+
if (currentRow) rows2.push(currentRow.trim());
|
|
717
|
+
let headers = hasHeaders ? rows2.splice(0, 1)[0] : null;
|
|
718
|
+
if (headers) headers = (_a = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)) == null ? void 0 : _a.map((h) => h.trim());
|
|
719
|
+
return rows2.map((r2) => {
|
|
710
720
|
const props = parseLine(r2);
|
|
711
|
-
const h = headers || Array(props.length).fill(null).map((
|
|
721
|
+
const h = headers || Array(props.length).fill(null).map((_, i) => {
|
|
712
722
|
let letter = "";
|
|
713
723
|
const first = i / 26;
|
|
714
724
|
if (first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
|
|
@@ -729,7 +739,7 @@ function toCsv(target, flatten = true) {
|
|
|
729
739
|
...t.map((row) => headers.map((h) => {
|
|
730
740
|
const value2 = dotNotation(row, h);
|
|
731
741
|
if (value2 == null) return "";
|
|
732
|
-
if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll("
|
|
742
|
+
if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll('"', '""')}"`;
|
|
733
743
|
if (typeof value2 == "string" && /[\n"]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
|
|
734
744
|
return value2;
|
|
735
745
|
}).join(","))
|