@ztimson/utils 0.23.16 → 0.23.17

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