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