@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 CHANGED
@@ -688,31 +688,41 @@ ${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
- let current = "", inQuotes = false;
694
+ let current = "", inQuotes2 = false;
694
695
  for (let i = 0; i < line.length; i++) {
695
696
  const char = line[i];
696
697
  const nextChar = line[i + 1];
697
698
  if (char === '"') {
698
- if (inQuotes && nextChar === '"') {
699
+ if (inQuotes2 && nextChar === '"') {
699
700
  current += '"';
700
701
  i++;
701
- } else inQuotes = !inQuotes;
702
- } else if (char === "," && !inQuotes) {
703
- columns.push(current);
702
+ } else inQuotes2 = !inQuotes2;
703
+ } else if (char === "," && !inQuotes2) {
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
- const row = csv.split(/\r?\n/);
711
- let headers = hasHeaders ? row.splice(0, 1)[0] : null;
712
- if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g);
713
- return row.map((r2) => {
711
+ const rows2 = [];
712
+ let currentRow = "", inQuotes = false;
713
+ for (const char of csv.replace(/\r\n/g, "\n")) {
714
+ if (char === '"') inQuotes = !inQuotes;
715
+ if (char === "\n" && !inQuotes) {
716
+ rows2.push(currentRow.trim());
717
+ currentRow = "";
718
+ } else currentRow += char;
719
+ }
720
+ if (currentRow) rows2.push(currentRow.trim());
721
+ let headers = hasHeaders ? rows2.splice(0, 1)[0] : null;
722
+ if (headers) headers = (_a = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)) == null ? void 0 : _a.map((h) => h.trim());
723
+ return rows2.map((r2) => {
714
724
  const props = parseLine(r2);
715
- const h = headers || Array(props.length).fill(null).map((r22, i) => {
725
+ const h = headers || Array(props.length).fill(null).map((_, i) => {
716
726
  let letter = "";
717
727
  const first = i / 26;
718
728
  if (first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
@@ -733,7 +743,7 @@ ${opts.message || this.desc}`;
733
743
  ...t.map((row) => headers.map((h) => {
734
744
  const value2 = dotNotation(row, h);
735
745
  if (value2 == null) return "";
736
- if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll("`", '""')}"`;
746
+ if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll('"', '""')}"`;
737
747
  if (typeof value2 == "string" && /[\n"]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
738
748
  return value2;
739
749
  }).join(","))