@ztimson/utils 0.22.2 → 0.22.4

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/files.d.ts CHANGED
@@ -24,6 +24,13 @@ export declare function fileBrowser(options?: {
24
24
  accept?: string;
25
25
  multiple?: boolean;
26
26
  }): Promise<File[]>;
27
+ /**
28
+ * Extract text from a file
29
+ *
30
+ * @param file File to extract text from
31
+ * @return {Promise<string | null>} File contents
32
+ */
33
+ export declare function fileText(file: any): Promise<string | null>;
27
34
  /**
28
35
  * Create timestamp intended for filenames from a date
29
36
  *
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
  }
@@ -500,6 +493,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
500
493
  input.click();
501
494
  });
502
495
  }
496
+ function fileText(file) {
497
+ return new Promise((resolve, reject) => {
498
+ const reader = new FileReader();
499
+ reader.onload = () => resolve(reader.result);
500
+ reader.onerror = () => reject(reader.error);
501
+ reader.readAsText(file);
502
+ });
503
+ }
503
504
  function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
504
505
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
505
506
  const timestamp = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}_${date.getHours().toString().padStart(2, "0")}-${date.getMinutes().toString().padStart(2, "0")}-${date.getSeconds().toString().padStart(2, "0")}`;
@@ -1518,7 +1519,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1518
1519
  exports2.arrayDiff = arrayDiff;
1519
1520
  exports2.caseInsensitiveSort = caseInsensitiveSort;
1520
1521
  exports2.clean = clean;
1521
- exports2.csv = csv;
1522
1522
  exports2.dec2Frac = dec2Frac;
1523
1523
  exports2.deepCopy = deepCopy;
1524
1524
  exports2.deepMerge = deepMerge;
@@ -1529,6 +1529,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1529
1529
  exports2.errorFromCode = errorFromCode;
1530
1530
  exports2.escapeRegex = escapeRegex;
1531
1531
  exports2.fileBrowser = fileBrowser;
1532
+ exports2.fileText = fileText;
1532
1533
  exports2.findByProp = findByProp;
1533
1534
  exports2.flattenArr = flattenArr;
1534
1535
  exports2.flattenObj = flattenObj;
@@ -1558,6 +1559,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1558
1559
  exports2.strSplice = strSplice;
1559
1560
  exports2.timeUntil = timeUntil;
1560
1561
  exports2.timestampFilename = timestampFilename;
1562
+ exports2.toCsv = toCsv;
1561
1563
  exports2.typeKeys = typeKeys;
1562
1564
  exports2.uploadWithProgress = uploadWithProgress;
1563
1565
  exports2.validateEmail = validateEmail;