@fgv/ts-utils 5.0.0-3 → 5.0.0-30

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 (164) hide show
  1. package/CHANGELOG.json +0 -21
  2. package/dist/ts-utils.d.ts +364 -41
  3. package/dist/tsdoc-metadata.json +1 -1
  4. package/eslint.config.js +37 -0
  5. package/lib/index.d.ts +2 -1
  6. package/lib/index.js +3 -1
  7. package/lib/packlets/base/index.d.ts +0 -2
  8. package/lib/packlets/base/index.js +1 -25
  9. package/lib/packlets/base/result.d.ts +61 -0
  10. package/lib/packlets/base/result.js +49 -0
  11. package/lib/packlets/collections/collector.d.ts +9 -0
  12. package/lib/packlets/collections/collector.js +6 -0
  13. package/lib/packlets/collections/validatingCollector.d.ts +5 -1
  14. package/lib/packlets/conversion/baseConverter.js +0 -1
  15. package/lib/packlets/conversion/converters.js +0 -2
  16. package/lib/packlets/conversion/objectConverter.js +1 -2
  17. package/lib/packlets/hash/crcNormalizer.js +3 -1
  18. package/lib/packlets/hash/hashingNormalizer.js +4 -3
  19. package/lib/packlets/logging/index.d.ts +3 -0
  20. package/lib/packlets/logging/index.js +40 -0
  21. package/lib/packlets/logging/logReporter.d.ts +88 -0
  22. package/lib/packlets/logging/logReporter.js +114 -0
  23. package/lib/packlets/logging/logger.d.ts +197 -0
  24. package/lib/packlets/logging/logger.js +244 -0
  25. package/lib/packlets/validation/array.d.ts +2 -1
  26. package/lib/packlets/validation/array.js +2 -1
  27. package/lib/packlets/validation/common.d.ts +0 -8
  28. package/lib/packlets/validation/field.d.ts +1 -1
  29. package/lib/packlets/validation/field.js +1 -1
  30. package/lib/packlets/validation/genericValidator.d.ts +1 -6
  31. package/lib/packlets/validation/genericValidator.js +11 -11
  32. package/lib/packlets/validation/object.d.ts +1 -1
  33. package/lib/packlets/validation/object.js +1 -2
  34. package/lib/packlets/validation/oneOf.d.ts +2 -1
  35. package/lib/packlets/validation/oneOf.js +2 -1
  36. package/lib/packlets/validation/typeGuard.d.ts +3 -2
  37. package/lib/packlets/validation/typeGuard.js +2 -1
  38. package/lib/packlets/validation/validator.d.ts +7 -0
  39. package/lib/packlets/validation/validatorBase.d.ts +2 -1
  40. package/lib/packlets/validation/validatorBase.js +1 -1
  41. package/lib/packlets/validation/validators.d.ts +2 -2
  42. package/lib/packlets/validation/validators.js +3 -3
  43. package/package.json +16 -16
  44. package/CHANGELOG.md +0 -135
  45. package/lib/index.d.ts.map +0 -1
  46. package/lib/index.js.map +0 -1
  47. package/lib/packlets/base/brand.d.ts.map +0 -1
  48. package/lib/packlets/base/brand.js.map +0 -1
  49. package/lib/packlets/base/index.d.ts.map +0 -1
  50. package/lib/packlets/base/index.js.map +0 -1
  51. package/lib/packlets/base/logger.d.ts +0 -52
  52. package/lib/packlets/base/logger.d.ts.map +0 -1
  53. package/lib/packlets/base/logger.js +0 -128
  54. package/lib/packlets/base/logger.js.map +0 -1
  55. package/lib/packlets/base/mapResults.d.ts.map +0 -1
  56. package/lib/packlets/base/mapResults.js.map +0 -1
  57. package/lib/packlets/base/messageAggregator.d.ts.map +0 -1
  58. package/lib/packlets/base/messageAggregator.js.map +0 -1
  59. package/lib/packlets/base/normalize.d.ts.map +0 -1
  60. package/lib/packlets/base/normalize.js.map +0 -1
  61. package/lib/packlets/base/result.d.ts.map +0 -1
  62. package/lib/packlets/base/result.js.map +0 -1
  63. package/lib/packlets/base/utils.d.ts.map +0 -1
  64. package/lib/packlets/base/utils.js.map +0 -1
  65. package/lib/packlets/collections/collectible.d.ts.map +0 -1
  66. package/lib/packlets/collections/collectible.js.map +0 -1
  67. package/lib/packlets/collections/collector.d.ts.map +0 -1
  68. package/lib/packlets/collections/collector.js.map +0 -1
  69. package/lib/packlets/collections/collectorValidator.d.ts.map +0 -1
  70. package/lib/packlets/collections/collectorValidator.js.map +0 -1
  71. package/lib/packlets/collections/common.d.ts.map +0 -1
  72. package/lib/packlets/collections/common.js.map +0 -1
  73. package/lib/packlets/collections/convertingCollector.d.ts.map +0 -1
  74. package/lib/packlets/collections/convertingCollector.js.map +0 -1
  75. package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +0 -1
  76. package/lib/packlets/collections/convertingCollectorValidator.js.map +0 -1
  77. package/lib/packlets/collections/index.d.ts.map +0 -1
  78. package/lib/packlets/collections/index.js.map +0 -1
  79. package/lib/packlets/collections/keyValueConverters.d.ts.map +0 -1
  80. package/lib/packlets/collections/keyValueConverters.js.map +0 -1
  81. package/lib/packlets/collections/readonlyResultMap.d.ts.map +0 -1
  82. package/lib/packlets/collections/readonlyResultMap.js.map +0 -1
  83. package/lib/packlets/collections/resultMap.d.ts.map +0 -1
  84. package/lib/packlets/collections/resultMap.js.map +0 -1
  85. package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
  86. package/lib/packlets/collections/resultMapValidator.js.map +0 -1
  87. package/lib/packlets/collections/utils.d.ts.map +0 -1
  88. package/lib/packlets/collections/utils.js.map +0 -1
  89. package/lib/packlets/collections/validatingCollector.d.ts.map +0 -1
  90. package/lib/packlets/collections/validatingCollector.js.map +0 -1
  91. package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +0 -1
  92. package/lib/packlets/collections/validatingConvertingCollector.js.map +0 -1
  93. package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
  94. package/lib/packlets/collections/validatingResultMap.js.map +0 -1
  95. package/lib/packlets/conversion/baseConverter.d.ts.map +0 -1
  96. package/lib/packlets/conversion/baseConverter.js.map +0 -1
  97. package/lib/packlets/conversion/converter.d.ts.map +0 -1
  98. package/lib/packlets/conversion/converter.js.map +0 -1
  99. package/lib/packlets/conversion/converters.d.ts.map +0 -1
  100. package/lib/packlets/conversion/converters.js.map +0 -1
  101. package/lib/packlets/conversion/defaultingConverter.d.ts.map +0 -1
  102. package/lib/packlets/conversion/defaultingConverter.js.map +0 -1
  103. package/lib/packlets/conversion/index.d.ts.map +0 -1
  104. package/lib/packlets/conversion/index.js.map +0 -1
  105. package/lib/packlets/conversion/objectConverter.d.ts.map +0 -1
  106. package/lib/packlets/conversion/objectConverter.js.map +0 -1
  107. package/lib/packlets/conversion/stringConverter.d.ts.map +0 -1
  108. package/lib/packlets/conversion/stringConverter.js.map +0 -1
  109. package/lib/packlets/file-tree/directoryItem.d.ts.map +0 -1
  110. package/lib/packlets/file-tree/directoryItem.js.map +0 -1
  111. package/lib/packlets/file-tree/fileItem.d.ts.map +0 -1
  112. package/lib/packlets/file-tree/fileItem.js.map +0 -1
  113. package/lib/packlets/file-tree/fileTree.d.ts.map +0 -1
  114. package/lib/packlets/file-tree/fileTree.js.map +0 -1
  115. package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +0 -1
  116. package/lib/packlets/file-tree/fileTreeAccessors.js.map +0 -1
  117. package/lib/packlets/file-tree/fsTree.d.ts.map +0 -1
  118. package/lib/packlets/file-tree/fsTree.js.map +0 -1
  119. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +0 -1
  120. package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +0 -1
  121. package/lib/packlets/file-tree/in-memory/index.d.ts.map +0 -1
  122. package/lib/packlets/file-tree/in-memory/index.js.map +0 -1
  123. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +0 -1
  124. package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +0 -1
  125. package/lib/packlets/file-tree/index.d.ts.map +0 -1
  126. package/lib/packlets/file-tree/index.js.map +0 -1
  127. package/lib/packlets/hash/crcNormalizer.d.ts.map +0 -1
  128. package/lib/packlets/hash/crcNormalizer.js.map +0 -1
  129. package/lib/packlets/hash/hashingNormalizer.d.ts.map +0 -1
  130. package/lib/packlets/hash/hashingNormalizer.js.map +0 -1
  131. package/lib/packlets/hash/index.d.ts.map +0 -1
  132. package/lib/packlets/hash/index.js.map +0 -1
  133. package/lib/packlets/validation/array.d.ts.map +0 -1
  134. package/lib/packlets/validation/array.js.map +0 -1
  135. package/lib/packlets/validation/boolean.d.ts.map +0 -1
  136. package/lib/packlets/validation/boolean.js.map +0 -1
  137. package/lib/packlets/validation/classes.d.ts.map +0 -1
  138. package/lib/packlets/validation/classes.js.map +0 -1
  139. package/lib/packlets/validation/common.d.ts.map +0 -1
  140. package/lib/packlets/validation/common.js.map +0 -1
  141. package/lib/packlets/validation/field.d.ts.map +0 -1
  142. package/lib/packlets/validation/field.js.map +0 -1
  143. package/lib/packlets/validation/genericValidator.d.ts.map +0 -1
  144. package/lib/packlets/validation/genericValidator.js.map +0 -1
  145. package/lib/packlets/validation/index.d.ts.map +0 -1
  146. package/lib/packlets/validation/index.js.map +0 -1
  147. package/lib/packlets/validation/number.d.ts.map +0 -1
  148. package/lib/packlets/validation/number.js.map +0 -1
  149. package/lib/packlets/validation/object.d.ts.map +0 -1
  150. package/lib/packlets/validation/object.js.map +0 -1
  151. package/lib/packlets/validation/oneOf.d.ts.map +0 -1
  152. package/lib/packlets/validation/oneOf.js.map +0 -1
  153. package/lib/packlets/validation/string.d.ts.map +0 -1
  154. package/lib/packlets/validation/string.js.map +0 -1
  155. package/lib/packlets/validation/traits.d.ts.map +0 -1
  156. package/lib/packlets/validation/traits.js.map +0 -1
  157. package/lib/packlets/validation/typeGuard.d.ts.map +0 -1
  158. package/lib/packlets/validation/typeGuard.js.map +0 -1
  159. package/lib/packlets/validation/validator.d.ts.map +0 -1
  160. package/lib/packlets/validation/validator.js.map +0 -1
  161. package/lib/packlets/validation/validatorBase.d.ts.map +0 -1
  162. package/lib/packlets/validation/validatorBase.js.map +0 -1
  163. package/lib/packlets/validation/validators.d.ts.map +0 -1
  164. package/lib/packlets/validation/validators.js.map +0 -1
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LogReporter = void 0;
4
+ /*
5
+ * Copyright (c) 2020 Erik Fortune
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ const logger_1 = require("./logger");
26
+ /**
27
+ * Abstract base class which wraps an existing {@link Logging.ILogger | ILogger} to implement
28
+ * both {@link Logging.ILogger | ILogger} and {@link IResultReporter | IResultReporter}.
29
+ * @public
30
+ */
31
+ class LogReporter {
32
+ /**
33
+ * Creates a new {@link Logging.LogReporter | LogReporter}.
34
+ * @param params - The parameters for creating the {@link Logging.LogReporter | LogReporter}.
35
+ */
36
+ constructor(params) {
37
+ var _a, _b, _c;
38
+ /* c8 ignore next 1 */
39
+ params = params !== null && params !== void 0 ? params : {};
40
+ this._logger = (_a = params.logger) !== null && _a !== void 0 ? _a : new logger_1.NoOpLogger();
41
+ this._valueFormatter = (_b = params.valueFormatter) !== null && _b !== void 0 ? _b : LogReporter.tryFormatObject;
42
+ this._messageFormatter = (_c = params.messageFormatter) !== null && _c !== void 0 ? _c : ((message, __detail) => message);
43
+ }
44
+ /**
45
+ * {@inheritDoc Logging.ILogger.logLevel}
46
+ */
47
+ get logLevel() {
48
+ return this._logger.logLevel;
49
+ }
50
+ /**
51
+ * {@inheritDoc Logging.ILogger.detail}
52
+ */
53
+ detail(message, ...parameters) {
54
+ return this._logger.detail(message, ...parameters);
55
+ }
56
+ /**
57
+ * {@inheritDoc Logging.ILogger.info}
58
+ */
59
+ info(message, ...parameters) {
60
+ return this._logger.info(message, ...parameters);
61
+ }
62
+ /**
63
+ * {@inheritDoc Logging.ILogger.warn}
64
+ */
65
+ warn(message, ...parameters) {
66
+ return this._logger.warn(message, ...parameters);
67
+ }
68
+ /**
69
+ * {@inheritDoc Logging.ILogger.error}
70
+ */
71
+ error(message, ...parameters) {
72
+ return this._logger.error(message, ...parameters);
73
+ }
74
+ /**
75
+ * {@inheritDoc Logging.ILogger.log}
76
+ */
77
+ log(level, message, ...parameters) {
78
+ return this._logger.log(level, message, ...parameters);
79
+ }
80
+ /**
81
+ * {@inheritDoc IResultReporter.reportSuccess}
82
+ */
83
+ reportSuccess(level, value, detail) {
84
+ const formatted = this._valueFormatter(value, detail);
85
+ this.log(level, formatted);
86
+ }
87
+ /**
88
+ * {@inheritDoc IResultReporter.reportFailure}
89
+ */
90
+ reportFailure(level, message, detail) {
91
+ const formatted = this._messageFormatter(message, detail);
92
+ this.log(level, formatted);
93
+ }
94
+ /**
95
+ * Generic method to try to format an object for logging.
96
+ * @param value - The value to format.
97
+ * @param detail - The detail to format.
98
+ * @returns
99
+ */
100
+ static tryFormatObject(value, detail) {
101
+ const message = String(value);
102
+ if (message === '[object Object]') {
103
+ try {
104
+ return JSON.stringify(value);
105
+ }
106
+ catch (error) {
107
+ return message;
108
+ }
109
+ }
110
+ return message;
111
+ }
112
+ }
113
+ exports.LogReporter = LogReporter;
114
+ //# sourceMappingURL=logReporter.js.map
@@ -0,0 +1,197 @@
1
+ import { MessageLogLevel, Success } from '../base';
2
+ /**
3
+ * The level of logging to be used.
4
+ * @public
5
+ */
6
+ export type ReporterLogLevel = 'all' | 'detail' | 'info' | 'warning' | 'error' | 'silent';
7
+ /**
8
+ * Compares two log levels.
9
+ * @param message - The first log level.
10
+ * @param reporter - The second log level.
11
+ * @returns `true` if the message should be logged, `false` if it should be suppressed.
12
+ * @public
13
+ */
14
+ export declare function shouldLog(message: MessageLogLevel, reporter: ReporterLogLevel): boolean;
15
+ /**
16
+ * Generic Result-aware logger interface with multiple levels of logging.
17
+ * @public
18
+ */
19
+ export interface ILogger {
20
+ /**
21
+ * The level of logging to be used.
22
+ */
23
+ readonly logLevel: ReporterLogLevel;
24
+ /**
25
+ * Logs a message at the given level.
26
+ * @param level - The level of the message.
27
+ * @param message - The message to log.
28
+ * @param parameters - The parameters to log.
29
+ * @returns `Success` with the logged message if the level is enabled, or
30
+ * `Success` with `undefined` if the message is suppressed.
31
+ */
32
+ log(level: MessageLogLevel, message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
33
+ /**
34
+ * Logs a detail message.
35
+ * @param message - The message to log.
36
+ * @param parameters - The parameters to log.
37
+ * @returns `Success` with the logged message if the level is enabled, or
38
+ * `Success` with `undefined` if the message is suppressed.
39
+ */
40
+ detail(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
41
+ /**
42
+ * Logs an info message.
43
+ * @param message - The message to log.
44
+ * @param parameters - The parameters to log.
45
+ * @returns `Success` with the logged message if the level is enabled, or
46
+ * `Success` with `undefined` if the message is suppressed.
47
+ */
48
+ info(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
49
+ /**
50
+ * Logs a warning message.
51
+ * @param message - The message to log.
52
+ * @param parameters - The parameters to log.
53
+ * @returns `Success` with the logged message if the level is enabled, or
54
+ * `Success` with `undefined` if the message is suppressed.
55
+ */
56
+ warn(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
57
+ /**
58
+ * Logs an error message.
59
+ * @param message - The message to log.
60
+ * @param parameters - The parameters to log.
61
+ * @returns `Success` with the logged message if the level is enabled, or
62
+ * `Success` with `undefined` if the message is suppressed.
63
+ */
64
+ error(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
65
+ }
66
+ /**
67
+ * Abstract base class which implements {@link Logging.ILogger | ILogger}.
68
+ * @public
69
+ */
70
+ export declare abstract class LoggerBase implements ILogger {
71
+ /**
72
+ * {@inheritDoc Logging.ILogger.logLevel}
73
+ */
74
+ logLevel: ReporterLogLevel;
75
+ protected constructor(logLevel?: ReporterLogLevel);
76
+ /**
77
+ * {@inheritDoc Logging.ILogger.detail}
78
+ */
79
+ detail(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
80
+ /**
81
+ * {@inheritDoc Logging.ILogger.info}
82
+ */
83
+ info(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
84
+ /**
85
+ * {@inheritDoc Logging.ILogger.warn}
86
+ */
87
+ warn(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
88
+ /**
89
+ * {@inheritDoc Logging.ILogger.error}
90
+ */
91
+ error(message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
92
+ /**
93
+ * {@inheritDoc Logging.ILogger.log}
94
+ */
95
+ log(level: MessageLogLevel, message?: unknown, ...parameters: unknown[]): Success<string | undefined>;
96
+ /**
97
+ * Formats a message and parameters into a string.
98
+ * @param message - The message to format.
99
+ * @param parameters - The parameters to format.
100
+ * @returns The formatted message.
101
+ * @public
102
+ */
103
+ protected _format(message?: unknown, ...parameters: unknown[]): string;
104
+ /**
105
+ * Inner method called for suppressed log messages.
106
+ * @public
107
+ */
108
+ protected _suppressLog(__level: MessageLogLevel, __message?: unknown, ...__parameters: unknown[]): Success<undefined>;
109
+ /**
110
+ * Inner method called for logged messages. Should be implemented by derived classes.
111
+ * @param message - The message to log.
112
+ * @param level - The {@link MessageLogLevel | level} of the message.
113
+ * @returns `Success` with the logged message, or `Success` with `undefined` if the message is suppressed.
114
+ * @public
115
+ */
116
+ protected abstract _log(message: string, level: MessageLogLevel): Success<string | undefined>;
117
+ }
118
+ /**
119
+ * An in-memory logger that stores logged and suppressed messages.
120
+ * @public
121
+ */
122
+ export declare class InMemoryLogger extends LoggerBase {
123
+ /**
124
+ * The messages that have been logged.
125
+ * @internal
126
+ */
127
+ private _logged;
128
+ /**
129
+ * The messages that have been suppressed.
130
+ * @internal
131
+ */
132
+ private _suppressed;
133
+ /**
134
+ * Creates a new in-memory logger.
135
+ * @param logLevel - The level of logging to be used.
136
+ */
137
+ constructor(logLevel?: ReporterLogLevel);
138
+ /**
139
+ * The messages that have been logged.
140
+ */
141
+ get logged(): string[];
142
+ /**
143
+ * The messages that have been suppressed.
144
+ */
145
+ get suppressed(): string[];
146
+ /**
147
+ * Clears the logged and suppressed messages.
148
+ */
149
+ clear(): void;
150
+ /**
151
+ * {@inheritDoc Logging.LoggerBase._log}
152
+ * @internal
153
+ */
154
+ protected _log(message: string, __level: MessageLogLevel): Success<string | undefined>;
155
+ /**
156
+ * {@inheritDoc Logging.LoggerBase._suppressLog}
157
+ * @param level - The level of the message.
158
+ * @param message - The message to suppress.
159
+ * @param parameters - The parameters to suppress.
160
+ * @returns `Success` with `undefined` if the message is suppressed.
161
+ * @internal
162
+ */
163
+ protected _suppressLog(level: MessageLogLevel, message?: unknown, ...parameters: unknown[]): Success<undefined>;
164
+ }
165
+ /**
166
+ * A console logger that outputs messages to the console.
167
+ * @public
168
+ */
169
+ export declare class ConsoleLogger extends LoggerBase {
170
+ /**
171
+ * Creates a new console logger.
172
+ * @param logLevel - The level of logging to be used.
173
+ */
174
+ constructor(logLevel?: ReporterLogLevel);
175
+ /**
176
+ * {@inheritDoc Logging.LoggerBase._log}
177
+ * @internal
178
+ */
179
+ protected _log(message: string, level: MessageLogLevel): Success<string | undefined>;
180
+ }
181
+ /**
182
+ * A no-op {@link Logging.LoggerBase | LoggerBase} that does not log anything.
183
+ * @public
184
+ */
185
+ export declare class NoOpLogger extends LoggerBase {
186
+ /**
187
+ * Creates a new no-op logger.
188
+ * @param logLevel - The level of logging to be used.
189
+ */
190
+ constructor(logLevel?: ReporterLogLevel);
191
+ /**
192
+ * {@inheritDoc Logging.LoggerBase._log}
193
+ * @internal
194
+ */
195
+ protected _log(message: string, __level: MessageLogLevel): Success<string | undefined>;
196
+ }
197
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoOpLogger = exports.ConsoleLogger = exports.InMemoryLogger = exports.LoggerBase = void 0;
4
+ exports.shouldLog = shouldLog;
5
+ /*
6
+ * Copyright (c) 2020 Erik Fortune
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ * of this software and associated documentation files (the "Software"), to deal
10
+ * in the Software without restriction, including without limitation the rights
11
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ * copies of the Software, and to permit persons to whom the Software is
13
+ * furnished to do so, subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all
16
+ * copies or substantial portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ * SOFTWARE.
25
+ */
26
+ const base_1 = require("../base");
27
+ /**
28
+ * Compares two log levels.
29
+ * @param message - The first log level.
30
+ * @param reporter - The second log level.
31
+ * @returns `true` if the message should be logged, `false` if it should be suppressed.
32
+ * @public
33
+ */
34
+ function shouldLog(message, reporter) {
35
+ if (reporter === 'all') {
36
+ return true; // 'all' logs everything, including 'quiet'
37
+ }
38
+ if (reporter === 'silent') {
39
+ return false; // 'silent' suppresses everything
40
+ }
41
+ if (message === 'quiet') {
42
+ return false; // 'quiet' messages only show when reporter is 'all'
43
+ }
44
+ switch (reporter) {
45
+ case 'error':
46
+ return message === 'error';
47
+ case 'warning':
48
+ return message === 'warning' || message === 'error';
49
+ case 'info':
50
+ return message !== 'detail';
51
+ }
52
+ return true;
53
+ }
54
+ /**
55
+ * Abstract base class which implements {@link Logging.ILogger | ILogger}.
56
+ * @public
57
+ */
58
+ class LoggerBase {
59
+ constructor(logLevel) {
60
+ /**
61
+ * {@inheritDoc Logging.ILogger.logLevel}
62
+ */
63
+ this.logLevel = 'info';
64
+ this.logLevel = logLevel !== null && logLevel !== void 0 ? logLevel : 'info';
65
+ }
66
+ /**
67
+ * {@inheritDoc Logging.ILogger.detail}
68
+ */
69
+ detail(message, ...parameters) {
70
+ return this.log('detail', message, ...parameters);
71
+ }
72
+ /**
73
+ * {@inheritDoc Logging.ILogger.info}
74
+ */
75
+ info(message, ...parameters) {
76
+ return this.log('info', message, ...parameters);
77
+ }
78
+ /**
79
+ * {@inheritDoc Logging.ILogger.warn}
80
+ */
81
+ warn(message, ...parameters) {
82
+ return this.log('warning', message, ...parameters);
83
+ }
84
+ /**
85
+ * {@inheritDoc Logging.ILogger.error}
86
+ */
87
+ error(message, ...parameters) {
88
+ return this.log('error', message, ...parameters);
89
+ }
90
+ /**
91
+ * {@inheritDoc Logging.ILogger.log}
92
+ */
93
+ log(level, message, ...parameters) {
94
+ if (shouldLog(level, this.logLevel)) {
95
+ const formatted = this._format(message, ...parameters);
96
+ return this._log(formatted, level);
97
+ }
98
+ return this._suppressLog(level, message, ...parameters);
99
+ }
100
+ /**
101
+ * Formats a message and parameters into a string.
102
+ * @param message - The message to format.
103
+ * @param parameters - The parameters to format.
104
+ * @returns The formatted message.
105
+ * @public
106
+ */
107
+ _format(message, ...parameters) {
108
+ const raw = [message, ...parameters];
109
+ const filtered = raw.filter((m) => m !== undefined);
110
+ const strings = filtered.map((m) => m.toString());
111
+ const joined = strings.join('');
112
+ return joined;
113
+ }
114
+ /**
115
+ * Inner method called for suppressed log messages.
116
+ * @public
117
+ */
118
+ _suppressLog(__level, __message, ...__parameters) {
119
+ return (0, base_1.succeed)(undefined);
120
+ }
121
+ }
122
+ exports.LoggerBase = LoggerBase;
123
+ /**
124
+ * An in-memory logger that stores logged and suppressed messages.
125
+ * @public
126
+ */
127
+ class InMemoryLogger extends LoggerBase {
128
+ /**
129
+ * Creates a new in-memory logger.
130
+ * @param logLevel - The level of logging to be used.
131
+ */
132
+ constructor(logLevel) {
133
+ super(logLevel);
134
+ /**
135
+ * The messages that have been logged.
136
+ * @internal
137
+ */
138
+ this._logged = [];
139
+ /**
140
+ * The messages that have been suppressed.
141
+ * @internal
142
+ */
143
+ this._suppressed = [];
144
+ }
145
+ /**
146
+ * The messages that have been logged.
147
+ */
148
+ get logged() {
149
+ return this._logged;
150
+ }
151
+ /**
152
+ * The messages that have been suppressed.
153
+ */
154
+ get suppressed() {
155
+ return this._suppressed;
156
+ }
157
+ /**
158
+ * Clears the logged and suppressed messages.
159
+ */
160
+ clear() {
161
+ this._logged = [];
162
+ this._suppressed = [];
163
+ }
164
+ /**
165
+ * {@inheritDoc Logging.LoggerBase._log}
166
+ * @internal
167
+ */
168
+ _log(message, __level) {
169
+ this._logged.push(message);
170
+ return (0, base_1.succeed)(message);
171
+ }
172
+ /**
173
+ * {@inheritDoc Logging.LoggerBase._suppressLog}
174
+ * @param level - The level of the message.
175
+ * @param message - The message to suppress.
176
+ * @param parameters - The parameters to suppress.
177
+ * @returns `Success` with `undefined` if the message is suppressed.
178
+ * @internal
179
+ */
180
+ _suppressLog(level, message, ...parameters) {
181
+ const formatted = this._format(message, ...parameters);
182
+ this._suppressed.push(formatted);
183
+ return (0, base_1.succeed)(undefined);
184
+ }
185
+ }
186
+ exports.InMemoryLogger = InMemoryLogger;
187
+ /**
188
+ * A console logger that outputs messages to the console.
189
+ * @public
190
+ */
191
+ class ConsoleLogger extends LoggerBase {
192
+ /**
193
+ * Creates a new console logger.
194
+ * @param logLevel - The level of logging to be used.
195
+ */
196
+ constructor(logLevel) {
197
+ super(logLevel);
198
+ }
199
+ /**
200
+ * {@inheritDoc Logging.LoggerBase._log}
201
+ * @internal
202
+ */
203
+ _log(message, level) {
204
+ switch (level) {
205
+ case 'error':
206
+ console.error(message);
207
+ break;
208
+ case 'warning':
209
+ console.warn(message);
210
+ break;
211
+ case 'info':
212
+ console.info(message);
213
+ break;
214
+ default:
215
+ console.log(message);
216
+ break;
217
+ }
218
+ return (0, base_1.succeed)(message);
219
+ }
220
+ }
221
+ exports.ConsoleLogger = ConsoleLogger;
222
+ /**
223
+ * A no-op {@link Logging.LoggerBase | LoggerBase} that does not log anything.
224
+ * @public
225
+ */
226
+ class NoOpLogger extends LoggerBase {
227
+ /**
228
+ * Creates a new no-op logger.
229
+ * @param logLevel - The level of logging to be used.
230
+ */
231
+ constructor(logLevel) {
232
+ super(logLevel);
233
+ }
234
+ /**
235
+ * {@inheritDoc Logging.LoggerBase._log}
236
+ * @internal
237
+ */
238
+ _log(message, __level) {
239
+ // no-op
240
+ return (0, base_1.succeed)(message);
241
+ }
242
+ }
243
+ exports.NoOpLogger = NoOpLogger;
244
+ //# sourceMappingURL=logger.js.map
@@ -32,9 +32,10 @@ export declare class ArrayValidator<T, TC = unknown> extends ValidatorBase<T[],
32
32
  * validator.
33
33
  * @param from - The `unknown` value to be tested.
34
34
  * @param context - Optional validation context will be propagated to element validator.
35
+ * @param self - Optional self-reference for recursive validation.
35
36
  * @returns Returns `true` if `from` is an `array` of valid elements, or
36
37
  * {@link Failure} with an error message if not.
37
38
  */
38
- protected _validate<T>(from: unknown, context?: TC): boolean | Failure<T>;
39
+ protected _validate(from: unknown, context?: TC, self?: Validator<T[], TC>): boolean | Failure<T[]>;
39
40
  }
40
41
  //# sourceMappingURL=array.d.ts.map
@@ -47,10 +47,11 @@ class ArrayValidator extends validatorBase_1.ValidatorBase {
47
47
  * validator.
48
48
  * @param from - The `unknown` value to be tested.
49
49
  * @param context - Optional validation context will be propagated to element validator.
50
+ * @param self - Optional self-reference for recursive validation.
50
51
  * @returns Returns `true` if `from` is an `array` of valid elements, or
51
52
  * {@link Failure} with an error message if not.
52
53
  */
53
- _validate(from, context) {
54
+ _validate(from, context, self) {
54
55
  if (Array.isArray(from)) {
55
56
  for (const elem of from) {
56
57
  const result = this._validateElement.validate(elem, context);
@@ -1,15 +1,7 @@
1
- import { Failure } from '../base';
2
1
  /**
3
2
  * A type guard function which validates a specific type, with an optional context
4
3
  * that can be used to shape the validation.
5
4
  * @public
6
5
  */
7
6
  export type TypeGuardWithContext<T, TC = unknown> = (from: unknown, context?: TC) => from is T;
8
- /**
9
- * Type for a validation function, which validates that a supplied `unknown`
10
- * value is a valid value of type `<T>`, possibly as influenced by
11
- * an optionally-supplied validation context of type `<TC>`.
12
- * @public
13
- */
14
- export type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;
15
7
  //# sourceMappingURL=common.d.ts.map
@@ -38,6 +38,6 @@ export declare class FieldValidator<T, TC = unknown> extends ValidatorBase<T, TC
38
38
  /**
39
39
  * {@inheritdoc Validation.ValidatorBase._validate}
40
40
  */
41
- protected _validate(from: unknown, context?: TC): boolean | Failure<T>;
41
+ protected _validate(from: unknown, context?: TC, self?: Validator<T, TC>): boolean | Failure<T>;
42
42
  }
43
43
  //# sourceMappingURL=field.d.ts.map
@@ -48,7 +48,7 @@ class FieldValidator extends validatorBase_1.ValidatorBase {
48
48
  /**
49
49
  * {@inheritdoc Validation.ValidatorBase._validate}
50
50
  */
51
- _validate(from, context) {
51
+ _validate(from, context, self) {
52
52
  if (typeof from === 'object' && !Array.isArray(from) && from !== null) {
53
53
  const optional = this._fieldOptions.optional === true;
54
54
  if ((0, base_1.isKeyOf)(this.fieldName, from)) {
@@ -1,11 +1,6 @@
1
1
  import { Brand, Result } from '../base';
2
2
  import { ConstraintTrait, ValidatorTraits } from './traits';
3
- import { Constraint, ValidationErrorFormatter, Validator, ValidatorOptions } from './validator';
4
- import { ValidatorFunc } from './common';
5
- /**
6
- * @deprecated Use {@link Validation.Common.ValidatorFunc | Validation.Common.ValidatorFunc} instead.
7
- */
8
- export { ValidatorFunc };
3
+ import { Constraint, ValidationErrorFormatter, Validator, ValidatorFunc, ValidatorOptions } from './validator';
9
4
  /**
10
5
  * Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.
11
6
  * @public