@fgv/ts-utils 5.0.0-2 → 5.0.0-20
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/ts-utils.d.ts +20 -8
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/base/result.d.ts +8 -0
- package/lib/packlets/base/result.js +11 -0
- package/lib/packlets/hash/crcNormalizer.js +3 -1
- package/lib/packlets/validation/array.d.ts +2 -1
- package/lib/packlets/validation/array.js +2 -1
- package/lib/packlets/validation/common.d.ts +0 -8
- package/lib/packlets/validation/field.d.ts +1 -1
- package/lib/packlets/validation/field.js +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +1 -6
- package/lib/packlets/validation/genericValidator.js +11 -11
- package/lib/packlets/validation/object.d.ts +1 -1
- package/lib/packlets/validation/object.js +1 -1
- package/lib/packlets/validation/oneOf.d.ts +2 -1
- package/lib/packlets/validation/oneOf.js +2 -1
- package/lib/packlets/validation/typeGuard.d.ts +3 -2
- package/lib/packlets/validation/typeGuard.js +2 -1
- package/lib/packlets/validation/validator.d.ts +7 -0
- package/lib/packlets/validation/validatorBase.d.ts +2 -1
- package/lib/packlets/validation/validatorBase.js +1 -1
- package/lib/packlets/validation/validators.d.ts +2 -2
- package/lib/packlets/validation/validators.js +3 -3
- 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/recursiveValidation.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 +84 -0
- package/src/packlets/validation/boolean.ts +63 -0
- package/src/packlets/validation/classes.ts +38 -0
- package/src/packlets/validation/common.ts +28 -0
- package/src/packlets/validation/field.ts +99 -0
- package/src/packlets/validation/genericValidator.ts +204 -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 +78 -0
- package/src/packlets/validation/string.ts +66 -0
- package/src/packlets/validation/traits.ts +113 -0
- package/src/packlets/validation/typeGuard.ts +83 -0
- package/src/packlets/validation/validator.ts +157 -0
- package/src/packlets/validation/validatorBase.ts +66 -0
- package/src/packlets/validation/validators.ts +254 -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,260 @@
|
|
|
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, Success } from '../base';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Action to take on conversion failures.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export type OnError = 'failOnError' | 'ignoreErrors';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Converter traits.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
35
|
+
export interface ConverterTraits {
|
|
36
|
+
readonly isOptional: boolean;
|
|
37
|
+
readonly brand?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Formats an incoming error message and value that failed validation.
|
|
42
|
+
* @param val - The value that failed validation.
|
|
43
|
+
* @param message - The default error message, if any.
|
|
44
|
+
* @param context - Optional validation context.
|
|
45
|
+
* @returns The formatted error message.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export type ConversionErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Options for {@link Converter.withConstraint}.
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
55
|
+
export interface ConstraintOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Optional description for error messages when constraint
|
|
58
|
+
* function returns false.
|
|
59
|
+
*/
|
|
60
|
+
readonly description: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Generic converter to convert unknown to a templated type `<T>`, using
|
|
65
|
+
* intrinsic rules or as modified by an optional conversion context
|
|
66
|
+
* of optional templated type `<TC>` (default `undefined`).
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
70
|
+
export interface Converter<T, TC = unknown> extends ConverterTraits {
|
|
71
|
+
/**
|
|
72
|
+
* Indicates whether this element is explicitly optional.
|
|
73
|
+
*/
|
|
74
|
+
readonly isOptional: boolean;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns the brand for a branded type.
|
|
78
|
+
*/
|
|
79
|
+
readonly brand?: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Converts from `unknown` to `<T>`. For objects and arrays, is guaranteed
|
|
83
|
+
* to return a new entity, with any unrecognized properties removed.
|
|
84
|
+
* @param from - The `unknown` to be converted
|
|
85
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
86
|
+
* the conversion.
|
|
87
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
88
|
+
* {@link Failure} with a a message on failure.
|
|
89
|
+
*/
|
|
90
|
+
convert(from: unknown, context?: TC): Result<T>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Converts from `unknown` to `<T>` or `undefined`, as appropriate.
|
|
94
|
+
*
|
|
95
|
+
* @remarks
|
|
96
|
+
* If `onError` is `failOnError`, the converter succeeds for
|
|
97
|
+
* `undefined` or any convertible value, but reports an error
|
|
98
|
+
* if it encounters a value that cannot be converted.
|
|
99
|
+
*
|
|
100
|
+
* If `onError` is `ignoreErrors` (default) then values that
|
|
101
|
+
* cannot be converted result in a successful return of `undefined`.
|
|
102
|
+
* @param from - The `unknown` to be converted
|
|
103
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
104
|
+
* the conversion.
|
|
105
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
106
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
107
|
+
* {@link Failure} with a a message on failure.
|
|
108
|
+
*/
|
|
109
|
+
convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates a {@link Converter} for an optional value.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* If `onError` is `failOnError`, the resulting converter will accept `undefined`
|
|
116
|
+
* or a convertible value, but report an error if it encounters a value that cannot be
|
|
117
|
+
* converted.
|
|
118
|
+
*
|
|
119
|
+
* If `onError` is `ignoreErrors` (default) then values that cannot be converted will
|
|
120
|
+
* result in a successful return of `undefined`.
|
|
121
|
+
*
|
|
122
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
123
|
+
* @returns A new {@link Converter} returning `<T|undefined>`.
|
|
124
|
+
* */
|
|
125
|
+
optional(onError?: OnError): Converter<T | undefined, TC>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Creates a {@link Converter} which applies a (possibly) mapping conversion to
|
|
129
|
+
* the converted value of this {@link Converter}.
|
|
130
|
+
* @param mapper - A function which maps from the the result type `<T>` of this
|
|
131
|
+
* converter to a new result type `<T2>`.
|
|
132
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
133
|
+
*/
|
|
134
|
+
map<T2>(mapper: (from: T, context?: TC) => Result<T2>): Converter<T2, TC>;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Creates a {@link Converter} which applies an additional supplied
|
|
138
|
+
* converter to the result of this converter.
|
|
139
|
+
*
|
|
140
|
+
* @param mapConverter - The {@link Converter} to be applied to the
|
|
141
|
+
* converted result from this {@link Converter}.
|
|
142
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
143
|
+
*/
|
|
144
|
+
mapConvert<T2>(mapConverter: Converter<T2, unknown>): Converter<T2, TC>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
148
|
+
* resulting from this {@link Converter} using the supplied map function.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
151
|
+
* Fails if `from` is not an array.
|
|
152
|
+
*
|
|
153
|
+
* @param mapper - The map function to be applied to each element of the
|
|
154
|
+
* result of this {@link Converter}.
|
|
155
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
156
|
+
*/
|
|
157
|
+
mapItems<TI>(mapper: (from: unknown, context?: TC) => Result<TI>): Converter<TI[], TC>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
161
|
+
* resulting from this {@link Converter} using the supplied {@link Converter}.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* Fails if `from` is not an array.
|
|
165
|
+
*
|
|
166
|
+
* @param mapConverter - The {@link Converter} to be applied to each element of the
|
|
167
|
+
* result of this {@link Converter}.
|
|
168
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
169
|
+
*/
|
|
170
|
+
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Creates a {@link Converter | Converter} which applies a supplied action after
|
|
174
|
+
* conversion. The supplied action is always called regardless of success or failure
|
|
175
|
+
* of the base conversion and is allowed to mutate the return type.
|
|
176
|
+
* @param action - The action to be applied.
|
|
177
|
+
*/
|
|
178
|
+
withAction<T2>(action: (result: Result<T>, context?: TC) => Result<T2>): Converter<T2, TC>;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates a {@link Converter} which applies a supplied type guard to the conversion
|
|
182
|
+
* result.
|
|
183
|
+
* @param guard - The type guard function to apply.
|
|
184
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
185
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
186
|
+
*/
|
|
187
|
+
withTypeGuard<TI>(guard: (from: unknown, context?: TC) => from is TI, message?: string): Converter<TI, TC>;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Creates a {@link Converter} which applies a supplied type guard to each member of
|
|
191
|
+
* the conversion result from this converter.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* Fails if the conversion result is not an array or if any member fails the
|
|
195
|
+
* type guard.
|
|
196
|
+
* @param guard - The type guard function to apply to each element.
|
|
197
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
198
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
199
|
+
*/
|
|
200
|
+
withItemTypeGuard<TI>(
|
|
201
|
+
guard: (from: unknown, context?: TC) => from is TI,
|
|
202
|
+
message?: string
|
|
203
|
+
): Converter<TI[], TC>;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Creates a {@link Converter} which applies an optional constraint to the result
|
|
207
|
+
* of this conversion. If this {@link Converter} (the base converter) succeeds, the new
|
|
208
|
+
* converter calls a supplied constraint evaluation function with the conversion, which
|
|
209
|
+
* fails the entire conversion if the constraint function returns either `false` or
|
|
210
|
+
* {@link Failure | Failure<T>}.
|
|
211
|
+
*
|
|
212
|
+
* @param constraint - Constraint evaluation function.
|
|
213
|
+
* @param options - {@link Conversion.ConstraintOptions | Options} for constraint evaluation.
|
|
214
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
215
|
+
*/
|
|
216
|
+
withConstraint(
|
|
217
|
+
constraint: (val: T, context?: TC) => boolean | Result<T>,
|
|
218
|
+
options?: ConstraintOptions
|
|
219
|
+
): Converter<T, TC>;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Creates a new {@link Converter} which is derived from this one but which returns an
|
|
223
|
+
* error message formatted by the supplied formatter if the conversion fails.
|
|
224
|
+
* @param formatter - The formatter to be applied.
|
|
225
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
226
|
+
*/
|
|
227
|
+
withFormattedError(formatter: ConversionErrorFormatter<TC>): Converter<T, TC>;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* returns a converter which adds a brand to the type to prevent mismatched usage
|
|
231
|
+
* of simple types.
|
|
232
|
+
* @param brand - The brand to be applied to the result value.
|
|
233
|
+
* @returns A {@link Converter} returning `Brand<T, B>`.
|
|
234
|
+
*/
|
|
235
|
+
withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Returns a Converter which always succeeds with a default value rather than failing.
|
|
239
|
+
*/
|
|
240
|
+
withDefault<TD = T>(dflt: TD): DefaultingConverter<T, TD, TC>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* @public
|
|
245
|
+
*/
|
|
246
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
247
|
+
export interface DefaultingConverter<T, TD = T, TC = unknown> extends Converter<T | TD, TC> {
|
|
248
|
+
/**
|
|
249
|
+
* Default value to use if the conversion fails.
|
|
250
|
+
*/
|
|
251
|
+
readonly defaultValue: TD;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Convert the supplied `unknown` to `Success<T>` or to the `Success` with the default value
|
|
255
|
+
* if conversion is not possible.
|
|
256
|
+
* @param from - the value to be converted.
|
|
257
|
+
* @param ctx - optional context for the conversion.
|
|
258
|
+
*/
|
|
259
|
+
convert(from: unknown, ctx?: TC): Success<T | TD>;
|
|
260
|
+
}
|