@ztimson/utils 0.28.15 → 0.28.17

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
@@ -1,3 +1,4 @@
1
+ export * from 'var-persist';
1
2
  export * from './arg-parser';
2
3
  export * from './array';
3
4
  export * from './aset';
@@ -23,4 +24,4 @@ export * from './template';
23
24
  export * from './time';
24
25
  export * from './tts';
25
26
  export * from './types';
26
- export * from 'var-persist';
27
+ export * from './xml';
package/dist/index.mjs CHANGED
@@ -1,3 +1,190 @@
1
+ var dist = {};
2
+ var persist = {};
3
+ var hasRequiredPersist;
4
+ function requirePersist() {
5
+ if (hasRequiredPersist) return persist;
6
+ hasRequiredPersist = 1;
7
+ Object.defineProperty(persist, "__esModule", { value: true });
8
+ persist.persist = persist.Persist = void 0;
9
+ class Persist {
10
+ key;
11
+ options;
12
+ /** Backend service to store data, must implement `Storage` interface */
13
+ storage;
14
+ /** Listeners which should be notified on changes */
15
+ watches = {};
16
+ /** Private value field */
17
+ _value;
18
+ /** Current value or default if undefined */
19
+ get value() {
20
+ return this._value !== void 0 ? this._value : this.options?.default;
21
+ }
22
+ /** Set value with proxy object wrapper to sync future changes */
23
+ set value(v) {
24
+ if (v == null || typeof v != "object")
25
+ this._value = v;
26
+ else
27
+ this._value = new Proxy(v, {
28
+ get: (target, p) => {
29
+ const f = typeof target[p] == "function";
30
+ if (!f)
31
+ return target[p];
32
+ return (...args) => {
33
+ const value = target[p](...args);
34
+ this.save();
35
+ return value;
36
+ };
37
+ },
38
+ set: (target, p, newValue) => {
39
+ target[p] = newValue;
40
+ this.save();
41
+ return true;
42
+ }
43
+ });
44
+ this.save();
45
+ }
46
+ /**
47
+ * @param {string} key Primary key value will be stored under
48
+ * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
49
+ */
50
+ constructor(key, options = {}) {
51
+ this.key = key;
52
+ this.options = options;
53
+ this.storage = options.storage || localStorage;
54
+ this.load();
55
+ }
56
+ /** Notify listeners of change */
57
+ notify(value) {
58
+ Object.values(this.watches).forEach((watch) => watch(value));
59
+ }
60
+ /** Delete value from storage */
61
+ clear() {
62
+ this.storage.removeItem(this.key);
63
+ }
64
+ /** Save current value to storage */
65
+ save() {
66
+ if (this._value === void 0)
67
+ this.clear();
68
+ else
69
+ this.storage.setItem(this.key, JSON.stringify(this._value));
70
+ this.notify(this.value);
71
+ }
72
+ /** Load value from storage */
73
+ load() {
74
+ if (this.storage[this.key] != void 0) {
75
+ let value = JSON.parse(this.storage.getItem(this.key));
76
+ if (value != null && typeof value == "object" && this.options.type)
77
+ value.__proto__ = this.options.type.prototype;
78
+ this.value = value;
79
+ } else
80
+ this.value = this.options.default || void 0;
81
+ }
82
+ /**
83
+ * Callback function which is run when there are changes
84
+ *
85
+ * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
86
+ * @returns {() => void} Function which will unsubscribe the watch/callback when called
87
+ */
88
+ watch(fn2) {
89
+ const index = Object.keys(this.watches).length;
90
+ this.watches[index] = fn2;
91
+ return () => {
92
+ delete this.watches[index];
93
+ };
94
+ }
95
+ /**
96
+ * Return value as JSON string
97
+ *
98
+ * @returns {string} Stringified object as JSON
99
+ */
100
+ toString() {
101
+ return JSON.stringify(this.value);
102
+ }
103
+ /**
104
+ * Return current value
105
+ *
106
+ * @returns {T} Current value
107
+ */
108
+ valueOf() {
109
+ return this.value;
110
+ }
111
+ }
112
+ persist.Persist = Persist;
113
+ function persist$1(options) {
114
+ return (target, prop) => {
115
+ const key = options?.key || `${target.constructor.name}.${prop.toString()}`;
116
+ const wrapper = new Persist(key, options);
117
+ Object.defineProperty(target, prop, {
118
+ get: function() {
119
+ return wrapper.value;
120
+ },
121
+ set: function(v) {
122
+ wrapper.value = v;
123
+ }
124
+ });
125
+ };
126
+ }
127
+ persist.persist = persist$1;
128
+ return persist;
129
+ }
130
+ var memoryStorage = {};
131
+ var hasRequiredMemoryStorage;
132
+ function requireMemoryStorage() {
133
+ if (hasRequiredMemoryStorage) return memoryStorage;
134
+ hasRequiredMemoryStorage = 1;
135
+ Object.defineProperty(memoryStorage, "__esModule", { value: true });
136
+ memoryStorage.MemoryStorage = void 0;
137
+ class MemoryStorage {
138
+ get length() {
139
+ return Object.keys(this).length;
140
+ }
141
+ clear() {
142
+ Object.keys(this).forEach((k) => this.removeItem(k));
143
+ }
144
+ getItem(key) {
145
+ return this[key];
146
+ }
147
+ key(index) {
148
+ return Object.keys(this)[index];
149
+ }
150
+ removeItem(key) {
151
+ delete this[key];
152
+ }
153
+ setItem(key, value) {
154
+ this[key] = value;
155
+ }
156
+ }
157
+ memoryStorage.MemoryStorage = MemoryStorage;
158
+ return memoryStorage;
159
+ }
160
+ var hasRequiredDist;
161
+ function requireDist() {
162
+ if (hasRequiredDist) return dist;
163
+ hasRequiredDist = 1;
164
+ (function(exports$1) {
165
+ var __createBinding = dist && dist.__createBinding || (Object.create ? (function(o, m, k, k2) {
166
+ if (k2 === void 0) k2 = k;
167
+ var desc = Object.getOwnPropertyDescriptor(m, k);
168
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
169
+ desc = { enumerable: true, get: function() {
170
+ return m[k];
171
+ } };
172
+ }
173
+ Object.defineProperty(o, k2, desc);
174
+ }) : (function(o, m, k, k2) {
175
+ if (k2 === void 0) k2 = k;
176
+ o[k2] = m[k];
177
+ }));
178
+ var __exportStar = dist && dist.__exportStar || function(m, exports$12) {
179
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
180
+ };
181
+ Object.defineProperty(exports$1, "__esModule", { value: true });
182
+ __exportStar(requirePersist(), exports$1);
183
+ __exportStar(requireMemoryStorage(), exports$1);
184
+ })(dist);
185
+ return dist;
186
+ }
187
+ requireDist();
1
188
  class ArgParser {
2
189
  /**
3
190
  * Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
@@ -26,6 +213,10 @@ class ArgParser {
26
213
  `--help ${this.commands.length ? "[COMMAND]" : ""}`
27
214
  ].filter((e) => !!e);
28
215
  }
216
+ name;
217
+ desc;
218
+ argList;
219
+ examples;
29
220
  commands = [];
30
221
  args = [];
31
222
  flags = [];
@@ -489,6 +680,8 @@ class Cache {
489
680
  }
490
681
  });
491
682
  }
683
+ key;
684
+ options;
492
685
  _loading;
493
686
  store = /* @__PURE__ */ new Map();
494
687
  timers = /* @__PURE__ */ new Map();
@@ -769,6 +962,9 @@ function camelCase(str) {
769
962
  const pascal = pascalCase(str);
770
963
  return pascal.charAt(0).toLowerCase() + pascal.slice(1);
771
964
  }
965
+ function decodeHtml(html) {
966
+ return html.replace(/&nbsp;/g, " ").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&cent;/g, "¢").replace(/&pound;/g, "£").replace(/&yen;/g, "¥").replace(/&euro;/g, "€").replace(/&copy;/g, "©").replace(/&reg;/g, "®").replace(/&trade;/g, "™").replace(/&times;/g, "×").replace(/&divide;/g, "÷").replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec)).replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16))).replace(/&amp;/g, "&");
967
+ }
772
968
  function formatBytes(bytes, decimals = 2) {
773
969
  if (bytes === 0) return "0 Bytes";
774
970
  const k = 1024;
@@ -1271,6 +1467,8 @@ class Database {
1271
1467
  };
1272
1468
  });
1273
1469
  }
1470
+ database;
1471
+ version;
1274
1472
  schemaLock = new AsyncLock();
1275
1473
  upgrading = false;
1276
1474
  connection;
@@ -1322,6 +1520,9 @@ class Table {
1322
1520
  if (!exists) this.database.createTable(this.name);
1323
1521
  });
1324
1522
  }
1523
+ database;
1524
+ name;
1525
+ key;
1325
1526
  async tx(table, fn2, readonly = false) {
1326
1527
  await this.database.waitForUpgrade();
1327
1528
  const db = await this.database.connection;
@@ -1864,6 +2065,7 @@ class Logger extends TypedEmitter {
1864
2065
  super();
1865
2066
  this.namespace = namespace;
1866
2067
  }
2068
+ namespace;
1867
2069
  static LOG_LEVEL = 4;
1868
2070
  format(...text) {
1869
2071
  const now = /* @__PURE__ */ new Date();
@@ -2294,6 +2496,7 @@ class PathEventEmitter {
2294
2496
  constructor(prefix = "") {
2295
2497
  this.prefix = prefix;
2296
2498
  }
2499
+ prefix;
2297
2500
  listeners = [];
2298
2501
  emit(event, ...args) {
2299
2502
  const parsed = event instanceof PathEvent ? event : new PathEvent(`${this.prefix}/${event}`);
@@ -2806,193 +3009,114 @@ class TTS {
2806
3009
  };
2807
3010
  }
2808
3011
  }
2809
- var dist = {};
2810
- var persist = {};
2811
- var hasRequiredPersist;
2812
- function requirePersist() {
2813
- if (hasRequiredPersist) return persist;
2814
- hasRequiredPersist = 1;
2815
- Object.defineProperty(persist, "__esModule", { value: true });
2816
- persist.persist = persist.Persist = void 0;
2817
- class Persist {
2818
- key;
2819
- options;
2820
- /** Backend service to store data, must implement `Storage` interface */
2821
- storage;
2822
- /** Listeners which should be notified on changes */
2823
- watches = {};
2824
- /** Private value field */
2825
- _value;
2826
- /** Current value or default if undefined */
2827
- get value() {
2828
- return this._value !== void 0 ? this._value : this.options?.default;
2829
- }
2830
- /** Set value with proxy object wrapper to sync future changes */
2831
- set value(v) {
2832
- if (v == null || typeof v != "object")
2833
- this._value = v;
2834
- else
2835
- this._value = new Proxy(v, {
2836
- get: (target, p) => {
2837
- const f = typeof target[p] == "function";
2838
- if (!f)
2839
- return target[p];
2840
- return (...args) => {
2841
- const value = target[p](...args);
2842
- this.save();
2843
- return value;
2844
- };
2845
- },
2846
- set: (target, p, newValue) => {
2847
- target[p] = newValue;
2848
- this.save();
2849
- return true;
2850
- }
2851
- });
2852
- this.save();
2853
- }
2854
- /**
2855
- * @param {string} key Primary key value will be stored under
2856
- * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
2857
- */
2858
- constructor(key, options = {}) {
2859
- this.key = key;
2860
- this.options = options;
2861
- this.storage = options.storage || localStorage;
2862
- this.load();
2863
- }
2864
- /** Notify listeners of change */
2865
- notify(value) {
2866
- Object.values(this.watches).forEach((watch) => watch(value));
2867
- }
2868
- /** Delete value from storage */
2869
- clear() {
2870
- this.storage.removeItem(this.key);
2871
- }
2872
- /** Save current value to storage */
2873
- save() {
2874
- if (this._value === void 0)
2875
- this.clear();
2876
- else
2877
- this.storage.setItem(this.key, JSON.stringify(this._value));
2878
- this.notify(this.value);
2879
- }
2880
- /** Load value from storage */
2881
- load() {
2882
- if (this.storage[this.key] != void 0) {
2883
- let value = JSON.parse(this.storage.getItem(this.key));
2884
- if (value != null && typeof value == "object" && this.options.type)
2885
- value.__proto__ = this.options.type.prototype;
2886
- this.value = value;
2887
- } else
2888
- this.value = this.options.default || void 0;
2889
- }
2890
- /**
2891
- * Callback function which is run when there are changes
2892
- *
2893
- * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
2894
- * @returns {() => void} Function which will unsubscribe the watch/callback when called
2895
- */
2896
- watch(fn2) {
2897
- const index = Object.keys(this.watches).length;
2898
- this.watches[index] = fn2;
2899
- return () => {
2900
- delete this.watches[index];
2901
- };
2902
- }
2903
- /**
2904
- * Return value as JSON string
2905
- *
2906
- * @returns {string} Stringified object as JSON
2907
- */
2908
- toString() {
2909
- return JSON.stringify(this.value);
2910
- }
2911
- /**
2912
- * Return current value
2913
- *
2914
- * @returns {T} Current value
2915
- */
2916
- valueOf() {
2917
- return this.value;
3012
+ function fromXml(xml) {
3013
+ xml = xml.trim();
3014
+ let pos = 0;
3015
+ function parseNode() {
3016
+ skipWhitespace();
3017
+ if (xml[pos] !== "<") return parseText();
3018
+ pos++;
3019
+ if (xml[pos] === "?") {
3020
+ parseDeclaration();
3021
+ return parseNode();
3022
+ }
3023
+ if (xml[pos] === "!") {
3024
+ parseComment();
3025
+ return parseNode();
3026
+ }
3027
+ const tagName = parseTagName();
3028
+ const attributes = parseAttributes();
3029
+ skipWhitespace();
3030
+ if (xml[pos] === "/" && xml[pos + 1] === ">") {
3031
+ pos += 2;
3032
+ return { tag: tagName, attributes, children: [] };
3033
+ }
3034
+ pos++;
3035
+ const children = [];
3036
+ while (pos < xml.length) {
3037
+ skipWhitespace();
3038
+ if (xml[pos] === "<" && xml[pos + 1] === "/") {
3039
+ pos += 2;
3040
+ parseTagName();
3041
+ skipWhitespace();
3042
+ pos++;
3043
+ break;
3044
+ }
3045
+ const child = parseNode();
3046
+ if (child) children.push(child);
3047
+ }
3048
+ return { tag: tagName, attributes, children };
3049
+ }
3050
+ function parseTagName() {
3051
+ let name = "";
3052
+ while (pos < xml.length && /[a-zA-Z0-9_:-]/.test(xml[pos])) name += xml[pos++];
3053
+ return name;
3054
+ }
3055
+ function parseAttributes() {
3056
+ const attrs = {};
3057
+ while (pos < xml.length) {
3058
+ skipWhitespace();
3059
+ if (xml[pos] === ">" || xml[pos] === "/") break;
3060
+ const name = parseTagName();
3061
+ skipWhitespace();
3062
+ if (xml[pos] === "=") {
3063
+ pos++;
3064
+ skipWhitespace();
3065
+ const quote = xml[pos++];
3066
+ let value = "";
3067
+ while (xml[pos] !== quote) value += xml[pos++];
3068
+ pos++;
3069
+ attrs[name] = escapeXml(value, true);
3070
+ }
2918
3071
  }
3072
+ return attrs;
2919
3073
  }
2920
- persist.Persist = Persist;
2921
- function persist$1(options) {
2922
- return (target, prop) => {
2923
- const key = options?.key || `${target.constructor.name}.${prop.toString()}`;
2924
- const wrapper = new Persist(key, options);
2925
- Object.defineProperty(target, prop, {
2926
- get: function() {
2927
- return wrapper.value;
2928
- },
2929
- set: function(v) {
2930
- wrapper.value = v;
2931
- }
2932
- });
2933
- };
3074
+ function parseText() {
3075
+ let text = "";
3076
+ while (pos < xml.length && xml[pos] !== "<") text += xml[pos++];
3077
+ text = text.trim();
3078
+ return text ? escapeXml(text, true) : null;
2934
3079
  }
2935
- persist.persist = persist$1;
2936
- return persist;
3080
+ function parseDeclaration() {
3081
+ while (xml[pos] !== ">") pos++;
3082
+ pos++;
3083
+ }
3084
+ function parseComment() {
3085
+ while (!(xml[pos] === "-" && xml[pos + 1] === "-" && xml[pos + 2] === ">")) pos++;
3086
+ pos += 3;
3087
+ }
3088
+ function skipWhitespace() {
3089
+ while (pos < xml.length && /\s/.test(xml[pos])) pos++;
3090
+ }
3091
+ return parseNode();
2937
3092
  }
2938
- var memoryStorage = {};
2939
- var hasRequiredMemoryStorage;
2940
- function requireMemoryStorage() {
2941
- if (hasRequiredMemoryStorage) return memoryStorage;
2942
- hasRequiredMemoryStorage = 1;
2943
- Object.defineProperty(memoryStorage, "__esModule", { value: true });
2944
- memoryStorage.MemoryStorage = void 0;
2945
- class MemoryStorage {
2946
- get length() {
2947
- return Object.keys(this).length;
2948
- }
2949
- clear() {
2950
- Object.keys(this).forEach((k) => this.removeItem(k));
2951
- }
2952
- getItem(key) {
2953
- return this[key];
2954
- }
2955
- key(index) {
2956
- return Object.keys(this)[index];
2957
- }
2958
- removeItem(key) {
2959
- delete this[key];
2960
- }
2961
- setItem(key, value) {
2962
- this[key] = value;
2963
- }
3093
+ function toXml(obj, indent = "") {
3094
+ if (typeof obj === "string") return escapeXml(obj);
3095
+ const { tag, attributes = {}, children = [] } = obj;
3096
+ let xml = `${indent}<${tag}`;
3097
+ for (const [key, value] of Object.entries(attributes))
3098
+ xml += ` ${key}="${escapeXml(value)}"`;
3099
+ if (children.length === 0) {
3100
+ xml += " />";
3101
+ return xml;
2964
3102
  }
2965
- memoryStorage.MemoryStorage = MemoryStorage;
2966
- return memoryStorage;
3103
+ xml += ">";
3104
+ const hasComplexChildren = children.some((c) => typeof c === "object");
3105
+ for (const child of children) {
3106
+ if (hasComplexChildren) xml += "\n";
3107
+ xml += toXml(child, hasComplexChildren ? indent + " " : "");
3108
+ }
3109
+ if (hasComplexChildren) xml += `
3110
+ ${indent}`;
3111
+ xml += `</${tag}>`;
3112
+ return xml;
2967
3113
  }
2968
- var hasRequiredDist;
2969
- function requireDist() {
2970
- if (hasRequiredDist) return dist;
2971
- hasRequiredDist = 1;
2972
- (function(exports$1) {
2973
- var __createBinding = dist && dist.__createBinding || (Object.create ? (function(o, m, k, k2) {
2974
- if (k2 === void 0) k2 = k;
2975
- var desc = Object.getOwnPropertyDescriptor(m, k);
2976
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
2977
- desc = { enumerable: true, get: function() {
2978
- return m[k];
2979
- } };
2980
- }
2981
- Object.defineProperty(o, k2, desc);
2982
- }) : (function(o, m, k, k2) {
2983
- if (k2 === void 0) k2 = k;
2984
- o[k2] = m[k];
2985
- }));
2986
- var __exportStar = dist && dist.__exportStar || function(m, exports$12) {
2987
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
2988
- };
2989
- Object.defineProperty(exports$1, "__esModule", { value: true });
2990
- __exportStar(requirePersist(), exports$1);
2991
- __exportStar(requireMemoryStorage(), exports$1);
2992
- })(dist);
2993
- return dist;
3114
+ function escapeXml(str, decode = false) {
3115
+ if (decode) {
3116
+ return str.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&amp;/g, "&");
3117
+ }
3118
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
2994
3119
  }
2995
- requireDist();
2996
3120
  export {
2997
3121
  ASet,
2998
3122
  ArgParser,
@@ -3051,6 +3175,7 @@ export {
3051
3175
  dayOfYear,
3052
3176
  dec2Frac,
3053
3177
  dec2Hex,
3178
+ decodeHtml,
3054
3179
  decodeJwt,
3055
3180
  deepCopy,
3056
3181
  deepMerge,
@@ -3060,6 +3185,7 @@ export {
3060
3185
  encodeQuery,
3061
3186
  errorFromCode,
3062
3187
  escapeRegex,
3188
+ escapeXml,
3063
3189
  fileBrowser,
3064
3190
  fileText,
3065
3191
  findByProp,
@@ -3074,6 +3200,7 @@ export {
3074
3200
  formatPhoneNumber,
3075
3201
  frac2Dec,
3076
3202
  fromCsv,
3203
+ fromXml,
3077
3204
  gravatar,
3078
3205
  hex2Int,
3079
3206
  hue2rgb,
@@ -3114,6 +3241,7 @@ export {
3114
3241
  timestampFilename,
3115
3242
  timezoneOffset,
3116
3243
  toCsv,
3244
+ toXml,
3117
3245
  uploadWithProgress,
3118
3246
  validateEmail,
3119
3247
  wordSegments