@fgv/ts-utils 5.0.0-24 → 5.0.0-26

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.
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.52.10"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,37 @@
1
+ // ESLint 9 flat config for ts-utils
2
+ const nodeProfile = require('@rushstack/eslint-config/flat/profile/node');
3
+ const packletsPlugin = require('@rushstack/eslint-config/flat/mixins/packlets');
4
+ const tsdocPlugin = require('@rushstack/eslint-config/flat/mixins/tsdoc');
5
+
6
+ module.exports = [
7
+ ...nodeProfile,
8
+ packletsPlugin,
9
+ ...tsdocPlugin,
10
+ {
11
+ // Override specific rules if needed
12
+ rules: {
13
+ '@rushstack/packlets/mechanics': 'warn',
14
+
15
+ // Tighten naming conventions for interface properties
16
+ '@typescript-eslint/naming-convention': [
17
+ 'warn',
18
+ // Keep all the base rules from rushstack but add stricter property rules
19
+ {
20
+ selector: 'property',
21
+ format: ['camelCase', 'UPPER_CASE'],
22
+ leadingUnderscore: 'allow',
23
+ filter: {
24
+ // Only allow quoted identifiers that truly need special chars (not just hyphens)
25
+ regex: '^__',
26
+ match: false
27
+ }
28
+ },
29
+ {
30
+ selector: 'method',
31
+ format: ['camelCase', 'PascalCase'],
32
+ leadingUnderscore: 'allow'
33
+ }
34
+ ]
35
+ }
36
+ }
37
+ ];
@@ -50,7 +50,7 @@ export type MessageLogLevel = 'quiet' | 'detail' | 'info' | 'warning' | 'error';
50
50
  * Options for reporting a result.
51
51
  * @public
52
52
  */
53
- export interface IResultReportOptions {
53
+ export interface IResultReportOptions<TD = unknown> {
54
54
  /**
55
55
  * The level of reporting to be used for failure results. Default is 'error'.
56
56
  */
@@ -59,6 +59,10 @@ export interface IResultReportOptions {
59
59
  * The level of reporting to be used for success results. Default is 'quiet'.
60
60
  */
61
61
  success?: MessageLogLevel;
62
+ /**
63
+ * The error formatter to be used for reporting an error result.
64
+ */
65
+ message?: ErrorFormatter<TD>;
62
66
  }
63
67
  /**
64
68
  * Interface for reporting a result.
@@ -269,7 +273,7 @@ export interface IResult<T> {
269
273
  * @param reporter - The {@link IResultReporter | reporter} to which the result will be reported.
270
274
  * @param options - The {@link IResultReportOptions | options} for reporting the result.
271
275
  */
272
- report(reporter: IResultReporter<T>, options?: IResultReportOptions): this;
276
+ report(reporter?: IResultReporter<T>, options?: IResultReportOptions): this;
273
277
  }
274
278
  /**
275
279
  * Reports a successful {@link IResult | result} from some operation and the
@@ -359,7 +363,7 @@ export declare class Success<T> implements IResult<T> {
359
363
  /**
360
364
  * {@inheritdoc IResult.report}
361
365
  */
362
- report(reporter: IResultReporter<T>, options?: IResultReportOptions): this;
366
+ report(reporter?: IResultReporter<T>, options?: IResultReportOptions): this;
363
367
  /**
364
368
  * Creates a {@link Success | Success<T>} with the supplied value.
365
369
  * @param value - The value to be returned.
@@ -455,7 +459,7 @@ export declare class Failure<T> implements IResult<T> {
455
459
  /**
456
460
  * {@inheritdoc IResult.report}
457
461
  */
458
- report(reporter: IResultReporter<T>, options?: IResultReportOptions): this;
462
+ report(reporter?: IResultReporter<T>, options?: IResultReportOptions): this;
459
463
  /**
460
464
  * Get a 'friendly' string representation of this object.
461
465
  * @remarks
@@ -581,7 +585,7 @@ export declare class DetailedSuccess<T, TD> extends Success<T> {
581
585
  /**
582
586
  * {@inheritdoc IResult.report}
583
587
  */
584
- report(reporter: IResultReporter<T, TD>, options?: IResultReportOptions): this;
588
+ report(reporter?: IResultReporter<T, TD>, options?: IResultReportOptions<TD>): this;
585
589
  /**
586
590
  * Creates a {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value and
587
591
  * optional detail.
@@ -647,7 +651,7 @@ export declare class DetailedFailure<T, TD> extends Failure<T> {
647
651
  /**
648
652
  * {@inheritdoc IResult.report}
649
653
  */
650
- report(reporter: IResultReporter<T, TD>, options?: IResultReportOptions): this;
654
+ report(reporter?: IResultReporter<T, TD>, options?: IResultReportOptions<TD>): this;
651
655
  orThrow(logOrFormat?: IResultLogger<TD> | ErrorFormatter<TD>): never;
652
656
  orThrow(cb: ErrorFormatter): never;
653
657
  /**
@@ -136,7 +136,7 @@ class Success {
136
136
  report(reporter, options) {
137
137
  var _a;
138
138
  const level = (_a = options === null || options === void 0 ? void 0 : options.success) !== null && _a !== void 0 ? _a : 'quiet';
139
- reporter.reportSuccess(level, this._value);
139
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportSuccess(level, this._value);
140
140
  return this;
141
141
  }
142
142
  /**
@@ -260,9 +260,10 @@ class Failure {
260
260
  * {@inheritdoc IResult.report}
261
261
  */
262
262
  report(reporter, options) {
263
- var _a;
263
+ var _a, _b, _c;
264
264
  const level = (_a = options === null || options === void 0 ? void 0 : options.failure) !== null && _a !== void 0 ? _a : 'error';
265
- reporter.reportFailure(level, this._message);
265
+ const message = (_c = (_b = options === null || options === void 0 ? void 0 : options.message) === null || _b === void 0 ? void 0 : _b.call(options, this._message)) !== null && _c !== void 0 ? _c : this._message;
266
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportFailure(level, message);
266
267
  return this;
267
268
  }
268
269
  /**
@@ -400,7 +401,7 @@ class DetailedSuccess extends Success {
400
401
  report(reporter, options) {
401
402
  var _a;
402
403
  const level = (_a = options === null || options === void 0 ? void 0 : options.success) !== null && _a !== void 0 ? _a : 'quiet';
403
- reporter.reportSuccess(level, this._value, this._detail);
404
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportSuccess(level, this._value, this._detail);
404
405
  return this;
405
406
  }
406
407
  /**
@@ -483,9 +484,10 @@ class DetailedFailure extends Failure {
483
484
  * {@inheritdoc IResult.report}
484
485
  */
485
486
  report(reporter, options) {
486
- var _a;
487
+ var _a, _b, _c;
487
488
  const level = (_a = options === null || options === void 0 ? void 0 : options.failure) !== null && _a !== void 0 ? _a : 'error';
488
- reporter.reportFailure(level, this._message, this._detail);
489
+ const message = (_c = (_b = options === null || options === void 0 ? void 0 : options.message) === null || _b === void 0 ? void 0 : _b.call(options, this._message, this._detail)) !== null && _c !== void 0 ? _c : this._message;
490
+ reporter === null || reporter === void 0 ? void 0 : reporter.reportFailure(level, message, this._detail);
489
491
  return this;
490
492
  }
491
493
  orThrow(logOrFormat) {
@@ -106,7 +106,6 @@ class BaseConverter {
106
106
  return mapConverter.convert(innerResult.value, this._context(context));
107
107
  }
108
108
  return (0, base_1.fail)(innerResult.message);
109
- // eslint-disable-next-line no-return-assign
110
109
  })._with(this._traits());
111
110
  }
112
111
  /**
@@ -580,7 +580,6 @@ function discriminatedObject(discriminatorProp, converters) {
580
580
  function transform(properties) {
581
581
  return new baseConverter_1.BaseConverter((from, __self, context) => {
582
582
  // eslint bug thinks key is used before defined
583
- // eslint-disable-next-line no-use-before-define
584
583
  const converted = {};
585
584
  const errors = [];
586
585
  for (const key in properties) {
@@ -620,7 +619,6 @@ function transform(properties) {
620
619
  function transformObject(destinationFields, options) {
621
620
  return new baseConverter_1.BaseConverter((from, __self, context) => {
622
621
  // eslint bug thinks key is used before defined
623
- // eslint-disable-next-line no-use-before-define
624
622
  const converted = {};
625
623
  const errors = [];
626
624
  const used = new Set(options === null || options === void 0 ? void 0 : options.ignore);
@@ -104,9 +104,8 @@ class ObjectConverter extends baseConverter_1.BaseConverter {
104
104
  return new ObjectConverter(this.fields, options)._with(this._traits());
105
105
  }
106
106
  static _convert(from, context, fields, options) {
107
- var _a;
108
107
  // eslint bug thinks key is used before defined
109
- // eslint-disable-next-line no-use-before-define
108
+ var _a;
110
109
  const converted = {};
111
110
  const errors = [];
112
111
  for (const key in fields) {
@@ -15,7 +15,7 @@ export type LogMessageFormatter<TD = unknown> = (message: string, detail?: TD) =
15
15
  * @public
16
16
  */
17
17
  export interface ILogReporterCreateParams<T, TD = unknown> {
18
- logger: ILogger;
18
+ logger?: ILogger;
19
19
  valueFormatter?: LogValueFormatter<T, TD>;
20
20
  messageFormatter?: LogMessageFormatter<TD>;
21
21
  }
@@ -25,14 +25,26 @@ export interface ILogReporterCreateParams<T, TD = unknown> {
25
25
  * @public
26
26
  */
27
27
  export declare class LogReporter<T, TD = unknown> implements ILogger, IResultReporter<T, TD> {
28
+ /**
29
+ * The logger to wrap.
30
+ * @internal
31
+ */
28
32
  protected readonly _logger: ILogger;
33
+ /**
34
+ * The formatter to use for values.
35
+ * @internal
36
+ */
29
37
  protected readonly _valueFormatter: LogValueFormatter<T, TD>;
38
+ /**
39
+ * The formatter to use for messages.
40
+ * @internal
41
+ */
30
42
  protected readonly _messageFormatter: LogMessageFormatter<TD>;
31
43
  /**
32
44
  * Creates a new {@link Logging.LogReporter | LogReporter}.
33
45
  * @param params - The parameters for creating the {@link Logging.LogReporter | LogReporter}.
34
46
  */
35
- constructor(params: ILogReporterCreateParams<T, TD>);
47
+ constructor(params?: ILogReporterCreateParams<T, TD>);
36
48
  /**
37
49
  * {@inheritDoc Logging.ILogger.logLevel}
38
50
  */
@@ -65,5 +77,12 @@ export declare class LogReporter<T, TD = unknown> implements ILogger, IResultRep
65
77
  * {@inheritDoc IResultReporter.reportFailure}
66
78
  */
67
79
  reportFailure(level: MessageLogLevel, message: string, detail?: TD): void;
80
+ /**
81
+ * Generic method to try to format an object for logging.
82
+ * @param value - The value to format.
83
+ * @param detail - The detail to format.
84
+ * @returns
85
+ */
86
+ static tryFormatObject<T = unknown, TD = unknown>(value: T, detail?: TD): string;
68
87
  }
69
88
  //# sourceMappingURL=logReporter.d.ts.map
@@ -1,6 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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");
4
26
  /**
5
27
  * Abstract base class which wraps an existing {@link Logging.ILogger | ILogger} to implement
6
28
  * both {@link Logging.ILogger | ILogger} and {@link IResultReporter | IResultReporter}.
@@ -12,10 +34,12 @@ class LogReporter {
12
34
  * @param params - The parameters for creating the {@link Logging.LogReporter | LogReporter}.
13
35
  */
14
36
  constructor(params) {
15
- var _a, _b;
16
- this._logger = params.logger;
17
- this._valueFormatter = (_a = params.valueFormatter) !== null && _a !== void 0 ? _a : ((message, __detail) => String(message));
18
- this._messageFormatter = (_b = params.messageFormatter) !== null && _b !== void 0 ? _b : ((message, __detail) => message);
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);
19
43
  }
20
44
  /**
21
45
  * {@inheritDoc Logging.ILogger.logLevel}
@@ -67,6 +91,24 @@ class LogReporter {
67
91
  const formatted = this._messageFormatter(message, detail);
68
92
  this.log(level, formatted);
69
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
+ }
70
112
  }
71
113
  exports.LogReporter = LogReporter;
72
114
  //# sourceMappingURL=logReporter.js.map
@@ -103,8 +103,16 @@ export declare abstract class LoggerBase implements ILogger {
103
103
  protected _format(message?: unknown, ...parameters: unknown[]): string;
104
104
  /**
105
105
  * Inner method called for suppressed log messages.
106
+ * @public
106
107
  */
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
+ */
108
116
  protected abstract _log(message: string, level: MessageLogLevel): Success<string | undefined>;
109
117
  }
110
118
  /**
@@ -114,12 +122,14 @@ export declare abstract class LoggerBase implements ILogger {
114
122
  export declare class InMemoryLogger extends LoggerBase {
115
123
  /**
116
124
  * The messages that have been logged.
125
+ * @internal
117
126
  */
118
- protected _logged: string[];
127
+ private _logged;
119
128
  /**
120
129
  * The messages that have been suppressed.
130
+ * @internal
121
131
  */
122
- protected _suppressed: string[];
132
+ private _suppressed;
123
133
  /**
124
134
  * Creates a new in-memory logger.
125
135
  * @param logLevel - The level of logging to be used.
@@ -139,6 +149,7 @@ export declare class InMemoryLogger extends LoggerBase {
139
149
  clear(): void;
140
150
  /**
141
151
  * {@inheritDoc Logging.LoggerBase._log}
152
+ * @internal
142
153
  */
143
154
  protected _log(message: string, __level: MessageLogLevel): Success<string | undefined>;
144
155
  /**
@@ -147,6 +158,7 @@ export declare class InMemoryLogger extends LoggerBase {
147
158
  * @param message - The message to suppress.
148
159
  * @param parameters - The parameters to suppress.
149
160
  * @returns `Success` with `undefined` if the message is suppressed.
161
+ * @internal
150
162
  */
151
163
  protected _suppressLog(level: MessageLogLevel, message?: unknown, ...parameters: unknown[]): Success<undefined>;
152
164
  }
@@ -162,6 +174,7 @@ export declare class ConsoleLogger extends LoggerBase {
162
174
  constructor(logLevel?: ReporterLogLevel);
163
175
  /**
164
176
  * {@inheritDoc Logging.LoggerBase._log}
177
+ * @internal
165
178
  */
166
179
  protected _log(message: string, level: MessageLogLevel): Success<string | undefined>;
167
180
  }
@@ -177,6 +190,7 @@ export declare class NoOpLogger extends LoggerBase {
177
190
  constructor(logLevel?: ReporterLogLevel);
178
191
  /**
179
192
  * {@inheritDoc Logging.LoggerBase._log}
193
+ * @internal
180
194
  */
181
195
  protected _log(message: string, __level: MessageLogLevel): Success<string | undefined>;
182
196
  }
@@ -113,6 +113,7 @@ class LoggerBase {
113
113
  }
114
114
  /**
115
115
  * Inner method called for suppressed log messages.
116
+ * @public
116
117
  */
117
118
  _suppressLog(__level, __message, ...__parameters) {
118
119
  return (0, base_1.succeed)(undefined);
@@ -132,10 +133,12 @@ class InMemoryLogger extends LoggerBase {
132
133
  super(logLevel);
133
134
  /**
134
135
  * The messages that have been logged.
136
+ * @internal
135
137
  */
136
138
  this._logged = [];
137
139
  /**
138
140
  * The messages that have been suppressed.
141
+ * @internal
139
142
  */
140
143
  this._suppressed = [];
141
144
  }
@@ -160,6 +163,7 @@ class InMemoryLogger extends LoggerBase {
160
163
  }
161
164
  /**
162
165
  * {@inheritDoc Logging.LoggerBase._log}
166
+ * @internal
163
167
  */
164
168
  _log(message, __level) {
165
169
  this._logged.push(message);
@@ -171,6 +175,7 @@ class InMemoryLogger extends LoggerBase {
171
175
  * @param message - The message to suppress.
172
176
  * @param parameters - The parameters to suppress.
173
177
  * @returns `Success` with `undefined` if the message is suppressed.
178
+ * @internal
174
179
  */
175
180
  _suppressLog(level, message, ...parameters) {
176
181
  const formatted = this._format(message, ...parameters);
@@ -193,6 +198,7 @@ class ConsoleLogger extends LoggerBase {
193
198
  }
194
199
  /**
195
200
  * {@inheritDoc Logging.LoggerBase._log}
201
+ * @internal
196
202
  */
197
203
  _log(message, level) {
198
204
  switch (level) {
@@ -227,6 +233,7 @@ class NoOpLogger extends LoggerBase {
227
233
  }
228
234
  /**
229
235
  * {@inheritDoc Logging.LoggerBase._log}
236
+ * @internal
230
237
  */
231
238
  _log(message, __level) {
232
239
  // no-op
@@ -114,7 +114,6 @@ class ObjectValidator extends validatorBase_1.ValidatorBase {
114
114
  return (0, base_1.fail)('source is not an object');
115
115
  }
116
116
  // eslint bug thinks key is used before defined
117
- // eslint-disable-next-line no-use-before-define
118
117
  const converted = {};
119
118
  const errors = [];
120
119
  for (const key in this._innerValidators) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
- "version": "5.0.0-24",
3
+ "version": "5.0.0-26",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-utils.d.ts",
@@ -23,20 +23,20 @@
23
23
  "@types/luxon": "^3.7.1",
24
24
  "@types/mustache": "^4.2.5",
25
25
  "@types/node": "^20.14.9",
26
- "@typescript-eslint/eslint-plugin": "^7.14.1",
27
- "@typescript-eslint/parser": "^7.14.1",
28
- "eslint": "^8.57.0",
26
+ "@typescript-eslint/eslint-plugin": "^8.42.0",
27
+ "@typescript-eslint/parser": "^8.42.0",
28
+ "eslint": "^9.35.0",
29
29
  "eslint-plugin-import": "^2.32.0",
30
30
  "eslint-plugin-node": "^11.1.0",
31
- "eslint-plugin-promise": "^6.2.0",
31
+ "eslint-plugin-promise": "^7.2.1",
32
32
  "jest": "^29.7.0",
33
33
  "jest-extended": "^4.0.2",
34
34
  "jest-matcher-utils": "^29.7.0",
35
- "rimraf": "^5.0.7",
35
+ "rimraf": "^6.0.1",
36
36
  "ts-jest": "^29.4.1",
37
37
  "ts-node": "^10.9.2",
38
38
  "typescript": "5.8.3",
39
- "eslint-plugin-n": "^16.6.2",
39
+ "eslint-plugin-n": "^17.21.3",
40
40
  "jest-snapshot": "~29.7.0",
41
41
  "@rushstack/heft": "0.74.3",
42
42
  "@rushstack/heft-node-rig": "2.9.4",