@ztimson/utils 0.28.14 → 0.28.16

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
@@ -769,6 +769,9 @@ function camelCase(str) {
769
769
  const pascal = pascalCase(str);
770
770
  return pascal.charAt(0).toLowerCase() + pascal.slice(1);
771
771
  }
772
+ function decodeHtml(html) {
773
+ return html.replace(/&nbsp;/g, " ").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&cent;/g, "¢").replace(/&pound;/g, "£").replace(/&yen;/g, "¥").replace(/&euro;/g, "€").replace(/&copy;/g, "©").replace(/&reg;/g, "®").replace(/&trade;/g, "™").replace(/&times;/g, "×").replace(/&divide;/g, "÷").replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec)).replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16))).replace(/&amp;/g, "&");
774
+ }
772
775
  function formatBytes(bytes, decimals = 2) {
773
776
  if (bytes === 0) return "0 Bytes";
774
777
  const k = 1024;
@@ -939,27 +942,42 @@ function validateEmail(email) {
939
942
  function fromCsv(csv, hasHeaders = true) {
940
943
  function parseLine(line) {
941
944
  const columns = [];
942
- let current = "", inQuotes2 = false;
945
+ let current = "", inQuotes2 = false, quoteChar2 = null;
943
946
  for (let i = 0; i < line.length; i++) {
944
947
  const char = line[i];
945
948
  const nextChar = line[i + 1];
946
- if (char === '"') {
947
- if (inQuotes2 && nextChar === '"') {
948
- current += '"';
949
+ if ((char === '"' || char === "'") && !inQuotes2) {
950
+ inQuotes2 = true;
951
+ quoteChar2 = char;
952
+ } else if (char === quoteChar2 && inQuotes2) {
953
+ if (nextChar === quoteChar2) {
954
+ current += quoteChar2;
949
955
  i++;
950
- } else inQuotes2 = !inQuotes2;
956
+ } else {
957
+ inQuotes2 = false;
958
+ quoteChar2 = null;
959
+ }
951
960
  } else if (char === "," && !inQuotes2) {
952
961
  columns.push(current.trim());
953
962
  current = "";
954
963
  } else current += char;
955
964
  }
956
965
  columns.push(current.trim());
957
- return columns.map((col) => col.replace(/^"|"$/g, "").replace(/""/g, '"'));
966
+ return columns.map((col) => {
967
+ col = col.replace(/^["']|["']$/g, "");
968
+ return col.replace(/""/g, '"').replace(/''/g, "'");
969
+ });
958
970
  }
959
971
  const rows = [];
960
- let currentRow = "", inQuotes = false;
972
+ let currentRow = "", inQuotes = false, quoteChar = null;
961
973
  for (const char of csv.replace(/\r\n/g, "\n")) {
962
- if (char === '"') inQuotes = !inQuotes;
974
+ if ((char === '"' || char === "'") && !inQuotes) {
975
+ inQuotes = true;
976
+ quoteChar = char;
977
+ } else if (char === quoteChar && inQuotes) {
978
+ inQuotes = false;
979
+ quoteChar = null;
980
+ }
963
981
  if (char === "\n" && !inQuotes) {
964
982
  rows.push(currentRow.trim());
965
983
  currentRow = "";
@@ -3036,6 +3054,7 @@ export {
3036
3054
  dayOfYear,
3037
3055
  dec2Frac,
3038
3056
  dec2Hex,
3057
+ decodeHtml,
3039
3058
  decodeJwt,
3040
3059
  deepCopy,
3041
3060
  deepMerge,