@fgv/ts-utils 4.2.0 → 4.2.1

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.
package/CHANGELOG.json CHANGED
@@ -1,6 +1,18 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
3
  "entries": [
4
+ {
5
+ "version": "4.2.1",
6
+ "tag": "@fgv/ts-utils_v4.2.1",
7
+ "date": "Tue, 21 Jan 2025 04:19:21 GMT",
8
+ "comments": {
9
+ "none": [
10
+ {
11
+ "comment": "allow factories in getOrAdd"
12
+ }
13
+ ]
14
+ }
15
+ },
4
16
  {
5
17
  "version": "4.2.0",
6
18
  "tag": "@fgv/ts-utils_v4.2.0",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log - @fgv/ts-utils
2
2
 
3
- This log was last generated on Mon, 20 Jan 2025 09:46:53 GMT and should not be manually modified.
3
+ This log was last generated on Tue, 21 Jan 2025 04:19:21 GMT and should not be manually modified.
4
+
5
+ ## 4.2.1
6
+ Tue, 21 Jan 2025 04:19:21 GMT
7
+
8
+ ### Updates
9
+
10
+ - allow factories in getOrAdd
4
11
 
5
12
  ## 4.2.0
6
13
  Mon, 20 Jan 2025 09:46:53 GMT
@@ -273,6 +273,7 @@ declare namespace Collections {
273
273
  ResultMapForEachCb,
274
274
  IReadOnlyResultMap,
275
275
  IResultMapConstructorParams,
276
+ ResultMapValueFactory,
276
277
  ResultMap,
277
278
  IReadOnlyResultMapValidator,
278
279
  IResultMapValidatorCreateParams,
@@ -2627,14 +2628,26 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
2627
2628
  get(key: TK): DetailedResult<TV, ResultMapResultDetail>;
2628
2629
  /**
2629
2630
  * Gets a value from the map, or adds a supplied value it if it does not exist.
2630
- * @param key - The key to retrieve.
2631
+ * @param key - The key to be retrieved or created.
2631
2632
  * @param value - The value to add if the key does not exist.
2632
2633
  * @returns `Success` with the value and detail `exists` if the key was found,
2633
2634
  * `Success` with the value and detail `added` if the key was not found and added.
2634
2635
  * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either
2635
2636
  * is invalid.
2637
+ * {@label WITH_VALUE}
2636
2638
  */
2637
2639
  getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
2640
+ /**
2641
+ * Gets a value from the map, or adds a value created by a factory function if it does not exist.
2642
+ * @param key - The key of the element to be retrieved or created.
2643
+ * @param factory - A {@link Collections.ResultMapValueFactory | factory function} to create the value if
2644
+ * the key does not exist.
2645
+ * @returns `Success` with the value and detail `exists` if the key was found, `Success` with
2646
+ * the value and detail `added` if the key was not found and added. Fails with detail 'invalid-key'
2647
+ * or 'invalid-value' and an error message if either is invalid.
2648
+ * {@label WITH_FACTORY}
2649
+ */
2650
+ getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
2638
2651
  /**
2639
2652
  * Returns `true` if the map contains a key.
2640
2653
  * @param key - The key to check.
@@ -2686,6 +2699,14 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
2686
2699
  * @returns A readonly version of this map.
2687
2700
  */
2688
2701
  toReadOnly(): IReadOnlyResultMap<TK, TV>;
2702
+ /**
2703
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
2704
+ * @param value - The value to check.
2705
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
2706
+ * `false` otherwise.
2707
+ * @public
2708
+ */
2709
+ protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
2689
2710
  }
2690
2711
 
2691
2712
  /**
@@ -2727,9 +2748,13 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
2727
2748
  */
2728
2749
  get(key: string): DetailedResult<TV, ResultMapResultDetail>;
2729
2750
  /**
2730
- * {@inheritdoc Collections.ResultMap.getOrAdd}
2751
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
2731
2752
  */
2732
2753
  getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
2754
+ /**
2755
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
2756
+ */
2757
+ getOrAdd(key: string, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
2733
2758
  /**
2734
2759
  * {@inheritdoc Collections.ResultMap.has}
2735
2760
  */
@@ -2746,8 +2771,22 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
2746
2771
  * Gets a read-only version of this validator.
2747
2772
  */
2748
2773
  toReadOnly(): IReadOnlyResultMapValidator<TK, TV>;
2774
+ /**
2775
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
2776
+ * @param value - The value to check.
2777
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
2778
+ * `false` otherwise.
2779
+ * @public
2780
+ */
2781
+ protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
2749
2782
  }
2750
2783
 
2784
+ /**
2785
+ * Deferred constructor for the {@link Collections.ResultMap.(getOrAdd:2) | getOrAdd} method.
2786
+ * @public
2787
+ */
2788
+ declare type ResultMapValueFactory<TK extends string = string, TV = unknown> = (key: TK) => Result<TV>;
2789
+
2751
2790
  /**
2752
2791
  * Type inference to determine the result type of an {@link Result}.
2753
2792
  * @beta
@@ -3194,9 +3233,16 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
3194
3233
  */
3195
3234
  add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
3196
3235
  /**
3197
- * {@inheritdoc Collections.ResultMap.getOrAdd}
3236
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
3198
3237
  */
3199
3238
  getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
3239
+ /**
3240
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
3241
+ */
3242
+ getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
3243
+ /**
3244
+ * {@inheritdoc Collections.ResultMap.set}
3245
+ */
3200
3246
  set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
3201
3247
  /**
3202
3248
  * {@inheritdoc Collections.ResultMap.update}
@@ -8,6 +8,11 @@ import { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from '.
8
8
  export interface IResultMapConstructorParams<TK extends string = string, TV = unknown> {
9
9
  entries?: Iterable<KeyValueEntry<TK, TV>>;
10
10
  }
11
+ /**
12
+ * Deferred constructor for the {@link Collections.ResultMap.(getOrAdd:2) | getOrAdd} method.
13
+ * @public
14
+ */
15
+ export type ResultMapValueFactory<TK extends string = string, TV = unknown> = (key: TK) => Result<TV>;
11
16
  /**
12
17
  * A {@link Collections.ResultMap | ResultMap} class as a `Map<TK, TV>`-like object which
13
18
  * reports success or failure with additional details using the
@@ -92,14 +97,26 @@ export declare class ResultMap<TK extends string = string, TV = unknown> impleme
92
97
  get(key: TK): DetailedResult<TV, ResultMapResultDetail>;
93
98
  /**
94
99
  * Gets a value from the map, or adds a supplied value it if it does not exist.
95
- * @param key - The key to retrieve.
100
+ * @param key - The key to be retrieved or created.
96
101
  * @param value - The value to add if the key does not exist.
97
102
  * @returns `Success` with the value and detail `exists` if the key was found,
98
103
  * `Success` with the value and detail `added` if the key was not found and added.
99
104
  * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either
100
105
  * is invalid.
106
+ * {@label WITH_VALUE}
101
107
  */
102
108
  getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
109
+ /**
110
+ * Gets a value from the map, or adds a value created by a factory function if it does not exist.
111
+ * @param key - The key of the element to be retrieved or created.
112
+ * @param factory - A {@link Collections.ResultMapValueFactory | factory function} to create the value if
113
+ * the key does not exist.
114
+ * @returns `Success` with the value and detail `exists` if the key was found, `Success` with
115
+ * the value and detail `added` if the key was not found and added. Fails with detail 'invalid-key'
116
+ * or 'invalid-value' and an error message if either is invalid.
117
+ * {@label WITH_FACTORY}
118
+ */
119
+ getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
103
120
  /**
104
121
  * Returns `true` if the map contains a key.
105
122
  * @param key - The key to check.
@@ -151,5 +168,13 @@ export declare class ResultMap<TK extends string = string, TV = unknown> impleme
151
168
  * @returns A readonly version of this map.
152
169
  */
153
170
  toReadOnly(): IReadOnlyResultMap<TK, TV>;
171
+ /**
172
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
173
+ * @param value - The value to check.
174
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
175
+ * `false` otherwise.
176
+ * @public
177
+ */
178
+ protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
154
179
  }
155
180
  //# sourceMappingURL=resultMap.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resultMap.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/resultMap.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAiB,cAAc,EAAkB,MAAM,EAAqB,MAAM,SAAS,CAAC;AACnG,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAGpG;;;GAGG;AACH,MAAM,WAAW,2BAA2B,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IACnF,OAAO,CAAC,EAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;CAC3C;AAED;;;;;GAKG;AACH,qBAAa,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AM,CAExB;IAED;;;;;;;;;OASG;IACI,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAQ5E;;;OAGG;IACI,MAAM,IAAI,WAAW,CAAC,EAAE,CAAC;IAIhC;;;OAGG;IACI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAInE;;;OAGG;IACI,UAAU,IAAI,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC;IAI/C;;;;;;OAMG;IACH,SAAS,CAAC,wBAAwB,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,EACtD,KAAK,EAAE,EAAE,GAAG,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC,GACxC,KAAK,IAAI,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC;CAG1C"}
@@ -122,21 +122,19 @@ class ResultMap {
122
122
  }
123
123
  return (0, base_1.failWithDetail)(`${key}: not found.`, 'not-found');
124
124
  }
125
- /**
126
- * Gets a value from the map, or adds a supplied value it if it does not exist.
127
- * @param key - The key to retrieve.
128
- * @param value - The value to add if the key does not exist.
129
- * @returns `Success` with the value and detail `exists` if the key was found,
130
- * `Success` with the value and detail `added` if the key was not found and added.
131
- * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either
132
- * is invalid.
133
- */
134
- getOrAdd(key, value) {
125
+ getOrAdd(key, valueOrFactory) {
135
126
  if (this._inner.has(key)) {
136
127
  return (0, base_1.succeedWithDetail)(this._inner.get(key), 'exists');
137
128
  }
138
- this._inner.set(key, value);
139
- return (0, base_1.succeedWithDetail)(value, 'added');
129
+ const factory = this._isResultMapValueFactory(valueOrFactory)
130
+ ? valueOrFactory
131
+ : () => (0, base_1.succeed)(valueOrFactory);
132
+ return factory(key)
133
+ .onSuccess((val) => {
134
+ this._inner.set(key, val);
135
+ return (0, base_1.succeedWithDetail)(val, 'added');
136
+ })
137
+ .withDetail('invalid-value', 'added');
140
138
  }
141
139
  /**
142
140
  * Returns `true` if the map contains a key.
@@ -211,6 +209,16 @@ class ResultMap {
211
209
  toReadOnly() {
212
210
  return this;
213
211
  }
212
+ /**
213
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
214
+ * @param value - The value to check.
215
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
216
+ * `false` otherwise.
217
+ * @public
218
+ */
219
+ _isResultMapValueFactory(value) {
220
+ return typeof value === 'function';
221
+ }
214
222
  }
215
223
  exports.ResultMap = ResultMap;
216
224
  //# sourceMappingURL=resultMap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"resultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/resultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAmG;AAGnG,mCAAqC;AAUrC;;;;;GAKG;AACH,MAAa,SAAS;IACpB;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAoBD;;;;OAIG;IACH,YACE,gBAAwF;QAExF,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,gBAAgB,aAAhB,gBAAgB,cAAhB,gBAAgB,GAAI,EAAE,CAAC;QACrG,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC;IAwBD;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CAClB,gBAAwF;QAExF,OAAO,IAAA,oBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,SAAS,CAAC,gBAAuD,CAAC,CAAC,CAAC;IACrG,CAAC;IAED;;;;;;;OAOG;IACI,GAAG,CAAC,GAAO,EAAE,KAAS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AAO,EAAE,KAAS;QAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC5B,OAAO,IAAA,wBAAiB,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,IAAA,qBAAc,EAAC,GAAG,GAAG,cAAc,EAAE,WAAW,CAAC,CAAC;IAC3D,CAAC;IAED;;;OAGG;IACI,MAAM;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACI,CAAC,MAAM,CAAC,QAAQ,CAAC;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;IAED;;;OAGG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAlPD,8BAkPC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, DetailedResult, failWithDetail, Result, succeedWithDetail } from '../base';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';\nimport { isIterable } from './utils';\n\n/**\n * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.\n * @public\n */\nexport interface IResultMapConstructorParams<TK extends string = string, TV = unknown> {\n entries?: Iterable<KeyValueEntry<TK, TV>>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} class as a `Map<TK, TV>`-like object which\n * reports success or failure with additional details using the\n * {@link https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#the-result-pattern | result pattern}.\n * @public\n */\nexport class ResultMap<TK extends string = string, TV = unknown> implements IReadOnlyResultMap<TK, TV> {\n /**\n * Readonly raw access to the inner `Map<TK, TV>` object.\n */\n public get inner(): ReadonlyMap<TK, TV> {\n return this._inner;\n }\n\n /**\n * Protected raw access to the inner `Map<TK, TV>` object.\n * @public\n */\n protected readonly _inner: Map<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param iterable - An iterable to initialize the map.\n */\n public constructor(iterable?: Iterable<KeyValueEntry<TK, TV>>);\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param params - An optional set of parameters to configure the map.\n */\n public constructor(params: IResultMapConstructorParams);\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param iterableOrParams - An iterable to initialize the map, or a set of parameters\n * to configure the map.\n */\n public constructor(\n iterableOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>\n ) {\n const params = isIterable(iterableOrParams) ? { entries: iterableOrParams } : iterableOrParams ?? {};\n this._inner = new Map(params.entries);\n }\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param elements - An optional iterable to initialize the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n elements: Iterable<KeyValueEntry<TK, TV>>\n ): Result<ResultMap<TK, TV>>;\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param params - An optional set of parameters to configure the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n params?: IResultMapConstructorParams<TK, TV>\n ): Result<ResultMap<TK, TV>>;\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param elementsOrParams - An optional iterable to initialize the map, or a set of parameters\n * to configure the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n elementsOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>\n ): Result<ResultMap<TK, TV>> {\n return captureResult(() => new ResultMap(elementsOrParams as IResultMapConstructorParams<TK, TV>));\n }\n\n /**\n * Sets a key/value pair in the map if the key does not already exist.\n * @param key - The key to set.\n * @param value - The value to set.\n * @returns `Success` with the value and detail `added` if the key was added,\n * `Failure` with detail `exists` if the key already exists. Fails with detail\n * 'invalid-key' or 'invalid-value' and an error message if either is invalid.\n */\n public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return failWithDetail(`${key}: already exists.`, 'exists');\n }\n this._inner.set(key, value);\n return succeedWithDetail(value, 'added');\n }\n\n /**\n * Clears the map.\n */\n public clear(): void {\n this._inner.clear();\n }\n\n /**\n * Deletes a key from the map.\n * @param key - The key to delete.\n * @returns `Success` with the previous value and the detail 'deleted'\n * if the key was found and deleted, `Failure` with detail 'not-found'\n * if the key was not found, or with detail 'invalid-key' if the key is invalid.\n */\n public delete(key: TK): DetailedResult<TV, ResultMapResultDetail> {\n const was = this._inner.get(key);\n if (this._inner.delete(key)) {\n return succeedWithDetail(was!, 'deleted');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Returns an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public entries(): MapIterator<KeyValueEntry<TK, TV>> {\n return this._inner.entries();\n }\n\n /**\n * Calls a function for each entry in the map.\n * @param cb - The function to call for each entry.\n * @param arg - An optional argument to pass to the callback.\n */\n public forEach(cb: ResultMapForEachCb<TK, TV>, arg?: unknown): void {\n for (const [key, value] of this._inner.entries()) {\n cb(value, key as TK, this, arg);\n }\n }\n\n /**\n * Gets a value from the map.\n * @param key - The key to retrieve.\n * @returns `Success` with the value and detail `exists` if the key was found,\n * `Failure` with detail `not-found` if the key was not found or with detail\n * `invalid-key` if the key is invalid.\n */\n public get(key: TK): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return succeedWithDetail(this._inner.get(key)!, 'exists');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Gets a value from the map, or adds a supplied value it if it does not exist.\n * @param key - The key to retrieve.\n * @param value - The value to add if the key does not exist.\n * @returns `Success` with the value and detail `exists` if the key was found,\n * `Success` with the value and detail `added` if the key was not found and added.\n * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either\n * is invalid.\n */\n public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return succeedWithDetail(this._inner.get(key)!, 'exists');\n }\n this._inner.set(key, value);\n return succeedWithDetail(value, 'added');\n }\n\n /**\n * Returns `true` if the map contains a key.\n * @param key - The key to check.\n * @returns `true` if the key exists, `false` otherwise.\n */\n public has(key: TK): boolean {\n return this._inner.has(key);\n }\n\n /**\n * Returns an iterator over the map keys.\n * @returns An iterator over the map keys.\n */\n public keys(): MapIterator<TK> {\n return this._inner.keys();\n }\n\n /**\n * Sets a key/value pair in the map.\n * @param key - The key to set.\n * @param value - The value to set.\n * @returns `Success` with the new value and the detail `updated` if the\n * key was found and updated, `Success` with the new value and detail\n * `added` if the key was not found and added. Fails with detail\n * 'invalid-key' or 'invalid-value' and an error message if either is invalid.\n */\n public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n const detail: ResultMapResultDetail = this._inner.has(key) ? 'updated' : 'added';\n this._inner.set(key, value);\n return succeedWithDetail(value, detail);\n }\n\n /**\n * Returns the number of entries in the map.\n */\n public get size(): number {\n return this._inner.size;\n }\n\n /**\n * Updates an existing key in the map - the map is not updated if the key does\n * not exist.\n * @param key - The key to update.\n * @param value - The value to set.\n * @returns `Success` with the value and detail 'exists' if the key was found\n * and the value updated, `Failure` an error message and with detail `not-found`\n * if the key was not found, or with detail 'invalid-key' or 'invalid-value'\n * if either is invalid.\n */\n public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n this._inner.set(key, value);\n return succeedWithDetail(value, 'updated');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Returns an iterator over the map values.\n * @returns An iterator over the map values.\n */\n public values(): MapIterator<TV> {\n return this._inner.values();\n }\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public [Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>> {\n return this._inner[Symbol.iterator]();\n }\n\n /**\n * Gets a readonly version of this map.\n * @returns A readonly version of this map.\n */\n public toReadOnly(): IReadOnlyResultMap<TK, TV> {\n return this;\n }\n}\n"]}
1
+ {"version":3,"file":"resultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/resultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAA4G;AAG5G,mCAAqC;AAgBrC;;;;;GAKG;AACH,MAAa,SAAS;IACpB;;OAEG;IACH,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAoBD;;;;OAIG;IACH,YACE,gBAAwF;QAExF,MAAM,MAAM,GAAG,IAAA,kBAAU,EAAC,gBAAgB,CA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C,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;IAED;;;OAGG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;OAMG;IACO,wBAAwB,CAChC,KAAyC;QAEzC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;IACrC,CAAC;CACF;AAzRD,8BAyRC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, DetailedResult, failWithDetail, Result, succeed, succeedWithDetail } from '../base';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapForEachCb, ResultMapResultDetail } from './readonlyResultMap';\nimport { isIterable } from './utils';\n\n/**\n * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.\n * @public\n */\nexport interface IResultMapConstructorParams<TK extends string = string, TV = unknown> {\n entries?: Iterable<KeyValueEntry<TK, TV>>;\n}\n\n/**\n * Deferred constructor for the {@link Collections.ResultMap.(getOrAdd:2) | getOrAdd} method.\n * @public\n */\nexport type ResultMapValueFactory<TK extends string = string, TV = unknown> = (key: TK) => Result<TV>;\n\n/**\n * A {@link Collections.ResultMap | ResultMap} class as a `Map<TK, TV>`-like object which\n * reports success or failure with additional details using the\n * {@link https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#the-result-pattern | result pattern}.\n * @public\n */\nexport class ResultMap<TK extends string = string, TV = unknown> implements IReadOnlyResultMap<TK, TV> {\n /**\n * Readonly raw access to the inner `Map<TK, TV>` object.\n */\n public get inner(): ReadonlyMap<TK, TV> {\n return this._inner;\n }\n\n /**\n * Protected raw access to the inner `Map<TK, TV>` object.\n * @public\n */\n protected readonly _inner: Map<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param iterable - An iterable to initialize the map.\n */\n public constructor(iterable?: Iterable<KeyValueEntry<TK, TV>>);\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param params - An optional set of parameters to configure the map.\n */\n public constructor(params: IResultMapConstructorParams);\n\n /**\n * Constructs a new {@link Collections.ResultMap | ResultMap}.\n * @param iterableOrParams - An iterable to initialize the map, or a set of parameters\n * to configure the map.\n */\n public constructor(\n iterableOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>\n ) {\n const params = isIterable(iterableOrParams) ? { entries: iterableOrParams } : iterableOrParams ?? {};\n this._inner = new Map(params.entries);\n }\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param elements - An optional iterable to initialize the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n elements: Iterable<KeyValueEntry<TK, TV>>\n ): Result<ResultMap<TK, TV>>;\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param params - An optional set of parameters to configure the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n params?: IResultMapConstructorParams<TK, TV>\n ): Result<ResultMap<TK, TV>>;\n\n /**\n * Creates a new {@link Collections.ResultMap | ResultMap}.\n * @param elementsOrParams - An optional iterable to initialize the map, or a set of parameters\n * to configure the map.\n * @returns `Success` with the new map, or `Failure` with error details\n * if an error occurred.\n * @public\n */\n public static create<TK extends string = string, TV = unknown>(\n elementsOrParams?: Iterable<KeyValueEntry<TK, TV>> | IResultMapConstructorParams<TK, TV>\n ): Result<ResultMap<TK, TV>> {\n return captureResult(() => new ResultMap(elementsOrParams as IResultMapConstructorParams<TK, TV>));\n }\n\n /**\n * Sets a key/value pair in the map if the key does not already exist.\n * @param key - The key to set.\n * @param value - The value to set.\n * @returns `Success` with the value and detail `added` if the key was added,\n * `Failure` with detail `exists` if the key already exists. Fails with detail\n * 'invalid-key' or 'invalid-value' and an error message if either is invalid.\n */\n public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return failWithDetail(`${key}: already exists.`, 'exists');\n }\n this._inner.set(key, value);\n return succeedWithDetail(value, 'added');\n }\n\n /**\n * Clears the map.\n */\n public clear(): void {\n this._inner.clear();\n }\n\n /**\n * Deletes a key from the map.\n * @param key - The key to delete.\n * @returns `Success` with the previous value and the detail 'deleted'\n * if the key was found and deleted, `Failure` with detail 'not-found'\n * if the key was not found, or with detail 'invalid-key' if the key is invalid.\n */\n public delete(key: TK): DetailedResult<TV, ResultMapResultDetail> {\n const was = this._inner.get(key);\n if (this._inner.delete(key)) {\n return succeedWithDetail(was!, 'deleted');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Returns an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public entries(): MapIterator<KeyValueEntry<TK, TV>> {\n return this._inner.entries();\n }\n\n /**\n * Calls a function for each entry in the map.\n * @param cb - The function to call for each entry.\n * @param arg - An optional argument to pass to the callback.\n */\n public forEach(cb: ResultMapForEachCb<TK, TV>, arg?: unknown): void {\n for (const [key, value] of this._inner.entries()) {\n cb(value, key as TK, this, arg);\n }\n }\n\n /**\n * Gets a value from the map.\n * @param key - The key to retrieve.\n * @returns `Success` with the value and detail `exists` if the key was found,\n * `Failure` with detail `not-found` if the key was not found or with detail\n * `invalid-key` if the key is invalid.\n */\n public get(key: TK): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return succeedWithDetail(this._inner.get(key)!, 'exists');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Gets a value from the map, or adds a supplied value it if it does not exist.\n * @param key - The key to be retrieved or created.\n * @param value - The value to add if the key does not exist.\n * @returns `Success` with the value and detail `exists` if the key was found,\n * `Success` with the value and detail `added` if the key was not found and added.\n * Fails with detail 'invalid-key' or 'invalid-value' and an error message if either\n * is invalid.\n * {@label WITH_VALUE}\n */\n public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * Gets a value from the map, or adds a value created by a factory function if it does not exist.\n * @param key - The key of the element to be retrieved or created.\n * @param factory - A {@link Collections.ResultMapValueFactory | factory function} to create the value if\n * the key does not exist.\n * @returns `Success` with the value and detail `exists` if the key was found, `Success` with\n * the value and detail `added` if the key was not found and added. Fails with detail 'invalid-key'\n * or 'invalid-value' and an error message if either is invalid.\n * {@label WITH_FACTORY}\n */\n public getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;\n public getOrAdd(\n key: TK,\n valueOrFactory: TV | ResultMapValueFactory<TK, TV>\n ): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n return succeedWithDetail(this._inner.get(key)!, 'exists');\n }\n\n const factory: ResultMapValueFactory<TK, TV> = this._isResultMapValueFactory(valueOrFactory)\n ? valueOrFactory\n : () => succeed(valueOrFactory);\n\n return factory(key)\n .onSuccess((val) => {\n this._inner.set(key, val);\n return succeedWithDetail(val, 'added');\n })\n .withDetail('invalid-value', 'added');\n }\n\n /**\n * Returns `true` if the map contains a key.\n * @param key - The key to check.\n * @returns `true` if the key exists, `false` otherwise.\n */\n public has(key: TK): boolean {\n return this._inner.has(key);\n }\n\n /**\n * Returns an iterator over the map keys.\n * @returns An iterator over the map keys.\n */\n public keys(): MapIterator<TK> {\n return this._inner.keys();\n }\n\n /**\n * Sets a key/value pair in the map.\n * @param key - The key to set.\n * @param value - The value to set.\n * @returns `Success` with the new value and the detail `updated` if the\n * key was found and updated, `Success` with the new value and detail\n * `added` if the key was not found and added. Fails with detail\n * 'invalid-key' or 'invalid-value' and an error message if either is invalid.\n */\n public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n const detail: ResultMapResultDetail = this._inner.has(key) ? 'updated' : 'added';\n this._inner.set(key, value);\n return succeedWithDetail(value, detail);\n }\n\n /**\n * Returns the number of entries in the map.\n */\n public get size(): number {\n return this._inner.size;\n }\n\n /**\n * Updates an existing key in the map - the map is not updated if the key does\n * not exist.\n * @param key - The key to update.\n * @param value - The value to set.\n * @returns `Success` with the value and detail 'exists' if the key was found\n * and the value updated, `Failure` an error message and with detail `not-found`\n * if the key was not found, or with detail 'invalid-key' or 'invalid-value'\n * if either is invalid.\n */\n public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n if (this._inner.has(key)) {\n this._inner.set(key, value);\n return succeedWithDetail(value, 'updated');\n }\n return failWithDetail(`${key}: not found.`, 'not-found');\n }\n\n /**\n * Returns an iterator over the map values.\n * @returns An iterator over the map values.\n */\n public values(): MapIterator<TV> {\n return this._inner.values();\n }\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n public [Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>> {\n return this._inner[Symbol.iterator]();\n }\n\n /**\n * Gets a readonly version of this map.\n * @returns A readonly version of this map.\n */\n public toReadOnly(): IReadOnlyResultMap<TK, TV> {\n return this;\n }\n\n /**\n * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.\n * @param value - The value to check.\n * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},\n * `false` otherwise.\n * @public\n */\n protected _isResultMapValueFactory<TK extends string, TV>(\n value: TV | ResultMapValueFactory<TK, TV>\n ): value is ResultMapValueFactory<TK, TV> {\n return typeof value === 'function';\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import { DetailedResult } from '../base';
2
2
  import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
3
- import { ResultMap } from './resultMap';
3
+ import { ResultMap, ResultMapValueFactory } from './resultMap';
4
4
  import { KeyValueValidators } from './utils';
5
5
  /**
6
6
  * A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapValidator | ResultMapValidator}.
@@ -59,9 +59,13 @@ export declare class ResultMapValidator<TK extends string = string, TV = unknown
59
59
  */
60
60
  get(key: string): DetailedResult<TV, ResultMapResultDetail>;
61
61
  /**
62
- * {@inheritdoc Collections.ResultMap.getOrAdd}
62
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
63
63
  */
64
64
  getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
65
+ /**
66
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
67
+ */
68
+ getOrAdd(key: string, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
65
69
  /**
66
70
  * {@inheritdoc Collections.ResultMap.has}
67
71
  */
@@ -78,5 +82,13 @@ export declare class ResultMapValidator<TK extends string = string, TV = unknown
78
82
  * Gets a read-only version of this validator.
79
83
  */
80
84
  toReadOnly(): IReadOnlyResultMapValidator<TK, TV>;
85
+ /**
86
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
87
+ * @param value - The value to check.
88
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
89
+ * `false` otherwise.
90
+ * @public
91
+ */
92
+ protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
81
93
  }
82
94
  //# sourceMappingURL=resultMapValidator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resultMapValidator.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/resultMapValidator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE7C;;;GAGG;AACH,MAAM,WAAW,2BAA2B,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IACnF;;OAEG;IACH,QAAQ,CAAC,UAAU,EAAE,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAEhD;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAEzC;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC,CAAC;IAE5D;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,WAAW,+BAA+B,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IACvF,GAAG,EAAE,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IACvB,UAAU,EAAE,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;CACxC;AAED;;;;GAIG;AACH,qBAAa,kBAAkB,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,CACtE,YAAW,2BAA2B,CAAC,EAAE,EAAE,EAAE,CAAC;IAE9C,SA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23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.ResultMapValidator = void 0;
25
+ const base_1 = require("../base");
25
26
  /**
26
27
  * A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys
27
28
  * before calling the wrapped result map.
@@ -63,13 +64,24 @@ class ResultMapValidator {
63
64
  return this._map.get(k);
64
65
  });
65
66
  }
66
- /**
67
- * {@inheritdoc Collections.ResultMap.getOrAdd}
68
- */
69
- getOrAdd(key, value) {
70
- return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {
71
- return this._map.getOrAdd(vk, vv);
72
- });
67
+ getOrAdd(key, valueOrFactory) {
68
+ if (!this._isResultMapValueFactory(valueOrFactory)) {
69
+ return this.validators.validateEntry([key, valueOrFactory]).onSuccess(([vk, vv]) => {
70
+ return this._map.getOrAdd(vk, vv);
71
+ });
72
+ }
73
+ else {
74
+ return this.validators.validateKey(key).onSuccess((k) => {
75
+ return this._map.get(k).onFailure(() => {
76
+ const value = valueOrFactory(k)
77
+ .onSuccess((value) => this.validators.validateEntry([k, value]))
78
+ .onSuccess(([__key, value]) => (0, base_1.succeedWithDetail)(value, 'added'));
79
+ return value.success
80
+ ? this._map.add(k, value.value)
81
+ : (0, base_1.failWithDetail)(value.message, 'invalid-value');
82
+ });
83
+ });
84
+ }
73
85
  }
74
86
  /**
75
87
  * {@inheritdoc Collections.ResultMap.has}
@@ -99,6 +111,16 @@ class ResultMapValidator {
99
111
  toReadOnly() {
100
112
  return this;
101
113
  }
114
+ /**
115
+ * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
116
+ * @param value - The value to check.
117
+ * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
118
+ * `false` otherwise.
119
+ * @public
120
+ */
121
+ _isResultMapValueFactory(value) {
122
+ return typeof value === 'function';
123
+ }
102
124
  }
103
125
  exports.ResultMapValidator = ResultMapValidator;
104
126
  //# sourceMappingURL=resultMapValidator.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"resultMapValidator.js","sourceRoot":"","sources":["../../../src/packlets/collections/resultMapValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AA0CH;;;;GAIG;AACH,MAAa,kBAAkB;IAI7B,IAAW,GAAG;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAID;;;OAGG;IACH,YAAmB,MAA+C;QAChE,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IACtC,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW,EAAE,KAAc;QACpC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAW;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;YACtD,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;YACtD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,QAAQ,CAAC,GAAW,EAAE,KAAc;QACzC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QACpC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAS,CAAC,CAAC;IAClC,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW,EAAE,KAAc;QACpC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAW,EAAE,KAAc;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAtFD,gDAsFC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { DetailedResult } from '../base';\nimport { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';\nimport { ResultMap } from './resultMap';\nimport { KeyValueValidators } from './utils';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapValidator | ResultMapValidator}.\n * @public\n */\nexport interface IReadOnlyResultMapValidator<TK extends string = string, TV = unknown> {\n /**\n * {@inheritdoc Collections.ResultMapValidator.validators}\n */\n readonly validators: KeyValueValidators<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMapValidator.map}\n */\n readonly map: IReadOnlyResultMap<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n get(key: string): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n has(key: string): boolean;\n}\n\n/**\n * Parameters for constructing a {@link Collections.ResultMapValidator | ResultMapValidator}.\n * @public\n */\nexport interface IResultMapValidatorCreateParams<TK extends string = string, TV = unknown> {\n map: ResultMap<TK, TV>;\n validators: KeyValueValidators<TK, TV>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys\n * before calling the wrapped result map.\n * @public\n */\nexport class ResultMapValidator<TK extends string = string, TV = unknown>\n implements IReadOnlyResultMapValidator<TK, TV>\n{\n public readonly validators: KeyValueValidators<TK, TV>;\n public get map(): IReadOnlyResultMap<TK, TV> {\n return this._map;\n }\n\n protected _map: ResultMap<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @param params - Required parameters for constructing the map.\n */\n public constructor(params: IResultMapValidatorCreateParams<TK, TV>) {\n this._map = params.map;\n this.validators = params.validators;\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.add(vk, vv);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.delete}\n */\n public delete(key: string): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateKey(key).onSuccess((k) => {\n return this._map.delete(k);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n public get(key: string): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateKey(key).onSuccess((k) => {\n return this._map.get(k);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.getOrAdd}\n */\n public getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.getOrAdd(vk, vv);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n public has(key: string): boolean {\n return this._map.has(key as TK);\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.set}\n */\n public set(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.set(vk, vv);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.update}\n */\n public update(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.update(vk, vv);\n });\n }\n\n /**\n * Gets a read-only version of this validator.\n */\n public toReadOnly(): IReadOnlyResultMapValidator<TK, TV> {\n return this;\n }\n}\n"]}
1
+ {"version":3,"file":"resultMapValidator.js","sourceRoot":"","sources":["../../../src/packlets/collections/resultMapValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAA4E;AAwC5E;;;;GAIG;AACH,MAAa,kBAAkB;IAI7B,IAAW,GAAG;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAID;;;OAGG;IACH,YAAmB,MAA+C;QAChE,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IACtC,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW,EAAE,KAAc;QACpC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAW;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;YACtD,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE;YACt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,CAAC;QACL,CAAC;IACH,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAS,CAAC,CAAC;IAClC,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAW,EAAE,KAAc;QACpC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAW,EAAE,KAAc;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACxE,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;OAMG;IACO,wBAAwB,CAChC,KAAyC;QAEzC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;IACrC,CAAC;CACF;AA7HD,gDA6HC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { DetailedResult, failWithDetail, succeedWithDetail } from '../base';\nimport { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';\nimport { ResultMap, ResultMapValueFactory } from './resultMap';\nimport { KeyValueValidators } from './utils';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapValidator | ResultMapValidator}.\n * @public\n */\nexport interface IReadOnlyResultMapValidator<TK extends string = string, TV = unknown> {\n /**\n * {@inheritdoc Collections.ResultMapValidator.validators}\n */\n readonly validators: KeyValueValidators<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMapValidator.map}\n */\n readonly map: IReadOnlyResultMap<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n get(key: string): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n has(key: string): boolean;\n}\n\n/**\n * Parameters for constructing a {@link Collections.ResultMapValidator | ResultMapValidator}.\n * @public\n */\nexport interface IResultMapValidatorCreateParams<TK extends string = string, TV = unknown> {\n map: ResultMap<TK, TV>;\n validators: KeyValueValidators<TK, TV>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys\n * before calling the wrapped result map.\n * @public\n */\nexport class ResultMapValidator<TK extends string = string, TV = unknown>\n implements IReadOnlyResultMapValidator<TK, TV>\n{\n public readonly validators: KeyValueValidators<TK, TV>;\n public get map(): IReadOnlyResultMap<TK, TV> {\n return this._map;\n }\n\n protected _map: ResultMap<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @param params - Required parameters for constructing the map.\n */\n public constructor(params: IResultMapValidatorCreateParams<TK, TV>) {\n this._map = params.map;\n this.validators = params.validators;\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.add(vk, vv);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.delete}\n */\n public delete(key: string): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateKey(key).onSuccess((k) => {\n return this._map.delete(k);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n public get(key: string): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateKey(key).onSuccess((k) => {\n return this._map.get(k);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}\n */\n public getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}\n */\n public getOrAdd(\n key: string,\n factory: ResultMapValueFactory<TK, TV>\n ): DetailedResult<TV, ResultMapResultDetail>;\n public getOrAdd(\n key: string,\n valueOrFactory: unknown | ResultMapValueFactory<TK, TV>\n ): DetailedResult<TV, ResultMapResultDetail> {\n if (!this._isResultMapValueFactory(valueOrFactory)) {\n return this.validators.validateEntry([key, valueOrFactory]).onSuccess(([vk, vv]) => {\n return this._map.getOrAdd(vk, vv);\n });\n } else {\n return this.validators.validateKey(key).onSuccess((k) => {\n return this._map.get(k).onFailure(() => {\n const value = valueOrFactory(k)\n .onSuccess((value) => this.validators.validateEntry([k, value]))\n .onSuccess(([__key, value]) => succeedWithDetail(value, 'added'));\n\n return value.success\n ? this._map.add(k, value.value)\n : failWithDetail<TV, ResultMapResultDetail>(value.message, 'invalid-value');\n });\n });\n }\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n public has(key: string): boolean {\n return this._map.has(key as TK);\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.set}\n */\n public set(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.set(vk, vv);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.update}\n */\n public update(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.validators.validateEntry([key, value]).onSuccess(([vk, vv]) => {\n return this._map.update(vk, vv);\n });\n }\n\n /**\n * Gets a read-only version of this validator.\n */\n public toReadOnly(): IReadOnlyResultMapValidator<TK, TV> {\n return this;\n }\n\n /**\n * Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.\n * @param value - The value to check.\n * @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},\n * `false` otherwise.\n * @public\n */\n protected _isResultMapValueFactory<TK extends string, TV>(\n value: TV | ResultMapValueFactory<TK, TV>\n ): value is ResultMapValueFactory<TK, TV> {\n return typeof value === 'function';\n }\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { DetailedResult, Result } from '../base';
2
2
  import { KeyValueEntry } from './common';
3
3
  import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
4
- import { ResultMap } from './resultMap';
4
+ import { ResultMap, ResultMapValueFactory } from './resultMap';
5
5
  import { IReadOnlyResultMapValidator, ResultMapValidator } from './resultMapValidator';
6
6
  import { KeyValueValidators } from './utils';
7
7
  /**
@@ -50,9 +50,16 @@ export declare class ValidatingResultMap<TK extends string = string, TV = unknow
50
50
  */
51
51
  add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
52
52
  /**
53
- * {@inheritdoc Collections.ResultMap.getOrAdd}
53
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
54
54
  */
55
55
  getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
56
+ /**
57
+ * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
58
+ */
59
+ getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
60
+ /**
61
+ * {@inheritdoc Collections.ResultMap.set}
62
+ */
56
63
  set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
57
64
  /**
58
65
  * {@inheritdoc Collections.ResultMap.update}
@@ -1 +1 @@
1
- {"version":3,"file":"validatingResultMap.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/validatingResultMap.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAiB,cAAc,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,2BAA2B,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACvF,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE7C;;;GAGG;AACH,MAAM,WAAW,4BAA4B,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,CACpF,SAAQ,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC;IAClC;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,2BAA2B,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;CACxD;AAED;;;GAGG;AACH,MAAM,WAAW,qCAAqC,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IAC7F,OAAO,CAAC,EAAE,QAAQ,CAAC,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,C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AE,CAAC;IAMxE;;;;;OAKG;WACW,gBAAgB,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,EACrE,MAAM,EAAE,qCAAqC,CAAC,EAAE,EAAE,EAAE,CAAC,GACpD,MAAM,CAAC,mBAAmB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAItC;;OAEG;IACI,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMzE;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAC9E;;OAEG;IACI,QAAQ,CAAC,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAqB3G;;OAEG;IACI,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMzE;;OAEG;IACI,MAAM,CAAC,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAM5E;;OAEG;IACI,UAAU,IAAI,4BAA4B,CAAC,EAAE,EAAE,EAAE,CAAC;CAG1D"}
@@ -58,15 +58,24 @@ class ValidatingResultMap extends resultMap_1.ResultMap {
58
58
  return super.add(entry[0], entry[1]);
59
59
  });
60
60
  }
61
- /**
62
- * {@inheritdoc Collections.ResultMap.getOrAdd}
63
- */
64
- getOrAdd(key, value) {
65
- return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {
66
- return super.getOrAdd(entry[0], entry[1]);
67
- });
61
+ getOrAdd(key, valueOrFactory) {
62
+ if (!this._isResultMapValueFactory(valueOrFactory)) {
63
+ return this.validate.validators.validateEntry([key, valueOrFactory]).onSuccess((entry) => {
64
+ return super.getOrAdd(entry[0], entry[1]);
65
+ });
66
+ }
67
+ else {
68
+ return super.get(key).onFailure(() => {
69
+ const value = valueOrFactory(key)
70
+ .onSuccess((value) => this.validate.validators.validateEntry([key, value]))
71
+ .onSuccess(([key, value]) => (0, base_1.succeed)(value));
72
+ return value.success
73
+ ? super.add(key, value.value)
74
+ : (0, base_1.failWithDetail)(value.message, 'invalid-value');
75
+ });
76
+ }
68
77
  }
69
- /*
78
+ /**
70
79
  * {@inheritdoc Collections.ResultMap.set}
71
80
  */
72
81
  set(key, value) {
@@ -1 +1 @@
1
- {"version":3,"file":"validatingResultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/validatingResultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAgE;AAGhE,2CAAwC;AACxC,6DAAuF;AAwBvF;;;;GAIG;AACH,MAAa,mBACX,SAAQ,qBAAiB;IASzB;;;OAGG;IACH,YAAmB,MAAqD;;QACtE,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACzF,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,uCAAkB,CAAS,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAC/F,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,gBAAgB,CAC5B,MAAqD;QAErD,OAAO,IAAA,oBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAO,EAAE,KAAS;QAC3B,OAA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,CAAC;IACL,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA1ED,kDA0EC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, DetailedResult, Result } from '../base';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';\nimport { ResultMap } from './resultMap';\nimport { IReadOnlyResultMapValidator, ResultMapValidator } from './resultMapValidator';\nimport { KeyValueValidators } from './utils';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @public\n */\nexport interface IReadOnlyValidatingResultMap<TK extends string = string, TV = unknown>\n extends IReadOnlyResultMap<TK, TV> {\n /**\n * {@inheritdoc Collections.ValidatingResultMap.validate}\n */\n readonly validate: IReadOnlyResultMapValidator<TK, TV>;\n}\n\n/**\n * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.\n * @public\n */\nexport interface IValidatingResultMapConstructorParams<TK extends string = string, TV = unknown> {\n entries?: Iterable<KeyValueEntry<string, unknown>>;\n validators: KeyValueValidators<TK, TV>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapValidator | validator}\n * property that enables validated use of the underlying map with weakly-typed keys and values.\n * @public\n */\nexport class ValidatingResultMap<TK extends string = string, TV = unknown>\n extends ResultMap<TK, TV>\n implements IReadOnlyValidatingResultMap<TK, TV>\n{\n /**\n * A {@link Collections.ResultMapValidator | ResultMapValidator} which validates keys and values\n * before inserting them into this collection.\n */\n public readonly validate: ResultMapValidator<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @param params - Required parameters for constructing the map.\n */\n public constructor(params: IValidatingResultMapConstructorParams<TK, TV>) {\n const entries = params.validators.validateEntries([...(params.entries ?? [])]).orThrow();\n super({ entries });\n this.validate = new ResultMapValidator<TK, TV>({ map: this, validators: params.validators });\n }\n\n /**\n * Creates a new {@link Collections.ValidatingResultMap | ValidatingResultMap} instance.\n * @param params - Required parameters for constructing the map.\n * @returns `Success` with the new map if successful, `Failure` otherwise.\n * @public\n */\n public static createValidating<TK extends string = string, TV = unknown>(\n params: IValidatingResultMapConstructorParams<TK, TV>\n ): Result<ValidatingResultMap<TK, TV>> {\n return captureResult(() => new ValidatingResultMap(params));\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.add(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.getOrAdd}\n */\n public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.getOrAdd(entry[0], entry[1]);\n });\n }\n\n /*\n * {@inheritdoc Collections.ResultMap.set}\n */\n public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.set(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.update}\n */\n public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.update(entry[0], entry[1]);\n });\n }\n\n /**\n * Gets a read-only version of this map.\n */\n public toReadOnly(): IReadOnlyValidatingResultMap<TK, TV> {\n return this;\n }\n}\n"]}
1
+ {"version":3,"file":"validatingResultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/validatingResultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAyF;AAGzF,2CAA+D;AAC/D,6DAAuF;AAwBvF;;;;GAIG;AACH,MAAa,mBACX,SAAQ,qBAAiB;IASzB;;;OAGG;IACH,YAAmB,MAAqD;;QACtE,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACzF,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,uCAAkB,CAAS,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IAC/F,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,gBAAgB,CAC5B,MAAqD;QAErD,OAAO,IAAA,oBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAO,EAAE,KAAS;QAC3B,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAA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ACH,CAAC;IAED;;OAEG;IACI,GAAG,CAAC,GAAO,EAAE,KAAS;QAC3B,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;YAC9E,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACvC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAO,EAAE,KAAS;QAC9B,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;YAC9E,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,UAAU;QACf,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA7FD,kDA6FC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, DetailedResult, failWithDetail, Result, succeed } from '../base';\nimport { KeyValueEntry } from './common';\nimport { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';\nimport { ResultMap, ResultMapValueFactory } from './resultMap';\nimport { IReadOnlyResultMapValidator, ResultMapValidator } from './resultMapValidator';\nimport { KeyValueValidators } from './utils';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @public\n */\nexport interface IReadOnlyValidatingResultMap<TK extends string = string, TV = unknown>\n extends IReadOnlyResultMap<TK, TV> {\n /**\n * {@inheritdoc Collections.ValidatingResultMap.validate}\n */\n readonly validate: IReadOnlyResultMapValidator<TK, TV>;\n}\n\n/**\n * Parameters for constructing a {@link Collections.ResultMap | ResultMap}.\n * @public\n */\nexport interface IValidatingResultMapConstructorParams<TK extends string = string, TV = unknown> {\n entries?: Iterable<KeyValueEntry<string, unknown>>;\n validators: KeyValueValidators<TK, TV>;\n}\n\n/**\n * A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapValidator | validator}\n * property that enables validated use of the underlying map with weakly-typed keys and values.\n * @public\n */\nexport class ValidatingResultMap<TK extends string = string, TV = unknown>\n extends ResultMap<TK, TV>\n implements IReadOnlyValidatingResultMap<TK, TV>\n{\n /**\n * A {@link Collections.ResultMapValidator | ResultMapValidator} which validates keys and values\n * before inserting them into this collection.\n */\n public readonly validate: ResultMapValidator<TK, TV>;\n\n /**\n * Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.\n * @param params - Required parameters for constructing the map.\n */\n public constructor(params: IValidatingResultMapConstructorParams<TK, TV>) {\n const entries = params.validators.validateEntries([...(params.entries ?? [])]).orThrow();\n super({ entries });\n this.validate = new ResultMapValidator<TK, TV>({ map: this, validators: params.validators });\n }\n\n /**\n * Creates a new {@link Collections.ValidatingResultMap | ValidatingResultMap} instance.\n * @param params - Required parameters for constructing the map.\n * @returns `Success` with the new map if successful, `Failure` otherwise.\n * @public\n */\n public static createValidating<TK extends string = string, TV = unknown>(\n params: IValidatingResultMapConstructorParams<TK, TV>\n ): Result<ValidatingResultMap<TK, TV>> {\n return captureResult(() => new ValidatingResultMap(params));\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.add(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:1)}\n */\n public getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;\n /**\n * {@inheritdoc Collections.ResultMap.(getOrAdd:2)}\n */\n public getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;\n public getOrAdd(\n key: TK,\n valueOrFactory: TV | ResultMapValueFactory<TK, TV>\n ): DetailedResult<TV, ResultMapResultDetail> {\n if (!this._isResultMapValueFactory(valueOrFactory)) {\n return this.validate.validators.validateEntry([key, valueOrFactory]).onSuccess((entry) => {\n return super.getOrAdd(entry[0], entry[1]);\n });\n } else {\n return super.get(key).onFailure(() => {\n const value = valueOrFactory(key)\n .onSuccess((value) => this.validate.validators.validateEntry([key, value]))\n .onSuccess(([key, value]) => succeed(value));\n return value.success\n ? super.add(key, value.value)\n : failWithDetail<TV, ResultMapResultDetail>(value.message, 'invalid-value');\n });\n }\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.set}\n */\n public set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.set(entry[0], entry[1]);\n });\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.update}\n */\n public update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail> {\n return this.validate.validators.validateEntry([key, value]).onSuccess((entry) => {\n return super.update(entry[0], entry[1]);\n });\n }\n\n /**\n * Gets a read-only version of this map.\n */\n public toReadOnly(): IReadOnlyValidatingResultMap<TK, TV> {\n return this;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-utils.d.ts",