@fgv/ts-utils 4.2.2 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +18 -0
- package/CHANGELOG.md +13 -1
- package/dist/ts-utils.d.ts +3177 -2645
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -2
- package/lib/index.js.map +1 -1
- package/lib/packlets/base/result.d.ts +33 -0
- package/lib/packlets/base/result.d.ts.map +1 -1
- package/lib/packlets/base/result.js +24 -0
- package/lib/packlets/base/result.js.map +1 -1
- package/lib/packlets/collections/collectible.d.ts +80 -0
- package/lib/packlets/collections/collectible.d.ts.map +1 -0
- package/lib/packlets/collections/collectible.js +93 -0
- package/lib/packlets/collections/collectible.js.map +1 -0
- package/lib/packlets/collections/collector.d.ts +194 -0
- package/lib/packlets/collections/collector.d.ts.map +1 -0
- package/lib/packlets/collections/collector.js +189 -0
- package/lib/packlets/collections/collector.js.map +1 -0
- package/lib/packlets/collections/collectorConverter.d.ts +86 -0
- package/lib/packlets/collections/collectorConverter.d.ts.map +1 -0
- package/lib/packlets/collections/collectorConverter.js +98 -0
- package/lib/packlets/collections/collectorConverter.js.map +1 -0
- package/lib/packlets/collections/collectors.d.ts +31 -0
- package/lib/packlets/collections/collectors.d.ts.map +1 -0
- package/lib/packlets/collections/collectors.js +54 -0
- package/lib/packlets/collections/collectors.js.map +1 -0
- package/lib/packlets/collections/convertingCollector.d.ts +73 -0
- package/lib/packlets/collections/convertingCollector.d.ts.map +1 -0
- package/lib/packlets/collections/convertingCollector.js +67 -0
- package/lib/packlets/collections/convertingCollector.js.map +1 -0
- package/lib/packlets/collections/convertingResultMap.d.ts +53 -0
- package/lib/packlets/collections/convertingResultMap.d.ts.map +1 -0
- package/lib/packlets/collections/convertingResultMap.js +61 -0
- package/lib/packlets/collections/convertingResultMap.js.map +1 -0
- package/lib/packlets/collections/index.d.ts +8 -2
- package/lib/packlets/collections/index.d.ts.map +1 -1
- package/lib/packlets/collections/index.js +8 -2
- package/lib/packlets/collections/index.js.map +1 -1
- package/lib/packlets/collections/keyValueConverters.d.ts +74 -0
- package/lib/packlets/collections/keyValueConverters.d.ts.map +1 -0
- package/lib/packlets/collections/keyValueConverters.js +104 -0
- package/lib/packlets/collections/keyValueConverters.js.map +1 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts +1 -1
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -1
- package/lib/packlets/collections/readonlyResultMap.js.map +1 -1
- package/lib/packlets/collections/{resultMapValidator.d.ts → resultMapConverter.d.ts} +14 -18
- package/lib/packlets/collections/resultMapConverter.d.ts.map +1 -0
- package/lib/packlets/collections/{resultMapValidator.js → resultMapConverter.js} +15 -15
- package/lib/packlets/collections/resultMapConverter.js.map +1 -0
- package/lib/packlets/collections/utils.d.ts +0 -61
- package/lib/packlets/collections/utils.d.ts.map +1 -1
- package/lib/packlets/collections/utils.js +0 -82
- package/lib/packlets/collections/utils.js.map +1 -1
- package/lib/packlets/conversion/baseConverter.d.ts +12 -7
- package/lib/packlets/conversion/baseConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/baseConverter.js +12 -8
- package/lib/packlets/conversion/baseConverter.js.map +1 -1
- package/lib/packlets/conversion/converter.d.ts +6 -6
- package/lib/packlets/conversion/converter.d.ts.map +1 -1
- package/lib/packlets/conversion/converter.js.map +1 -1
- package/lib/packlets/conversion/converters.d.ts +8 -0
- package/lib/packlets/conversion/converters.d.ts.map +1 -1
- package/lib/packlets/conversion/converters.js +11 -0
- package/lib/packlets/conversion/converters.js.map +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/objectConverter.js.map +1 -1
- package/lib/packlets/validation/common.d.ts +8 -0
- package/lib/packlets/validation/common.d.ts.map +1 -1
- package/lib/packlets/validation/common.js.map +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +4 -6
- package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
- package/lib/packlets/validation/genericValidator.js.map +1 -1
- package/lib/packlets/validation/number.d.ts.map +1 -1
- package/lib/packlets/validation/number.js +3 -1
- package/lib/packlets/validation/number.js.map +1 -1
- package/lib/packlets/validation/string.d.ts.map +1 -1
- package/lib/packlets/validation/string.js +3 -1
- package/lib/packlets/validation/string.js.map +1 -1
- package/lib/packlets/validation/validators.d.ts +12 -2
- package/lib/packlets/validation/validators.d.ts.map +1 -1
- package/lib/packlets/validation/validators.js +13 -0
- package/lib/packlets/validation/validators.js.map +1 -1
- package/package.json +1 -1
- package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
- package/lib/packlets/collections/resultMapValidator.js.map +0 -1
- package/lib/packlets/collections/validatingResultMap.d.ts +0 -73
- package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/validatingResultMap.js +0 -102
- package/lib/packlets/collections/validatingResultMap.js.map +0 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.KeyValueConverters = void 0;
|
|
25
|
+
const base_1 = require("../base");
|
|
26
|
+
const conversion_1 = require("../conversion");
|
|
27
|
+
/**
|
|
28
|
+
* Helper class for converting strongly-typed keys, values, or entries
|
|
29
|
+
* from unknown values.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
class KeyValueConverters {
|
|
33
|
+
/**
|
|
34
|
+
* Constructs a new key-value validator.
|
|
35
|
+
* @param key - Required key {@link Validator | validator}, {@link Converter | converter},
|
|
36
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
37
|
+
* @param value - Required value {@link Validator | validator}, {@link Converter | converter},
|
|
38
|
+
* or {@link Conversion.ConverterFunc | converter function}.
|
|
39
|
+
*/
|
|
40
|
+
constructor({ key, value }) {
|
|
41
|
+
this.key = typeof key === 'function' ? conversion_1.Converters.generic(key) : key;
|
|
42
|
+
this.value = typeof value === 'function' ? conversion_1.Converters.generic(value) : value;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Converts a supplied unknown to a valid key value of type `<TK>`.
|
|
46
|
+
* @param key - The unknown to be converted.
|
|
47
|
+
* @returns `Success` with the converted key value and 'success' detail if the key is valid,
|
|
48
|
+
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
49
|
+
*/
|
|
50
|
+
convertKey(key) {
|
|
51
|
+
return this.key.convert(key).withFailureDetail('invalid-key');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Converts a supplied unknown to a valid value of type `<TV>`.
|
|
55
|
+
* @param key - The unknown to be converted.
|
|
56
|
+
* @returns `Success` with the converted value and 'success' detail if the value is valid,
|
|
57
|
+
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
58
|
+
*/
|
|
59
|
+
convertValue(key) {
|
|
60
|
+
return this.value.convert(key).withFailureDetail('invalid-value');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Converts a supplied unknown to a valid entry of type `[<TK>, <TV>]`.
|
|
64
|
+
* @param entry - The unknown to be converted.
|
|
65
|
+
* @returns `Success` with the converted entry and 'success' detail if the entry
|
|
66
|
+
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
67
|
+
* the entry is invalid
|
|
68
|
+
*/
|
|
69
|
+
convertEntry(entry) {
|
|
70
|
+
if (Array.isArray(entry) && entry.length === 2) {
|
|
71
|
+
const errors = new base_1.MessageAggregator();
|
|
72
|
+
const key = this.convertKey(entry[0]).aggregateError(errors).orDefault();
|
|
73
|
+
const value = this.convertValue(entry[1]).aggregateError(errors).orDefault();
|
|
74
|
+
if (key && value) {
|
|
75
|
+
return (0, base_1.succeedWithDetail)([key, value], 'success');
|
|
76
|
+
}
|
|
77
|
+
const detail = key ? 'invalid-value' : 'invalid-key';
|
|
78
|
+
return (0, base_1.failWithDetail)(`invalid entry: ${errors.toString()}`, detail);
|
|
79
|
+
}
|
|
80
|
+
/* c8 ignore next 2 */
|
|
81
|
+
return (0, base_1.failWithDetail)(`malformed entry: "${JSON.stringify(entry)}"`, 'invalid-value');
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Converts a supplied iterable of unknowns to valid key-value pairs.
|
|
85
|
+
* @param entries - The iterable of unknowns to be converted.
|
|
86
|
+
* @returns `Success` with an array of converted key-value pairs if all entries are valid,
|
|
87
|
+
* or `Failure` with an error message if any entry is invalid.
|
|
88
|
+
*/
|
|
89
|
+
convertEntries(entries) {
|
|
90
|
+
const errors = new base_1.MessageAggregator();
|
|
91
|
+
const converted = [];
|
|
92
|
+
for (const element of entries) {
|
|
93
|
+
this.convertEntry(element)
|
|
94
|
+
.onSuccess((e) => {
|
|
95
|
+
converted.push(e);
|
|
96
|
+
return (0, base_1.succeedWithDetail)(e, 'success');
|
|
97
|
+
})
|
|
98
|
+
.aggregateError(errors);
|
|
99
|
+
}
|
|
100
|
+
return errors.returnOrReport((0, base_1.succeed)(converted));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.KeyValueConverters = KeyValueConverters;
|
|
104
|
+
//# sourceMappingURL=keyValueConverters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyValueConverters.js","sourceRoot":"","sources":["../../../src/packlets/collections/keyValueConverters.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAOiB;AACjB,8CAAqE;AAuBrE;;;;GAIG;AACH,MAAa,kBAAkB;IAU7B;;;;;;OAMG;IACH,YAAmB,EAAE,GAAG,EAAE,KAAK,EAA+C;QAC5E,IAAI,CAAC,GAAG,GAAG,OAAO,GAAG,KAAK,UAAU,CAAC,CAAC,CAAC,uBAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QACrE,IAAI,CAAC,KAAK,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,uBAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAC/E,CAAC;IAED;;;;;OAKG;IACI,UAAU,CAAC,GAAY;QAC5B,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC;IAChE,CAAC;IAED;;;;;OAKG;IACI,YAAY,CAAC,GAAY;QAC9B,OAAO,IAAI,CAAC,KA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AS,CAAC,CAAC,CAAC,EAAE,EAAE;gBACf,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBAClB,OAAO,IAAA,wBAAiB,EAAC,CAAC,EAAE,SAAS,CAAC,CAAC;YACzC,CAAC,CAAC;iBACD,cAAc,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO,MAAM,CAAC,cAAc,CAAC,IAAA,cAAO,EAAC,SAAS,CAAC,CAAC,CAAC;IACnD,CAAC;CACF;AAnFD,gDAmFC","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 {\n DetailedResult,\n failWithDetail,\n MessageAggregator,\n Result,\n succeed,\n succeedWithDetail\n} from '../base';\nimport { Converter, ConverterFunc, Converters } from '../conversion';\nimport { Validator } from '../validation';\nimport { KeyValueEntry } from './common';\nimport { ResultMapResultDetail } from './readonlyResultMap';\n\n/**\n * Parameters for constructing a {@link Collections.KeyValueConverters | KeyValueConverters} instance.\n * @public\n */\nexport interface IKeyValueConverterConstructorParams<TK extends string = string, TV = unknown> {\n /**\n * Required key {@link Validator | validator}, {@link Converter | converter},\n * or {@link Conversion.ConverterFunc | converter function}.\n */\n key: Validator<TK, unknown> | Converter<TK, unknown> | ConverterFunc<TK, unknown>;\n\n /**\n * Required value {@link Validator | validator}, {@link Converter | converter},\n * or {@link Conversion.ConverterFunc | converter function}.\n */\n value: Validator<TV, unknown> | Converter<TV, unknown> | ConverterFunc<TV, unknown>;\n}\n\n/**\n * Helper class for converting strongly-typed keys, values, or entries\n * from unknown values.\n * @public\n */\nexport class KeyValueConverters<TK extends string = string, TV = unknown> {\n /**\n * Required key {@link Validator | validator} or {@link Converter | converter}.\n */\n public readonly key: Validator<TK, unknown> | Converter<TK, unknown>;\n /**\n * Required value {@link Validator | validator} or {@link Converter | converter}.\n */\n public readonly value: Validator<TV, unknown> | Converter<TV, unknown>;\n\n /**\n * Constructs a new key-value validator.\n * @param key - Required key {@link Validator | validator}, {@link Converter | converter},\n * or {@link Conversion.ConverterFunc | converter function}.\n * @param value - Required value {@link Validator | validator}, {@link Converter | converter},\n * or {@link Conversion.ConverterFunc | converter function}.\n */\n public constructor({ key, value }: IKeyValueConverterConstructorParams<TK, TV>) {\n this.key = typeof key === 'function' ? Converters.generic(key) : key;\n this.value = typeof value === 'function' ? Converters.generic(value) : value;\n }\n\n /**\n * Converts a supplied unknown to a valid key value of type `<TK>`.\n * @param key - The unknown to be converted.\n * @returns `Success` with the converted key value and 'success' detail if the key is valid,\n * or `Failure` with an error message and 'invalid-key' detail if the key is invalid.\n */\n public convertKey(key: unknown): DetailedResult<TK, ResultMapResultDetail> {\n return this.key.convert(key).withFailureDetail('invalid-key');\n }\n\n /**\n * Converts a supplied unknown to a valid value of type `<TV>`.\n * @param key - The unknown to be converted.\n * @returns `Success` with the converted value and 'success' detail if the value is valid,\n * or `Failure` with an error message and 'invalid-value' detail if the value is invalid.\n */\n public convertValue(key: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.value.convert(key).withFailureDetail('invalid-value');\n }\n\n /**\n * Converts a supplied unknown to a valid entry of type `[<TK>, <TV>]`.\n * @param entry - The unknown to be converted.\n * @returns `Success` with the converted entry and 'success' detail if the entry\n * is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if\n * the entry is invalid\n */\n public convertEntry(entry: unknown): DetailedResult<KeyValueEntry<TK, TV>, ResultMapResultDetail> {\n if (Array.isArray(entry) && entry.length === 2) {\n const errors = new MessageAggregator();\n const key = this.convertKey(entry[0]).aggregateError(errors).orDefault();\n const value = this.convertValue(entry[1]).aggregateError(errors).orDefault();\n if (key && value) {\n return succeedWithDetail([key, value], 'success');\n }\n const detail = key ? 'invalid-value' : 'invalid-key';\n return failWithDetail(`invalid entry: ${errors.toString()}`, detail);\n }\n /* c8 ignore next 2 */\n return failWithDetail(`malformed entry: \"${JSON.stringify(entry)}\"`, 'invalid-value');\n }\n\n /**\n * Converts a supplied iterable of unknowns to valid key-value pairs.\n * @param entries - The iterable of unknowns to be converted.\n * @returns `Success` with an array of converted key-value pairs if all entries are valid,\n * or `Failure` with an error message if any entry is invalid.\n */\n public convertEntries(entries: Iterable<unknown>): Result<KeyValueEntry<TK, TV>[]> {\n const errors = new MessageAggregator();\n const converted: KeyValueEntry<TK, TV>[] = [];\n for (const element of entries) {\n this.convertEntry(element)\n .onSuccess((e) => {\n converted.push(e);\n return succeedWithDetail(e, 'success');\n })\n .aggregateError(errors);\n }\n return errors.returnOrReport(succeed(converted));\n }\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import { KeyValueEntry } from './common';
|
|
|
4
4
|
* Additional success or failure details for {@link Collections.ResultMap | ResultMap} calls.
|
|
5
5
|
* @public
|
|
6
6
|
*/
|
|
7
|
-
export type ResultMapResultDetail = 'added' | 'deleted' | 'exists' | 'invalid-key' | 'invalid-value' | 'not-found' | 'success' | 'updated';
|
|
7
|
+
export type ResultMapResultDetail = 'added' | 'deleted' | 'exists' | 'failure' | 'invalid-key' | 'invalid-value' | 'not-found' | 'success' | 'updated';
|
|
8
8
|
/**
|
|
9
9
|
* Callback for {@link Collections.ResultMap | ResultMap} `forEach` method.
|
|
10
10
|
* @public
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readonlyResultMap.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/readonlyResultMap.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAC7B,OAAO,GACP,SAAS,GACT,QAAQ,GACR,aAAa,GACb,eAAe,GACf,WAAW,GACX,SAAS,GACT,SAAS,CAAC;AAEd;;;GAGG;AACH,MAAM,MAAM,kBAAkB,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO,IAAI,CACzE,KAAK,EAAE,EAAE,EACT,GAAG,EAAE,EAAE,EACP,GAAG,EAAE,kBAAkB,CAAC,EAAE,EAAE,EAAE,CAAC,EAC/B,OAAO,CAAC,EAAE,OAAO,KACd,IAAI,CAAC;AAEV;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IAC1E;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,OAAO,IAAI,WAAW,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IAE9C;;OAEG;IACH,OAAO,CAAC,EAAE,EAAE,kBAAkB,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAErD;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,EAAE,GAAG,cA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IAAI,WAAW,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IAE9C;;OAEG;IACH,OAAO,CAAC,EAAE,EAAE,kBAAkB,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAErD;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,EAAE,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC,CAAC;IAExD;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAEtB;;OAEG;IACH,IAAI,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC;IAExB;;OAEG;IACH,MAAM,IAAI,WAAW,CAAC,EAAE,CAAC,CAAC;IAE1B;;;OAGG;IACH,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;CAC9D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readonlyResultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/readonlyResultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG","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 { KeyValueEntry } from './common';\n\n/**\n * Additional success or failure details for {@link Collections.ResultMap | ResultMap} calls.\n * @public\n */\nexport type ResultMapResultDetail =\n | 'added'\n | 'deleted'\n | 'exists'\n | 'invalid-key'\n | 'invalid-value'\n | 'not-found'\n | 'success'\n | 'updated';\n\n/**\n * Callback for {@link Collections.ResultMap | ResultMap} `forEach` method.\n * @public\n */\nexport type ResultMapForEachCb<TK extends string = string, TE = unknown> = (\n value: TE,\n key: TK,\n map: IReadOnlyResultMap<TK, TE>,\n thisArg?: unknown\n) => void;\n\n/**\n * A readonly `ReadonlyMap<TK, TV>`-like object which reports success or failure\n * 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 interface IReadOnlyResultMap<TK extends string = string, TV = unknown> {\n /**\n * {@inheritdoc Collections.ResultMap.inner}\n */\n readonly inner: ReadonlyMap<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMap.size}\n */\n readonly size: number;\n\n /**\n * {@inheritdoc Collections.ResultMap.entries}\n */\n entries(): MapIterator<KeyValueEntry<TK, TV>>;\n\n /**\n * {@inheritdoc Collections.ResultMap.forEach}\n */\n forEach(cb: ResultMapForEachCb, arg?: unknown): void;\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n get(key: TK): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n has(key: TK): boolean;\n\n /**\n * {@inheritdoc Collections.ResultMap.keys}\n */\n keys(): MapIterator<TK>;\n\n /**\n * {@inheritdoc Collections.ResultMap.values}\n */\n values(): MapIterator<TV>;\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n [Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"readonlyResultMap.js","sourceRoot":"","sources":["../../../src/packlets/collections/readonlyResultMap.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG","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 { KeyValueEntry } from './common';\n\n/**\n * Additional success or failure details for {@link Collections.ResultMap | ResultMap} calls.\n * @public\n */\nexport type ResultMapResultDetail =\n | 'added'\n | 'deleted'\n | 'exists'\n | 'failure'\n | 'invalid-key'\n | 'invalid-value'\n | 'not-found'\n | 'success'\n | 'updated';\n\n/**\n * Callback for {@link Collections.ResultMap | ResultMap} `forEach` method.\n * @public\n */\nexport type ResultMapForEachCb<TK extends string = string, TE = unknown> = (\n value: TE,\n key: TK,\n map: IReadOnlyResultMap<TK, TE>,\n thisArg?: unknown\n) => void;\n\n/**\n * A readonly `ReadonlyMap<TK, TV>`-like object which reports success or failure\n * 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 interface IReadOnlyResultMap<TK extends string = string, TV = unknown> {\n /**\n * {@inheritdoc Collections.ResultMap.inner}\n */\n readonly inner: ReadonlyMap<TK, TV>;\n\n /**\n * {@inheritdoc Collections.ResultMap.size}\n */\n readonly size: number;\n\n /**\n * {@inheritdoc Collections.ResultMap.entries}\n */\n entries(): MapIterator<KeyValueEntry<TK, TV>>;\n\n /**\n * {@inheritdoc Collections.ResultMap.forEach}\n */\n forEach(cb: ResultMapForEachCb, arg?: unknown): void;\n\n /**\n * {@inheritdoc Collections.ResultMap.get}\n */\n get(key: TK): DetailedResult<TV, ResultMapResultDetail>;\n\n /**\n * {@inheritdoc Collections.ResultMap.has}\n */\n has(key: TK): boolean;\n\n /**\n * {@inheritdoc Collections.ResultMap.keys}\n */\n keys(): MapIterator<TK>;\n\n /**\n * {@inheritdoc Collections.ResultMap.values}\n */\n values(): MapIterator<TV>;\n\n /**\n * Gets an iterator over the map entries.\n * @returns An iterator over the map entries.\n */\n [Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>>;\n}\n"]}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { DetailedResult } from '../base';
|
|
2
2
|
import { IReadOnlyResultMap, ResultMapResultDetail } from './readonlyResultMap';
|
|
3
3
|
import { ResultMap, ResultMapValueFactory } from './resultMap';
|
|
4
|
-
import {
|
|
4
|
+
import { KeyValueConverters } from './keyValueConverters';
|
|
5
5
|
/**
|
|
6
|
-
* A read-only interface exposing non-mutating methods of a {@link Collections.
|
|
6
|
+
* A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapConverter | ResultMapConverter}.
|
|
7
7
|
* @public
|
|
8
8
|
*/
|
|
9
|
-
export interface
|
|
9
|
+
export interface IReadOnlyResultMapConverter<TK extends string = string, TV = unknown> {
|
|
10
10
|
/**
|
|
11
|
-
* {@inheritdoc Collections.
|
|
12
|
-
*/
|
|
13
|
-
readonly validators: KeyValueValidators<TK, TV>;
|
|
14
|
-
/**
|
|
15
|
-
* {@inheritdoc Collections.ResultMapValidator.map}
|
|
11
|
+
* {@inheritdoc Collections.ResultMapConverter.map}
|
|
16
12
|
*/
|
|
17
13
|
readonly map: IReadOnlyResultMap<TK, TV>;
|
|
18
14
|
/**
|
|
@@ -25,27 +21,27 @@ export interface IReadOnlyResultMapValidator<TK extends string = string, TV = un
|
|
|
25
21
|
has(key: string): boolean;
|
|
26
22
|
}
|
|
27
23
|
/**
|
|
28
|
-
* Parameters for constructing a {@link Collections.
|
|
24
|
+
* Parameters for constructing a {@link Collections.ResultMapConverter | ResultMapConverter}.
|
|
29
25
|
* @public
|
|
30
26
|
*/
|
|
31
|
-
export interface
|
|
27
|
+
export interface IResultMapConverterCreateParams<TK extends string = string, TV = unknown> {
|
|
32
28
|
map: ResultMap<TK, TV>;
|
|
33
|
-
|
|
29
|
+
converters: KeyValueConverters<TK, TV>;
|
|
34
30
|
}
|
|
35
31
|
/**
|
|
36
32
|
* A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys
|
|
37
33
|
* before calling the wrapped result map.
|
|
38
34
|
* @public
|
|
39
35
|
*/
|
|
40
|
-
export declare class
|
|
41
|
-
readonly
|
|
36
|
+
export declare class ResultMapConverter<TK extends string = string, TV = unknown> implements IReadOnlyResultMapConverter<TK, TV> {
|
|
37
|
+
readonly converters: KeyValueConverters<TK, TV>;
|
|
42
38
|
get map(): IReadOnlyResultMap<TK, TV>;
|
|
43
39
|
protected _map: ResultMap<TK, TV>;
|
|
44
40
|
/**
|
|
45
|
-
* Constructs a new {@link Collections.
|
|
46
|
-
* @param params - Required parameters for constructing the map.
|
|
41
|
+
* Constructs a new {@link Collections.ResultMapConverter | ResultMapConverter}.
|
|
42
|
+
* @param params - Required parameters for constructing the result map converter.
|
|
47
43
|
*/
|
|
48
|
-
constructor(params:
|
|
44
|
+
constructor(params: IResultMapConverterCreateParams<TK, TV>);
|
|
49
45
|
/**
|
|
50
46
|
* {@inheritdoc Collections.ResultMap.add}
|
|
51
47
|
*/
|
|
@@ -81,7 +77,7 @@ export declare class ResultMapValidator<TK extends string = string, TV = unknown
|
|
|
81
77
|
/**
|
|
82
78
|
* Gets a read-only version of this validator.
|
|
83
79
|
*/
|
|
84
|
-
toReadOnly():
|
|
80
|
+
toReadOnly(): IReadOnlyResultMapConverter<TK, TV>;
|
|
85
81
|
/**
|
|
86
82
|
* Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
|
|
87
83
|
* @param value - The value to check.
|
|
@@ -91,4 +87,4 @@ export declare class ResultMapValidator<TK extends string = string, TV = unknown
|
|
|
91
87
|
*/
|
|
92
88
|
protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
|
|
93
89
|
}
|
|
94
|
-
//# sourceMappingURL=
|
|
90
|
+
//# sourceMappingURL=resultMapConverter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resultMapConverter.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/resultMapConverter.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAqC,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAChF,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;GAGG;AACH,MAAM,WAAW,2BAA2B,CAAC,EAAE,SAAS,MAAM,GAAG,MAAM,EAAE,EAAE,GAAG,OAAO;IACnF;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,k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AAE,KAAK,EAAE,OAAO,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAEvF;;OAEG;IACI,QAAQ,CACb,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC,GACrC,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAwB5C;;OAEG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIhC;;OAEG;IACI,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMlF;;OAEG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,cAAc,CAAC,EAAE,EAAE,qBAAqB,CAAC;IAMrF;;OAEG;IACI,UAAU,IAAI,2BAA2B,CAAC,EAAE,EAAE,EAAE,CAAC;IAIxD;;;;;;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"}
|
|
@@ -21,30 +21,30 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.
|
|
24
|
+
exports.ResultMapConverter = void 0;
|
|
25
25
|
const base_1 = require("../base");
|
|
26
26
|
/**
|
|
27
27
|
* A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys
|
|
28
28
|
* before calling the wrapped result map.
|
|
29
29
|
* @public
|
|
30
30
|
*/
|
|
31
|
-
class
|
|
31
|
+
class ResultMapConverter {
|
|
32
32
|
get map() {
|
|
33
33
|
return this._map;
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
-
* Constructs a new {@link Collections.
|
|
37
|
-
* @param params - Required parameters for constructing the map.
|
|
36
|
+
* Constructs a new {@link Collections.ResultMapConverter | ResultMapConverter}.
|
|
37
|
+
* @param params - Required parameters for constructing the result map converter.
|
|
38
38
|
*/
|
|
39
39
|
constructor(params) {
|
|
40
40
|
this._map = params.map;
|
|
41
|
-
this.
|
|
41
|
+
this.converters = params.converters;
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* {@inheritdoc Collections.ResultMap.add}
|
|
45
45
|
*/
|
|
46
46
|
add(key, value) {
|
|
47
|
-
return this.
|
|
47
|
+
return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
|
|
48
48
|
return this._map.add(vk, vv);
|
|
49
49
|
});
|
|
50
50
|
}
|
|
@@ -52,7 +52,7 @@ class ResultMapValidator {
|
|
|
52
52
|
* {@inheritdoc Collections.ResultMap.delete}
|
|
53
53
|
*/
|
|
54
54
|
delete(key) {
|
|
55
|
-
return this.
|
|
55
|
+
return this.converters.convertKey(key).onSuccess((k) => {
|
|
56
56
|
return this._map.delete(k);
|
|
57
57
|
});
|
|
58
58
|
}
|
|
@@ -60,21 +60,21 @@ class ResultMapValidator {
|
|
|
60
60
|
* {@inheritdoc Collections.ResultMap.get}
|
|
61
61
|
*/
|
|
62
62
|
get(key) {
|
|
63
|
-
return this.
|
|
63
|
+
return this.converters.convertKey(key).onSuccess((k) => {
|
|
64
64
|
return this._map.get(k);
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
getOrAdd(key, valueOrFactory) {
|
|
68
68
|
if (!this._isResultMapValueFactory(valueOrFactory)) {
|
|
69
|
-
return this.
|
|
69
|
+
return this.converters.convertEntry([key, valueOrFactory]).onSuccess(([vk, vv]) => {
|
|
70
70
|
return this._map.getOrAdd(vk, vv);
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
return this.
|
|
74
|
+
return this.converters.convertKey(key).onSuccess((k) => {
|
|
75
75
|
return this._map.get(k).onFailure(() => {
|
|
76
76
|
const value = valueOrFactory(k)
|
|
77
|
-
.onSuccess((value) => this.
|
|
77
|
+
.onSuccess((value) => this.converters.convertEntry([k, value]))
|
|
78
78
|
.onSuccess(([__key, value]) => (0, base_1.succeedWithDetail)(value, 'added'));
|
|
79
79
|
return value.success
|
|
80
80
|
? this._map.add(k, value.value)
|
|
@@ -93,7 +93,7 @@ class ResultMapValidator {
|
|
|
93
93
|
* {@inheritdoc Collections.ResultMap.set}
|
|
94
94
|
*/
|
|
95
95
|
set(key, value) {
|
|
96
|
-
return this.
|
|
96
|
+
return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
|
|
97
97
|
return this._map.set(vk, vv);
|
|
98
98
|
});
|
|
99
99
|
}
|
|
@@ -101,7 +101,7 @@ class ResultMapValidator {
|
|
|
101
101
|
* {@inheritdoc Collections.ResultMap.update}
|
|
102
102
|
*/
|
|
103
103
|
update(key, value) {
|
|
104
|
-
return this.
|
|
104
|
+
return this.converters.convertEntry([key, value]).onSuccess(([vk, vv]) => {
|
|
105
105
|
return this._map.update(vk, vv);
|
|
106
106
|
});
|
|
107
107
|
}
|
|
@@ -122,5 +122,5 @@ class ResultMapValidator {
|
|
|
122
122
|
return typeof value === 'function';
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
|
-
exports.
|
|
126
|
-
//# sourceMappingURL=
|
|
125
|
+
exports.ResultMapConverter = ResultMapConverter;
|
|
126
|
+
//# sourceMappingURL=resultMapConverter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resultMapConverter.js","sourceRoot":"","sources":["../../../src/packlets/collections/resultMapConverter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAA4E;AAmC5E;;;;GAIG;AACH,MAAa,kBAAkB;IAI7B,IAAW,GAAG;QACZ,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAID;;;OAGG;IACH,YAA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C,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,GAAW,EAAE,KAAc;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;YACvE,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 { KeyValueConverters } from './keyValueConverters';\n\n/**\n * A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapConverter | ResultMapConverter}.\n * @public\n */\nexport interface IReadOnlyResultMapConverter<TK extends string = string, TV = unknown> {\n /**\n * {@inheritdoc Collections.ResultMapConverter.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.ResultMapConverter | ResultMapConverter}.\n * @public\n */\nexport interface IResultMapConverterCreateParams<TK extends string = string, TV = unknown> {\n map: ResultMap<TK, TV>;\n converters: KeyValueConverters<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 ResultMapConverter<TK extends string = string, TV = unknown>\n implements IReadOnlyResultMapConverter<TK, TV>\n{\n public readonly converters: KeyValueConverters<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.ResultMapConverter | ResultMapConverter}.\n * @param params - Required parameters for constructing the result map converter.\n */\n public constructor(params: IResultMapConverterCreateParams<TK, TV>) {\n this._map = params.map;\n this.converters = params.converters;\n }\n\n /**\n * {@inheritdoc Collections.ResultMap.add}\n */\n public add(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail> {\n return this.converters.convertEntry([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.converters.convertKey(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.converters.convertKey(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.converters.convertEntry([key, valueOrFactory]).onSuccess(([vk, vv]) => {\n return this._map.getOrAdd(vk, vv);\n });\n } else {\n return this.converters.convertKey(key).onSuccess((k) => {\n return this._map.get(k).onFailure(() => {\n const value = valueOrFactory(k)\n .onSuccess((value) => this.converters.convertEntry([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.converters.convertEntry([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.converters.convertEntry([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(): IReadOnlyResultMapConverter<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,8 +1,3 @@
|
|
|
1
|
-
import { DetailedResult, Result } from '../base';
|
|
2
|
-
import { Converter } from '../conversion';
|
|
3
|
-
import { Validator } from '../validation';
|
|
4
|
-
import { KeyValueEntry } from './common';
|
|
5
|
-
import { ResultMapResultDetail } from './readonlyResultMap';
|
|
6
1
|
/**
|
|
7
2
|
* Determines if a supplied value is an iterable object or some other type.
|
|
8
3
|
* @param value - The value to be tested.
|
|
@@ -10,60 +5,4 @@ import { ResultMapResultDetail } from './readonlyResultMap';
|
|
|
10
5
|
* @public
|
|
11
6
|
*/
|
|
12
7
|
export declare function isIterable<TE = unknown, TI extends Iterable<TE> = Iterable<TE>, TO = unknown>(value: TI | TO): value is TI;
|
|
13
|
-
/**
|
|
14
|
-
* Helper class for validating key-value pairs.
|
|
15
|
-
* @public
|
|
16
|
-
*/
|
|
17
|
-
export declare class KeyValueValidators<TK extends string = string, TV = unknown> {
|
|
18
|
-
/**
|
|
19
|
-
* Required key validator or converter.
|
|
20
|
-
*/
|
|
21
|
-
readonly key: Validator<TK, unknown> | Converter<TK, unknown>;
|
|
22
|
-
/**
|
|
23
|
-
* Required value validator or converter.
|
|
24
|
-
*/
|
|
25
|
-
readonly value: Validator<TV, unknown> | Converter<TV, unknown>;
|
|
26
|
-
/**
|
|
27
|
-
* Optional entry validator or converter. If no entry validator is provided,
|
|
28
|
-
* an entry is considered valid if both key and value are valid.
|
|
29
|
-
*/
|
|
30
|
-
readonly entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>;
|
|
31
|
-
/**
|
|
32
|
-
* Constructs a new key-value validator.
|
|
33
|
-
* @param key - Required key validator or converter.
|
|
34
|
-
* @param value - Required value validator or converter.
|
|
35
|
-
* @param entry - Optional entry validator or converter. If no entry validator is provided,
|
|
36
|
-
* an entry is considered valid if both key and value are valid.
|
|
37
|
-
*/
|
|
38
|
-
constructor(key: Validator<TK, unknown> | Converter<TK, unknown>, value: Validator<TV, unknown> | Converter<TV, unknown>, entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>);
|
|
39
|
-
/**
|
|
40
|
-
* Validates a supplied unknown as a valid key value.
|
|
41
|
-
* @param key - The unknown to be validated.
|
|
42
|
-
* @returns `Success` with the validated key value and 'success' detail if the key is valid,
|
|
43
|
-
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
44
|
-
*/
|
|
45
|
-
validateKey(key: unknown): DetailedResult<TK, ResultMapResultDetail>;
|
|
46
|
-
/**
|
|
47
|
-
* Validates a supplied unknown as a valid value.
|
|
48
|
-
* @param key - The unknown to be validated.
|
|
49
|
-
* @returns `Success` with the validated value and 'success' detail if the value is valid,
|
|
50
|
-
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
51
|
-
*/
|
|
52
|
-
validateValue(key: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
53
|
-
/**
|
|
54
|
-
* Validates a supplied unknown as a valid key-value pair.
|
|
55
|
-
* @param entry - The unknown to be validated.
|
|
56
|
-
* @returns `Success` with the validated key-value pair and 'success' detail if the entry
|
|
57
|
-
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
58
|
-
* the entry is invalid
|
|
59
|
-
*/
|
|
60
|
-
validateEntry(entry: unknown): DetailedResult<KeyValueEntry<TK, TV>, ResultMapResultDetail>;
|
|
61
|
-
/**
|
|
62
|
-
* Validates a supplied iterable of unknowns as valid key-value pairs.
|
|
63
|
-
* @param entries - The iterable of unknowns to be validated.
|
|
64
|
-
* @returns `Success` with an array of validated key-value pairs if all entries are valid,
|
|
65
|
-
* or `Failure` with an error message if any entry is invalid.
|
|
66
|
-
*/
|
|
67
|
-
validateEntries(entries: Iterable<unknown>): Result<KeyValueEntry<TK, TV>[]>;
|
|
68
|
-
}
|
|
69
8
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/utils.ts"],"names":[],"mappings":"AAsBA
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/packlets/collections/utils.ts"],"names":[],"mappings":"AAsBA;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,EAAE,GAAG,OAAO,EAAE,EAAE,SAAS,QAAQ,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,OAAO,EAC3F,KAAK,EAAE,EAAE,GAAG,EAAE,GACb,KAAK,IAAI,EAAE,CAEb"}
|
|
@@ -21,9 +21,7 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.KeyValueValidators = void 0;
|
|
25
24
|
exports.isIterable = isIterable;
|
|
26
|
-
const base_1 = require("../base");
|
|
27
25
|
/**
|
|
28
26
|
* Determines if a supplied value is an iterable object or some other type.
|
|
29
27
|
* @param value - The value to be tested.
|
|
@@ -33,84 +31,4 @@ const base_1 = require("../base");
|
|
|
33
31
|
function isIterable(value) {
|
|
34
32
|
return (value && typeof value === 'object' && Symbol.iterator in value) === true;
|
|
35
33
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Helper class for validating key-value pairs.
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
class KeyValueValidators {
|
|
41
|
-
/**
|
|
42
|
-
* Constructs a new key-value validator.
|
|
43
|
-
* @param key - Required key validator or converter.
|
|
44
|
-
* @param value - Required value validator or converter.
|
|
45
|
-
* @param entry - Optional entry validator or converter. If no entry validator is provided,
|
|
46
|
-
* an entry is considered valid if both key and value are valid.
|
|
47
|
-
*/
|
|
48
|
-
constructor(key, value, entry) {
|
|
49
|
-
this.key = key;
|
|
50
|
-
this.value = value;
|
|
51
|
-
this.entry = entry;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Validates a supplied unknown as a valid key value.
|
|
55
|
-
* @param key - The unknown to be validated.
|
|
56
|
-
* @returns `Success` with the validated key value and 'success' detail if the key is valid,
|
|
57
|
-
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
58
|
-
*/
|
|
59
|
-
validateKey(key) {
|
|
60
|
-
return this.key.convert(key).withFailureDetail('invalid-key');
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Validates a supplied unknown as a valid value.
|
|
64
|
-
* @param key - The unknown to be validated.
|
|
65
|
-
* @returns `Success` with the validated value and 'success' detail if the value is valid,
|
|
66
|
-
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
67
|
-
*/
|
|
68
|
-
validateValue(key) {
|
|
69
|
-
return this.value.convert(key).withFailureDetail('invalid-value');
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Validates a supplied unknown as a valid key-value pair.
|
|
73
|
-
* @param entry - The unknown to be validated.
|
|
74
|
-
* @returns `Success` with the validated key-value pair and 'success' detail if the entry
|
|
75
|
-
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
76
|
-
* the entry is invalid
|
|
77
|
-
*/
|
|
78
|
-
validateEntry(entry) {
|
|
79
|
-
if (this.entry) {
|
|
80
|
-
return this.entry.convert(entry).withFailureDetail('invalid-value');
|
|
81
|
-
}
|
|
82
|
-
if (Array.isArray(entry) && entry.length === 2) {
|
|
83
|
-
const errors = new base_1.MessageAggregator();
|
|
84
|
-
const key = this.validateKey(entry[0]).aggregateError(errors).orDefault();
|
|
85
|
-
const value = this.validateValue(entry[1]).aggregateError(errors).orDefault();
|
|
86
|
-
if (key && value) {
|
|
87
|
-
return (0, base_1.succeedWithDetail)([key, value], 'success');
|
|
88
|
-
}
|
|
89
|
-
const detail = key ? 'invalid-value' : 'invalid-key';
|
|
90
|
-
return (0, base_1.failWithDetail)(`invalid entry: ${errors.toString()}`, detail);
|
|
91
|
-
}
|
|
92
|
-
/* c8 ignore next 2 */
|
|
93
|
-
return (0, base_1.failWithDetail)(`malformed entry: "${JSON.stringify(entry)}"`, 'invalid-value');
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Validates a supplied iterable of unknowns as valid key-value pairs.
|
|
97
|
-
* @param entries - The iterable of unknowns to be validated.
|
|
98
|
-
* @returns `Success` with an array of validated key-value pairs if all entries are valid,
|
|
99
|
-
* or `Failure` with an error message if any entry is invalid.
|
|
100
|
-
*/
|
|
101
|
-
validateEntries(entries) {
|
|
102
|
-
const errors = new base_1.MessageAggregator();
|
|
103
|
-
const validated = [];
|
|
104
|
-
for (const element of entries) {
|
|
105
|
-
this.validateEntry(element)
|
|
106
|
-
.onSuccess((e) => {
|
|
107
|
-
validated.push(e);
|
|
108
|
-
return (0, base_1.succeedWithDetail)(e, 'success');
|
|
109
|
-
})
|
|
110
|
-
.aggregateError(errors);
|
|
111
|
-
}
|
|
112
|
-
return errors.returnOrReport((0, base_1.succeed)(validated));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.KeyValueValidators = KeyValueValidators;
|
|
116
34
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/packlets/collections/utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/packlets/collections/utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAQH,gCAIC;AAVD;;;;;GAKG;AACH,SAAgB,UAAU,CACxB,KAAc;IAEd,OAAO,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC;AACnF,CAAC","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\n/**\n * Determines if a supplied value is an iterable object or some other type.\n * @param value - The value to be tested.\n * @returns `true` if the value is an iterable object, `false` otherwise.\n * @public\n */\nexport function isIterable<TE = unknown, TI extends Iterable<TE> = Iterable<TE>, TO = unknown>(\n value: TI | TO\n): value is TI {\n return (value && typeof value === 'object' && Symbol.iterator in value) === true;\n}\n"]}
|
|
@@ -17,6 +17,11 @@ export type Infer<TCONV> = TCONV extends Converter<infer TTO, unknown> ? InnerIn
|
|
|
17
17
|
* @internal
|
|
18
18
|
*/
|
|
19
19
|
export type ConvertedToType<TCONV> = Infer<TCONV>;
|
|
20
|
+
/**
|
|
21
|
+
* Function signature for a converter function.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export type ConverterFunc<T, TC> = (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>;
|
|
20
25
|
/**
|
|
21
26
|
* Base templated wrapper to simplify creation of new {@link Converter}s.
|
|
22
27
|
* @public
|
|
@@ -42,7 +47,7 @@ export declare class BaseConverter<T, TC = unknown> implements Converter<T, TC>
|
|
|
42
47
|
* @param traits - Optional {@link Conversion.ConverterTraits | traits} to be assigned to the resulting
|
|
43
48
|
* converter.
|
|
44
49
|
*/
|
|
45
|
-
constructor(converter:
|
|
50
|
+
constructor(converter: ConverterFunc<T, TC>, defaultContext?: TC, traits?: ConverterTraits);
|
|
46
51
|
/**
|
|
47
52
|
* {@inheritdoc Converter.isOptional}
|
|
48
53
|
*/
|
|
@@ -66,7 +71,7 @@ export declare class BaseConverter<T, TC = unknown> implements Converter<T, TC>
|
|
|
66
71
|
/**
|
|
67
72
|
* {@inheritdoc Converter.map}
|
|
68
73
|
*/
|
|
69
|
-
map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
|
|
74
|
+
map<T2>(mapper: (from: T, context?: TC) => Result<T2>): Converter<T2, TC>;
|
|
70
75
|
/**
|
|
71
76
|
* {@inheritdoc Converter.mapConvert}
|
|
72
77
|
*/
|
|
@@ -74,7 +79,7 @@ export declare class BaseConverter<T, TC = unknown> implements Converter<T, TC>
|
|
|
74
79
|
/**
|
|
75
80
|
* {@inheritdoc Converter.mapItems}
|
|
76
81
|
*/
|
|
77
|
-
mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
|
|
82
|
+
mapItems<TI>(mapper: (from: unknown, context?: TC) => Result<TI>): Converter<TI[], TC>;
|
|
78
83
|
/**
|
|
79
84
|
* {@inheritdoc Converter.mapConvertItems}
|
|
80
85
|
*/
|
|
@@ -82,19 +87,19 @@ export declare class BaseConverter<T, TC = unknown> implements Converter<T, TC>
|
|
|
82
87
|
/**
|
|
83
88
|
* {@inheritdoc Converter.withAction}
|
|
84
89
|
*/
|
|
85
|
-
withAction<TI>(action: (result: Result<T
|
|
90
|
+
withAction<TI>(action: (result: Result<T>, context?: TC) => Result<TI>): Converter<TI, TC>;
|
|
86
91
|
/**
|
|
87
92
|
* {@inheritdoc Converter.withTypeGuard}
|
|
88
93
|
*/
|
|
89
|
-
withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI, TC>;
|
|
94
|
+
withTypeGuard<TI>(guard: (from: unknown, context?: TC) => from is TI, message?: string): Converter<TI, TC>;
|
|
90
95
|
/**
|
|
91
96
|
* {@inheritdoc Converter.withItemTypeGuard}
|
|
92
97
|
*/
|
|
93
|
-
withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI[], TC>;
|
|
98
|
+
withItemTypeGuard<TI>(guard: (from: unknown, context?: TC) => from is TI, message?: string): Converter<TI[], TC>;
|
|
94
99
|
/**
|
|
95
100
|
* {@inheritdoc Converter.withConstraint}
|
|
96
101
|
*/
|
|
97
|
-
withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
|
|
102
|
+
withConstraint(constraint: (val: T, context?: TC) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
|
|
98
103
|
/**
|
|
99
104
|
* {@inheritdoc Converter.withBrand}
|
|
100
105
|
*/
|