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