@vpmedia/simplify 1.74.0 → 1.76.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/CHANGELOG.md +75 -0
- package/dist/const/http_status.d.ts +66 -0
- package/dist/const/http_status.d.ts.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1119 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/AbstractLogHandler.d.ts +17 -0
- package/dist/logging/AbstractLogHandler.d.ts.map +1 -0
- package/dist/logging/ConsoleLogHandler.d.ts +13 -0
- package/dist/logging/ConsoleLogHandler.d.ts.map +1 -0
- package/dist/logging/Logger.d.ts +19 -0
- package/dist/logging/Logger.d.ts.map +1 -0
- package/dist/logging/OpenTelemetryLogHandler.d.ts +15 -0
- package/dist/logging/OpenTelemetryLogHandler.d.ts.map +1 -0
- package/dist/logging/SentryLogHandler.d.ts +13 -0
- package/dist/logging/SentryLogHandler.d.ts.map +1 -0
- package/dist/logging/const.d.ts +14 -0
- package/dist/logging/const.d.ts.map +1 -0
- package/dist/logging/util.d.ts +14 -0
- package/dist/logging/util.d.ts.map +1 -0
- package/dist/pagelifecycle/const.d.ts +15 -0
- package/dist/pagelifecycle/const.d.ts.map +1 -0
- package/dist/pagelifecycle/typedef.d.ts +4 -0
- package/dist/pagelifecycle/typedef.d.ts.map +1 -0
- package/dist/pagelifecycle/util.d.ts +31 -0
- package/dist/pagelifecycle/util.d.ts.map +1 -0
- package/dist/typecheck/TypeCheckError.d.ts +11 -0
- package/dist/typecheck/TypeCheckError.d.ts.map +1 -0
- package/dist/typecheck/TypeChecker.d.ts +27 -0
- package/dist/typecheck/TypeChecker.d.ts.map +1 -0
- package/dist/typecheck/util.d.ts +17 -0
- package/dist/typecheck/util.d.ts.map +1 -0
- package/dist/util/async.d.ts +13 -0
- package/dist/util/async.d.ts.map +1 -0
- package/dist/util/error.d.ts +16 -0
- package/dist/util/error.d.ts.map +1 -0
- package/dist/util/event_emitter.d.ts +42 -0
- package/dist/util/event_emitter.d.ts.map +1 -0
- package/dist/util/fetch.d.ts +22 -0
- package/dist/util/fetch.d.ts.map +1 -0
- package/dist/util/number.d.ts +23 -0
- package/dist/util/number.d.ts.map +1 -0
- package/dist/util/object.d.ts +24 -0
- package/dist/util/object.d.ts.map +1 -0
- package/dist/util/query.d.ts +11 -0
- package/dist/util/query.d.ts.map +1 -0
- package/dist/util/state.d.ts +5 -0
- package/dist/util/state.d.ts.map +1 -0
- package/dist/util/string.d.ts +25 -0
- package/dist/util/string.d.ts.map +1 -0
- package/dist/util/uuid.d.ts +13 -0
- package/dist/util/uuid.d.ts.map +1 -0
- package/dist/util/validate.d.ts +106 -0
- package/dist/util/validate.d.ts.map +1 -0
- package/package.json +32 -16
- package/src/const/http_status.test.ts +7 -0
- package/src/const/{http_status.js → http_status.ts} +1 -1
- package/src/{index.js → index.ts} +8 -0
- package/src/logging/AbstractLogHandler.ts +31 -0
- package/src/logging/{ConsoleLogHandler.js → ConsoleLogHandler.ts} +15 -13
- package/src/logging/Logger.test.ts +69 -0
- package/src/logging/Logger.ts +77 -0
- package/src/logging/OpenTelemetryLogHandler.ts +40 -0
- package/src/logging/SentryLogHandler.ts +44 -0
- package/src/logging/{const.js → const.ts} +1 -1
- package/src/logging/util.test.ts +33 -0
- package/src/logging/util.ts +36 -0
- package/src/pagelifecycle/{const.js → const.ts} +2 -2
- package/src/pagelifecycle/typedef.ts +5 -0
- package/src/pagelifecycle/util.test.ts +99 -0
- package/src/pagelifecycle/{util.js → util.ts} +14 -27
- package/src/typecheck/{TypeCheckError.js → TypeCheckError.ts} +7 -3
- package/src/typecheck/TypeChecker.test.ts +70 -0
- package/src/typecheck/{TypeChecker.js → TypeChecker.ts} +10 -27
- package/src/typecheck/util.test.ts +36 -0
- package/src/typecheck/util.ts +50 -0
- package/src/util/async.test.ts +74 -0
- package/src/util/{async.js → async.ts} +3 -12
- package/src/util/error.test.ts +32 -0
- package/src/util/error.ts +37 -0
- package/src/util/event_emitter.test.ts +228 -0
- package/src/util/event_emitter.ts +147 -0
- package/src/util/fetch.test.ts +62 -0
- package/src/util/{fetch.js → fetch.ts} +40 -31
- package/src/util/number.test.ts +124 -0
- package/src/util/number.ts +58 -0
- package/src/util/object.test.ts +203 -0
- package/src/util/{object.js → object.ts} +14 -21
- package/src/util/query.test.ts +71 -0
- package/src/util/query.ts +35 -0
- package/src/util/state.test.ts +47 -0
- package/src/util/{state.js → state.ts} +3 -6
- package/src/util/string.test.ts +64 -0
- package/src/util/string.ts +65 -0
- package/src/util/uuid.test.ts +53 -0
- package/src/util/uuid.ts +31 -0
- package/src/util/validate.test.ts +309 -0
- package/src/util/validate.ts +230 -0
- package/.vscode/extensions.json +0 -6
- package/.vscode/settings.json +0 -27
- package/src/logging/AbstractLogHandler.js +0 -23
- package/src/logging/Logger.js +0 -115
- package/src/logging/OpenTelemetryLogHandler.js +0 -30
- package/src/logging/SentryLogHandler.js +0 -46
- package/src/logging/util.js +0 -41
- package/src/pagelifecycle/typedef.js +0 -9
- package/src/typecheck/util.js +0 -60
- package/src/util/error.js +0 -33
- package/src/util/event_emitter.js +0 -196
- package/src/util/number.js +0 -118
- package/src/util/query.js +0 -32
- package/src/util/string.js +0 -76
- package/src/util/uuid.js +0 -35
- package/src/util/validate.js +0 -247
- package/types/const/http_status.d.ts +0 -131
- package/types/const/http_status.d.ts.map +0 -1
- package/types/index.d.ts +0 -26
- package/types/index.d.ts.map +0 -1
- package/types/logging/AbstractLogHandler.d.ts +0 -20
- package/types/logging/AbstractLogHandler.d.ts.map +0 -1
- package/types/logging/ConsoleLogHandler.d.ts +0 -9
- package/types/logging/ConsoleLogHandler.d.ts.map +0 -1
- package/types/logging/Logger.d.ts +0 -66
- package/types/logging/Logger.d.ts.map +0 -1
- package/types/logging/OpenTelemetryLogHandler.d.ts +0 -11
- package/types/logging/OpenTelemetryLogHandler.d.ts.map +0 -1
- package/types/logging/SentryLogHandler.d.ts +0 -9
- package/types/logging/SentryLogHandler.d.ts.map +0 -1
- package/types/logging/const.d.ts +0 -14
- package/types/logging/const.d.ts.map +0 -1
- package/types/logging/util.d.ts +0 -4
- package/types/logging/util.d.ts.map +0 -1
- package/types/pagelifecycle/const.d.ts +0 -15
- package/types/pagelifecycle/const.d.ts.map +0 -1
- package/types/pagelifecycle/typedef.d.ts +0 -4
- package/types/pagelifecycle/typedef.d.ts.map +0 -1
- package/types/pagelifecycle/util.d.ts +0 -8
- package/types/pagelifecycle/util.d.ts.map +0 -1
- package/types/typecheck/TypeCheckError.d.ts +0 -13
- package/types/typecheck/TypeCheckError.d.ts.map +0 -1
- package/types/typecheck/TypeChecker.d.ts +0 -40
- package/types/typecheck/TypeChecker.d.ts.map +0 -1
- package/types/typecheck/util.d.ts +0 -4
- package/types/typecheck/util.d.ts.map +0 -1
- package/types/util/async.d.ts +0 -4
- package/types/util/async.d.ts.map +0 -1
- package/types/util/error.d.ts +0 -3
- package/types/util/error.d.ts.map +0 -1
- package/types/util/event_emitter.d.ts +0 -69
- package/types/util/event_emitter.d.ts.map +0 -1
- package/types/util/fetch.d.ts +0 -22
- package/types/util/fetch.d.ts.map +0 -1
- package/types/util/number.d.ts +0 -11
- package/types/util/number.d.ts.map +0 -1
- package/types/util/object.d.ts +0 -6
- package/types/util/object.d.ts.map +0 -1
- package/types/util/query.d.ts +0 -3
- package/types/util/query.d.ts.map +0 -1
- package/types/util/state.d.ts +0 -2
- package/types/util/state.d.ts.map +0 -1
- package/types/util/string.d.ts +0 -7
- package/types/util/string.d.ts.map +0 -1
- package/types/util/uuid.d.ts +0 -4
- package/types/util/uuid.d.ts.map +0 -1
- package/types/util/validate.d.ts +0 -45
- package/types/util/validate.d.ts.map +0 -1
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @callback EventListener
|
|
3
|
-
* A function invoked when an event is emitted.
|
|
4
|
-
* @param {...any} args - Arguments passed from the emitter.
|
|
5
|
-
* @returns {void}
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Internal listener wrapper that stores metadata
|
|
10
|
-
* about a registered event listener.
|
|
11
|
-
*/
|
|
12
|
-
class Listener {
|
|
13
|
-
/**
|
|
14
|
-
* @param {EventListener} fn - The listener callback function.
|
|
15
|
-
* @param {any} context - The `this` value used when invoking the listener.
|
|
16
|
-
* @param {boolean} [once] - Whether the listener should be invoked only once.
|
|
17
|
-
*/
|
|
18
|
-
constructor(fn, context, once = false) {
|
|
19
|
-
/** @type {EventListener} The listener callback */
|
|
20
|
-
this.fn = fn;
|
|
21
|
-
|
|
22
|
-
/** @type {any} Execution context for the callback */
|
|
23
|
-
this.context = context;
|
|
24
|
-
|
|
25
|
-
/** @type {boolean} Whether this listener is one-time */
|
|
26
|
-
this.once = once;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Event emitter implementation inspired by Node.js/EventEmitter3.
|
|
32
|
-
* Allows registering, emitting, and removing event listeners.
|
|
33
|
-
*/
|
|
34
|
-
export class EventEmitter {
|
|
35
|
-
#events;
|
|
36
|
-
|
|
37
|
-
constructor() {
|
|
38
|
-
/**
|
|
39
|
-
* Map of event name - array of listener wrappers.
|
|
40
|
-
* @type {Map<string | symbol, Listener[]>}
|
|
41
|
-
*/
|
|
42
|
-
this.#events = new Map();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Get all registered event names.
|
|
47
|
-
* @returns {(string | symbol)[]} Array of event identifiers.
|
|
48
|
-
*/
|
|
49
|
-
eventNames() {
|
|
50
|
-
return [...this.#events.keys()];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Get all listener functions registered for an event.
|
|
55
|
-
* @param {string | symbol} event - Event name.
|
|
56
|
-
* @returns {EventListener[]} List of listener callbacks.
|
|
57
|
-
*/
|
|
58
|
-
listeners(event) {
|
|
59
|
-
const listeners = this.#events.get(event);
|
|
60
|
-
return listeners ? listeners.map((l) => l.fn) : [];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get the number of listeners registered for an event.
|
|
65
|
-
* @param {string | symbol} event - Event name.
|
|
66
|
-
* @returns {number} Number of listeners.
|
|
67
|
-
*/
|
|
68
|
-
listenerCount(event) {
|
|
69
|
-
const listeners = this.#events.get(event);
|
|
70
|
-
return listeners ? listeners.length : 0;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Emit an event, invoking all registered listeners
|
|
75
|
-
* with the provided arguments.
|
|
76
|
-
* @param {string | symbol} event - Event name.
|
|
77
|
-
* @param {...any} args - Arguments passed to listeners.
|
|
78
|
-
* @returns {boolean} True if the event had listeners, otherwise false.
|
|
79
|
-
*/
|
|
80
|
-
emit(event, ...args) {
|
|
81
|
-
const listeners = this.#events.get(event);
|
|
82
|
-
if (!listeners || listeners.length === 0) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Clone to prevent mutation during iteration
|
|
87
|
-
for (const listener of [...listeners]) {
|
|
88
|
-
listener.fn.apply(listener.context, args);
|
|
89
|
-
if (listener.once) {
|
|
90
|
-
this.off(event, listener.fn, listener.context);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Internal helper for registering a listener.
|
|
99
|
-
* @param {string | symbol} event - Event name.
|
|
100
|
-
* @param {EventListener} fn - Listener callback.
|
|
101
|
-
* @param {any} context - Execution context for the callback.
|
|
102
|
-
* @param {boolean} once - Whether the listener is one-time.
|
|
103
|
-
* @returns {EventEmitter} The emitter instance.
|
|
104
|
-
*/
|
|
105
|
-
#addListener(event, fn, context, once) {
|
|
106
|
-
if (typeof fn !== 'function') {
|
|
107
|
-
throw new TypeError('Listener must be a function');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const listener = new Listener(fn, context ?? this, once);
|
|
111
|
-
const listeners = this.#events.get(event);
|
|
112
|
-
|
|
113
|
-
if (listeners) {
|
|
114
|
-
listeners.push(listener);
|
|
115
|
-
} else {
|
|
116
|
-
this.#events.set(event, [listener]);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Register a persistent listener for an event.
|
|
124
|
-
* @param {string | symbol} event - Event name.
|
|
125
|
-
* @param {EventListener} fn - Listener callback.
|
|
126
|
-
* @param {any} [context] - Optional execution context.
|
|
127
|
-
* @returns {EventEmitter} The emitter instance.
|
|
128
|
-
*/
|
|
129
|
-
on(event, fn, context) {
|
|
130
|
-
return this.#addListener(event, fn, context, false);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Register a one-time listener for an event.
|
|
135
|
-
* The listener is removed after its first invocation.
|
|
136
|
-
* @param {string | symbol} event - Event name.
|
|
137
|
-
* @param {EventListener} fn - Listener callback.
|
|
138
|
-
* @param {any} [context] - Optional execution context.
|
|
139
|
-
* @returns {EventEmitter} The emitter instance.
|
|
140
|
-
*/
|
|
141
|
-
once(event, fn, context) {
|
|
142
|
-
return this.#addListener(event, fn, context, true);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Remove a specific listener, or all listeners for an event.
|
|
147
|
-
* @param {string | symbol} event - Event name.
|
|
148
|
-
* @param {EventListener} [fn] - Listener callback to remove.
|
|
149
|
-
* @param {any} [context] - Context to match when removing.
|
|
150
|
-
* @returns {EventEmitter} The emitter instance.
|
|
151
|
-
*/
|
|
152
|
-
off(event, fn, context) {
|
|
153
|
-
if (!this.#events.has(event)) {
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (!fn) {
|
|
158
|
-
this.#events.delete(event);
|
|
159
|
-
return this;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const filtered = this.#events.get(event).filter((listener) => {
|
|
163
|
-
if (listener.fn !== fn) {
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
if (context !== undefined && listener.context !== context) {
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
return false;
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
if (filtered.length > 0) {
|
|
173
|
-
this.#events.set(event, filtered);
|
|
174
|
-
} else {
|
|
175
|
-
this.#events.delete(event);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return this;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Remove all listeners from the emitter,
|
|
183
|
-
* or all listeners for a specific event.
|
|
184
|
-
* @param {string | symbol} [event] - Optional event name.
|
|
185
|
-
* @returns {EventEmitter} The emitter instance.
|
|
186
|
-
*/
|
|
187
|
-
removeAllListeners(event) {
|
|
188
|
-
if (event === undefined) {
|
|
189
|
-
this.#events.clear();
|
|
190
|
-
} else {
|
|
191
|
-
this.#events.delete(event);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
}
|
package/src/util/number.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { TypeCheckError } from '../typecheck/TypeCheckError.js';
|
|
2
|
-
|
|
3
|
-
const DEG_TO_RAD = Math.PI / 180;
|
|
4
|
-
const RAD_TO_DEG = 180 / Math.PI;
|
|
5
|
-
|
|
6
|
-
const PRECISION = 12;
|
|
7
|
-
const EPSILON = 1e-11;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Converts degrees to radians.
|
|
11
|
-
* @param {number} degrees - Angle in degrees.
|
|
12
|
-
* @returns {number} Angle in radians.
|
|
13
|
-
* @throws {TypeCheckError}
|
|
14
|
-
*/
|
|
15
|
-
export const deg2rad = (degrees) => {
|
|
16
|
-
if (!Number.isFinite(degrees)) {
|
|
17
|
-
throw new TypeCheckError('Argument degrees must be a finite number', { value: degrees });
|
|
18
|
-
}
|
|
19
|
-
return degrees * DEG_TO_RAD;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Converts radians to degrees.
|
|
24
|
-
* @param {number} radians - Angle in radians.
|
|
25
|
-
* @returns {number} Angle in degrees.
|
|
26
|
-
* @throws {TypeCheckError}
|
|
27
|
-
*/
|
|
28
|
-
export const rad2deg = (radians) => {
|
|
29
|
-
if (!Number.isFinite(radians)) {
|
|
30
|
-
throw new TypeCheckError('Argument radians must be a finite number', { value: radians });
|
|
31
|
-
}
|
|
32
|
-
return radians * RAD_TO_DEG;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Returns random integer in range.
|
|
37
|
-
* @param {number} min - Min value.
|
|
38
|
-
* @param {number} max - Max value.
|
|
39
|
-
* @returns {number} Random integer in given range.
|
|
40
|
-
* @throws {TypeError}
|
|
41
|
-
*/
|
|
42
|
-
export const getRandomInt = (min, max) => {
|
|
43
|
-
if (!Number.isFinite(min)) {
|
|
44
|
-
throw new TypeCheckError('Argument min must be finite number', { value: min });
|
|
45
|
-
}
|
|
46
|
-
if (!Number.isFinite(max)) {
|
|
47
|
-
throw new TypeCheckError('Argument max must be finite number', { value: max });
|
|
48
|
-
}
|
|
49
|
-
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Normalizes floating point precision (e.g. 0.20000000000000004 → 0.2).
|
|
54
|
-
* @param {number | string} value - Input value.
|
|
55
|
-
* @returns {number} Fixed float precision value.
|
|
56
|
-
*/
|
|
57
|
-
export const fixFloatPrecision = (value) => {
|
|
58
|
-
const parsedValue = typeof value === 'string' ? Number(value) : value;
|
|
59
|
-
if (!Number.isFinite(parsedValue)) {
|
|
60
|
-
return Number.NaN;
|
|
61
|
-
}
|
|
62
|
-
return Math.abs(parsedValue) < EPSILON ? 0 : Number(parsedValue.toPrecision(PRECISION));
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Value greater than check.
|
|
67
|
-
* @param {number} value - Input value.
|
|
68
|
-
* @param {number} min - Limit that `value` must be greater than.
|
|
69
|
-
* @returns {boolean} `true` is check success.
|
|
70
|
-
* @private
|
|
71
|
-
*/
|
|
72
|
-
export const isGreater = (value, min) => value > min;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Value greater than check.
|
|
76
|
-
* @param {number} value - Input value.
|
|
77
|
-
* @param {number} min - Limit that `value` must be greater or equal than.
|
|
78
|
-
* @returns {boolean} `true` is check success.
|
|
79
|
-
* @private
|
|
80
|
-
*/
|
|
81
|
-
export const isGreaterOrEqual = (value, min) => value >= min;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Value less than check.
|
|
85
|
-
* @param {number} value - Input value.
|
|
86
|
-
* @param {number} min - Limit that `value` must be greater than.
|
|
87
|
-
* @returns {boolean} `true` is check success.
|
|
88
|
-
* @private
|
|
89
|
-
*/
|
|
90
|
-
export const isLess = (value, min) => value < min;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Value less than check.
|
|
94
|
-
* @param {number} value - Input value.
|
|
95
|
-
* @param {number} min - Limit that `value` must be greater than.
|
|
96
|
-
* @returns {boolean} `true` is check success.
|
|
97
|
-
* @private
|
|
98
|
-
*/
|
|
99
|
-
export const isLessOrEqual = (value, min) => value <= min;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Value greater than check.
|
|
103
|
-
* @param {number} value - Input value.
|
|
104
|
-
* @param {number} min - Limit `value` must be greater or equal than.
|
|
105
|
-
* @param {number} max - Limit `value` must be less or equal than.
|
|
106
|
-
* @returns {boolean} `true` is check success.
|
|
107
|
-
* @private
|
|
108
|
-
*/
|
|
109
|
-
export const isInRange = (value, min, max) => value >= min && value <= max;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Value equal check.
|
|
113
|
-
* @param {number} value - Input value.
|
|
114
|
-
* @param {number} expected - `expected` that `value` must equal.
|
|
115
|
-
* @returns {boolean} `true` is check success.
|
|
116
|
-
* @private
|
|
117
|
-
*/
|
|
118
|
-
export const isEqual = (value, expected) => value === expected;
|
package/src/util/query.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const urlSearchParams = new URLSearchParams(globalThis.location?.search);
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Sanitizes URL parameters allowing only alpha-numeric characters and dash.
|
|
5
|
-
* @param {string} input - The input string to be sanitized.
|
|
6
|
-
* @returns {string} The sanitized output string.
|
|
7
|
-
*/
|
|
8
|
-
export const sanitizeURLParam = (input) => {
|
|
9
|
-
if (!input) {
|
|
10
|
-
return input;
|
|
11
|
-
}
|
|
12
|
-
return input.replaceAll(/[^\w-]/giu, '');
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Get a URL parameter value.
|
|
17
|
-
* @template T
|
|
18
|
-
* @param {string} key - The name of the URL parameter to retrieve.
|
|
19
|
-
* @param {T} defaultValue - The default value to return if the parameter is not found.
|
|
20
|
-
* @param {boolean} isSanitize - Whether to sanitize the parameter value.
|
|
21
|
-
* @returns {string | T} The URL parameter value or the default value if not found.
|
|
22
|
-
*/
|
|
23
|
-
export const getURLParam = (key, defaultValue = null, isSanitize = true) => {
|
|
24
|
-
const paramValue = urlSearchParams.get(key);
|
|
25
|
-
if (paramValue === null || paramValue === undefined) {
|
|
26
|
-
return defaultValue;
|
|
27
|
-
}
|
|
28
|
-
if (isSanitize) {
|
|
29
|
-
return sanitizeURLParam(paramValue);
|
|
30
|
-
}
|
|
31
|
-
return paramValue;
|
|
32
|
-
};
|
package/src/util/string.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Add leading zeros to a value to ensure it has a minimum width.
|
|
3
|
-
* @param {number | string | null | undefined} value - The value to pad with leading zeros.
|
|
4
|
-
* @param {number} size - The minimum width of the resulting string.
|
|
5
|
-
* @returns {string | null} The value padded with leading zeros or null if the input is null/undefined.
|
|
6
|
-
*/
|
|
7
|
-
export const addLeadingZero = (value, size = 2) => {
|
|
8
|
-
if (value === null || value === undefined) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
value = value.toString();
|
|
12
|
-
while (value.length < size) {
|
|
13
|
-
value = `0${value}`;
|
|
14
|
-
}
|
|
15
|
-
return value;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Capitalize a string.
|
|
20
|
-
* @param {string | null | undefined} value - The input string to capitalize.
|
|
21
|
-
* @returns {string | null} The capitalized string or null if the input is null/undefined.
|
|
22
|
-
*/
|
|
23
|
-
export const capitalize = (value) => {
|
|
24
|
-
if (value === null || value === undefined) {
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
if (!value || value?.length === 0) {
|
|
28
|
-
return value;
|
|
29
|
-
}
|
|
30
|
-
const normValue = value.toLowerCase();
|
|
31
|
-
return normValue.charAt(0).toUpperCase() + normValue.slice(1);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Converts underscore case string to camel case.
|
|
36
|
-
* @param {string} value - The input string in underscore case.
|
|
37
|
-
* @returns {string} The output string in camel case.
|
|
38
|
-
*/
|
|
39
|
-
export const underscoreToCamelCase = (value) => value.replaceAll(/(_\w)/gu, (m) => m[1].toUpperCase());
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Saves text file.
|
|
43
|
-
* @param {string} filename - File name.
|
|
44
|
-
* @param {string} text - File content.
|
|
45
|
-
*/
|
|
46
|
-
export const saveAsFile = (filename, text) => {
|
|
47
|
-
const element = document.createElement('a');
|
|
48
|
-
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`);
|
|
49
|
-
element.setAttribute('download', filename);
|
|
50
|
-
element.style.display = 'none';
|
|
51
|
-
document.body.append(element);
|
|
52
|
-
element.click();
|
|
53
|
-
element.remove();
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get type from value in human readable format.
|
|
58
|
-
* @param {unknown} value - The value to check.
|
|
59
|
-
* @returns {string} Type in human readable format.
|
|
60
|
-
*/
|
|
61
|
-
export const getTypeFromValue = (value) => Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get value in human readable format.
|
|
65
|
-
* @param {unknown} value - The value to check.
|
|
66
|
-
* @returns {string} Value in human readable format.
|
|
67
|
-
*/
|
|
68
|
-
export const getDisplayValue = (value) => {
|
|
69
|
-
if (typeof value === 'string') {
|
|
70
|
-
return `"${value}"`;
|
|
71
|
-
}
|
|
72
|
-
if (typeof value === 'object') {
|
|
73
|
-
return JSON.stringify(value);
|
|
74
|
-
}
|
|
75
|
-
return String(value);
|
|
76
|
-
};
|
package/src/util/uuid.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert a byte (0–255) to a 2‑character hex string.
|
|
3
|
-
* @param {number} byte - Byte value.
|
|
4
|
-
* @returns {string} Hex value.
|
|
5
|
-
*/
|
|
6
|
-
export const byteToHex = (byte) => (byte >>> 4).toString(16) + (byte & 0b1111).toString(16);
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* UUIDv4 fallback generator (RFC 4122 compliant).
|
|
10
|
-
* @returns {string} UUIDv4 string.
|
|
11
|
-
*/
|
|
12
|
-
export const randomUUIDFallback = () => {
|
|
13
|
-
const bytes = crypto.getRandomValues
|
|
14
|
-
? crypto.getRandomValues(new Uint8Array(16))
|
|
15
|
-
: Array.from({ length: 16 }, () => Math.floor(Math.random() * 256));
|
|
16
|
-
|
|
17
|
-
// RFC 4122 version & variant bits
|
|
18
|
-
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
19
|
-
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
20
|
-
|
|
21
|
-
let uuid = '';
|
|
22
|
-
for (const [index, byte] of bytes.entries()) {
|
|
23
|
-
if (index === 4 || index === 6 || index === 8 || index === 10) {
|
|
24
|
-
uuid += '-';
|
|
25
|
-
}
|
|
26
|
-
uuid += byteToHex(byte);
|
|
27
|
-
}
|
|
28
|
-
return uuid;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Crypto UUIDv4 wrapper with fallback.
|
|
33
|
-
* @returns {string} UUIDv4 string.
|
|
34
|
-
*/
|
|
35
|
-
export const uuidv4 = () => (typeof crypto.randomUUID === 'function' ? crypto.randomUUID() : randomUUIDFallback());
|
package/src/util/validate.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { isEqual, isGreater, isGreaterOrEqual, isInRange, isLess, isLessOrEqual } from './number.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Validates `value` as `boolean`.
|
|
5
|
-
* @param {unknown} value - Input value.
|
|
6
|
-
* @returns {value is boolean} `true` if `value` is `boolean` type.
|
|
7
|
-
*/
|
|
8
|
-
export const isBoolean = (value) => typeof value === 'boolean';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Validates `value` as `number`.
|
|
12
|
-
* @param {unknown} value - Input value.
|
|
13
|
-
* @returns {value is number} `true` if `value` is `number` type.
|
|
14
|
-
*/
|
|
15
|
-
export const isNumber = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Validates `value` as positive `number`.
|
|
19
|
-
* @param {unknown} value - Input value.
|
|
20
|
-
* @returns {value is number} `true` if `value` is `number` type with positive value.
|
|
21
|
-
*/
|
|
22
|
-
export const isPositiveNumber = (value) => isNumber(value) && value > 0;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Validates `value` as non-negative `number`.
|
|
26
|
-
* @param {unknown} value - Input value.
|
|
27
|
-
* @returns {value is number} `true` if `value` is `number` type with non-negative value.
|
|
28
|
-
*/
|
|
29
|
-
export const isNonNegativeNumber = (value) => isNumber(value) && value >= 0;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Validates `value` as `integer`.
|
|
33
|
-
* @param {unknown} value - Input value.
|
|
34
|
-
* @returns {value is number} `true` if `value` is `integer` type.
|
|
35
|
-
*/
|
|
36
|
-
export const isInteger = (value) => isNumber(value) && Number.isInteger(value);
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Validates `value` as positive `integer`.
|
|
40
|
-
* @param {unknown} value - Input value.
|
|
41
|
-
* @returns {value is number} `true` if `value` is `integer` type with positive value.
|
|
42
|
-
*/
|
|
43
|
-
export const isPositiveInteger = (value) => isInteger(value) && value > 0;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Validates `value` as non-negative `integer`.
|
|
47
|
-
* @param {unknown} value - Input value.
|
|
48
|
-
* @returns {value is number} `true` if `value` is `integer` type with non-negative value.
|
|
49
|
-
*/
|
|
50
|
-
export const isNonNegativeInteger = (value) => isInteger(value) && value >= 0;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Validates `value` as `string`.
|
|
54
|
-
* @param {unknown} value - Input value.
|
|
55
|
-
* @returns {value is string} `true` if `value` is `string` type.
|
|
56
|
-
*/
|
|
57
|
-
export const isString = (value) => typeof value === 'string';
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Validates `value` as `array`.
|
|
61
|
-
* @template T
|
|
62
|
-
* @param {unknown} value - Input value.
|
|
63
|
-
* @returns {value is T[]} `true` if `value` is `array` type.
|
|
64
|
-
*/
|
|
65
|
-
export const isArray = (value) => Array.isArray(value);
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Validates `value` as `null`
|
|
69
|
-
* @param {unknown} value - Input value.
|
|
70
|
-
* @returns {value is null} `true` if `value` is `null` type.
|
|
71
|
-
*/
|
|
72
|
-
export const isNull = (value) => value === null;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Validates `value` as `undefined`
|
|
76
|
-
* @param {unknown} value - Input value.
|
|
77
|
-
* @returns {value is undefined} `true` if `value` is `undefined` type.
|
|
78
|
-
*/
|
|
79
|
-
export const isUndefined = (value) => value === undefined;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Validates `value` as `null` or `undefined`
|
|
83
|
-
* @param {unknown} value - Input value.
|
|
84
|
-
* @returns {value is null | undefined} `true` if `value` is `null` or `undefined` type.
|
|
85
|
-
*/
|
|
86
|
-
export const isNullOrUndefined = (value) => isNull(value) || isUndefined(value);
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Validates `value` as plain `object`.
|
|
90
|
-
* @param {unknown} value - Input value.
|
|
91
|
-
* @returns {value is Record<string, unknown>} `true` if `value` is `object` type.
|
|
92
|
-
*/
|
|
93
|
-
export const isPlainObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Validates `value` as `function`.
|
|
97
|
-
* @param {unknown} value - Input value.
|
|
98
|
-
* @returns {value is (...args: any[]) => any} `true` if `value` is `function` type.
|
|
99
|
-
*/
|
|
100
|
-
export const isFunction = (value) => typeof value === 'function';
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Validates `value` as `type`
|
|
104
|
-
* @template T
|
|
105
|
-
* @param {unknown} value - The value to test.
|
|
106
|
-
* @param {new (...args: any[]) => T} type - A class or constructor function.
|
|
107
|
-
* @returns {value is T} `true` if `value` is an instance of `type`.
|
|
108
|
-
*/
|
|
109
|
-
export const isInstance = (value, type) => isFunction(type) && value instanceof type;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Validates `value` as `enum`.
|
|
113
|
-
* @param {unknown} value - Input value.
|
|
114
|
-
* @param {(string | number)[] | Set<string | number> | Record<string | number, string | number>} choices - Enum list.
|
|
115
|
-
* @returns {boolean} `true` if `value` is `enum` type.
|
|
116
|
-
*/
|
|
117
|
-
export const isEnum = (value, choices) => {
|
|
118
|
-
if (!isString(value) && !isNumber(value)) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
return (
|
|
122
|
-
(isArray(choices) && choices.includes(value)) ||
|
|
123
|
-
(isPlainObject(choices) && Object.values(choices).includes(value)) ||
|
|
124
|
-
(isInstance(choices, Set) && choices.has(value))
|
|
125
|
-
);
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Type check an array of values using a validator.
|
|
130
|
-
* @template T
|
|
131
|
-
* @param {unknown[]} values - The value to check.
|
|
132
|
-
* @param {(value: unknown) => value is T} validator - The validator to check with.
|
|
133
|
-
* @returns {values is T[]} `true` if `values` has only `validator` checked types.
|
|
134
|
-
*/
|
|
135
|
-
export const isArrayOf = (values, validator) => {
|
|
136
|
-
if (!isArray(values)) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
for (const value of values) {
|
|
140
|
-
if (!validator(value)) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return true;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Type check a plain object of values using a validator.
|
|
149
|
-
* @template T
|
|
150
|
-
* @param {Record<string | number, unknown>} record - The value to check.
|
|
151
|
-
* @param {(value: unknown) => value is T} validator - The validator to check with.
|
|
152
|
-
* @returns {record is Record<string | number, T>} `true` if `values` has only `validator` checked types.
|
|
153
|
-
*/
|
|
154
|
-
export const isPlainObjectOf = (record, validator) => {
|
|
155
|
-
if (!isPlainObject(record)) {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
for (const value of Object.values(record)) {
|
|
159
|
-
if (!validator(value)) {
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return true;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Refine a base validator with an extra condition.
|
|
168
|
-
* @template T
|
|
169
|
-
* @param {(value: unknown) => value is T} base - The base validator.
|
|
170
|
-
* @param {(value: T) => boolean} predicate - Extra condition validator.
|
|
171
|
-
* @param {string | null} name - Refined validator name.
|
|
172
|
-
* @returns {(value: unknown) => value is T} The refined validator.
|
|
173
|
-
*/
|
|
174
|
-
export const refineValidator = (base, predicate, name = null) => {
|
|
175
|
-
const refinedValidator = (value) => base(value) && predicate(value);
|
|
176
|
-
Object.defineProperty(refinedValidator, 'name', {
|
|
177
|
-
value: isString(name) ? name : `${base.name}Refined`,
|
|
178
|
-
});
|
|
179
|
-
// @ts-expect-error
|
|
180
|
-
return refinedValidator;
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
//
|
|
184
|
-
// Refined validators
|
|
185
|
-
//
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Logical OR of two validators.
|
|
189
|
-
* @template A, B
|
|
190
|
-
* @param {(value: unknown) => value is A} a - Validator A.
|
|
191
|
-
* @param {(value: unknown) => value is B} b - Validator B.
|
|
192
|
-
* @returns {(value: unknown) => value is A | B} `true` if `value` is any of the checked types.
|
|
193
|
-
*/
|
|
194
|
-
export const isAnyOf = (a, b) => (value) => a(value) || b(value);
|
|
195
|
-
|
|
196
|
-
export const isNumberGreater = (min) => refineValidator(isNumber, (value) => isGreater(value, min));
|
|
197
|
-
|
|
198
|
-
export const isNumberGreaterOrEqual = (min) => refineValidator(isNumber, (value) => isGreaterOrEqual(value, min));
|
|
199
|
-
|
|
200
|
-
export const isNumberLess = (min) => refineValidator(isNumber, (value) => isLess(value, min));
|
|
201
|
-
|
|
202
|
-
export const isNumberLessOrEqual = (min) => refineValidator(isNumber, (value) => isLessOrEqual(value, min));
|
|
203
|
-
|
|
204
|
-
export const isNumberInRange = (min, max) => refineValidator(isNumber, (value) => isInRange(value, min, max));
|
|
205
|
-
|
|
206
|
-
export const isNumberEqual = (expected) => refineValidator(isNumber, (value) => isEqual(value, expected));
|
|
207
|
-
|
|
208
|
-
export const isIntegerGreater = (min) => refineValidator(isInteger, (value) => isGreater(value, min));
|
|
209
|
-
|
|
210
|
-
export const isIntegerGreaterOrEqual = (min) => refineValidator(isInteger, (value) => isGreaterOrEqual(value, min));
|
|
211
|
-
|
|
212
|
-
export const isIntegerLess = (min) => refineValidator(isInteger, (value) => isLess(value, min));
|
|
213
|
-
|
|
214
|
-
export const isIntegerLessOrEqual = (min) => refineValidator(isInteger, (value) => isLessOrEqual(value, min));
|
|
215
|
-
|
|
216
|
-
export const isIntegerInRange = (min, max) => refineValidator(isInteger, (value) => isInRange(value, min, max));
|
|
217
|
-
|
|
218
|
-
export const isIntegerEqual = (expected) => refineValidator(isInteger, (value) => isEqual(value, expected));
|
|
219
|
-
|
|
220
|
-
export const isStringLengthGreater = (min) => refineValidator(isString, (value) => isGreater(value.length, min));
|
|
221
|
-
|
|
222
|
-
export const isStringLengthGreaterOrEqual = (min) =>
|
|
223
|
-
refineValidator(isString, (value) => isGreaterOrEqual(value.length, min));
|
|
224
|
-
|
|
225
|
-
export const isStringLengthLess = (min) => refineValidator(isString, (value) => isLess(value.length, min));
|
|
226
|
-
|
|
227
|
-
export const isStringLengthLessOrEqual = (min) =>
|
|
228
|
-
refineValidator(isString, (value) => isLessOrEqual(value.length, min));
|
|
229
|
-
|
|
230
|
-
export const isStringLengthInRange = (min, max) =>
|
|
231
|
-
refineValidator(isString, (value) => isInRange(value.length, min, max));
|
|
232
|
-
|
|
233
|
-
export const isStringLengthEqual = (expected) => refineValidator(isString, (value) => isEqual(value.length, expected));
|
|
234
|
-
|
|
235
|
-
export const isArrayLengthGreater = (min) => refineValidator(isArray, (value) => isGreater(value.length, min));
|
|
236
|
-
|
|
237
|
-
export const isArrayLengthGreaterOrEqual = (min) =>
|
|
238
|
-
refineValidator(isArray, (value) => isGreaterOrEqual(value.length, min));
|
|
239
|
-
|
|
240
|
-
export const isArrayLengthLess = (min) => refineValidator(isArray, (value) => isLess(value.length, min));
|
|
241
|
-
|
|
242
|
-
export const isArrayLengthLessOrEqual = (min) => refineValidator(isArray, (value) => isLessOrEqual(value.length, min));
|
|
243
|
-
|
|
244
|
-
export const isArrayLengthInRange = (min, max) =>
|
|
245
|
-
refineValidator(isArray, (value) => isInRange(value.length, min, max));
|
|
246
|
-
|
|
247
|
-
export const isArrayLengthEqual = (expected) => refineValidator(isArray, (value) => isEqual(value.length, expected));
|