@ztimson/utils 0.28.14 → 0.28.15
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 +23 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +23 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -943,27 +943,42 @@ ${opts.message || this.desc}`;
|
|
|
943
943
|
function fromCsv(csv, hasHeaders = true) {
|
|
944
944
|
function parseLine(line) {
|
|
945
945
|
const columns = [];
|
|
946
|
-
let current = "", inQuotes2 = false;
|
|
946
|
+
let current = "", inQuotes2 = false, quoteChar2 = null;
|
|
947
947
|
for (let i = 0; i < line.length; i++) {
|
|
948
948
|
const char = line[i];
|
|
949
949
|
const nextChar = line[i + 1];
|
|
950
|
-
if (char === '"') {
|
|
951
|
-
|
|
952
|
-
|
|
950
|
+
if ((char === '"' || char === "'") && !inQuotes2) {
|
|
951
|
+
inQuotes2 = true;
|
|
952
|
+
quoteChar2 = char;
|
|
953
|
+
} else if (char === quoteChar2 && inQuotes2) {
|
|
954
|
+
if (nextChar === quoteChar2) {
|
|
955
|
+
current += quoteChar2;
|
|
953
956
|
i++;
|
|
954
|
-
} else
|
|
957
|
+
} else {
|
|
958
|
+
inQuotes2 = false;
|
|
959
|
+
quoteChar2 = null;
|
|
960
|
+
}
|
|
955
961
|
} else if (char === "," && !inQuotes2) {
|
|
956
962
|
columns.push(current.trim());
|
|
957
963
|
current = "";
|
|
958
964
|
} else current += char;
|
|
959
965
|
}
|
|
960
966
|
columns.push(current.trim());
|
|
961
|
-
return columns.map((col) =>
|
|
967
|
+
return columns.map((col) => {
|
|
968
|
+
col = col.replace(/^["']|["']$/g, "");
|
|
969
|
+
return col.replace(/""/g, '"').replace(/''/g, "'");
|
|
970
|
+
});
|
|
962
971
|
}
|
|
963
972
|
const rows = [];
|
|
964
|
-
let currentRow = "", inQuotes = false;
|
|
973
|
+
let currentRow = "", inQuotes = false, quoteChar = null;
|
|
965
974
|
for (const char of csv.replace(/\r\n/g, "\n")) {
|
|
966
|
-
if (char === '"')
|
|
975
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
976
|
+
inQuotes = true;
|
|
977
|
+
quoteChar = char;
|
|
978
|
+
} else if (char === quoteChar && inQuotes) {
|
|
979
|
+
inQuotes = false;
|
|
980
|
+
quoteChar = null;
|
|
981
|
+
}
|
|
967
982
|
if (char === "\n" && !inQuotes) {
|
|
968
983
|
rows.push(currentRow.trim());
|
|
969
984
|
currentRow = "";
|