@ztimson/utils 0.24.3 → 0.24.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/aset.d.ts CHANGED
@@ -15,6 +15,10 @@ export declare class ASet<T> extends Array {
15
15
  * @param items
16
16
  */
17
17
  add(...items: T[]): this;
18
+ /**
19
+ * Remove all elements
20
+ */
21
+ clear(): this;
18
22
  /**
19
23
  * Delete elements from set
20
24
  * @param items Elements that will be deleted
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.utils = {}));
1
+ (function(global2, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, factory(global2.utils = {}));
3
3
  })(this, function(exports2) {
4
4
  "use strict";var __defProp = Object.defineProperty;
5
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -302,6 +302,13 @@ ${opts.message || this.desc}`;
302
302
  items.filter((el) => !this.has(el)).forEach((el) => this.push(el));
303
303
  return this;
304
304
  }
305
+ /**
306
+ * Remove all elements
307
+ */
308
+ clear() {
309
+ this.splice(0, this.length);
310
+ return this;
311
+ }
305
312
  /**
306
313
  * Delete elements from set
307
314
  * @param items Elements that will be deleted
@@ -1457,7 +1464,7 @@ ${opts.message || this.desc}`;
1457
1464
  class PathError extends Error {
1458
1465
  }
1459
1466
  class PathEvent {
1460
- constructor(Event) {
1467
+ constructor(e) {
1461
1468
  /** First directory in path */
1462
1469
  __publicField(this, "module");
1463
1470
  /** Entire path, including the module & name */
@@ -1469,8 +1476,8 @@ ${opts.message || this.desc}`;
1469
1476
  /** List of methods */
1470
1477
  __publicField(this, "methods");
1471
1478
  var _a;
1472
- if (typeof Event == "object") return Object.assign(this, Event);
1473
- let [p, scope, method] = Event.replaceAll(/\/{2,}/g, "/").split(":");
1479
+ if (typeof e == "object") return Object.assign(this, e);
1480
+ let [p, scope, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
1474
1481
  if (!method) method = scope || "*";
1475
1482
  if (p == "*" || !p && method == "*") {
1476
1483
  p = "";
@@ -1478,8 +1485,8 @@ ${opts.message || this.desc}`;
1478
1485
  }
1479
1486
  let temp = p.split("/").filter((p2) => !!p2);
1480
1487
  this.module = ((_a = temp.splice(0, 1)[0]) == null ? void 0 : _a.toLowerCase()) || "";
1481
- this.fullPath = p;
1482
1488
  this.path = temp.join("/");
1489
+ this.fullPath = `${this.module}${this.module && this.path ? "/" : ""}${this.path}`;
1483
1490
  this.name = temp.pop() || "";
1484
1491
  this.methods = new ASet(method.split(""));
1485
1492
  }
@@ -1526,7 +1533,7 @@ ${opts.message || this.desc}`;
1526
1533
  v ? this.methods.delete("n").delete("*").add("d") : this.methods.delete("d");
1527
1534
  }
1528
1535
  /**
1529
- * Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
1536
+ * Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
1530
1537
  * combined until a "none" is reached
1531
1538
  *
1532
1539
  * @param {string | PathEvent} paths Events as strings or pre-parsed
@@ -1609,14 +1616,14 @@ ${opts.message || this.desc}`;
1609
1616
  if (!PathEvent.hasAll(target, ...has)) throw new PathError(`Requires all: ${makeArray(has).join(", ")}`);
1610
1617
  }
1611
1618
  /**
1612
- * Create event string from its components
1619
+ * Create event string from its components
1613
1620
  *
1614
1621
  * @param {string | string[]} path Event path
1615
1622
  * @param {Method} methods Event method
1616
1623
  * @return {string} String representation of Event
1617
1624
  */
1618
1625
  static toString(path, methods) {
1619
- let p = makeArray(path).filter((p2) => p2 != null).join("/");
1626
+ let p = makeArray(path).filter((p2) => !!p2).join("/");
1620
1627
  p = p == null ? void 0 : p.trim().replaceAll(/\/{2,}/g, "/").replaceAll(/(^\/|\/$)/g, "");
1621
1628
  if (methods == null ? void 0 : methods.length) p += `:${makeArray(methods).map((m) => m.toLowerCase()).join("")}`;
1622
1629
  return p;
@@ -1723,6 +1730,176 @@ ${opts.message || this.desc}`;
1723
1730
  function typeKeys() {
1724
1731
  return Object.keys({});
1725
1732
  }
1733
+ var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
1734
+ var dist = {};
1735
+ var persist$1 = {};
1736
+ Object.defineProperty(persist$1, "__esModule", { value: true });
1737
+ persist$1.persist = persist$1.Persist = void 0;
1738
+ class Persist {
1739
+ /**
1740
+ * @param {string} key Primary key value will be stored under
1741
+ * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
1742
+ */
1743
+ constructor(key, options = {}) {
1744
+ __publicField(this, "key");
1745
+ __publicField(this, "options");
1746
+ /** Backend service to store data, must implement `Storage` interface */
1747
+ __publicField(this, "storage");
1748
+ /** Listeners which should be notified on changes */
1749
+ __publicField(this, "watches", {});
1750
+ /** Private value field */
1751
+ __publicField(this, "_value");
1752
+ this.key = key;
1753
+ this.options = options;
1754
+ this.storage = options.storage || localStorage;
1755
+ this.load();
1756
+ }
1757
+ /** Current value or default if undefined */
1758
+ get value() {
1759
+ var _a;
1760
+ return this._value !== void 0 ? this._value : (_a = this.options) == null ? void 0 : _a.default;
1761
+ }
1762
+ /** Set value with proxy object wrapper to sync future changes */
1763
+ set value(v) {
1764
+ if (v == null || typeof v != "object")
1765
+ this._value = v;
1766
+ else
1767
+ this._value = new Proxy(v, {
1768
+ get: (target, p) => {
1769
+ const f = typeof target[p] == "function";
1770
+ if (!f)
1771
+ return target[p];
1772
+ return (...args) => {
1773
+ const value = target[p](...args);
1774
+ this.save();
1775
+ return value;
1776
+ };
1777
+ },
1778
+ set: (target, p, newValue) => {
1779
+ target[p] = newValue;
1780
+ this.save();
1781
+ return true;
1782
+ }
1783
+ });
1784
+ this.save();
1785
+ }
1786
+ /** Notify listeners of change */
1787
+ notify(value) {
1788
+ Object.values(this.watches).forEach((watch) => watch(value));
1789
+ }
1790
+ /** Delete value from storage */
1791
+ clear() {
1792
+ this.storage.removeItem(this.key);
1793
+ }
1794
+ /** Save current value to storage */
1795
+ save() {
1796
+ if (this._value === void 0)
1797
+ this.clear();
1798
+ else
1799
+ this.storage.setItem(this.key, JSON.stringify(this._value));
1800
+ this.notify(this.value);
1801
+ }
1802
+ /** Load value from storage */
1803
+ load() {
1804
+ if (this.storage[this.key] != void 0) {
1805
+ let value = JSON.parse(this.storage.getItem(this.key));
1806
+ if (value != null && typeof value == "object" && this.options.type)
1807
+ value.__proto__ = this.options.type.prototype;
1808
+ this.value = value;
1809
+ } else
1810
+ this.value = this.options.default || void 0;
1811
+ }
1812
+ /**
1813
+ * Callback function which is run when there are changes
1814
+ *
1815
+ * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
1816
+ * @returns {() => void} Function which will unsubscribe the watch/callback when called
1817
+ */
1818
+ watch(fn2) {
1819
+ const index = Object.keys(this.watches).length;
1820
+ this.watches[index] = fn2;
1821
+ return () => {
1822
+ delete this.watches[index];
1823
+ };
1824
+ }
1825
+ /**
1826
+ * Return value as JSON string
1827
+ *
1828
+ * @returns {string} Stringified object as JSON
1829
+ */
1830
+ toString() {
1831
+ return JSON.stringify(this.value);
1832
+ }
1833
+ /**
1834
+ * Return current value
1835
+ *
1836
+ * @returns {T} Current value
1837
+ */
1838
+ valueOf() {
1839
+ return this.value;
1840
+ }
1841
+ }
1842
+ persist$1.Persist = Persist;
1843
+ function persist(options) {
1844
+ return (target, prop) => {
1845
+ const key = (options == null ? void 0 : options.key) || `${target.constructor.name}.${prop.toString()}`;
1846
+ const wrapper = new Persist(key, options);
1847
+ Object.defineProperty(target, prop, {
1848
+ get: function() {
1849
+ return wrapper.value;
1850
+ },
1851
+ set: function(v) {
1852
+ wrapper.value = v;
1853
+ }
1854
+ });
1855
+ };
1856
+ }
1857
+ persist$1.persist = persist;
1858
+ var memoryStorage = {};
1859
+ Object.defineProperty(memoryStorage, "__esModule", { value: true });
1860
+ memoryStorage.MemoryStorage = void 0;
1861
+ class MemoryStorage {
1862
+ get length() {
1863
+ return Object.keys(this).length;
1864
+ }
1865
+ clear() {
1866
+ Object.keys(this).forEach((k) => this.removeItem(k));
1867
+ }
1868
+ getItem(key) {
1869
+ return this[key];
1870
+ }
1871
+ key(index) {
1872
+ return Object.keys(this)[index];
1873
+ }
1874
+ removeItem(key) {
1875
+ delete this[key];
1876
+ }
1877
+ setItem(key, value) {
1878
+ this[key] = value;
1879
+ }
1880
+ }
1881
+ memoryStorage.MemoryStorage = MemoryStorage;
1882
+ (function(exports3) {
1883
+ var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
1884
+ if (k2 === void 0) k2 = k;
1885
+ var desc = Object.getOwnPropertyDescriptor(m, k);
1886
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
1887
+ desc = { enumerable: true, get: function() {
1888
+ return m[k];
1889
+ } };
1890
+ }
1891
+ Object.defineProperty(o, k2, desc);
1892
+ } : function(o, m, k, k2) {
1893
+ if (k2 === void 0) k2 = k;
1894
+ o[k2] = m[k];
1895
+ });
1896
+ var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports4) {
1897
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports4, p)) __createBinding(exports4, m, p);
1898
+ };
1899
+ Object.defineProperty(exports3, "__esModule", { value: true });
1900
+ __exportStar(persist$1, exports3);
1901
+ __exportStar(memoryStorage, exports3);
1902
+ })(dist);
1726
1903
  exports2.ASet = ASet;
1727
1904
  exports2.ArgParser = ArgParser;
1728
1905
  exports2.BadGatewayError = BadGatewayError;