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