@ztimson/utils 0.22.2 → 0.22.3

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
@@ -53,8 +53,9 @@ function encodeQuery(data) {
53
53
  function flattenObj(obj, parent, result = {}) {
54
54
  if (typeof obj === "object" && !Array.isArray(obj)) {
55
55
  for (const key of Object.keys(obj)) {
56
- const propName = parent ? parent + "." + key : key;
57
- if (typeof obj[key] === "object") {
56
+ const propName = parent ? `${parent}.${key}` : key;
57
+ if (typeof obj[key] === "object" && obj[key] != null && !Array.isArray(obj[key])) {
58
+ console.log(propName);
58
59
  flattenObj(obj[key], propName, result);
59
60
  } else {
60
61
  result[propName] = obj[key];
@@ -407,21 +408,13 @@ class Cache {
407
408
  return this;
408
409
  }
409
410
  }
410
- function csv(target, flatten = true) {
411
- const headers = target.reduce((acc, row) => {
412
- Object.keys(flatten ? flattenObj(row) : row).forEach((key) => {
413
- if (!acc.includes(key)) acc.push(key);
414
- });
415
- return acc;
416
- }, []);
411
+ function toCsv(target, flatten = true) {
412
+ const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
417
413
  return [
418
414
  headers.join(","),
419
415
  ...target.map((row) => headers.map((h) => {
420
416
  const value = dotNotation(row, h);
421
- const type = typeof value;
422
- if (type == "string" && value.includes(",")) return `"${value}"`;
423
- if (type == "object") return `"${JSON.stringify(value)}"`;
424
- return value;
417
+ return typeof value == "object" && value != null ? '"' + JSONSanitize(value).replaceAll('"', '""') + '"' : value;
425
418
  }).join(","))
426
419
  ].join("\n");
427
420
  }
@@ -1515,7 +1508,6 @@ export {
1515
1508
  arrayDiff,
1516
1509
  caseInsensitiveSort,
1517
1510
  clean,
1518
- csv,
1519
1511
  dec2Frac,
1520
1512
  deepCopy,
1521
1513
  deepMerge,
@@ -1555,6 +1547,7 @@ export {
1555
1547
  strSplice,
1556
1548
  timeUntil,
1557
1549
  timestampFilename,
1550
+ toCsv,
1558
1551
  typeKeys,
1559
1552
  uploadWithProgress,
1560
1553
  validateEmail