@ztimson/utils 0.22.3 → 0.22.5

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
@@ -1,3 +1,4 @@
1
+ export declare function fromCsv<T = any>(csv: string, hasHeaders?: boolean): T[];
1
2
  /**
2
3
  * Convert an object to a CSV string
3
4
  *
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
@@ -412,6 +412,17 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
412
412
  return this;
413
413
  }
414
414
  }
415
+ function fromCsv(csv, hasHeaders = true) {
416
+ const row = csv.split("\n");
417
+ let headers = hasHeaders ? row.splice(0, 1)[0] : null;
418
+ if (headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
419
+ return row.map((r) => {
420
+ const props = r.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
421
+ const h = headers || Array(props.length).fill(null).map((r2, i) => {
422
+ });
423
+ return h.reduce((acc, h2, i) => ({ ...acc, [h2]: props[i] }), {});
424
+ });
425
+ }
415
426
  function toCsv(target, flatten = true) {
416
427
  const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
417
428
  return [
@@ -493,6 +504,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
493
504
  input.click();
494
505
  });
495
506
  }
507
+ function fileText(file) {
508
+ return new Promise((resolve, reject) => {
509
+ const reader = new FileReader();
510
+ reader.onload = () => resolve(reader.result);
511
+ reader.onerror = () => reject(reader.error);
512
+ reader.readAsText(file);
513
+ });
514
+ }
496
515
  function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
497
516
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
498
517
  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")}`;
@@ -939,10 +958,10 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
939
958
  split = split.pop().split("/");
940
959
  return whole + Number(split[0]) / Number(split[1]);
941
960
  }
942
- const LETTER_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
961
+ const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
943
962
  const NUMBER_LIST = "0123456789";
944
963
  const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
945
- const CHAR_LIST = LETTER_LIST + NUMBER_LIST + SYMBOL_LIST;
964
+ const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
946
965
  function formatBytes(bytes, decimals = 2) {
947
966
  if (bytes === 0) return "0 Bytes";
948
967
  const k = 1024;
@@ -1479,6 +1498,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1479
1498
  exports2.ASet = ASet;
1480
1499
  exports2.BadGatewayError = BadGatewayError;
1481
1500
  exports2.BadRequestError = BadRequestError;
1501
+ exports2.CHAR_LIST = CHAR_LIST;
1482
1502
  exports2.Cache = Cache;
1483
1503
  exports2.CliBackground = CliBackground;
1484
1504
  exports2.CliEffects = CliEffects;
@@ -1490,9 +1510,11 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1490
1510
  exports2.InternalServerError = InternalServerError;
1491
1511
  exports2.JSONAttemptParse = JSONAttemptParse;
1492
1512
  exports2.JSONSanitize = JSONSanitize;
1513
+ exports2.LETTER_LIST = LETTER_LIST;
1493
1514
  exports2.LOG_LEVEL = LOG_LEVEL;
1494
1515
  exports2.Logger = Logger;
1495
1516
  exports2.MethodNotAllowedError = MethodNotAllowedError;
1517
+ exports2.NUMBER_LIST = NUMBER_LIST;
1496
1518
  exports2.NotAcceptableError = NotAcceptableError;
1497
1519
  exports2.NotFoundError = NotFoundError;
1498
1520
  exports2.NotImplementedError = NotImplementedError;
@@ -1503,6 +1525,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1503
1525
  exports2.PathEventEmitter = PathEventEmitter;
1504
1526
  exports2.PaymentRequiredError = PaymentRequiredError;
1505
1527
  exports2.PromiseProgress = PromiseProgress;
1528
+ exports2.SYMBOL_LIST = SYMBOL_LIST;
1506
1529
  exports2.ServiceUnavailableError = ServiceUnavailableError;
1507
1530
  exports2.TypedEmitter = TypedEmitter;
1508
1531
  exports2.UnauthorizedError = UnauthorizedError;
@@ -1521,6 +1544,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1521
1544
  exports2.errorFromCode = errorFromCode;
1522
1545
  exports2.escapeRegex = escapeRegex;
1523
1546
  exports2.fileBrowser = fileBrowser;
1547
+ exports2.fileText = fileText;
1524
1548
  exports2.findByProp = findByProp;
1525
1549
  exports2.flattenArr = flattenArr;
1526
1550
  exports2.flattenObj = flattenObj;
@@ -1529,6 +1553,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1529
1553
  exports2.formatDate = formatDate;
1530
1554
  exports2.formatPhoneNumber = formatPhoneNumber;
1531
1555
  exports2.fracToDec = fracToDec;
1556
+ exports2.fromCsv = fromCsv;
1532
1557
  exports2.gravatar = gravatar;
1533
1558
  exports2.includes = includes;
1534
1559
  exports2.insertAt = insertAt;