@ztimson/utils 0.24.3 → 0.24.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/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;
@@ -1723,6 +1723,176 @@ ${opts.message || this.desc}`;
1723
1723
  function typeKeys() {
1724
1724
  return Object.keys({});
1725
1725
  }
1726
+ var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
1727
+ var dist = {};
1728
+ var persist$1 = {};
1729
+ Object.defineProperty(persist$1, "__esModule", { value: true });
1730
+ persist$1.persist = persist$1.Persist = void 0;
1731
+ class Persist {
1732
+ /**
1733
+ * @param {string} key Primary key value will be stored under
1734
+ * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
1735
+ */
1736
+ constructor(key, options = {}) {
1737
+ __publicField(this, "key");
1738
+ __publicField(this, "options");
1739
+ /** Backend service to store data, must implement `Storage` interface */
1740
+ __publicField(this, "storage");
1741
+ /** Listeners which should be notified on changes */
1742
+ __publicField(this, "watches", {});
1743
+ /** Private value field */
1744
+ __publicField(this, "_value");
1745
+ this.key = key;
1746
+ this.options = options;
1747
+ this.storage = options.storage || localStorage;
1748
+ this.load();
1749
+ }
1750
+ /** Current value or default if undefined */
1751
+ get value() {
1752
+ var _a;
1753
+ return this._value !== void 0 ? this._value : (_a = this.options) == null ? void 0 : _a.default;
1754
+ }
1755
+ /** Set value with proxy object wrapper to sync future changes */
1756
+ set value(v) {
1757
+ if (v == null || typeof v != "object")
1758
+ this._value = v;
1759
+ else
1760
+ this._value = new Proxy(v, {
1761
+ get: (target, p) => {
1762
+ const f = typeof target[p] == "function";
1763
+ if (!f)
1764
+ return target[p];
1765
+ return (...args) => {
1766
+ const value = target[p](...args);
1767
+ this.save();
1768
+ return value;
1769
+ };
1770
+ },
1771
+ set: (target, p, newValue) => {
1772
+ target[p] = newValue;
1773
+ this.save();
1774
+ return true;
1775
+ }
1776
+ });
1777
+ this.save();
1778
+ }
1779
+ /** Notify listeners of change */
1780
+ notify(value) {
1781
+ Object.values(this.watches).forEach((watch) => watch(value));
1782
+ }
1783
+ /** Delete value from storage */
1784
+ clear() {
1785
+ this.storage.removeItem(this.key);
1786
+ }
1787
+ /** Save current value to storage */
1788
+ save() {
1789
+ if (this._value === void 0)
1790
+ this.clear();
1791
+ else
1792
+ this.storage.setItem(this.key, JSON.stringify(this._value));
1793
+ this.notify(this.value);
1794
+ }
1795
+ /** Load value from storage */
1796
+ load() {
1797
+ if (this.storage[this.key] != void 0) {
1798
+ let value = JSON.parse(this.storage.getItem(this.key));
1799
+ if (value != null && typeof value == "object" && this.options.type)
1800
+ value.__proto__ = this.options.type.prototype;
1801
+ this.value = value;
1802
+ } else
1803
+ this.value = this.options.default || void 0;
1804
+ }
1805
+ /**
1806
+ * Callback function which is run when there are changes
1807
+ *
1808
+ * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
1809
+ * @returns {() => void} Function which will unsubscribe the watch/callback when called
1810
+ */
1811
+ watch(fn2) {
1812
+ const index = Object.keys(this.watches).length;
1813
+ this.watches[index] = fn2;
1814
+ return () => {
1815
+ delete this.watches[index];
1816
+ };
1817
+ }
1818
+ /**
1819
+ * Return value as JSON string
1820
+ *
1821
+ * @returns {string} Stringified object as JSON
1822
+ */
1823
+ toString() {
1824
+ return JSON.stringify(this.value);
1825
+ }
1826
+ /**
1827
+ * Return current value
1828
+ *
1829
+ * @returns {T} Current value
1830
+ */
1831
+ valueOf() {
1832
+ return this.value;
1833
+ }
1834
+ }
1835
+ persist$1.Persist = Persist;
1836
+ function persist(options) {
1837
+ return (target, prop) => {
1838
+ const key = (options == null ? void 0 : options.key) || `${target.constructor.name}.${prop.toString()}`;
1839
+ const wrapper = new Persist(key, options);
1840
+ Object.defineProperty(target, prop, {
1841
+ get: function() {
1842
+ return wrapper.value;
1843
+ },
1844
+ set: function(v) {
1845
+ wrapper.value = v;
1846
+ }
1847
+ });
1848
+ };
1849
+ }
1850
+ persist$1.persist = persist;
1851
+ var memoryStorage = {};
1852
+ Object.defineProperty(memoryStorage, "__esModule", { value: true });
1853
+ memoryStorage.MemoryStorage = void 0;
1854
+ class MemoryStorage {
1855
+ get length() {
1856
+ return Object.keys(this).length;
1857
+ }
1858
+ clear() {
1859
+ Object.keys(this).forEach((k) => this.removeItem(k));
1860
+ }
1861
+ getItem(key) {
1862
+ return this[key];
1863
+ }
1864
+ key(index) {
1865
+ return Object.keys(this)[index];
1866
+ }
1867
+ removeItem(key) {
1868
+ delete this[key];
1869
+ }
1870
+ setItem(key, value) {
1871
+ this[key] = value;
1872
+ }
1873
+ }
1874
+ memoryStorage.MemoryStorage = MemoryStorage;
1875
+ (function(exports3) {
1876
+ var __createBinding = commonjsGlobal && commonjsGlobal.__createBinding || (Object.create ? function(o, m, k, k2) {
1877
+ if (k2 === void 0) k2 = k;
1878
+ var desc = Object.getOwnPropertyDescriptor(m, k);
1879
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
1880
+ desc = { enumerable: true, get: function() {
1881
+ return m[k];
1882
+ } };
1883
+ }
1884
+ Object.defineProperty(o, k2, desc);
1885
+ } : function(o, m, k, k2) {
1886
+ if (k2 === void 0) k2 = k;
1887
+ o[k2] = m[k];
1888
+ });
1889
+ var __exportStar = commonjsGlobal && commonjsGlobal.__exportStar || function(m, exports4) {
1890
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports4, p)) __createBinding(exports4, m, p);
1891
+ };
1892
+ Object.defineProperty(exports3, "__esModule", { value: true });
1893
+ __exportStar(persist$1, exports3);
1894
+ __exportStar(memoryStorage, exports3);
1895
+ })(dist);
1726
1896
  exports2.ASet = ASet;
1727
1897
  exports2.ArgParser = ArgParser;
1728
1898
  exports2.BadGatewayError = BadGatewayError;