@vpmedia/simplify 1.74.0 → 1.75.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/const/http_status.d.ts +66 -0
  3. package/dist/const/http_status.d.ts.map +1 -0
  4. package/dist/const/http_status.js +133 -0
  5. package/dist/const/http_status.js.map +1 -0
  6. package/dist/index.d.ts +34 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/{src → dist}/index.js +3 -20
  9. package/dist/index.js.map +1 -0
  10. package/dist/logging/AbstractLogHandler.d.ts +17 -0
  11. package/dist/logging/AbstractLogHandler.d.ts.map +1 -0
  12. package/dist/logging/AbstractLogHandler.js +16 -0
  13. package/dist/logging/AbstractLogHandler.js.map +1 -0
  14. package/dist/logging/ConsoleLogHandler.d.ts +13 -0
  15. package/dist/logging/ConsoleLogHandler.d.ts.map +1 -0
  16. package/dist/logging/ConsoleLogHandler.js +41 -0
  17. package/dist/logging/ConsoleLogHandler.js.map +1 -0
  18. package/dist/logging/Logger.d.ts +19 -0
  19. package/dist/logging/Logger.d.ts.map +1 -0
  20. package/dist/logging/Logger.js +51 -0
  21. package/dist/logging/Logger.js.map +1 -0
  22. package/dist/logging/OpenTelemetryLogHandler.d.ts +15 -0
  23. package/dist/logging/OpenTelemetryLogHandler.d.ts.map +1 -0
  24. package/dist/logging/OpenTelemetryLogHandler.js +21 -0
  25. package/dist/logging/OpenTelemetryLogHandler.js.map +1 -0
  26. package/dist/logging/SentryLogHandler.d.ts +13 -0
  27. package/dist/logging/SentryLogHandler.d.ts.map +1 -0
  28. package/dist/logging/SentryLogHandler.js +36 -0
  29. package/dist/logging/SentryLogHandler.js.map +1 -0
  30. package/dist/logging/const.d.ts +14 -0
  31. package/dist/logging/const.d.ts.map +1 -0
  32. package/dist/logging/const.js +21 -0
  33. package/dist/logging/const.js.map +1 -0
  34. package/dist/logging/util.d.ts +14 -0
  35. package/dist/logging/util.d.ts.map +1 -0
  36. package/dist/logging/util.js +34 -0
  37. package/dist/logging/util.js.map +1 -0
  38. package/dist/pagelifecycle/const.d.ts +15 -0
  39. package/dist/pagelifecycle/const.d.ts.map +1 -0
  40. package/dist/pagelifecycle/const.js +27 -0
  41. package/dist/pagelifecycle/const.js.map +1 -0
  42. package/dist/pagelifecycle/typedef.d.ts +4 -0
  43. package/dist/pagelifecycle/typedef.d.ts.map +1 -0
  44. package/dist/pagelifecycle/typedef.js +2 -0
  45. package/dist/pagelifecycle/typedef.js.map +1 -0
  46. package/dist/pagelifecycle/util.d.ts +31 -0
  47. package/dist/pagelifecycle/util.d.ts.map +1 -0
  48. package/dist/pagelifecycle/util.js +117 -0
  49. package/dist/pagelifecycle/util.js.map +1 -0
  50. package/dist/typecheck/TypeCheckError.d.ts +11 -0
  51. package/dist/typecheck/TypeCheckError.d.ts.map +1 -0
  52. package/dist/typecheck/TypeCheckError.js +12 -0
  53. package/dist/typecheck/TypeCheckError.js.map +1 -0
  54. package/dist/typecheck/TypeChecker.d.ts +27 -0
  55. package/dist/typecheck/TypeChecker.d.ts.map +1 -0
  56. package/dist/typecheck/TypeChecker.js +69 -0
  57. package/dist/typecheck/TypeChecker.js.map +1 -0
  58. package/dist/typecheck/util.d.ts +17 -0
  59. package/dist/typecheck/util.d.ts.map +1 -0
  60. package/dist/typecheck/util.js +42 -0
  61. package/dist/typecheck/util.js.map +1 -0
  62. package/dist/util/async.d.ts +13 -0
  63. package/dist/util/async.d.ts.map +1 -0
  64. package/dist/util/async.js +39 -0
  65. package/dist/util/async.js.map +1 -0
  66. package/dist/util/error.d.ts +16 -0
  67. package/dist/util/error.d.ts.map +1 -0
  68. package/dist/util/error.js +27 -0
  69. package/dist/util/error.js.map +1 -0
  70. package/dist/util/event_emitter.d.ts +42 -0
  71. package/dist/util/event_emitter.d.ts.map +1 -0
  72. package/dist/util/event_emitter.js +127 -0
  73. package/dist/util/event_emitter.js.map +1 -0
  74. package/dist/util/fetch.d.ts +22 -0
  75. package/dist/util/fetch.d.ts.map +1 -0
  76. package/dist/util/fetch.js +75 -0
  77. package/dist/util/fetch.js.map +1 -0
  78. package/dist/util/number.d.ts +23 -0
  79. package/dist/util/number.d.ts.map +1 -0
  80. package/dist/util/number.js +52 -0
  81. package/dist/util/number.js.map +1 -0
  82. package/dist/util/object.d.ts +24 -0
  83. package/dist/util/object.d.ts.map +1 -0
  84. package/dist/util/object.js +83 -0
  85. package/dist/util/object.js.map +1 -0
  86. package/dist/util/query.d.ts +11 -0
  87. package/dist/util/query.d.ts.map +1 -0
  88. package/dist/util/query.js +24 -0
  89. package/dist/util/query.js.map +1 -0
  90. package/dist/util/state.d.ts +5 -0
  91. package/dist/util/state.d.ts.map +1 -0
  92. package/dist/util/state.js +19 -0
  93. package/dist/util/state.js.map +1 -0
  94. package/dist/util/string.d.ts +25 -0
  95. package/dist/util/string.d.ts.map +1 -0
  96. package/dist/util/string.js +59 -0
  97. package/dist/util/string.js.map +1 -0
  98. package/dist/util/uuid.d.ts +13 -0
  99. package/dist/util/uuid.d.ts.map +1 -0
  100. package/dist/util/uuid.js +27 -0
  101. package/dist/util/uuid.js.map +1 -0
  102. package/dist/util/validate.d.ts +106 -0
  103. package/dist/util/validate.d.ts.map +1 -0
  104. package/dist/util/validate.js +139 -0
  105. package/dist/util/validate.js.map +1 -0
  106. package/package.json +31 -16
  107. package/src/const/http_status.test.ts +7 -0
  108. package/src/const/{http_status.js → http_status.ts} +1 -1
  109. package/src/index.ts +51 -0
  110. package/src/logging/AbstractLogHandler.ts +31 -0
  111. package/src/logging/{ConsoleLogHandler.js → ConsoleLogHandler.ts} +15 -13
  112. package/src/logging/Logger.test.ts +69 -0
  113. package/src/logging/Logger.ts +77 -0
  114. package/src/logging/OpenTelemetryLogHandler.ts +40 -0
  115. package/src/logging/SentryLogHandler.ts +44 -0
  116. package/src/logging/{const.js → const.ts} +1 -1
  117. package/src/logging/util.test.ts +33 -0
  118. package/src/logging/util.ts +36 -0
  119. package/src/pagelifecycle/{const.js → const.ts} +2 -2
  120. package/src/pagelifecycle/typedef.ts +5 -0
  121. package/src/pagelifecycle/util.test.ts +99 -0
  122. package/src/pagelifecycle/{util.js → util.ts} +14 -27
  123. package/src/typecheck/{TypeCheckError.js → TypeCheckError.ts} +7 -3
  124. package/src/typecheck/TypeChecker.test.ts +70 -0
  125. package/src/typecheck/{TypeChecker.js → TypeChecker.ts} +10 -27
  126. package/src/typecheck/util.test.ts +36 -0
  127. package/src/typecheck/util.ts +50 -0
  128. package/src/util/async.test.ts +74 -0
  129. package/src/util/{async.js → async.ts} +3 -12
  130. package/src/util/error.test.ts +32 -0
  131. package/src/util/error.ts +37 -0
  132. package/src/util/event_emitter.test.ts +228 -0
  133. package/src/util/event_emitter.ts +147 -0
  134. package/src/util/fetch.test.ts +62 -0
  135. package/src/util/{fetch.js → fetch.ts} +40 -31
  136. package/src/util/number.test.ts +124 -0
  137. package/src/util/number.ts +58 -0
  138. package/src/util/object.test.ts +203 -0
  139. package/src/util/{object.js → object.ts} +14 -21
  140. package/src/util/query.test.ts +71 -0
  141. package/src/util/query.ts +35 -0
  142. package/src/util/state.test.ts +47 -0
  143. package/src/util/{state.js → state.ts} +3 -6
  144. package/src/util/string.test.ts +64 -0
  145. package/src/util/string.ts +65 -0
  146. package/src/util/uuid.test.ts +53 -0
  147. package/src/util/uuid.ts +31 -0
  148. package/src/util/validate.test.ts +309 -0
  149. package/src/util/validate.ts +230 -0
  150. package/.vscode/extensions.json +0 -6
  151. package/.vscode/settings.json +0 -27
  152. package/src/logging/AbstractLogHandler.js +0 -23
  153. package/src/logging/Logger.js +0 -115
  154. package/src/logging/OpenTelemetryLogHandler.js +0 -30
  155. package/src/logging/SentryLogHandler.js +0 -46
  156. package/src/logging/util.js +0 -41
  157. package/src/pagelifecycle/typedef.js +0 -9
  158. package/src/typecheck/util.js +0 -60
  159. package/src/util/error.js +0 -33
  160. package/src/util/event_emitter.js +0 -196
  161. package/src/util/number.js +0 -118
  162. package/src/util/query.js +0 -32
  163. package/src/util/string.js +0 -76
  164. package/src/util/uuid.js +0 -35
  165. package/src/util/validate.js +0 -247
  166. package/types/const/http_status.d.ts +0 -131
  167. package/types/const/http_status.d.ts.map +0 -1
  168. package/types/index.d.ts +0 -26
  169. package/types/index.d.ts.map +0 -1
  170. package/types/logging/AbstractLogHandler.d.ts +0 -20
  171. package/types/logging/AbstractLogHandler.d.ts.map +0 -1
  172. package/types/logging/ConsoleLogHandler.d.ts +0 -9
  173. package/types/logging/ConsoleLogHandler.d.ts.map +0 -1
  174. package/types/logging/Logger.d.ts +0 -66
  175. package/types/logging/Logger.d.ts.map +0 -1
  176. package/types/logging/OpenTelemetryLogHandler.d.ts +0 -11
  177. package/types/logging/OpenTelemetryLogHandler.d.ts.map +0 -1
  178. package/types/logging/SentryLogHandler.d.ts +0 -9
  179. package/types/logging/SentryLogHandler.d.ts.map +0 -1
  180. package/types/logging/const.d.ts +0 -14
  181. package/types/logging/const.d.ts.map +0 -1
  182. package/types/logging/util.d.ts +0 -4
  183. package/types/logging/util.d.ts.map +0 -1
  184. package/types/pagelifecycle/const.d.ts +0 -15
  185. package/types/pagelifecycle/const.d.ts.map +0 -1
  186. package/types/pagelifecycle/typedef.d.ts +0 -4
  187. package/types/pagelifecycle/typedef.d.ts.map +0 -1
  188. package/types/pagelifecycle/util.d.ts +0 -8
  189. package/types/pagelifecycle/util.d.ts.map +0 -1
  190. package/types/typecheck/TypeCheckError.d.ts +0 -13
  191. package/types/typecheck/TypeCheckError.d.ts.map +0 -1
  192. package/types/typecheck/TypeChecker.d.ts +0 -40
  193. package/types/typecheck/TypeChecker.d.ts.map +0 -1
  194. package/types/typecheck/util.d.ts +0 -4
  195. package/types/typecheck/util.d.ts.map +0 -1
  196. package/types/util/async.d.ts +0 -4
  197. package/types/util/async.d.ts.map +0 -1
  198. package/types/util/error.d.ts +0 -3
  199. package/types/util/error.d.ts.map +0 -1
  200. package/types/util/event_emitter.d.ts +0 -69
  201. package/types/util/event_emitter.d.ts.map +0 -1
  202. package/types/util/fetch.d.ts +0 -22
  203. package/types/util/fetch.d.ts.map +0 -1
  204. package/types/util/number.d.ts +0 -11
  205. package/types/util/number.d.ts.map +0 -1
  206. package/types/util/object.d.ts +0 -6
  207. package/types/util/object.d.ts.map +0 -1
  208. package/types/util/query.d.ts +0 -3
  209. package/types/util/query.d.ts.map +0 -1
  210. package/types/util/state.d.ts +0 -2
  211. package/types/util/state.d.ts.map +0 -1
  212. package/types/util/string.d.ts +0 -7
  213. package/types/util/string.d.ts.map +0 -1
  214. package/types/util/uuid.d.ts +0 -4
  215. package/types/util/uuid.d.ts.map +0 -1
  216. package/types/util/validate.d.ts +0 -45
  217. package/types/util/validate.d.ts.map +0 -1
@@ -1,115 +0,0 @@
1
- import { getURLParam } from '../util/query.js';
2
- import {
3
- LOG_LEVEL_DEBUG,
4
- LOG_LEVEL_ERROR,
5
- LOG_LEVEL_FATAL,
6
- LOG_LEVEL_INFO,
7
- LOG_LEVEL_SILENT,
8
- LOG_LEVEL_WARNING,
9
- } from './const.js';
10
- import { getAppEnvironment } from './util.js';
11
-
12
- const ROOT_LOGGER_NAME = 'root';
13
-
14
- export class Logger {
15
- /**
16
- * @type {import('./AbstractLogHandler.js').AbstractLogHandler[]}
17
- */
18
- static handlers = [];
19
-
20
- /**
21
- * Creates a new Logger instance.
22
- * @param {string} [name] - The logger name.
23
- */
24
- constructor(name = ROOT_LOGGER_NAME) {
25
- this.name = name ?? ROOT_LOGGER_NAME;
26
- const appEnvironment = getAppEnvironment();
27
- const isProduction = appEnvironment === 'production' || appEnvironment === 'release';
28
- const defaultLevel = isProduction ? LOG_LEVEL_SILENT : LOG_LEVEL_DEBUG;
29
- const parameterName = `log_${this.name.toLowerCase()}`;
30
- const paramLevel =
31
- getURLParam(parameterName, getURLParam('log_all', defaultLevel.toString())) ?? defaultLevel.toString();
32
- /** @type {number} */
33
- this.level = Number.parseInt(paramLevel, 10);
34
- }
35
-
36
- /**
37
- * Add log handler.
38
- * @param {import('./AbstractLogHandler.js').AbstractLogHandler} handler - Log handler.
39
- */
40
- static addHandler = (handler) => {
41
- Logger.handlers.push(handler);
42
- };
43
-
44
- /**
45
- * Emit log record.
46
- * @param {Logger} logger - Logger instance.
47
- * @param {number} level - Log level.
48
- * @param {string} message - Log message.
49
- * @param {object} extra - Log extra data.
50
- * @param {Error | null | undefined} [error] - Log exception.
51
- */
52
- static emit = (logger, level, message, extra, error) => {
53
- const timestamp = Date.now();
54
- for (const handler of Logger.handlers) {
55
- if (handler.level >= level) {
56
- handler.emit(logger, timestamp, level, message, extra, error);
57
- }
58
- }
59
- };
60
-
61
- /**
62
- * Emit debug log.
63
- * @param {string} message - Log message.
64
- * @param {object | null | undefined} [extra] - Log extra data.
65
- */
66
- debug(message, extra) {
67
- Logger.emit(this, LOG_LEVEL_DEBUG, message, extra);
68
- }
69
-
70
- /**
71
- * Emit info log.
72
- * @param {string} message - Log message.
73
- * @param {object | null | undefined} [extra] - Log extra data.
74
- */
75
- info(message, extra) {
76
- Logger.emit(this, LOG_LEVEL_INFO, message, extra);
77
- }
78
-
79
- /**
80
- * Emit warning log.
81
- * @param {string} message - Log message.
82
- * @param {object | null | undefined} [extra] - Log extra data.
83
- */
84
- warn(message, extra) {
85
- Logger.emit(this, LOG_LEVEL_WARNING, message, extra);
86
- }
87
-
88
- /**
89
- * Emit warning log.
90
- * @param {string} message - Log message.
91
- * @param {object | null | undefined} [extra] - Log extra data.
92
- */
93
- warning(message, extra) {
94
- Logger.emit(this, LOG_LEVEL_WARNING, message, extra);
95
- }
96
-
97
- /**
98
- * Emit error log.
99
- * @param {string} message - Log message.
100
- * @param {object | null | undefined} [extra] - Log extra data.
101
- */
102
- error(message, extra) {
103
- Logger.emit(this, LOG_LEVEL_ERROR, message, extra);
104
- }
105
-
106
- /**
107
- * Emit exception log.
108
- * @param {string} message - Log message.
109
- * @param {Error} error - Log error.
110
- * @param {object | null | undefined} [extra] - Log extra data.
111
- */
112
- exception(message, error, extra) {
113
- Logger.emit(this, LOG_LEVEL_FATAL, message, extra, error);
114
- }
115
- }
@@ -1,30 +0,0 @@
1
- import { AbstractLogHandler } from './AbstractLogHandler.js';
2
-
3
- export class OpenTelemetryLogHandler extends AbstractLogHandler {
4
- /**
5
- * Open Telemetry log handler.
6
- * @param {number} level - Log handler level.
7
- * @param {(logger: import('./Logger.js').Logger, timestamp: number, level: number, message: string, extra: object | null | undefined, error: Error | null | undefined) => void} emitter - Log handler emitter.
8
- */
9
- constructor(level, emitter) {
10
- super(level);
11
- this.emitter = emitter;
12
- }
13
-
14
- /**
15
- * Emit log record.
16
- * @param {import('./Logger.js').Logger} logger - Logger instance.
17
- * @param {number} timestamp - Log timestamp.
18
- * @param {number} level - Log level.
19
- * @param {string} message - Log message.
20
- * @param {object | null | undefined} extra - Log extra data.
21
- * @param {Error | null | undefined} error - Log error.
22
- * @throws {Error}
23
- */
24
- emit(logger, timestamp, level, message, extra, error) {
25
- if (!this.emitter) {
26
- return;
27
- }
28
- this.emitter(logger, timestamp, level, message, extra, error);
29
- }
30
- }
@@ -1,46 +0,0 @@
1
- import { addBreadcrumb, captureException, captureMessage } from '@sentry/browser';
2
- import { AbstractLogHandler } from './AbstractLogHandler.js';
3
- import { LOG_LEVEL_DEBUG, LOG_LEVEL_WARNING } from './const.js';
4
- import { getLogLevelName } from './util.js';
5
-
6
- export class SentryLogHandler extends AbstractLogHandler {
7
- /**
8
- * Console log handler.
9
- * @param {number} level - Log handler level.
10
- */
11
- constructor(level = LOG_LEVEL_DEBUG) {
12
- super(level);
13
- }
14
-
15
- /**
16
- * Emit log record.
17
- * @param {import('./Logger.js').Logger} logger - Logger instance.
18
- * @param {number} timestamp - Log timestamp.
19
- * @param {number} level - Log level.
20
- * @param {string} message - Log message.
21
- * @param {object | null | undefined} extra - Log extra data.
22
- * @param {Error | null | undefined} error - Log error.
23
- * @throws {Error}
24
- */
25
- emit(logger, timestamp, level, message, extra, error) {
26
- /** @type {import('@sentry/browser').SeverityLevel} */
27
- // @ts-expect-error
28
- const levelName = getLogLevelName(level);
29
- const logMessage = `[${logger.name}] ${message}`;
30
- const breadcrumb = {
31
- type: 'default',
32
- category: 'console',
33
- message: logMessage,
34
- level: levelName,
35
- data: extra,
36
- };
37
- addBreadcrumb(breadcrumb);
38
- if (error) {
39
- extra?.tags ? captureException(error, { tags: extra.tags }) : captureException(error);
40
- } else if (level === LOG_LEVEL_WARNING) {
41
- extra?.tags
42
- ? captureMessage(logMessage, { level: 'warning', tags: extra.tags })
43
- : captureMessage(logMessage, { level: 'warning' });
44
- }
45
- }
46
- }
@@ -1,41 +0,0 @@
1
- import { LOG_LEVEL_NAMES } from './const.js';
2
-
3
- /**
4
- * Format log message.
5
- * @param {import('./Logger.js').Logger} logger - Logger target name.
6
- * @param {number} timestamp - Log timestamp.
7
- * @param {number} level - Log level.
8
- * @param {string} message - Log message.
9
- * @returns {string} Formatted log message.
10
- */
11
- export const formatLogMessage = (logger, timestamp, level, message) => `${timestamp} [${logger.name}] ${message}`;
12
-
13
- /**
14
- * Get log level name.
15
- * @param {number} level - Log level.
16
- * @returns {string} Log level name.
17
- */
18
- export const getLogLevelName = (level) => LOG_LEVEL_NAMES[level];
19
-
20
- /**
21
- * Returns the application environment identifier.
22
- * @returns {string} App environment type.
23
- */
24
- export const getAppEnvironment = () => {
25
- let appEnvironment = 'local';
26
- try {
27
- if (import.meta['env'].VITE_APP_ENVIRONMENT) {
28
- appEnvironment = import.meta['env'].VITE_APP_ENVIRONMENT;
29
- }
30
- } catch {
31
- // pass
32
- }
33
- try {
34
- if (process.env.APP_ENVIRONMENT) {
35
- appEnvironment = process.env.APP_ENVIRONMENT;
36
- }
37
- } catch {
38
- // pass
39
- }
40
- return appEnvironment;
41
- };
@@ -1,9 +0,0 @@
1
- export const TYPEDEF = true;
2
-
3
- /**
4
- * @typedef {import('./const.js').PAGE_LIFECYCLE_STATE_HIDDEN | import('./const.js').PAGE_LIFECYCLE_STATE_ACTIVE | import('./const.js').PAGE_LIFECYCLE_STATE_PASSIVE | import('./const.js').PAGE_LIFECYCLE_STATE_FROZEN | import('./const.js').PAGE_LIFECYCLE_STATE_TERMINATED} PageLifecycleState
5
- */
6
-
7
- /**
8
- * @typedef {import('./const.js').DOCUMENT_STATE_LOADING | import('./const.js').DOCUMENT_STATE_INTERACTIVE | import('./const.js').DOCUMENT_STATE_COMPLETE | import('./const.js').DOCUMENT_STATE_DOM_LOADED | import('./const.js').DOCUMENT_STATE_FULLY_LOADED} DocumentState
9
- */
@@ -1,60 +0,0 @@
1
- import { getDisplayValue, getTypeFromValue } from '../util/string.js';
2
- import { isArrayOf, isEnum } from '../util/validate.js';
3
- import { TypeCheckError } from './TypeCheckError.js';
4
-
5
- /**
6
- * Get error message for validator exceptions.
7
- * @param {string} validatorName - Validator name.
8
- * @param {unknown} value - Input value.
9
- */
10
- const getErrorMessage = (validatorName, value) => {
11
- const displayValue = getDisplayValue(value);
12
- const displayType = getTypeFromValue(value);
13
- throw new TypeCheckError(`Validation failed: ${validatorName || '<anonymous>'} - ${displayValue} (${displayType})`);
14
- };
15
- /**
16
- * Type check a value using a validator.
17
- * @template T
18
- * @param {unknown} value - The value to check.
19
- * @param {(value: unknown) => value is T} validator - The validator to check with.
20
- * @returns {T} The type checked value.
21
- * @throws {TypeCheckError}
22
- */
23
- export const typeCheck = (value, validator) => {
24
- if (!validator(value)) {
25
- const errorMessage = getErrorMessage(validator.name, value);
26
- throw new TypeCheckError(errorMessage);
27
- }
28
- return value;
29
- };
30
-
31
- /**
32
- * Type check a value using a validator.
33
- * @template T
34
- * @param {unknown[]} value - The value to check.
35
- * @param {(value: unknown) => value is T} validator - The validator to check the array with.
36
- * @returns {T[]} The type checked value.
37
- * @throws {TypeCheckError}
38
- */
39
- export const typeCheckArray = (value, validator) => {
40
- if (!isArrayOf(value, validator)) {
41
- const errorMessage = getErrorMessage(validator.name, value);
42
- throw new TypeCheckError(errorMessage);
43
- }
44
- return value;
45
- };
46
-
47
- /**
48
- * Type check an enum.
49
- * @param {string | number} value - The value to check.
50
- * @param {(string | number)[] | Set<string | number> | Record<string | number, string | number>} choices - Enum list.
51
- * @returns {string | number} The type checked value.
52
- * @throws {TypeCheckError}
53
- */
54
- export const typeCheckEnum = (value, choices) => {
55
- if (!isEnum(value, choices)) {
56
- const errorMessage = getErrorMessage('isEnum', value);
57
- throw new TypeCheckError(errorMessage);
58
- }
59
- return value;
60
- };
package/src/util/error.js DELETED
@@ -1,33 +0,0 @@
1
- const DEFAULT_EXCLUDES = new Set(['stack']);
2
-
3
- /**
4
- * Retrieves detailed information from an error object.
5
- * @param {Error} error - The error to extract details from.
6
- * @param {null | undefined | string[]} [excludes] - An array of property names to exclude from the result.
7
- * @returns {object} - An object containing the error details.
8
- */
9
- export const getErrorDetails = (error, excludes) => {
10
- const errorDetails = {
11
- name: error.name,
12
- type: error.constructor?.name ?? typeof error,
13
- };
14
- if (error.message) {
15
- errorDetails.message = error.message;
16
- }
17
- if (error.cause) {
18
- errorDetails.cause = error.cause;
19
- }
20
- for (const key of Object.getOwnPropertyNames(error)) {
21
- if (!excludes?.includes(key) && !DEFAULT_EXCLUDES.has(key)) {
22
- errorDetails[key] = error[key];
23
- }
24
- }
25
- return errorDetails;
26
- };
27
-
28
- /**
29
- * Get typed error from an unknown type.
30
- * @param {unknown} error - The error to cast.
31
- * @returns {Error} The typed error object.
32
- */
33
- export const getTypedError = (error) => (error instanceof Error ? error : new Error(String(error)));
@@ -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
- }
@@ -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
- };