@ztimson/utils 0.28.16 → 0.29.0

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();
@@ -1274,6 +1467,8 @@ class Database {
1274
1467
  };
1275
1468
  });
1276
1469
  }
1470
+ database;
1471
+ version;
1277
1472
  schemaLock = new AsyncLock();
1278
1473
  upgrading = false;
1279
1474
  connection;
@@ -1325,6 +1520,9 @@ class Table {
1325
1520
  if (!exists) this.database.createTable(this.name);
1326
1521
  });
1327
1522
  }
1523
+ database;
1524
+ name;
1525
+ key;
1328
1526
  async tx(table, fn2, readonly = false) {
1329
1527
  await this.database.waitForUpgrade();
1330
1528
  const db = await this.database.connection;
@@ -1867,6 +2065,7 @@ class Logger extends TypedEmitter {
1867
2065
  super();
1868
2066
  this.namespace = namespace;
1869
2067
  }
2068
+ namespace;
1870
2069
  static LOG_LEVEL = 4;
1871
2070
  format(...text) {
1872
2071
  const now = /* @__PURE__ */ new Date();
@@ -2297,6 +2496,7 @@ class PathEventEmitter {
2297
2496
  constructor(prefix = "") {
2298
2497
  this.prefix = prefix;
2299
2498
  }
2499
+ prefix;
2300
2500
  listeners = [];
2301
2501
  emit(event, ...args) {
2302
2502
  const parsed = event instanceof PathEvent ? event : new PathEvent(`${this.prefix}/${event}`);
@@ -2809,193 +3009,114 @@ class TTS {
2809
3009
  };
2810
3010
  }
2811
3011
  }
2812
- var dist = {};
2813
- var persist = {};
2814
- var hasRequiredPersist;
2815
- function requirePersist() {
2816
- if (hasRequiredPersist) return persist;
2817
- hasRequiredPersist = 1;
2818
- Object.defineProperty(persist, "__esModule", { value: true });
2819
- persist.persist = persist.Persist = void 0;
2820
- class Persist {
2821
- key;
2822
- options;
2823
- /** Backend service to store data, must implement `Storage` interface */
2824
- storage;
2825
- /** Listeners which should be notified on changes */
2826
- watches = {};
2827
- /** Private value field */
2828
- _value;
2829
- /** Current value or default if undefined */
2830
- get value() {
2831
- return this._value !== void 0 ? this._value : this.options?.default;
2832
- }
2833
- /** Set value with proxy object wrapper to sync future changes */
2834
- set value(v) {
2835
- if (v == null || typeof v != "object")
2836
- this._value = v;
2837
- else
2838
- this._value = new Proxy(v, {
2839
- get: (target, p) => {
2840
- const f = typeof target[p] == "function";
2841
- if (!f)
2842
- return target[p];
2843
- return (...args) => {
2844
- const value = target[p](...args);
2845
- this.save();
2846
- return value;
2847
- };
2848
- },
2849
- set: (target, p, newValue) => {
2850
- target[p] = newValue;
2851
- this.save();
2852
- return true;
2853
- }
2854
- });
2855
- this.save();
2856
- }
2857
- /**
2858
- * @param {string} key Primary key value will be stored under
2859
- * @param {PersistOptions<T>} options Configure using {@link PersistOptions}
2860
- */
2861
- constructor(key, options = {}) {
2862
- this.key = key;
2863
- this.options = options;
2864
- this.storage = options.storage || localStorage;
2865
- this.load();
2866
- }
2867
- /** Notify listeners of change */
2868
- notify(value) {
2869
- Object.values(this.watches).forEach((watch) => watch(value));
2870
- }
2871
- /** Delete value from storage */
2872
- clear() {
2873
- this.storage.removeItem(this.key);
2874
- }
2875
- /** Save current value to storage */
2876
- save() {
2877
- if (this._value === void 0)
2878
- this.clear();
2879
- else
2880
- this.storage.setItem(this.key, JSON.stringify(this._value));
2881
- this.notify(this.value);
2882
- }
2883
- /** Load value from storage */
2884
- load() {
2885
- if (this.storage[this.key] != void 0) {
2886
- let value = JSON.parse(this.storage.getItem(this.key));
2887
- if (value != null && typeof value == "object" && this.options.type)
2888
- value.__proto__ = this.options.type.prototype;
2889
- this.value = value;
2890
- } else
2891
- this.value = this.options.default || void 0;
2892
- }
2893
- /**
2894
- * Callback function which is run when there are changes
2895
- *
2896
- * @param {(value: T) => any} fn Callback will run on each change; it's passed the next value & it's return is ignored
2897
- * @returns {() => void} Function which will unsubscribe the watch/callback when called
2898
- */
2899
- watch(fn2) {
2900
- const index = Object.keys(this.watches).length;
2901
- this.watches[index] = fn2;
2902
- return () => {
2903
- delete this.watches[index];
2904
- };
2905
- }
2906
- /**
2907
- * Return value as JSON string
2908
- *
2909
- * @returns {string} Stringified object as JSON
2910
- */
2911
- toString() {
2912
- return JSON.stringify(this.value);
2913
- }
2914
- /**
2915
- * Return current value
2916
- *
2917
- * @returns {T} Current value
2918
- */
2919
- valueOf() {
2920
- 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
+ }
2921
3071
  }
3072
+ return attrs;
2922
3073
  }
2923
- persist.Persist = Persist;
2924
- function persist$1(options) {
2925
- return (target, prop) => {
2926
- const key = options?.key || `${target.constructor.name}.${prop.toString()}`;
2927
- const wrapper = new Persist(key, options);
2928
- Object.defineProperty(target, prop, {
2929
- get: function() {
2930
- return wrapper.value;
2931
- },
2932
- set: function(v) {
2933
- wrapper.value = v;
2934
- }
2935
- });
2936
- };
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;
2937
3079
  }
2938
- persist.persist = persist$1;
2939
- 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();
2940
3092
  }
2941
- var memoryStorage = {};
2942
- var hasRequiredMemoryStorage;
2943
- function requireMemoryStorage() {
2944
- if (hasRequiredMemoryStorage) return memoryStorage;
2945
- hasRequiredMemoryStorage = 1;
2946
- Object.defineProperty(memoryStorage, "__esModule", { value: true });
2947
- memoryStorage.MemoryStorage = void 0;
2948
- class MemoryStorage {
2949
- get length() {
2950
- return Object.keys(this).length;
2951
- }
2952
- clear() {
2953
- Object.keys(this).forEach((k) => this.removeItem(k));
2954
- }
2955
- getItem(key) {
2956
- return this[key];
2957
- }
2958
- key(index) {
2959
- return Object.keys(this)[index];
2960
- }
2961
- removeItem(key) {
2962
- delete this[key];
2963
- }
2964
- setItem(key, value) {
2965
- this[key] = value;
2966
- }
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;
2967
3102
  }
2968
- memoryStorage.MemoryStorage = MemoryStorage;
2969
- 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;
2970
3113
  }
2971
- var hasRequiredDist;
2972
- function requireDist() {
2973
- if (hasRequiredDist) return dist;
2974
- hasRequiredDist = 1;
2975
- (function(exports$1) {
2976
- var __createBinding = dist && dist.__createBinding || (Object.create ? (function(o, m, k, k2) {
2977
- if (k2 === void 0) k2 = k;
2978
- var desc = Object.getOwnPropertyDescriptor(m, k);
2979
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
2980
- desc = { enumerable: true, get: function() {
2981
- return m[k];
2982
- } };
2983
- }
2984
- Object.defineProperty(o, k2, desc);
2985
- }) : (function(o, m, k, k2) {
2986
- if (k2 === void 0) k2 = k;
2987
- o[k2] = m[k];
2988
- }));
2989
- var __exportStar = dist && dist.__exportStar || function(m, exports$12) {
2990
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$12, p)) __createBinding(exports$12, m, p);
2991
- };
2992
- Object.defineProperty(exports$1, "__esModule", { value: true });
2993
- __exportStar(requirePersist(), exports$1);
2994
- __exportStar(requireMemoryStorage(), exports$1);
2995
- })(dist);
2996
- 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;");
2997
3119
  }
2998
- requireDist();
2999
3120
  export {
3000
3121
  ASet,
3001
3122
  ArgParser,
@@ -3064,6 +3185,7 @@ export {
3064
3185
  encodeQuery,
3065
3186
  errorFromCode,
3066
3187
  escapeRegex,
3188
+ escapeXml,
3067
3189
  fileBrowser,
3068
3190
  fileText,
3069
3191
  findByProp,
@@ -3078,6 +3200,7 @@ export {
3078
3200
  formatPhoneNumber,
3079
3201
  frac2Dec,
3080
3202
  fromCsv,
3203
+ fromXml,
3081
3204
  gravatar,
3082
3205
  hex2Int,
3083
3206
  hue2rgb,
@@ -3118,6 +3241,7 @@ export {
3118
3241
  timestampFilename,
3119
3242
  timezoneOffset,
3120
3243
  toCsv,
3244
+ toXml,
3121
3245
  uploadWithProgress,
3122
3246
  validateEmail,
3123
3247
  wordSegments