@fgv/ts-utils 5.0.0-22 → 5.0.0-23

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 (80) hide show
  1. package/dist/ts-utils.d.ts +307 -33
  2. package/lib/index.d.ts +2 -1
  3. package/lib/index.js +3 -1
  4. package/lib/packlets/base/index.d.ts +0 -2
  5. package/lib/packlets/base/index.js +1 -25
  6. package/lib/packlets/base/result.d.ts +49 -0
  7. package/lib/packlets/base/result.js +36 -0
  8. package/lib/packlets/collections/collector.d.ts +9 -0
  9. package/lib/packlets/collections/collector.js +6 -0
  10. package/lib/packlets/collections/validatingCollector.d.ts +5 -1
  11. package/lib/packlets/hash/hashingNormalizer.js +4 -3
  12. package/lib/packlets/logging/index.d.ts +3 -0
  13. package/{src/packlets/hash/index.ts → lib/packlets/logging/index.js} +19 -3
  14. package/lib/packlets/logging/logReporter.d.ts +69 -0
  15. package/lib/packlets/logging/logReporter.js +72 -0
  16. package/lib/packlets/logging/logger.d.ts +183 -0
  17. package/lib/packlets/logging/logger.js +237 -0
  18. package/lib/test/unit/consoleLogger.test.d.ts +2 -0
  19. package/lib/test/unit/hashingNormalizerCollisions.test.d.ts +2 -0
  20. package/package.json +1 -1
  21. package/lib/packlets/base/logger.d.ts +0 -52
  22. package/lib/packlets/base/logger.js +0 -128
  23. package/src/index.ts +0 -63
  24. package/src/packlets/base/brand.ts +0 -28
  25. package/src/packlets/base/index.ts +0 -31
  26. package/src/packlets/base/logger.ts +0 -156
  27. package/src/packlets/base/mapResults.ts +0 -302
  28. package/src/packlets/base/messageAggregator.ts +0 -120
  29. package/src/packlets/base/normalize.ts +0 -144
  30. package/src/packlets/base/result.ts +0 -961
  31. package/src/packlets/base/utils.ts +0 -236
  32. package/src/packlets/collections/collectible.ts +0 -238
  33. package/src/packlets/collections/collector.ts +0 -273
  34. package/src/packlets/collections/collectorValidator.ts +0 -178
  35. package/src/packlets/collections/common.ts +0 -27
  36. package/src/packlets/collections/convertingCollector.ts +0 -223
  37. package/src/packlets/collections/convertingCollectorValidator.ts +0 -164
  38. package/src/packlets/collections/index.ts +0 -39
  39. package/src/packlets/collections/keyValueConverters.ts +0 -142
  40. package/src/packlets/collections/readonlyResultMap.ts +0 -99
  41. package/src/packlets/collections/resultMap.ts +0 -322
  42. package/src/packlets/collections/resultMapValidator.ts +0 -188
  43. package/src/packlets/collections/utils.ts +0 -33
  44. package/src/packlets/collections/validatingCollector.ts +0 -123
  45. package/src/packlets/collections/validatingConvertingCollector.ts +0 -113
  46. package/src/packlets/collections/validatingResultMap.ts +0 -94
  47. package/src/packlets/conversion/baseConverter.ts +0 -330
  48. package/src/packlets/conversion/converter.ts +0 -260
  49. package/src/packlets/conversion/converters.ts +0 -1039
  50. package/src/packlets/conversion/defaultingConverter.ts +0 -188
  51. package/src/packlets/conversion/index.ts +0 -30
  52. package/src/packlets/conversion/objectConverter.ts +0 -259
  53. package/src/packlets/conversion/stringConverter.ts +0 -165
  54. package/src/packlets/file-tree/directoryItem.ts +0 -84
  55. package/src/packlets/file-tree/fileItem.ts +0 -119
  56. package/src/packlets/file-tree/fileTree.ts +0 -152
  57. package/src/packlets/file-tree/fileTreeAccessors.ts +0 -176
  58. package/src/packlets/file-tree/fsTree.ts +0 -118
  59. package/src/packlets/file-tree/in-memory/inMemoryTree.ts +0 -171
  60. package/src/packlets/file-tree/in-memory/index.ts +0 -23
  61. package/src/packlets/file-tree/in-memory/treeBuilder.ts +0 -203
  62. package/src/packlets/file-tree/index.ts +0 -31
  63. package/src/packlets/hash/crcNormalizer.ts +0 -81
  64. package/src/packlets/hash/hashingNormalizer.ts +0 -102
  65. package/src/packlets/validation/array.ts +0 -84
  66. package/src/packlets/validation/boolean.ts +0 -63
  67. package/src/packlets/validation/classes.ts +0 -38
  68. package/src/packlets/validation/common.ts +0 -28
  69. package/src/packlets/validation/field.ts +0 -99
  70. package/src/packlets/validation/genericValidator.ts +0 -204
  71. package/src/packlets/validation/index.ts +0 -31
  72. package/src/packlets/validation/number.ts +0 -66
  73. package/src/packlets/validation/object.ts +0 -208
  74. package/src/packlets/validation/oneOf.ts +0 -78
  75. package/src/packlets/validation/string.ts +0 -66
  76. package/src/packlets/validation/traits.ts +0 -113
  77. package/src/packlets/validation/typeGuard.ts +0 -83
  78. package/src/packlets/validation/validator.ts +0 -157
  79. package/src/packlets/validation/validatorBase.ts +0 -66
  80. package/src/packlets/validation/validators.ts +0 -254
@@ -1,322 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import { captureResult, DetailedResult, failWithDetail, Result, succeed, succeedWithDetail } from '../base';
24
- import { KeyValueEntry } from './common';
25
- import { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';
26
- import { isIterable } from './utils';
27
-
28
- /**
29
- * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.
30
- * @public
31
- */
32
- export interface IResultMapConstructorParams<TK extends string = string, TV = unknown> {
33
- entries?: Iterable<KeyValueEntry<TK, TV>>;
34
- }
35
-
36
- /**
37
- * Deferred constructor for the {@link Collections.ResultMap.(getOrAdd:2) | getOrAdd} method.
38
- * @public
39
- */
40
- export type ResultMapValueFactory<TK extends string = string, TV = unknown> = (key: TK) => Result<TV>;
41
-
42
- /**
43
- * A {@link Collections.ResultMap | ResultMap} class as a `Map<TK, TV>`-like object which
44
- * reports success or failure with additional details using the
45
- * {@link https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#the-result-pattern | result pattern}.
46
- * @public
47
- */
48
- export class ResultMap<TK extends string = string, TV = unknown> implements IReadOnlyResultMap<TK, TV> {
49
- /**
50
- * Protected raw access to the inner `Map<TK, TV>` object.
51
- * @public
52
- */
53
- protected readonly _inner: Map<TK, TV>;
54
-
55
- /**
56
- * Constructs a new {@link Collections.ResultMap | ResultMap}.
57
- * @param iterable - An iterable to initialize the map.
58
- */
59
- public constructor(iterable?: Iterable<KeyValueEntry<TK, TV>>);
60
-
61
- /**
62
- * Constructs a new {@link Collections.ResultMap | ResultMap}.
63
- * @param params - An optional set of parameters to configure the map.
64
- */
65
- public constructor(params: IResultMapConstructorParams);
66
-
67
- /**
68
- * Constructs a new {@link Collections.ResultMap | ResultMap}.
69
- * @param iterableOrParams - An iterable to initialize the map, or a set of parameters
70
- * to configure the map.
71
- */
72
- public constructor(
73
- iterableOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>
74
- ) {
75
- const params = isIterable(iterableOrParams) ? { entries: iterableOrParams } : iterableOrParams ?? {};
76
- this._inner = new Map(params.entries);
77
- }
78
-
79
- /**
80
- * Creates a new {@link Collections.ResultMap | ResultMap}.
81
- * @param elements - An optional iterable to initialize the map.
82
- * @returns `Success` with the new map, or `Failure` with error details
83
- * if an error occurred.
84
- * @public
85
- */
86
- public static create<TK extends string = string, TV = unknown>(
87
- elements: Iterable<KeyValueEntry<TK, TV>>
88
- ): Result<ResultMap<TK, TV>>;
89
-
90
- /**
91
- * Creates a new {@link Collections.ResultMap | ResultMap}.
92
- * @param params - An optional set of parameters to configure the map.
93
- * @returns `Success` with the new map, or `Failure` with error details
94
- * if an error occurred.
95
- * @public
96
- */
97
- public static create<TK extends string = string, TV = unknown>(
98
- params?: IResultMapConstructorParams<TK, TV>
99
- ): Result<ResultMap<TK, TV>>;
100
-
101
- /**
102
- * Creates a new {@link Collections.ResultMap | ResultMap}.
103
- * @param elementsOrParams - An optional iterable to initialize the map, or a set of parameters
104
- * to configure the map.
105
- * @returns `Success` with the new map, or `Failure` with error details
106
- * if an error occurred.
107
- * @public
108
- */
109
- public static create<TK extends string = string, TV = unknown>(
110
- elementsOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>
111
- ): Result<ResultMap<TK, TV>> {
112
- return captureResult(() => new ResultMap(elementsOrParams as IResultMapConstructorParams<TK, TV>));
113
- }
114
-
115
- /**
116
- * Sets a key/value pair in the map if the key does not already exist.
117
- * @param key - The key to set.
118
- * @param value - The value to set.
119
- * @returns `Success` with the value and detail `added` if the key was added,
120
- * `Failure` with detail `exists` if the key already exists. Fails with detail
121
- * 'invalid-key' or 'invalid-value' and an error message if either is invalid.
122
- */
123
- public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {
124
- if (this._inner.has(key)) {
125
- return failWithDetail(`${key}: already exists.`, 'exists');
126
- }
127
- this._inner.set(key, value);
128
- return succeedWithDetail(value, 'added');
129
- }
130
-
131
- /**
132
- * Clears the map.
133
- */
134
- public clear(): void {
135
- this._inner.clear();
136
- }
137
-
138
- /**
139
- * Deletes a key from the map.
140
- * @param key - The key to delete.
141
- * @returns `Success` with the previous value and the detail 'deleted'
142
- * if the key was found and deleted, `Failure` with detail 'not-found'
143
- * if the key was not found, or with detail 'invalid-key' if the key is invalid.
144
- */
145
- public delete(key: TK): DetailedResult<TV, ResultMapResultDetail> {
146
- const was = this._inner.get(key);
147
- if (this._inner.delete(key)) {
148
- return succeedWithDetail(was!, 'deleted');
149
- }
150
- return failWithDetail(`${key}: not found.`, 'not-found');
151
- }
152
-
153
- /**
154
- * Returns an iterator over the map entries.
155
- * @returns An iterator over the map entries.
156
- */
157
- public entries(): IterableIterator<KeyValueEntry<TK, TV>> {
158
- return this._inner.entries();
159
- }
160
-
161
- /**
162
- * Calls a function for each entry in the map.
163
- * @param cb - The function to call for each entry.
164
- * @param arg - An optional argument to pass to the callback.
165
- */
166
- public forEach(cb: ResultMapForEachCb<TK, TV>, arg?: unknown): void {
167
- for (const [key, value] of this._inner.entries()) {
168
- cb(value, key as TK, this, arg);
169
- }
170
- }
171
-
172
- /**
173
- * Gets a value from the map.
174
- * @param key - The key to retrieve.
175
- * @returns `Success` with the value and detail `exists` if the key was found,
176
- * `Failure` with detail `not-found` if the key was not found or with detail
177
- * `invalid-key` if the key is invalid.
178
- */
179
- public get(key: TK): DetailedResult<TV, ResultMapResultDetail> {
180
- if (this._inner.has(key)) {
181
- return succeedWithDetail(this._inner.get(key)!, 'exists');
182
- }
183
- return failWithDetail(`${key}: not found.`, 'not-found');
184
- }
185
-
186
- /**
187
- * Gets a value from the map, or adds a supplied value it if it does not exist.
188
- * @param key - The key to be retrieved or created.
189
- * @param value - The value to add if the key does not exist.
190
- * @returns `Success` with the value and detail `exists` if the key was found,
191
- * `Success` with the value and detail `added` if the key was not found and added.
192
- * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either
193
- * is invalid.
194
- * {@label WITH_VALUE}
195
- */
196
- public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
197
-
198
- /**
199
- * Gets a value from the map, or adds a value created by a factory function if it does not exist.
200
- * @param key - The key of the element to be retrieved or created.
201
- * @param factory - A {@link Collections.ResultMapValueFactory | factory function} to create the value if
202
- * the key does not exist.
203
- * @returns `Success` with the value and detail `exists` if the key was found, `Success` with
204
- * the value and detail `added` if the key was not found and added. Fails with detail 'invalid-key'
205
- * or 'invalid-value' and an error message if either is invalid.
206
- * {@label WITH_FACTORY}
207
- */
208
- public getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
209
- public getOrAdd(
210
- key: TK,
211
- valueOrFactory: TV | ResultMapValueFactory<TK, TV>
212
- ): DetailedResult<TV, ResultMapResultDetail> {
213
- if (this._inner.has(key)) {
214
- return succeedWithDetail(this._inner.get(key)!, 'exists');
215
- }
216
-
217
- const factory: ResultMapValueFactory<TK, TV> = this._isResultMapValueFactory(valueOrFactory)
218
- ? valueOrFactory
219
- : () => succeed(valueOrFactory);
220
-
221
- return factory(key)
222
- .onSuccess((val) => {
223
- this._inner.set(key, val);
224
- return succeedWithDetail(val, 'added');
225
- })
226
- .withDetail('invalid-value', 'added');
227
- }
228
-
229
- /**
230
- * Returns `true` if the map contains a key.
231
- * @param key - The key to check.
232
- * @returns `true` if the key exists, `false` otherwise.
233
- */
234
- public has(key: TK): boolean {
235
- return this._inner.has(key);
236
- }
237
-
238
- /**
239
- * Returns an iterator over the map keys.
240
- * @returns An iterator over the map keys.
241
- */
242
- public keys(): IterableIterator<TK> {
243
- return this._inner.keys();
244
- }
245
-
246
- /**
247
- * Sets a key/value pair in the map.
248
- * @param key - The key to set.
249
- * @param value - The value to set.
250
- * @returns `Success` with the new value and the detail `updated` if the
251
- * key was found and updated, `Success` with the new value and detail
252
- * `added` if the key was not found and added. Fails with detail
253
- * 'invalid-key' or 'invalid-value' and an error message if either is invalid.
254
- */
255
- public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {
256
- const detail: ResultMapResultDetail = this._inner.has(key) ? 'updated' : 'added';
257
- this._inner.set(key, value);
258
- return succeedWithDetail(value, detail);
259
- }
260
-
261
- /**
262
- * Returns the number of entries in the map.
263
- */
264
- public get size(): number {
265
- return this._inner.size;
266
- }
267
-
268
- /**
269
- * Updates an existing key in the map - the map is not updated if the key does
270
- * not exist.
271
- * @param key - The key to update.
272
- * @param value - The value to set.
273
- * @returns `Success` with the value and detail 'exists' if the key was found
274
- * and the value updated, `Failure` an error message and with detail `not-found`
275
- * if the key was not found, or with detail 'invalid-key' or 'invalid-value'
276
- * if either is invalid.
277
- */
278
- public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {
279
- if (this._inner.has(key)) {
280
- this._inner.set(key, value);
281
- return succeedWithDetail(value, 'updated');
282
- }
283
- return failWithDetail(`${key}: not found.`, 'not-found');
284
- }
285
-
286
- /**
287
- * Returns an iterator over the map values.
288
- * @returns An iterator over the map values.
289
- */
290
- public values(): IterableIterator<TV> {
291
- return this._inner.values();
292
- }
293
-
294
- /**
295
- * Gets an iterator over the map entries.
296
- * @returns An iterator over the map entries.
297
- */
298
- public [Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>> {
299
- return this._inner[Symbol.iterator]();
300
- }
301
-
302
- /**
303
- * Gets a readonly version of this map.
304
- * @returns A readonly version of this map.
305
- */
306
- public toReadOnly(): IReadOnlyResultMap<TK, TV> {
307
- return this;
308
- }
309
-
310
- /**
311
- * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
312
- * @param value - The value to check.
313
- * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
314
- * `false` otherwise.
315
- * @public
316
- */
317
- protected _isResultMapValueFactory<TK extends string, TV>(
318
- value: TV | ResultMapValueFactory<TK, TV>
319
- ): value is ResultMapValueFactory<TK, TV> {
320
- return typeof value === 'function';
321
- }
322
- }
@@ -1,188 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import { DetailedResult, failWithDetail, succeedWithDetail } from '../base';
24
- import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
25
- import { ResultMap, ResultMapValueFactory } from './resultMap';
26
- import { KeyValueConverters } from './keyValueConverters';
27
-
28
- /**
29
- * A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapValidator | ResultMapValidator}.
30
- * @public
31
- */
32
- export interface IReadOnlyResultMapValidator<TK extends string = string, TV = unknown> {
33
- /**
34
- * {@inheritdoc Collections.ResultMapValidator.map}
35
- */
36
- readonly map: IReadOnlyResultMap<TK, TV>;
37
-
38
- /**
39
- * {@inheritdoc Collections.ResultMap.get}
40
- */
41
- get(key: string): DetailedResult<TV, ResultMapResultDetail>;
42
-
43
- /**
44
- * {@inheritdoc Collections.ResultMap.has}
45
- */
46
- has(key: string): boolean;
47
- }
48
-
49
- /**
50
- * Parameters for constructing a {@link Collections.ResultMapValidator | ResultMapValidator}.
51
- * @public
52
- */
53
- export interface IResultMapValidatorCreateParams<TK extends string = string, TV = unknown> {
54
- map: ResultMap<TK, TV>;
55
- converters: KeyValueConverters<TK, TV>;
56
- }
57
-
58
- /**
59
- * A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys
60
- * before calling the wrapped result map.
61
- * @public
62
- */
63
- export class ResultMapValidator<TK extends string = string, TV = unknown>
64
- implements IReadOnlyResultMapValidator<TK, TV>
65
- {
66
- public readonly converters: KeyValueConverters<TK, TV>;
67
- public get map(): IReadOnlyResultMap<TK, TV> {
68
- return this._map;
69
- }
70
-
71
- protected _map: ResultMap<TK, TV>;
72
-
73
- /**
74
- * Constructs a new {@link Collections.ResultMapValidator | ResultMapValidator}.
75
- * @param params - Required parameters for constructing the result map validator.
76
- */
77
- public constructor(params: IResultMapValidatorCreateParams<TK, TV>) {
78
- this._map = params.map;
79
- this.converters = params.converters;
80
- }
81
-
82
- /**
83
- * {@inheritdoc Collections.ResultMap.add}
84
- */
85
- public add(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {
86
- return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
87
- return this._map.add(vk, vv);
88
- });
89
- }
90
-
91
- /**
92
- * {@inheritdoc Collections.ResultMap.delete}
93
- */
94
- public delete(key: string): DetailedResult<TV, ResultMapResultDetail> {
95
- return this.converters.convertKey(key).onSuccess((k) => {
96
- return this._map.delete(k);
97
- });
98
- }
99
-
100
- /**
101
- * {@inheritdoc Collections.ResultMap.get}
102
- */
103
- public get(key: string): DetailedResult<TV, ResultMapResultDetail> {
104
- return this.converters.convertKey(key).onSuccess((k) => {
105
- return this._map.get(k);
106
- });
107
- }
108
-
109
- /**
110
- * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
111
- */
112
- public getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
113
-
114
- /**
115
- * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
116
- */
117
- public getOrAdd(
118
- key: string,
119
- factory: ResultMapValueFactory<TK, TV>
120
- ): DetailedResult<TV, ResultMapResultDetail>;
121
- public getOrAdd(
122
- key: string,
123
- valueOrFactory: unknown | ResultMapValueFactory<TK, TV>
124
- ): DetailedResult<TV, ResultMapResultDetail> {
125
- if (!this._isResultMapValueFactory(valueOrFactory)) {
126
- return this.converters.convertEntry([key, valueOrFactory]).onSuccess(([vk, vv]) => {
127
- return this._map.getOrAdd(vk, vv);
128
- });
129
- } else {
130
- return this.converters.convertKey(key).onSuccess((k) => {
131
- return this._map.get(k).onFailure(() => {
132
- const value = valueOrFactory(k)
133
- .onSuccess((value) => this.converters.convertEntry([k, value]))
134
- .onSuccess(([__key, value]) => succeedWithDetail(value, 'added'));
135
-
136
- return value.success
137
- ? this._map.add(k, value.value)
138
- : failWithDetail<TV, ResultMapResultDetail>(value.message, 'invalid-value');
139
- });
140
- });
141
- }
142
- }
143
-
144
- /**
145
- * {@inheritdoc Collections.ResultMap.has}
146
- */
147
- public has(key: string): boolean {
148
- return this._map.has(key as TK);
149
- }
150
-
151
- /**
152
- * {@inheritdoc Collections.ResultMap.set}
153
- */
154
- public set(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {
155
- return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
156
- return this._map.set(vk, vv);
157
- });
158
- }
159
-
160
- /**
161
- * {@inheritdoc Collections.ResultMap.update}
162
- */
163
- public update(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {
164
- return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
165
- return this._map.update(vk, vv);
166
- });
167
- }
168
-
169
- /**
170
- * Gets a read-only version of this validator.
171
- */
172
- public toReadOnly(): IReadOnlyResultMapValidator<TK, TV> {
173
- return this;
174
- }
175
-
176
- /**
177
- * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
178
- * @param value - The value to check.
179
- * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
180
- * `false` otherwise.
181
- * @public
182
- */
183
- protected _isResultMapValueFactory<TK extends string, TV>(
184
- value: TV | ResultMapValueFactory<TK, TV>
185
- ): value is ResultMapValueFactory<TK, TV> {
186
- return typeof value === 'function';
187
- }
188
- }
@@ -1,33 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- /**
24
- * Determines if a supplied value is an iterable object or some other type.
25
- * @param value - The value to be tested.
26
- * @returns `true` if the value is an iterable object, `false` otherwise.
27
- * @public
28
- */
29
- export function isIterable<TE = unknown, TI extends Iterable<TE> = Iterable<TE>, TO = unknown>(
30
- value: TI | TO
31
- ): value is TI {
32
- return (value && typeof value === 'object' && Symbol.iterator in value) === true;
33
- }
@@ -1,123 +0,0 @@
1
- /*
2
- * Copyright (c) 2025 Erik Fortune
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in all
12
- * copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
21
- */
22
-
23
- import { captureResult, Result } from '../base';
24
- import { CollectibleKey, ICollectible } from './collectible';
25
- import { IReadOnlyCollectorValidator } from './collectorValidator';
26
- import { IReadOnlyValidatingResultMap } from './validatingResultMap';
27
- import { KeyValueConverters } from './keyValueConverters';
28
- import { Collector } from './collector';
29
- import { CollectorValidator } from './collectorValidator';
30
-
31
- /**
32
- * A read-only interface exposing non-mutating methods of a
33
- * {@link Collections.ValidatingCollector | ValidatingCollector}.
34
- * @public
35
- */
36
-
37
- export interface IReadOnlyValidatingCollector<
38
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- TITEM extends ICollectible<any, any>
40
- > extends IReadOnlyValidatingResultMap<CollectibleKey<TITEM>, TITEM> {
41
- /**
42
- * {@inheritdoc Collections.ValidatingCollector.validating}
43
- */
44
- readonly validating: IReadOnlyCollectorValidator<TITEM>;
45
-
46
- /**
47
- * {@inheritdoc Collections.IReadOnlyValidatingCollector.getAt}
48
- */
49
- readonly getAt: (index: number) => Result<TITEM>;
50
- }
51
-
52
- /**
53
- * Parameters for constructing a {@link Collections.ValidatingCollector | ValidatingCollector}.
54
- * @public
55
- */
56
- export interface IValidatingCollectorConstructorParams<
57
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- TITEM extends ICollectible<any, any>
59
- > {
60
- /**
61
- * {@inheritdoc Collections.ICollectorValidatorCreateParams.converters}
62
- */
63
- converters: KeyValueConverters<CollectibleKey<TITEM>, TITEM>;
64
-
65
- /**
66
- * {@inheritdoc Collections.ICollectorConstructorParams.items}
67
- */
68
- items?: unknown[];
69
- }
70
-
71
- /**
72
- * A {@link Collections.Collector | Collector} with a {@link Collections.CollectorValidator | CollectorValidator}
73
- * property that enables validated use of the underlying map with weakly-typed keys and values.
74
- * @public
75
- */
76
- export class ValidatingCollector<
77
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
- TITEM extends ICollectible<any, any>
79
- > extends Collector<TITEM> {
80
- /**
81
- * A {@link Collections.CollectorValidator | CollectorValidator} which validates keys and values
82
- * before inserting them into this collector.
83
- */
84
- public readonly validating: CollectorValidator<TITEM>;
85
-
86
- protected readonly _converters: KeyValueConverters<CollectibleKey<TITEM>, TITEM>;
87
-
88
- /**
89
- * Constructs a new {@link Collections.ValidatingCollector | ValidatingConvertingCollector}
90
- * from the supplied {@link Collections.IValidatingCollectorConstructorParams | parameters}.
91
- * @param params - Required parameters for constructing the collector.
92
- */
93
- public constructor(params: IValidatingCollectorConstructorParams<TITEM>) {
94
- super();
95
- this._converters = params.converters;
96
- this.validating = new CollectorValidator({ collector: this, converters: params.converters });
97
- for (const item of params.items ?? []) {
98
- this.validating.getOrAdd(item).orThrow();
99
- }
100
- }
101
-
102
- /**
103
- * Creates a new {@link Collections.ValidatingCollector | ValidatingCollector} instance from
104
- * the supplied {@link Collections.IValidatingCollectorConstructorParams | parameters}.
105
- * @param params - Required parameters for constructing the collector.
106
- * @returns {@link Success} with the new collector if successful, {@link Failure} otherwise.
107
- */
108
- public static createValidatingCollector<
109
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
110
- TITEM extends ICollectible<any, any>
111
- >(params: IValidatingCollectorConstructorParams<TITEM>): Result<ValidatingCollector<TITEM>> {
112
- return captureResult(() => new ValidatingCollector(params));
113
- }
114
-
115
- /**
116
- * Gets a read-only version of this collector as a
117
- * {@link Collections.IReadOnlyValidatingResultMap | read-only map}.
118
- * @returns
119
- */
120
- public toReadOnly(): IReadOnlyValidatingCollector<TITEM> {
121
- return this;
122
- }
123
- }