@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/index.mjs CHANGED
@@ -408,6 +408,17 @@ class Cache {
408
408
  return this;
409
409
  }
410
410
  }
411
+ function fromCsv(csv, hasHeaders = true) {
412
+ const row = csv.split("\n");
413
+ let headers = hasHeaders ? row.splice(0, 1)[0] : null;
414
+ if (headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
415
+ return row.map((r) => {
416
+ const props = r.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
417
+ const h = headers || Array(props.length).fill(null).map((r2, i) => {
418
+ });
419
+ return h.reduce((acc, h2, i) => ({ ...acc, [h2]: props[i] }), {});
420
+ });
421
+ }
411
422
  function toCsv(target, flatten = true) {
412
423
  const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
413
424
  return [
@@ -489,6 +500,14 @@ function fileBrowser(options = {}) {
489
500
  input.click();
490
501
  });
491
502
  }
503
+ function fileText(file) {
504
+ return new Promise((resolve, reject) => {
505
+ const reader = new FileReader();
506
+ reader.onload = () => resolve(reader.result);
507
+ reader.onerror = () => reject(reader.error);
508
+ reader.readAsText(file);
509
+ });
510
+ }
492
511
  function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
493
512
  if (typeof date == "number" || typeof date == "string") date = new Date(date);
494
513
  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")}`;
@@ -935,10 +954,10 @@ function fracToDec(frac) {
935
954
  split = split.pop().split("/");
936
955
  return whole + Number(split[0]) / Number(split[1]);
937
956
  }
938
- const LETTER_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
957
+ const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
939
958
  const NUMBER_LIST = "0123456789";
940
959
  const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
941
- const CHAR_LIST = LETTER_LIST + NUMBER_LIST + SYMBOL_LIST;
960
+ const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
942
961
  function formatBytes(bytes, decimals = 2) {
943
962
  if (bytes === 0) return "0 Bytes";
944
963
  const k = 1024;
@@ -1476,6 +1495,7 @@ export {
1476
1495
  ASet,
1477
1496
  BadGatewayError,
1478
1497
  BadRequestError,
1498
+ CHAR_LIST,
1479
1499
  Cache,
1480
1500
  CliBackground,
1481
1501
  CliEffects,
@@ -1487,9 +1507,11 @@ export {
1487
1507
  InternalServerError,
1488
1508
  JSONAttemptParse,
1489
1509
  JSONSanitize,
1510
+ LETTER_LIST,
1490
1511
  LOG_LEVEL,
1491
1512
  Logger,
1492
1513
  MethodNotAllowedError,
1514
+ NUMBER_LIST,
1493
1515
  NotAcceptableError,
1494
1516
  NotFoundError,
1495
1517
  NotImplementedError,
@@ -1500,6 +1522,7 @@ export {
1500
1522
  PathEventEmitter,
1501
1523
  PaymentRequiredError,
1502
1524
  PromiseProgress,
1525
+ SYMBOL_LIST,
1503
1526
  ServiceUnavailableError,
1504
1527
  TypedEmitter,
1505
1528
  UnauthorizedError,
@@ -1518,6 +1541,7 @@ export {
1518
1541
  errorFromCode,
1519
1542
  escapeRegex,
1520
1543
  fileBrowser,
1544
+ fileText,
1521
1545
  findByProp,
1522
1546
  flattenArr,
1523
1547
  flattenObj,
@@ -1526,6 +1550,7 @@ export {
1526
1550
  formatDate,
1527
1551
  formatPhoneNumber,
1528
1552
  fracToDec,
1553
+ fromCsv,
1529
1554
  gravatar,
1530
1555
  includes,
1531
1556
  insertAt,