@fgv/ts-utils 5.0.2 → 5.1.0-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 (104) hide show
  1. package/README.md +152 -84
  2. package/dist/index.js +2 -2
  3. package/dist/packlets/base/messageAggregator.js +6 -6
  4. package/dist/packlets/base/result.js +39 -39
  5. package/dist/packlets/base/utils.js +47 -0
  6. package/dist/packlets/collections/aggregatedResultMap.js +722 -0
  7. package/dist/packlets/collections/collector.js +9 -9
  8. package/dist/packlets/collections/collectorValidator.js +4 -4
  9. package/dist/packlets/collections/converters.js +39 -0
  10. package/dist/packlets/collections/convertingCollectorValidator.js +3 -3
  11. package/dist/packlets/collections/convertingResultMap.js +208 -0
  12. package/dist/packlets/collections/index.js +4 -0
  13. package/dist/packlets/collections/keyValueConverters.js +1 -4
  14. package/dist/packlets/collections/readOnlyConvertingResultMap.js +193 -0
  15. package/dist/packlets/collections/resultMapValidator.js +50 -6
  16. package/dist/packlets/collections/validatingConvertingResultMap.js +78 -0
  17. package/dist/packlets/conversion/advancedConverters.js +249 -0
  18. package/dist/packlets/conversion/baseConverter.js +23 -16
  19. package/dist/packlets/conversion/basicConverters.js +571 -0
  20. package/dist/packlets/conversion/converters.js +3 -617
  21. package/dist/packlets/conversion/defaultingConverter.js +21 -15
  22. package/dist/packlets/logging/bootLogger.js +122 -0
  23. package/dist/packlets/logging/index.js +1 -0
  24. package/dist/packlets/logging/logReporter.js +32 -1
  25. package/dist/packlets/logging/logger.js +25 -5
  26. package/dist/packlets/validation/classes.js +1 -0
  27. package/dist/packlets/validation/compositeId.js +60 -0
  28. package/dist/packlets/validation/field.js +1 -1
  29. package/dist/packlets/validation/genericValidator.js +24 -10
  30. package/dist/packlets/validation/object.js +3 -6
  31. package/dist/packlets/validation/validators.js +12 -0
  32. package/dist/ts-utils.d.ts +1633 -200
  33. package/dist/tsdoc-metadata.json +1 -1
  34. package/lib/index.d.ts +2 -2
  35. package/lib/index.js +2 -1
  36. package/lib/packlets/base/mapResults.d.ts +3 -3
  37. package/lib/packlets/base/messageAggregator.d.ts +6 -6
  38. package/lib/packlets/base/messageAggregator.js +6 -6
  39. package/lib/packlets/base/result.d.ts +74 -51
  40. package/lib/packlets/base/result.js +39 -39
  41. package/lib/packlets/base/utils.d.ts +42 -0
  42. package/lib/packlets/base/utils.js +52 -0
  43. package/lib/packlets/collections/aggregatedResultMap.d.ts +433 -0
  44. package/lib/packlets/collections/aggregatedResultMap.js +728 -0
  45. package/lib/packlets/collections/collector.d.ts +11 -10
  46. package/lib/packlets/collections/collector.js +9 -9
  47. package/lib/packlets/collections/collectorValidator.d.ts +12 -10
  48. package/lib/packlets/collections/collectorValidator.js +4 -4
  49. package/lib/packlets/collections/converters.d.ts +18 -0
  50. package/lib/packlets/collections/converters.js +42 -0
  51. package/lib/packlets/collections/convertingCollector.d.ts +4 -4
  52. package/lib/packlets/collections/convertingCollectorValidator.d.ts +10 -7
  53. package/lib/packlets/collections/convertingCollectorValidator.js +3 -3
  54. package/lib/packlets/collections/convertingResultMap.d.ts +176 -0
  55. package/lib/packlets/collections/convertingResultMap.js +213 -0
  56. package/lib/packlets/collections/index.d.ts +4 -0
  57. package/lib/packlets/collections/index.js +4 -0
  58. package/lib/packlets/collections/keyValueConverters.d.ts +1 -4
  59. package/lib/packlets/collections/keyValueConverters.js +1 -4
  60. package/lib/packlets/collections/readOnlyConvertingResultMap.d.ts +153 -0
  61. package/lib/packlets/collections/readOnlyConvertingResultMap.js +197 -0
  62. package/lib/packlets/collections/readonlyResultMap.d.ts +7 -7
  63. package/lib/packlets/collections/resultMap.d.ts +108 -2
  64. package/lib/packlets/collections/resultMapValidator.d.ts +45 -11
  65. package/lib/packlets/collections/resultMapValidator.js +52 -7
  66. package/lib/packlets/collections/validatingCollector.d.ts +5 -5
  67. package/lib/packlets/collections/validatingConvertingCollector.d.ts +3 -3
  68. package/lib/packlets/collections/validatingConvertingResultMap.d.ts +102 -0
  69. package/lib/packlets/collections/validatingConvertingResultMap.js +83 -0
  70. package/lib/packlets/collections/validatingResultMap.d.ts +1 -1
  71. package/lib/packlets/conversion/advancedConverters.d.ts +170 -0
  72. package/lib/packlets/conversion/advancedConverters.js +258 -0
  73. package/lib/packlets/conversion/baseConverter.d.ts +39 -23
  74. package/lib/packlets/conversion/baseConverter.js +23 -16
  75. package/lib/packlets/conversion/basicConverters.d.ts +470 -0
  76. package/lib/packlets/conversion/basicConverters.js +595 -0
  77. package/lib/packlets/conversion/converter.d.ts +7 -0
  78. package/lib/packlets/conversion/converters.d.ts +2 -535
  79. package/lib/packlets/conversion/converters.js +17 -639
  80. package/lib/packlets/conversion/defaultingConverter.d.ts +20 -16
  81. package/lib/packlets/conversion/defaultingConverter.js +21 -15
  82. package/lib/packlets/logging/bootLogger.d.ts +84 -0
  83. package/lib/packlets/logging/bootLogger.js +126 -0
  84. package/lib/packlets/logging/index.d.ts +1 -0
  85. package/lib/packlets/logging/index.js +1 -0
  86. package/lib/packlets/logging/logReporter.d.ts +18 -3
  87. package/lib/packlets/logging/logReporter.js +31 -0
  88. package/lib/packlets/logging/logger.d.ts +39 -6
  89. package/lib/packlets/logging/logger.js +26 -5
  90. package/lib/packlets/validation/classes.d.ts +1 -0
  91. package/lib/packlets/validation/classes.js +3 -1
  92. package/lib/packlets/validation/compositeId.d.ts +29 -0
  93. package/lib/packlets/validation/compositeId.js +64 -0
  94. package/lib/packlets/validation/field.d.ts +1 -1
  95. package/lib/packlets/validation/field.js +1 -1
  96. package/lib/packlets/validation/genericValidator.d.ts +15 -11
  97. package/lib/packlets/validation/genericValidator.js +24 -10
  98. package/lib/packlets/validation/object.d.ts +3 -6
  99. package/lib/packlets/validation/object.js +3 -6
  100. package/lib/packlets/validation/traits.d.ts +3 -3
  101. package/lib/packlets/validation/validator.d.ts +7 -0
  102. package/lib/packlets/validation/validators.d.ts +10 -0
  103. package/lib/packlets/validation/validators.js +13 -0
  104. package/package.json +14 -14
@@ -55,7 +55,7 @@ class Success {
55
55
  */
56
56
  constructor(value) {
57
57
  /**
58
- * {@inheritdoc IResult.success}
58
+ * {@inheritDoc IResult.success}
59
59
  */
60
60
  this.success = true;
61
61
  /**
@@ -71,13 +71,13 @@ class Success {
71
71
  return this._value;
72
72
  }
73
73
  /**
74
- * {@inheritdoc IResult.isSuccess}
74
+ * {@inheritDoc IResult.isSuccess}
75
75
  */
76
76
  isSuccess() {
77
77
  return true;
78
78
  }
79
79
  /**
80
- * {@inheritdoc IResult.isFailure}
80
+ * {@inheritDoc IResult.isFailure}
81
81
  */
82
82
  isFailure() {
83
83
  return false;
@@ -90,58 +90,58 @@ class Success {
90
90
  return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
91
91
  }
92
92
  /**
93
- * {@inheritdoc IResult.getValueOrThrow}
94
- * @deprecated Use {@link Success.(orThrow:1) | orThrow(logger)} or {@link Success.(orThrow:2) | orThrow(formatter)} instead.
93
+ * {@inheritDoc IResult.getValueOrThrow}
94
+ * @deprecated Use {@link Success.orThrow | orThrow(logger)} or {@link Success.orThrow | orThrow(formatter)} instead.
95
95
  */
96
96
  getValueOrThrow(__logger) {
97
97
  return this._value;
98
98
  }
99
99
  /**
100
- * {@inheritdoc IResult.getValueOrDefault}
101
- * @deprecated Use {@link Success.(orDefault:1) | orDefault(T)} or {@link Success.(orDefault:2) | orDefault()} instead.
100
+ * {@inheritDoc IResult.getValueOrDefault}
101
+ * @deprecated Use {@link Success.orDefault | orDefault(T)} or {@link Success.orDefault | orDefault()} instead.
102
102
  */
103
103
  getValueOrDefault(dflt) {
104
104
  var _a;
105
105
  return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
106
106
  }
107
107
  /**
108
- * {@inheritdoc IResult.onSuccess}
108
+ * {@inheritDoc IResult.onSuccess}
109
109
  */
110
110
  onSuccess(cb) {
111
111
  return cb(this._value);
112
112
  }
113
113
  /**
114
- * {@inheritdoc IResult.onFailure}
114
+ * {@inheritDoc IResult.onFailure}
115
115
  */
116
116
  onFailure(__) {
117
117
  return this;
118
118
  }
119
119
  /**
120
- * {@inheritdoc IResult.withErrorFormat}
120
+ * {@inheritDoc IResult.withErrorFormat}
121
121
  */
122
122
  withErrorFormat(__cb) {
123
123
  return this;
124
124
  }
125
125
  /**
126
- * {@inheritdoc IResult.withFailureDetail}
126
+ * {@inheritDoc IResult.withFailureDetail}
127
127
  */
128
128
  withFailureDetail(__detail) {
129
129
  return succeedWithDetail(this._value);
130
130
  }
131
131
  /**
132
- * {@inheritdoc IResult.withDetail}
132
+ * {@inheritDoc IResult.withDetail}
133
133
  */
134
134
  withDetail(detail, successDetail) {
135
135
  return succeedWithDetail(this._value, successDetail !== null && successDetail !== void 0 ? successDetail : detail);
136
136
  }
137
137
  /**
138
- * {@inheritdoc IResult.aggregateError}
138
+ * {@inheritDoc IResult.aggregateError}
139
139
  */
140
140
  aggregateError(__errors, __formatter) {
141
141
  return this;
142
142
  }
143
143
  /**
144
- * {@inheritdoc IResult.report}
144
+ * {@inheritDoc IResult.report}
145
145
  */
146
146
  report(reporter, options) {
147
147
  var _a;
@@ -172,7 +172,7 @@ class Failure {
172
172
  */
173
173
  constructor(message) {
174
174
  /**
175
- * {@inheritdoc IResult.success}
175
+ * {@inheritDoc IResult.success}
176
176
  */
177
177
  this.success = false;
178
178
  /**
@@ -188,13 +188,13 @@ class Failure {
188
188
  return this._message;
189
189
  }
190
190
  /**
191
- * {@inheritdoc IResult.isSuccess}
191
+ * {@inheritDoc IResult.isSuccess}
192
192
  */
193
193
  isSuccess() {
194
194
  return false;
195
195
  }
196
196
  /**
197
- * {@inheritdoc IResult.isFailure}
197
+ * {@inheritDoc IResult.isFailure}
198
198
  */
199
199
  isFailure() {
200
200
  return true;
@@ -214,8 +214,8 @@ class Failure {
214
214
  return dflt;
215
215
  }
216
216
  /**
217
- * {@inheritdoc IResult.getValueOrThrow}
218
- * @deprecated Use {@link Failure.(orThrow:1) | orThrow(logger)} or {@link Failure.(orThrow:2) | orThrow(formatter)} instead.
217
+ * {@inheritDoc IResult.getValueOrThrow}
218
+ * @deprecated Use {@link Failure.orThrow | orThrow(logger)} or {@link Failure.orThrow | orThrow(formatter)} instead.
219
219
  */
220
220
  getValueOrThrow(logger) {
221
221
  if (logger !== undefined) {
@@ -224,44 +224,44 @@ class Failure {
224
224
  throw new Error(this._message);
225
225
  }
226
226
  /**
227
- * {@inheritdoc IResult.getValueOrDefault}
228
- * @deprecated Use {@link Failure.(orDefault:1) | orDefault(T)} or {@link Failure.(orDefault:2) | orDefault()} instead.
227
+ * {@inheritDoc IResult.getValueOrDefault}
228
+ * @deprecated Use {@link Failure.orDefault | orDefault(T)} or {@link Failure.orDefault | orDefault()} instead.
229
229
  */
230
230
  getValueOrDefault(dflt) {
231
231
  return dflt;
232
232
  }
233
233
  /**
234
- * {@inheritdoc IResult.onSuccess}
234
+ * {@inheritDoc IResult.onSuccess}
235
235
  */
236
236
  onSuccess(__) {
237
237
  return new Failure(this._message);
238
238
  }
239
239
  /**
240
- * {@inheritdoc IResult.onFailure}
240
+ * {@inheritDoc IResult.onFailure}
241
241
  */
242
242
  onFailure(cb) {
243
243
  return cb(this._message);
244
244
  }
245
245
  /**
246
- * {@inheritdoc IResult.withErrorFormat}
246
+ * {@inheritDoc IResult.withErrorFormat}
247
247
  */
248
248
  withErrorFormat(cb) {
249
249
  return fail(cb(this._message));
250
250
  }
251
251
  /**
252
- * {@inheritdoc IResult.withFailureDetail}
252
+ * {@inheritDoc IResult.withFailureDetail}
253
253
  */
254
254
  withFailureDetail(detail) {
255
255
  return failWithDetail(this._message, detail);
256
256
  }
257
257
  /**
258
- * {@inheritdoc IResult.withDetail}
258
+ * {@inheritDoc IResult.withDetail}
259
259
  */
260
260
  withDetail(detail, __successDetail) {
261
261
  return failWithDetail(this._message, detail);
262
262
  }
263
263
  /**
264
- * {@inheritdoc IResult.aggregateError}
264
+ * {@inheritDoc IResult.aggregateError}
265
265
  */
266
266
  aggregateError(errors, formatter) {
267
267
  const message = formatter ? formatter(this._message) : this._message;
@@ -269,7 +269,7 @@ class Failure {
269
269
  return this;
270
270
  }
271
271
  /**
272
- * {@inheritdoc IResult.report}
272
+ * {@inheritDoc IResult.report}
273
273
  */
274
274
  report(reporter, options) {
275
275
  var _a, _b, _c;
@@ -311,7 +311,7 @@ function succeed(value) {
311
311
  return new Success(value);
312
312
  }
313
313
  /**
314
- * {@inheritdoc succeed}
314
+ * {@inheritDoc succeed}
315
315
  * @public
316
316
  */
317
317
  function succeeds(value) {
@@ -329,7 +329,7 @@ function fail(message) {
329
329
  return new Failure(message);
330
330
  }
331
331
  /**
332
- * {@inheritdoc fail}
332
+ * {@inheritDoc fail}
333
333
  * @public
334
334
  */
335
335
  function fails(message) {
@@ -395,7 +395,7 @@ class DetailedSuccess extends Success {
395
395
  * Propagates this {@link DetailedSuccess}.
396
396
  * @remarks
397
397
  * Failure does not mutate return type so we can return this event directly.
398
- * @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called
398
+ * @param __cb - {@link DetailedFailureContinuation | Failure callback} to be called
399
399
  * on a {@link DetailedResult} in case of failure (ignored).
400
400
  * @returns `this`
401
401
  */
@@ -403,13 +403,13 @@ class DetailedSuccess extends Success {
403
403
  return this;
404
404
  }
405
405
  /**
406
- * {@inheritdoc Success.withErrorFormat}
406
+ * {@inheritDoc Success.withErrorFormat}
407
407
  */
408
408
  withErrorFormat(cb) {
409
409
  return this;
410
410
  }
411
411
  /**
412
- * {@inheritdoc IResult.report}
412
+ * {@inheritDoc IResult.report}
413
413
  */
414
414
  report(reporter, options) {
415
415
  var _a;
@@ -473,7 +473,7 @@ class DetailedFailure extends Failure {
473
473
  * @remarks
474
474
  * Mutates the success type as the success callback would have, but does not
475
475
  * call the success callback.
476
- * @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called
476
+ * @param __cb - {@link DetailedSuccessContinuation | Success callback} to be called
477
477
  * on a {@link DetailedResult} in case of success (ignored).
478
478
  * @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
479
479
  * the error message and detail from this one.
@@ -491,13 +491,13 @@ class DetailedFailure extends Failure {
491
491
  return cb(this._message, this._detail);
492
492
  }
493
493
  /**
494
- * {@inheritdoc IResult.withErrorFormat}
494
+ * {@inheritDoc IResult.withErrorFormat}
495
495
  */
496
496
  withErrorFormat(cb) {
497
497
  return failWithDetail(cb(this._message, this._detail), this._detail);
498
498
  }
499
499
  /**
500
- * {@inheritdoc IResult.aggregateError}
500
+ * {@inheritDoc IResult.aggregateError}
501
501
  */
502
502
  aggregateError(errors, formatter) {
503
503
  const message = formatter ? formatter(this._message, this._detail) : this._message;
@@ -505,7 +505,7 @@ class DetailedFailure extends Failure {
505
505
  return this;
506
506
  }
507
507
  /**
508
- * {@inheritdoc IResult.report}
508
+ * {@inheritDoc IResult.report}
509
509
  */
510
510
  report(reporter, options) {
511
511
  var _a, _b;
@@ -567,7 +567,7 @@ function succeedWithDetail(value, detail) {
567
567
  return new DetailedSuccess(value, detail);
568
568
  }
569
569
  /**
570
- * {@inheritdoc succeedWithDetail}
570
+ * {@inheritDoc succeedWithDetail}
571
571
  * @public
572
572
  */
573
573
  function succeedsWithDetail(value, detail) {
@@ -588,7 +588,7 @@ function failWithDetail(message, detail) {
588
588
  return new DetailedFailure(message, detail);
589
589
  }
590
590
  /**
591
- * {@inheritdoc failWithDetail}
591
+ * {@inheritDoc failWithDetail}
592
592
  * @public
593
593
  */
594
594
  function failsWithDetail(message, detail) {
@@ -27,6 +27,48 @@ export declare function pick<T extends object, K extends keyof T>(from: T, inclu
27
27
  * @public
28
28
  */
29
29
  export declare function omit<T extends object, K extends keyof T>(from: T, exclude: K[]): Omit<T, K>;
30
+ /**
31
+ * Type-safe(ish) key extractor for typed records.
32
+ * @param obj - The record from which keys are to be extracted.
33
+ * @returns The keys of the record as an array.
34
+ * @public
35
+ */
36
+ export declare function keysForRecord<TK extends string>(obj: Record<TK, unknown>): TK[];
37
+ /**
38
+ * Type-safe(ish) value extractor for typed records.
39
+ * @param obj - The record from which values are to be extracted.
40
+ * @returns The values of the record as an array.
41
+ * @public
42
+ */
43
+ export declare function valuesForRecord<TK extends string, TV>(obj: Record<TK, TV>): TV[];
44
+ /**
45
+ * Type-safe(ish) entries extractor for typed records.
46
+ * @param obj - The record from which entries are to be extracted.
47
+ * @returns The entries of the record as an array of `[key, value]` tuples.
48
+ * @public
49
+ */
50
+ export declare function entriesForRecord<TK extends string, TV>(obj: Record<TK, TV>): Array<[TK, TV]>;
51
+ /**
52
+ * Type-safe(ish) record constructor from an iterable of `[key, value]` tuples.
53
+ * @param entries - The iterable of `[key, value]` tuples from which to construct the record.
54
+ * @returns A record constructed from the supplied entries.
55
+ * @public
56
+ */
57
+ export declare function recordFromEntries<TK extends string, TV>(entries: Iterable<[TK, TV]>): Record<TK, TV>;
58
+ /**
59
+ * Helper type to extract the element type and preserve readonly status.
60
+ * @public
61
+ */
62
+ export type EnsureArrayResult<T> = T extends readonly (infer _U)[] ? T : T[];
63
+ /**
64
+ * Ensures the input is an array. If already an array, returns it as-is.
65
+ * If a single item, wraps it in an array.
66
+ * Preserves readonly status of input arrays.
67
+ * @param items - A single item or an array of items.
68
+ * @returns The input array unchanged, or a new array containing the single item.
69
+ * @public
70
+ */
71
+ export declare function ensureArray<T>(items: T): EnsureArrayResult<T>;
30
72
  /**
31
73
  * Gets the value of a property specified by key from an arbitrary object,
32
74
  * or a default value if the property does not exist.
@@ -24,6 +24,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.isKeyOf = isKeyOf;
25
25
  exports.pick = pick;
26
26
  exports.omit = omit;
27
+ exports.keysForRecord = keysForRecord;
28
+ exports.valuesForRecord = valuesForRecord;
29
+ exports.entriesForRecord = entriesForRecord;
30
+ exports.recordFromEntries = recordFromEntries;
31
+ exports.ensureArray = ensureArray;
27
32
  exports.getValueOfPropertyOrDefault = getValueOfPropertyOrDefault;
28
33
  exports.getTypeOfProperty = getTypeOfProperty;
29
34
  exports.recordToMap = recordToMap;
@@ -77,6 +82,53 @@ function omit(from, exclude) {
77
82
  }
78
83
  return rtrn;
79
84
  }
85
+ /**
86
+ * Type-safe(ish) key extractor for typed records.
87
+ * @param obj - The record from which keys are to be extracted.
88
+ * @returns The keys of the record as an array.
89
+ * @public
90
+ */
91
+ function keysForRecord(obj) {
92
+ return Object.keys(obj);
93
+ }
94
+ /**
95
+ * Type-safe(ish) value extractor for typed records.
96
+ * @param obj - The record from which values are to be extracted.
97
+ * @returns The values of the record as an array.
98
+ * @public
99
+ */
100
+ function valuesForRecord(obj) {
101
+ return Object.values(obj);
102
+ }
103
+ /**
104
+ * Type-safe(ish) entries extractor for typed records.
105
+ * @param obj - The record from which entries are to be extracted.
106
+ * @returns The entries of the record as an array of `[key, value]` tuples.
107
+ * @public
108
+ */
109
+ function entriesForRecord(obj) {
110
+ return Object.entries(obj);
111
+ }
112
+ /**
113
+ * Type-safe(ish) record constructor from an iterable of `[key, value]` tuples.
114
+ * @param entries - The iterable of `[key, value]` tuples from which to construct the record.
115
+ * @returns A record constructed from the supplied entries.
116
+ * @public
117
+ */
118
+ function recordFromEntries(entries) {
119
+ return Object.fromEntries(entries);
120
+ }
121
+ /**
122
+ * Ensures the input is an array. If already an array, returns it as-is.
123
+ * If a single item, wraps it in an array.
124
+ * Preserves readonly status of input arrays.
125
+ * @param items - A single item or an array of items.
126
+ * @returns The input array unchanged, or a new array containing the single item.
127
+ * @public
128
+ */
129
+ function ensureArray(items) {
130
+ return (Array.isArray(items) ? items : [items]);
131
+ }
80
132
  /**
81
133
  * Gets the value of a property specified by key from an arbitrary object,
82
134
  * or a default value if the property does not exist.