@ztimson/utils 0.23.17 → 0.23.19

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 CHANGED
@@ -688,6 +688,7 @@ ${opts.message || this.desc}`;
688
688
  return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
689
689
  }
690
690
  function fromCsv(csv, hasHeaders = true) {
691
+ var _a;
691
692
  function parseLine(line) {
692
693
  const columns = [];
693
694
  let current = "", inQuotes2 = false;
@@ -700,25 +701,25 @@ ${opts.message || this.desc}`;
700
701
  i++;
701
702
  } else inQuotes2 = !inQuotes2;
702
703
  } else if (char === "," && !inQuotes2) {
703
- columns.push(current);
704
+ columns.push(current.trim());
704
705
  current = "";
705
706
  } else current += char;
706
707
  }
707
- columns.push(current);
708
+ columns.push(current.trim());
708
709
  return columns.map((col) => col.replace(/^"|"$/g, "").replace(/""/g, '"'));
709
710
  }
710
711
  const rows2 = [];
711
712
  let currentRow = "", inQuotes = false;
712
- for (const char of csv) {
713
+ for (const char of csv.replace(/\r\n/g, "\n")) {
713
714
  if (char === '"') inQuotes = !inQuotes;
714
715
  if (char === "\n" && !inQuotes) {
715
- rows2.push(currentRow);
716
+ rows2.push(currentRow.trim());
716
717
  currentRow = "";
717
718
  } else currentRow += char;
718
719
  }
719
- if (currentRow) rows2.push(currentRow);
720
+ if (currentRow) rows2.push(currentRow.trim());
720
721
  let headers = hasHeaders ? rows2.splice(0, 1)[0] : null;
721
- if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g);
722
+ if (headers) headers = (_a = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)) == null ? void 0 : _a.map((h) => h.trim());
722
723
  return rows2.map((r2) => {
723
724
  const props = parseLine(r2);
724
725
  const h = headers || Array(props.length).fill(null).map((_, i) => {
@@ -743,7 +744,7 @@ ${opts.message || this.desc}`;
743
744
  const value2 = dotNotation(row, h);
744
745
  if (value2 == null) return "";
745
746
  if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll('"', '""')}"`;
746
- if (typeof value2 == "string" && /[\n"]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
747
+ if (typeof value2 == "string" && /[\n",]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
747
748
  return value2;
748
749
  }).join(","))
749
750
  ].join("\n");