@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/csv.d.ts CHANGED
@@ -5,4 +5,4 @@
5
5
  * @param {boolean} flatten Should nested object be flattened or treated as values
6
6
  * @return {string} CSV string
7
7
  */
8
- export declare function csv(target: any[], flatten?: boolean): string;
8
+ export declare function toCsv(target: any[], flatten?: boolean): string;
package/dist/index.cjs CHANGED
@@ -57,8 +57,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
57
57
  function flattenObj(obj, parent, result = {}) {
58
58
  if (typeof obj === "object" && !Array.isArray(obj)) {
59
59
  for (const key of Object.keys(obj)) {
60
- const propName = parent ? parent + "." + key : key;
61
- if (typeof obj[key] === "object") {
60
+ const propName = parent ? `${parent}.${key}` : key;
61
+ if (typeof obj[key] === "object" && obj[key] != null && !Array.isArray(obj[key])) {
62
+ console.log(propName);
62
63
  flattenObj(obj[key], propName, result);
63
64
  } else {
64
65
  result[propName] = obj[key];
@@ -411,21 +412,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
411
412
  return this;
412
413
  }
413
414
  }
414
- function csv(target, flatten = true) {
415
- const headers = target.reduce((acc, row) => {
416
- Object.keys(flatten ? flattenObj(row) : row).forEach((key) => {
417
- if (!acc.includes(key)) acc.push(key);
418
- });
419
- return acc;
420
- }, []);
415
+ function toCsv(target, flatten = true) {
416
+ const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
421
417
  return [
422
418
  headers.join(","),
423
419
  ...target.map((row) => headers.map((h) => {
424
420
  const value = dotNotation(row, h);
425
- const type = typeof value;
426
- if (type == "string" && value.includes(",")) return `"${value}"`;
427
- if (type == "object") return `"${JSON.stringify(value)}"`;
428
- return value;
421
+ return typeof value == "object" && value != null ? '"' + JSONSanitize(value).replaceAll('"', '""') + '"' : value;
429
422
  }).join(","))
430
423
  ].join("\n");
431
424
  }
@@ -1518,7 +1511,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1518
1511
  exports2.arrayDiff = arrayDiff;
1519
1512
  exports2.caseInsensitiveSort = caseInsensitiveSort;
1520
1513
  exports2.clean = clean;
1521
- exports2.csv = csv;
1522
1514
  exports2.dec2Frac = dec2Frac;
1523
1515
  exports2.deepCopy = deepCopy;
1524
1516
  exports2.deepMerge = deepMerge;
@@ -1558,6 +1550,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1558
1550
  exports2.strSplice = strSplice;
1559
1551
  exports2.timeUntil = timeUntil;
1560
1552
  exports2.timestampFilename = timestampFilename;
1553
+ exports2.toCsv = toCsv;
1561
1554
  exports2.typeKeys = typeKeys;
1562
1555
  exports2.uploadWithProgress = uploadWithProgress;
1563
1556
  exports2.validateEmail = validateEmail;