@fgv/ts-utils 5.0.0-12 → 5.0.0-14
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/.vscode/launch.json +16 -0
- package/.vscode/settings.json +36 -0
- package/config/api-extractor.json +343 -0
- package/config/rig.json +16 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/test/helpers/jest/helpers/fsHelpers.d.ts +52 -0
- package/lib/test/helpers/jest/helpers/fsHelpers.js +180 -0
- package/lib/test/helpers/jest/helpers/index.d.ts +2 -0
- package/lib/test/helpers/jest/helpers/index.js +18 -0
- package/lib/test/helpers/jest/index.d.ts +3 -0
- package/lib/test/helpers/jest/index.js +27 -0
- package/lib/test/helpers/jest/matchers/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/index.js +19 -0
- package/lib/test/helpers/jest/matchers/toFail/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toFail/index.js +25 -0
- package/lib/test/helpers/jest/matchers/toFail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFail/predicate.js +9 -0
- package/lib/test/helpers/jest/matchers/toFailTest/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toFailTest/index.js +30 -0
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.d.ts +3 -0
- package/lib/test/helpers/jest/matchers/toFailTest/predicate.js +12 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +15 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/index.js +31 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailTestWith/predicate.js +30 -0
- package/lib/test/helpers/jest/matchers/toFailWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toFailWith/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailWith/predicate.js +20 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.d.ts +19 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toFailWithDetail/predicate.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceed/index.d.ts +16 -0
- package/lib/test/helpers/jest/matchers/toSucceed/index.js +25 -0
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceed/predicate.js +9 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.d.ts +17 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +26 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.d.ts +19 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +55 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +21 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedWith/predicate.js +16 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.d.ts +18 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/index.js +29 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.d.ts +4 -0
- package/lib/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +21 -0
- package/lib/test/helpers/jest/resolvers/cli.d.ts +5 -0
- package/lib/test/helpers/jest/resolvers/cli.js +48 -0
- package/lib/test/helpers/jest/resolvers/ide.d.ts +5 -0
- package/lib/test/helpers/jest/resolvers/ide.js +48 -0
- package/lib/test/helpers/jest/ts-utils.d.ts +2 -0
- package/lib/test/helpers/jest/ts-utils.js +18 -0
- package/lib/test/helpers/jest/types/index.d.ts +79 -0
- package/lib/test/helpers/jest/types/index.js +4 -0
- package/lib/test/helpers/jest/utils/matcherHelpers.d.ts +6 -0
- package/lib/test/helpers/jest/utils/matcherHelpers.js +53 -0
- package/lib/test/helpers/jest/utils/snapshotResolver.d.ts +4 -0
- package/lib/test/helpers/jest/utils/snapshotResolver.js +19 -0
- package/lib/test/unit/collections/collectible.test.d.ts +2 -0
- package/lib/test/unit/collections/collector.test.d.ts +2 -0
- package/lib/test/unit/collections/collectors.test.d.ts +2 -0
- package/lib/test/unit/collections/convertingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/helpers.d.ts +44 -0
- package/lib/test/unit/collections/helpers.js +115 -0
- package/lib/test/unit/collections/resultMap.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingConvertingCollector.test.d.ts +2 -0
- package/lib/test/unit/collections/validatingResultMap.test.d.ts +2 -0
- package/lib/test/unit/converter.test.d.ts +2 -0
- package/lib/test/unit/converters.test.d.ts +2 -0
- package/lib/test/unit/defaultingConverter.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fileItem.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fileTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/fsTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/inMemoryTree.test.d.ts +2 -0
- package/lib/test/unit/file-tree/sample/rootFile.json +1 -0
- package/lib/test/unit/file-tree/sample/some/other/path/file2.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/below/file4.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/file1.json +1 -0
- package/lib/test/unit/file-tree/sample/some/path/file3.json +1 -0
- package/lib/test/unit/hash.test.d.ts +2 -0
- package/lib/test/unit/logger.test.d.ts +2 -0
- package/lib/test/unit/mapResults.test.d.ts +3 -0
- package/lib/test/unit/messageAggregator.test.d.ts +3 -0
- package/lib/test/unit/normalize.test.d.ts +2 -0
- package/lib/test/unit/result.test.d.ts +3 -0
- package/lib/test/unit/utils.test.d.ts +3 -0
- package/lib/test/unit/validation/array.test.d.ts +2 -0
- package/lib/test/unit/validation/boolean.test.d.ts +2 -0
- package/lib/test/unit/validation/genericValidator.test.d.ts +2 -0
- package/lib/test/unit/validation/number.test.d.ts +2 -0
- package/lib/test/unit/validation/object.test.d.ts +2 -0
- package/lib/test/unit/validation/oneOf.test.d.ts +2 -0
- package/lib/test/unit/validation/recordOf.test.d.ts +2 -0
- package/lib/test/unit/validation/string.test.d.ts +2 -0
- package/lib/test/unit/validation/traits.test.d.ts +2 -0
- package/lib/test/unit/validation/typeGuard.test.d.ts +2 -0
- package/lib/test/unit/validation/validators.test.d.ts +2 -0
- package/package.json +9 -9
- package/src/index.ts +63 -0
- package/src/packlets/base/brand.ts +28 -0
- package/src/packlets/base/index.ts +31 -0
- package/src/packlets/base/logger.ts +156 -0
- package/src/packlets/base/mapResults.ts +302 -0
- package/src/packlets/base/messageAggregator.ts +120 -0
- package/src/packlets/base/normalize.ts +144 -0
- package/src/packlets/base/result.ts +961 -0
- package/src/packlets/base/utils.ts +236 -0
- package/src/packlets/collections/collectible.ts +238 -0
- package/src/packlets/collections/collector.ts +273 -0
- package/src/packlets/collections/collectorValidator.ts +178 -0
- package/src/packlets/collections/common.ts +27 -0
- package/src/packlets/collections/convertingCollector.ts +223 -0
- package/src/packlets/collections/convertingCollectorValidator.ts +164 -0
- package/src/packlets/collections/index.ts +39 -0
- package/src/packlets/collections/keyValueConverters.ts +142 -0
- package/src/packlets/collections/readonlyResultMap.ts +99 -0
- package/src/packlets/collections/resultMap.ts +322 -0
- package/src/packlets/collections/resultMapValidator.ts +188 -0
- package/src/packlets/collections/utils.ts +33 -0
- package/src/packlets/collections/validatingCollector.ts +123 -0
- package/src/packlets/collections/validatingConvertingCollector.ts +113 -0
- package/src/packlets/collections/validatingResultMap.ts +94 -0
- package/src/packlets/conversion/baseConverter.ts +330 -0
- package/src/packlets/conversion/converter.ts +260 -0
- package/src/packlets/conversion/converters.ts +1039 -0
- package/src/packlets/conversion/defaultingConverter.ts +188 -0
- package/src/packlets/conversion/index.ts +30 -0
- package/src/packlets/conversion/objectConverter.ts +259 -0
- package/src/packlets/conversion/stringConverter.ts +165 -0
- package/src/packlets/file-tree/directoryItem.ts +84 -0
- package/src/packlets/file-tree/fileItem.ts +119 -0
- package/src/packlets/file-tree/fileTree.ts +152 -0
- package/src/packlets/file-tree/fileTreeAccessors.ts +176 -0
- package/src/packlets/file-tree/fsTree.ts +118 -0
- package/src/packlets/file-tree/in-memory/inMemoryTree.ts +171 -0
- package/src/packlets/file-tree/in-memory/index.ts +23 -0
- package/src/packlets/file-tree/in-memory/treeBuilder.ts +203 -0
- package/src/packlets/file-tree/index.ts +31 -0
- package/src/packlets/hash/crcNormalizer.ts +81 -0
- package/src/packlets/hash/hashingNormalizer.ts +102 -0
- package/src/packlets/hash/index.ts +24 -0
- package/src/packlets/validation/array.ts +83 -0
- package/src/packlets/validation/boolean.ts +63 -0
- package/src/packlets/validation/classes.ts +38 -0
- package/src/packlets/validation/common.ts +38 -0
- package/src/packlets/validation/field.ts +99 -0
- package/src/packlets/validation/genericValidator.ts +205 -0
- package/src/packlets/validation/index.ts +31 -0
- package/src/packlets/validation/number.ts +66 -0
- package/src/packlets/validation/object.ts +208 -0
- package/src/packlets/validation/oneOf.ts +77 -0
- package/src/packlets/validation/string.ts +66 -0
- package/src/packlets/validation/traits.ts +113 -0
- package/src/packlets/validation/typeGuard.ts +82 -0
- package/src/packlets/validation/validator.ts +145 -0
- package/src/packlets/validation/validatorBase.ts +61 -0
- package/src/packlets/validation/validators.ts +246 -0
- package/CHANGELOG.md +0 -135
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/base/brand.d.ts.map +0 -1
- package/lib/packlets/base/brand.js.map +0 -1
- package/lib/packlets/base/index.d.ts.map +0 -1
- package/lib/packlets/base/index.js.map +0 -1
- package/lib/packlets/base/logger.d.ts.map +0 -1
- package/lib/packlets/base/logger.js.map +0 -1
- package/lib/packlets/base/mapResults.d.ts.map +0 -1
- package/lib/packlets/base/mapResults.js.map +0 -1
- package/lib/packlets/base/messageAggregator.d.ts.map +0 -1
- package/lib/packlets/base/messageAggregator.js.map +0 -1
- package/lib/packlets/base/normalize.d.ts.map +0 -1
- package/lib/packlets/base/normalize.js.map +0 -1
- package/lib/packlets/base/result.d.ts.map +0 -1
- package/lib/packlets/base/result.js.map +0 -1
- package/lib/packlets/base/utils.d.ts.map +0 -1
- package/lib/packlets/base/utils.js.map +0 -1
- package/lib/packlets/collections/collectible.d.ts.map +0 -1
- package/lib/packlets/collections/collectible.js.map +0 -1
- package/lib/packlets/collections/collector.d.ts.map +0 -1
- package/lib/packlets/collections/collector.js.map +0 -1
- package/lib/packlets/collections/collectorValidator.d.ts.map +0 -1
- package/lib/packlets/collections/collectorValidator.js.map +0 -1
- package/lib/packlets/collections/common.d.ts.map +0 -1
- package/lib/packlets/collections/common.js.map +0 -1
- package/lib/packlets/collections/convertingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/convertingCollector.js.map +0 -1
- package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +0 -1
- package/lib/packlets/collections/convertingCollectorValidator.js.map +0 -1
- package/lib/packlets/collections/index.d.ts.map +0 -1
- package/lib/packlets/collections/index.js.map +0 -1
- package/lib/packlets/collections/keyValueConverters.d.ts.map +0 -1
- package/lib/packlets/collections/keyValueConverters.js.map +0 -1
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/readonlyResultMap.js.map +0 -1
- package/lib/packlets/collections/resultMap.d.ts.map +0 -1
- package/lib/packlets/collections/resultMap.js.map +0 -1
- package/lib/packlets/collections/resultMapValidator.d.ts.map +0 -1
- package/lib/packlets/collections/resultMapValidator.js.map +0 -1
- package/lib/packlets/collections/utils.d.ts.map +0 -1
- package/lib/packlets/collections/utils.js.map +0 -1
- package/lib/packlets/collections/validatingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/validatingCollector.js.map +0 -1
- package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +0 -1
- package/lib/packlets/collections/validatingConvertingCollector.js.map +0 -1
- package/lib/packlets/collections/validatingResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/validatingResultMap.js.map +0 -1
- package/lib/packlets/conversion/baseConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/baseConverter.js.map +0 -1
- package/lib/packlets/conversion/converter.d.ts.map +0 -1
- package/lib/packlets/conversion/converter.js.map +0 -1
- package/lib/packlets/conversion/converters.d.ts.map +0 -1
- package/lib/packlets/conversion/converters.js.map +0 -1
- package/lib/packlets/conversion/defaultingConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/defaultingConverter.js.map +0 -1
- package/lib/packlets/conversion/index.d.ts.map +0 -1
- package/lib/packlets/conversion/index.js.map +0 -1
- package/lib/packlets/conversion/objectConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/objectConverter.js.map +0 -1
- package/lib/packlets/conversion/stringConverter.d.ts.map +0 -1
- package/lib/packlets/conversion/stringConverter.js.map +0 -1
- package/lib/packlets/file-tree/directoryItem.d.ts.map +0 -1
- package/lib/packlets/file-tree/directoryItem.js.map +0 -1
- package/lib/packlets/file-tree/fileItem.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileItem.js.map +0 -1
- package/lib/packlets/file-tree/fileTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileTree.js.map +0 -1
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +0 -1
- package/lib/packlets/file-tree/fileTreeAccessors.js.map +0 -1
- package/lib/packlets/file-tree/fsTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/fsTree.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/index.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/index.js.map +0 -1
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +0 -1
- package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +0 -1
- package/lib/packlets/file-tree/index.d.ts.map +0 -1
- package/lib/packlets/file-tree/index.js.map +0 -1
- package/lib/packlets/hash/crcNormalizer.d.ts.map +0 -1
- package/lib/packlets/hash/crcNormalizer.js.map +0 -1
- package/lib/packlets/hash/hashingNormalizer.d.ts.map +0 -1
- package/lib/packlets/hash/hashingNormalizer.js.map +0 -1
- package/lib/packlets/hash/index.d.ts.map +0 -1
- package/lib/packlets/hash/index.js.map +0 -1
- package/lib/packlets/validation/array.d.ts.map +0 -1
- package/lib/packlets/validation/array.js.map +0 -1
- package/lib/packlets/validation/boolean.d.ts.map +0 -1
- package/lib/packlets/validation/boolean.js.map +0 -1
- package/lib/packlets/validation/classes.d.ts.map +0 -1
- package/lib/packlets/validation/classes.js.map +0 -1
- package/lib/packlets/validation/common.d.ts.map +0 -1
- package/lib/packlets/validation/common.js.map +0 -1
- package/lib/packlets/validation/field.d.ts.map +0 -1
- package/lib/packlets/validation/field.js.map +0 -1
- package/lib/packlets/validation/genericValidator.d.ts.map +0 -1
- package/lib/packlets/validation/genericValidator.js.map +0 -1
- package/lib/packlets/validation/index.d.ts.map +0 -1
- package/lib/packlets/validation/index.js.map +0 -1
- package/lib/packlets/validation/number.d.ts.map +0 -1
- package/lib/packlets/validation/number.js.map +0 -1
- package/lib/packlets/validation/object.d.ts.map +0 -1
- package/lib/packlets/validation/object.js.map +0 -1
- package/lib/packlets/validation/oneOf.d.ts.map +0 -1
- package/lib/packlets/validation/oneOf.js.map +0 -1
- package/lib/packlets/validation/string.d.ts.map +0 -1
- package/lib/packlets/validation/string.js.map +0 -1
- package/lib/packlets/validation/traits.d.ts.map +0 -1
- package/lib/packlets/validation/traits.js.map +0 -1
- package/lib/packlets/validation/typeGuard.d.ts.map +0 -1
- package/lib/packlets/validation/typeGuard.js.map +0 -1
- package/lib/packlets/validation/validator.d.ts.map +0 -1
- package/lib/packlets/validation/validator.js.map +0 -1
- package/lib/packlets/validation/validatorBase.d.ts.map +0 -1
- package/lib/packlets/validation/validatorBase.js.map +0 -1
- package/lib/packlets/validation/validators.d.ts.map +0 -1
- package/lib/packlets/validation/validators.js.map +0 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { captureResult, Result } from '../base';
|
|
24
|
+
import { CollectibleFactory, CollectibleKey, ICollectible } from './collectible';
|
|
25
|
+
import { ConvertingCollector } from './convertingCollector';
|
|
26
|
+
import { ConvertingCollectorValidator } from './convertingCollectorValidator';
|
|
27
|
+
import { KeyValueEntry } from './common';
|
|
28
|
+
import { KeyValueConverters } from './keyValueConverters';
|
|
29
|
+
import { IReadOnlyValidatingCollector } from './validatingCollector';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parameters for constructing a {@link Collections.ValidatingConvertingCollector | ValidatingConvertingCollector}.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export interface IValidatingConvertingCollectorConstructorParams<
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
TITEM extends ICollectible<any, any>,
|
|
38
|
+
TSRC = TITEM
|
|
39
|
+
> {
|
|
40
|
+
/**
|
|
41
|
+
* {@inheritdoc Collections.IConvertingCollectorConstructorParams.factory}
|
|
42
|
+
*/
|
|
43
|
+
factory: CollectibleFactory<TITEM, TSRC>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* {@inheritdoc Collections.IConvertingCollectorValidatorCreateParams.converters}
|
|
47
|
+
*/
|
|
48
|
+
converters: KeyValueConverters<CollectibleKey<TITEM>, TSRC>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritdoc Collections.IConvertingCollectorConstructorParams.entries}
|
|
52
|
+
*/
|
|
53
|
+
entries?: KeyValueEntry<CollectibleKey<TITEM>, TSRC>[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A {@link Collections.ConvertingCollector | ConvertingCollector} with a
|
|
58
|
+
* {@link Collections.ConvertingCollectorValidator | ConvertingCollectorValidator}
|
|
59
|
+
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export class ValidatingConvertingCollector<
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
TITEM extends ICollectible<any, any>,
|
|
65
|
+
TSRC = TITEM
|
|
66
|
+
> extends ConvertingCollector<TITEM, TSRC> {
|
|
67
|
+
/**
|
|
68
|
+
* A {@link Collections.ConvertingCollectorValidator | ConvertingCollectorValidator} which validates keys and values
|
|
69
|
+
* before inserting them into this collector.
|
|
70
|
+
*/
|
|
71
|
+
public readonly validating: ConvertingCollectorValidator<TITEM, TSRC>;
|
|
72
|
+
|
|
73
|
+
protected readonly _converters: KeyValueConverters<CollectibleKey<TITEM>, TSRC>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Constructs a new {@link Collections.ValidatingConvertingCollector | ValidatingConvertingCollector}
|
|
77
|
+
* from the supplied {@link Collections.IValidatingConvertingCollectorConstructorParams | parameters}.
|
|
78
|
+
* @param params - Required parameters for constructing the collector.
|
|
79
|
+
*/
|
|
80
|
+
public constructor(params: IValidatingConvertingCollectorConstructorParams<TITEM, TSRC>) {
|
|
81
|
+
super({ factory: params.factory });
|
|
82
|
+
this._converters = params.converters;
|
|
83
|
+
this.validating = new ConvertingCollectorValidator({ collector: this, converters: params.converters });
|
|
84
|
+
for (const entry of params.entries ?? []) {
|
|
85
|
+
this.getOrAdd(entry[0], entry[1]).orThrow();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a new {@link Collections.ValidatingConvertingCollector | ValidatingConvertingCollector} instance from
|
|
91
|
+
* the supplied {@link Collections.IValidatingConvertingCollectorConstructorParams | parameters}.
|
|
92
|
+
* @param params - Required parameters for constructing the collector.
|
|
93
|
+
* @returns {@link Success} with the new collector if successful, {@link Failure} otherwise.
|
|
94
|
+
*/
|
|
95
|
+
public static createValidatingCollector<
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
|
+
TITEM extends ICollectible<any, any>,
|
|
98
|
+
TSRC = TITEM
|
|
99
|
+
>(
|
|
100
|
+
params: IValidatingConvertingCollectorConstructorParams<TITEM, TSRC>
|
|
101
|
+
): Result<ValidatingConvertingCollector<TITEM, TSRC>> {
|
|
102
|
+
return captureResult(() => new ValidatingConvertingCollector(params));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Gets a read-only version of this collector as a
|
|
107
|
+
* {@link Collections.IReadOnlyValidatingResultMap | read-only map}.
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
public toReadOnly(): IReadOnlyValidatingCollector<TITEM> {
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { captureResult, Result } from '../base';
|
|
24
|
+
import { KeyValueEntry } from './common';
|
|
25
|
+
import { KeyValueConverters } from './keyValueConverters';
|
|
26
|
+
import { IReadOnlyResultMap } from './readonlyResultMap';
|
|
27
|
+
import { ResultMap } from './resultMap';
|
|
28
|
+
import { IReadOnlyResultMapValidator, ResultMapValidator } from './resultMapValidator';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A read-only interface exposing non-mutating methods of a {@link Collections.ValidatingResultMap | ValidatingResultMap}.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export interface IReadOnlyValidatingResultMap<TK extends string = string, TV = unknown>
|
|
35
|
+
extends IReadOnlyResultMap<TK, TV> {
|
|
36
|
+
/**
|
|
37
|
+
* {@inheritdoc Collections.ValidatingResultMap.validating}
|
|
38
|
+
*/
|
|
39
|
+
readonly validating: IReadOnlyResultMapValidator<TK, TV>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parameters for constructing a {@link Collections.ResultMap | ResultMap}.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export interface IValidatingResultMapConstructorParams<TK extends string = string, TV = unknown> {
|
|
47
|
+
entries?: Iterable<KeyValueEntry<string, unknown>>;
|
|
48
|
+
converters: KeyValueConverters<TK, TV>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapValidator | validator}
|
|
53
|
+
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export class ValidatingResultMap<TK extends string = string, TV = unknown>
|
|
57
|
+
extends ResultMap<TK, TV>
|
|
58
|
+
implements IReadOnlyValidatingResultMap<TK, TV>
|
|
59
|
+
{
|
|
60
|
+
/**
|
|
61
|
+
* A {@link Collections.ResultMapValidator | ResultMapValidator} which validates keys and values
|
|
62
|
+
* before inserting them into this collection.
|
|
63
|
+
*/
|
|
64
|
+
public readonly validating: ResultMapValidator<TK, TV>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.
|
|
68
|
+
* @param params - Required parameters for constructing the map.
|
|
69
|
+
*/
|
|
70
|
+
public constructor(params: IValidatingResultMapConstructorParams<TK, TV>) {
|
|
71
|
+
const entries = params.converters.convertEntries([...(params.entries ?? [])]).orThrow();
|
|
72
|
+
super({ entries });
|
|
73
|
+
this.validating = new ResultMapValidator<TK, TV>({ map: this, converters: params.converters });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Creates a new {@link Collections.ValidatingResultMap | ValidatingResultMap} instance.
|
|
78
|
+
* @param params - Required parameters for constructing the map.
|
|
79
|
+
* @returns `Success` with the new map if successful, `Failure` otherwise.
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
public static createValidatingResultMap<TK extends string = string, TV = unknown>(
|
|
83
|
+
params: IValidatingResultMapConstructorParams<TK, TV>
|
|
84
|
+
): Result<ValidatingResultMap<TK, TV>> {
|
|
85
|
+
return captureResult(() => new ValidatingResultMap(params));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Gets a read-only version of this map.
|
|
90
|
+
*/
|
|
91
|
+
public toReadOnly(): IReadOnlyValidatingResultMap<TK, TV> {
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import { Brand, Result, fail, mapResults, succeed } from '../base';
|
|
23
|
+
import {
|
|
24
|
+
ConstraintOptions,
|
|
25
|
+
ConversionErrorFormatter,
|
|
26
|
+
Converter,
|
|
27
|
+
ConverterTraits,
|
|
28
|
+
DefaultingConverter,
|
|
29
|
+
OnError
|
|
30
|
+
} from './converter';
|
|
31
|
+
import { GenericDefaultingConverter } from './defaultingConverter';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* internal
|
|
35
|
+
*/
|
|
36
|
+
type InnerInferredType<TCONV> = TCONV extends Converter<infer TTO>
|
|
37
|
+
? TTO extends Array<infer TTOELEM>
|
|
38
|
+
? InnerInferredType<TTOELEM>[]
|
|
39
|
+
: TTO
|
|
40
|
+
: TCONV extends Array<infer TELEM>
|
|
41
|
+
? InnerInferredType<TELEM>[]
|
|
42
|
+
: TCONV;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Infers the type that will be returned by an instantiated converter. Works
|
|
46
|
+
* for complex as well as simple types.
|
|
47
|
+
* @example `Infer<typeof Converters.mapOf(Converters.stringArray)>` is `Map<string, string[]>`
|
|
48
|
+
* @beta
|
|
49
|
+
*/
|
|
50
|
+
export type Infer<TCONV> = TCONV extends Converter<infer TTO, unknown> ? InnerInferredType<TTO> : never;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Deprecated name for Infer<T> retained for compatibility
|
|
54
|
+
* @deprecated use @see Infer instead
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
export type ConvertedToType<TCONV> = Infer<TCONV>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Function signature for a converter function.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export type ConverterFunc<T, TC> = (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Base templated wrapper to simplify creation of new {@link Converter}s.
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export class BaseConverter<T, TC = unknown> implements Converter<T, TC> {
|
|
70
|
+
/**
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
protected readonly _defaultContext?: TC;
|
|
74
|
+
/**
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
protected _isOptional: boolean = false;
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
protected _brand?: string;
|
|
82
|
+
|
|
83
|
+
private readonly _converter: (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Constructs a new {@link Converter} which uses the supplied function to perform the conversion.
|
|
87
|
+
* @param converter - The conversion function to be applied.
|
|
88
|
+
* @param defaultContext - Optional conversion context to be used by default.
|
|
89
|
+
* @param traits - Optional {@link Conversion.ConverterTraits | traits} to be assigned to the resulting
|
|
90
|
+
* converter.
|
|
91
|
+
*/
|
|
92
|
+
public constructor(converter: ConverterFunc<T, TC>, defaultContext?: TC, traits?: ConverterTraits) {
|
|
93
|
+
this._converter = converter;
|
|
94
|
+
this._defaultContext = defaultContext;
|
|
95
|
+
this._isOptional = traits?.isOptional === true;
|
|
96
|
+
this._brand = traits?.brand;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* {@inheritdoc Converter.isOptional}
|
|
101
|
+
*/
|
|
102
|
+
public get isOptional(): boolean {
|
|
103
|
+
return this._isOptional;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* {@inheritdoc Converter.brand}
|
|
108
|
+
*/
|
|
109
|
+
public get brand(): string | undefined {
|
|
110
|
+
return this._brand;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* {@inheritdoc Converter.convert}
|
|
115
|
+
*/
|
|
116
|
+
public convert(from: unknown, context?: TC): Result<T> {
|
|
117
|
+
return this._converter(from, this, context ?? this._defaultContext);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* {@inheritdoc Converter.convertOptional}
|
|
122
|
+
*/
|
|
123
|
+
public convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined> {
|
|
124
|
+
const result = this._converter(from, this, this._context(context));
|
|
125
|
+
if (result.isFailure()) {
|
|
126
|
+
onError = onError ?? 'ignoreErrors';
|
|
127
|
+
return from === undefined || onError === 'ignoreErrors' ? succeed(undefined) : result;
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* {@inheritdoc Converter.optional}
|
|
134
|
+
*/
|
|
135
|
+
public optional(onError?: OnError): Converter<T | undefined, TC> {
|
|
136
|
+
return new BaseConverter((from: unknown, __self: Converter<T | undefined, TC>, context?: TC) => {
|
|
137
|
+
onError = onError ?? 'failOnError';
|
|
138
|
+
return this.convertOptional(from, this._context(context), onError);
|
|
139
|
+
})._with(this._traits({ isOptional: true }));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* {@inheritdoc Converter.map}
|
|
144
|
+
*/
|
|
145
|
+
public map<T2>(mapper: (from: T, context?: TC) => Result<T2>): Converter<T2, TC> {
|
|
146
|
+
return new BaseConverter<T2, TC>((from: unknown, __self: Converter<T2, TC>, context?: TC) => {
|
|
147
|
+
const innerResult = this._converter(from, this, this._context(context));
|
|
148
|
+
if (innerResult.isSuccess()) {
|
|
149
|
+
return mapper(innerResult.value, this._context(context));
|
|
150
|
+
}
|
|
151
|
+
return fail(innerResult.message);
|
|
152
|
+
})._with(this._traits());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* {@inheritdoc Converter.mapConvert}
|
|
157
|
+
*/
|
|
158
|
+
public mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC> {
|
|
159
|
+
return new BaseConverter<T2, TC>((from: unknown, __self: Converter<T2, TC>, context?: TC) => {
|
|
160
|
+
const innerResult = this._converter(from, this, this._context(context));
|
|
161
|
+
if (innerResult.isSuccess()) {
|
|
162
|
+
return mapConverter.convert(innerResult.value, this._context(context));
|
|
163
|
+
}
|
|
164
|
+
return fail(innerResult.message);
|
|
165
|
+
// eslint-disable-next-line no-return-assign
|
|
166
|
+
})._with(this._traits());
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* {@inheritdoc Converter.mapItems}
|
|
171
|
+
*/
|
|
172
|
+
public mapItems<TI>(mapper: (from: unknown, context?: TC) => Result<TI>): Converter<TI[], TC> {
|
|
173
|
+
return new BaseConverter<TI[], TC>((from: unknown, __self: Converter<TI[], TC>, context?: TC) => {
|
|
174
|
+
return this._converter(from, this, this._context(context)).onSuccess((items) => {
|
|
175
|
+
if (Array.isArray(items)) {
|
|
176
|
+
return mapResults(items.map((i) => mapper(i, this._context(context))));
|
|
177
|
+
}
|
|
178
|
+
return fail('Cannot map items - not an array');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* {@inheritdoc Converter.mapConvertItems}
|
|
185
|
+
*/
|
|
186
|
+
public mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC> {
|
|
187
|
+
return new BaseConverter<TI[], TC>((from: unknown, __self: Converter<TI[], TC>, context?: TC) => {
|
|
188
|
+
return this._converter(from, this, this._context(context)).onSuccess((items) => {
|
|
189
|
+
if (Array.isArray(items)) {
|
|
190
|
+
return mapResults(items.map((i) => mapConverter.convert(i, this._context(context))));
|
|
191
|
+
}
|
|
192
|
+
return fail('Cannot map items - not an array');
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* {@inheritdoc Converter.withAction}
|
|
199
|
+
*/
|
|
200
|
+
public withAction<TI>(action: (result: Result<T>, context?: TC) => Result<TI>): Converter<TI, TC> {
|
|
201
|
+
return new BaseConverter<TI, TC>((from: unknown, __self: Converter<TI, TC>, context?: TC) => {
|
|
202
|
+
return action(this._converter(from, this, this._context(context)), this._context(context));
|
|
203
|
+
})._with(this._traits());
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* {@inheritdoc Converter.withTypeGuard}
|
|
208
|
+
*/
|
|
209
|
+
public withTypeGuard<TI>(
|
|
210
|
+
guard: (from: unknown, context?: TC) => from is TI,
|
|
211
|
+
message?: string
|
|
212
|
+
): Converter<TI, TC> {
|
|
213
|
+
return new BaseConverter<TI, TC>((from: unknown, __self: Converter<TI, TC>, context?: TC) => {
|
|
214
|
+
return this._converter(from, this, this._context(context)).onSuccess((inner) => {
|
|
215
|
+
message = message ?? 'invalid type';
|
|
216
|
+
return guard(inner, this._context(context))
|
|
217
|
+
? succeed(inner)
|
|
218
|
+
: fail(`${message}: ${JSON.stringify(from)}`);
|
|
219
|
+
});
|
|
220
|
+
})._with(this._traits());
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* {@inheritdoc Converter.withItemTypeGuard}
|
|
225
|
+
*/
|
|
226
|
+
public withItemTypeGuard<TI>(
|
|
227
|
+
guard: (from: unknown, context?: TC) => from is TI,
|
|
228
|
+
message?: string
|
|
229
|
+
): Converter<TI[], TC> {
|
|
230
|
+
return new BaseConverter<TI[], TC>((from: unknown, __self: Converter<TI[], TC>, context?: TC) => {
|
|
231
|
+
return this._converter(from, this, this._context(context)).onSuccess((items) => {
|
|
232
|
+
if (Array.isArray(items)) {
|
|
233
|
+
return mapResults(
|
|
234
|
+
items.map((i) => {
|
|
235
|
+
message = message ?? 'invalid type';
|
|
236
|
+
return guard(i, this._context(context))
|
|
237
|
+
? succeed(i)
|
|
238
|
+
: fail(`${message}: ${JSON.stringify(from)}`);
|
|
239
|
+
})
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return fail('Cannot guard item type - not an array');
|
|
243
|
+
});
|
|
244
|
+
})._with(this._traits());
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* {@inheritdoc Converter.withConstraint}
|
|
249
|
+
*/
|
|
250
|
+
public withConstraint(
|
|
251
|
+
constraint: (val: T, context?: TC) => boolean | Result<T>,
|
|
252
|
+
options?: ConstraintOptions
|
|
253
|
+
): Converter<T, TC> {
|
|
254
|
+
return new BaseConverter<T, TC>((from: unknown, __self: Converter<T, TC>, context?: TC) => {
|
|
255
|
+
const result = this._converter(from, this, this._context(context));
|
|
256
|
+
if (result.isSuccess()) {
|
|
257
|
+
const constraintResult = constraint(result.value, this._context(context));
|
|
258
|
+
if (typeof constraintResult === 'boolean') {
|
|
259
|
+
return constraintResult
|
|
260
|
+
? result
|
|
261
|
+
: fail(
|
|
262
|
+
`"${JSON.stringify(result.value)}": ${options?.description ?? 'does not meet constraint'}`
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
return constraintResult;
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
268
|
+
})._with(this._traits());
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* {@inheritdoc Converter.withBrand}
|
|
273
|
+
*/
|
|
274
|
+
public withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC> {
|
|
275
|
+
if (this._brand) {
|
|
276
|
+
throw new Error(`Cannot replace existing brand "${this._brand}" with "${brand}".`);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return new BaseConverter<Brand<T, B>, TC>((from: unknown, __self: Converter<T, TC>, context?: TC) => {
|
|
280
|
+
return this._converter(from, this, this._context(context)).onSuccess((v) => {
|
|
281
|
+
return succeed(v as Brand<T, B>);
|
|
282
|
+
});
|
|
283
|
+
})._with(this._traits({ brand }));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* {@inheritdoc Converter.withDefault}
|
|
288
|
+
*/
|
|
289
|
+
public withDefault<TD = T>(defaultValue: TD): DefaultingConverter<T, TD, TC> {
|
|
290
|
+
return new GenericDefaultingConverter<T, TD, TC>(this, defaultValue);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @internal
|
|
295
|
+
*/
|
|
296
|
+
protected _context(supplied?: TC): TC | undefined {
|
|
297
|
+
return supplied ?? this._defaultContext;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* {@inheritdoc Converter.withFormattedError}
|
|
302
|
+
*/
|
|
303
|
+
public withFormattedError(formatter: ConversionErrorFormatter<TC>): Converter<T, TC> {
|
|
304
|
+
return new BaseConverter<T, TC>((from: unknown, __self: Converter<T, TC>, context?: TC) => {
|
|
305
|
+
return this._converter(from, this, this._context(context)).onFailure((msg) => {
|
|
306
|
+
return fail(formatter(from, msg, this._context(context)));
|
|
307
|
+
});
|
|
308
|
+
})._with(this._traits());
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @internal
|
|
313
|
+
*/
|
|
314
|
+
protected _traits(traits?: Partial<ConverterTraits>): ConverterTraits {
|
|
315
|
+
return {
|
|
316
|
+
isOptional: this.isOptional,
|
|
317
|
+
brand: this.brand,
|
|
318
|
+
...(traits ?? {})
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @internal
|
|
324
|
+
*/
|
|
325
|
+
protected _with(traits: Partial<ConverterTraits>): this {
|
|
326
|
+
this._isOptional = traits.isOptional === true;
|
|
327
|
+
this._brand = traits.brand;
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
}
|