@fgv/ts-utils 5.0.1-0 → 5.0.1-10
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/dist/index.js +32 -0
- package/dist/packlets/base/brand.js +23 -0
- package/dist/packlets/base/index.js +28 -0
- package/dist/packlets/base/mapResults.js +217 -0
- package/dist/packlets/base/messageAggregator.js +110 -0
- package/dist/packlets/base/normalize.js +132 -0
- package/dist/packlets/base/result.js +611 -0
- package/dist/packlets/base/utils.js +185 -0
- package/dist/packlets/collections/collectible.js +82 -0
- package/dist/packlets/collections/collector.js +189 -0
- package/dist/packlets/collections/collectorValidator.js +94 -0
- package/dist/packlets/collections/common.js +23 -0
- package/dist/packlets/collections/convertingCollector.js +118 -0
- package/dist/packlets/collections/convertingCollectorValidator.js +95 -0
- package/dist/packlets/collections/index.js +37 -0
- package/dist/packlets/collections/keyValueConverters.js +100 -0
- package/dist/packlets/collections/readonlyResultMap.js +23 -0
- package/dist/packlets/collections/resultMap.js +214 -0
- package/dist/packlets/collections/resultMapValidator.js +122 -0
- package/dist/packlets/collections/utils.js +31 -0
- package/dist/packlets/collections/validatingCollector.js +63 -0
- package/dist/packlets/collections/validatingConvertingCollector.js +64 -0
- package/dist/packlets/collections/validatingResultMap.js +57 -0
- package/dist/packlets/conversion/baseConverter.js +242 -0
- package/dist/packlets/conversion/converter.js +2 -0
- package/dist/packlets/conversion/converters.js +637 -0
- package/dist/packlets/conversion/defaultingConverter.js +149 -0
- package/dist/packlets/conversion/index.js +29 -0
- package/dist/packlets/conversion/objectConverter.js +141 -0
- package/dist/packlets/conversion/stringConverter.js +93 -0
- package/dist/packlets/hash/crcNormalizer.js +114 -0
- package/dist/packlets/hash/hashingNormalizer.js +92 -0
- package/dist/packlets/hash/index.js +24 -0
- package/dist/packlets/logging/index.js +24 -0
- package/dist/packlets/logging/logReporter.js +114 -0
- package/dist/packlets/logging/logger.js +258 -0
- package/dist/packlets/validation/array.js +64 -0
- package/dist/packlets/validation/boolean.js +52 -0
- package/dist/packlets/validation/classes.js +31 -0
- package/dist/packlets/validation/common.js +23 -0
- package/dist/packlets/validation/field.js +67 -0
- package/dist/packlets/validation/genericValidator.js +160 -0
- package/dist/packlets/validation/index.js +29 -0
- package/dist/packlets/validation/number.js +52 -0
- package/dist/packlets/validation/object.js +134 -0
- package/dist/packlets/validation/oneOf.js +58 -0
- package/dist/packlets/validation/string.js +52 -0
- package/dist/packlets/validation/traits.js +56 -0
- package/dist/packlets/validation/typeGuard.js +59 -0
- package/dist/packlets/validation/validator.js +23 -0
- package/dist/packlets/validation/validatorBase.js +37 -0
- package/dist/packlets/validation/validators.js +191 -0
- package/dist/test/helpers/jest/helpers/fsHelpers.js +139 -0
- package/dist/test/helpers/jest/helpers/index.js +2 -0
- package/dist/test/helpers/jest/index.js +17 -0
- package/dist/test/helpers/jest/matchers/index.js +14 -0
- package/dist/test/helpers/jest/matchers/toFail/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toFail/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toFailTest/index.js +28 -0
- package/dist/test/helpers/jest/matchers/toFailTest/predicate.js +8 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +11 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/index.js +29 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/predicate.js +26 -0
- package/dist/test/helpers/jest/matchers/toFailWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWith/predicate.js +16 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/predicate.js +22 -0
- package/dist/test/helpers/jest/matchers/toSucceed/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toSucceed/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +53 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +17 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/predicate.js +12 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +17 -0
- package/dist/test/helpers/jest/resolvers/cli.js +10 -0
- package/dist/test/helpers/jest/resolvers/ide.js +10 -0
- package/dist/test/helpers/jest/ts-utils.js +2 -0
- package/dist/test/helpers/jest/types/index.js +3 -0
- package/dist/test/helpers/jest/utils/matcherHelpers.js +47 -0
- package/dist/test/helpers/jest/utils/snapshotResolver.js +11 -0
- package/dist/test/unit/collections/helpers.js +106 -0
- package/dist/test/unit/hashTextEncodeCompat.js +23 -0
- package/dist/ts-utils.d.ts +28 -16
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/base/result.d.ts +17 -12
- package/lib/packlets/base/result.js +20 -10
- package/lib/packlets/hash/crcNormalizer.js +42 -2
- package/lib/packlets/logging/logReporter.d.ts +11 -5
- package/lib/packlets/logging/logReporter.js +22 -9
- package/package.json +25 -15
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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 * as Collections from './packlets/collections';
|
|
23
|
+
import * as Conversion from './packlets/conversion';
|
|
24
|
+
import * as Hash from './packlets/hash';
|
|
25
|
+
import * as Logging from './packlets/logging';
|
|
26
|
+
import * as Validation from './packlets/validation';
|
|
27
|
+
import { Collector, ConvertingCollector, ResultMap, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap } from './packlets/collections';
|
|
28
|
+
import { Converters, ObjectConverter, StringConverter } from './packlets/conversion';
|
|
29
|
+
import { Validators } from './packlets/validation';
|
|
30
|
+
export * from './packlets/base';
|
|
31
|
+
export { Collections, Collector, ConvertingCollector, Conversion, Converters, Hash, Logging, ObjectConverter, ResultMap, StringConverter, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap, Validation, Validators };
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 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
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=brand.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
export * from './brand';
|
|
23
|
+
export * from './mapResults';
|
|
24
|
+
export * from './messageAggregator';
|
|
25
|
+
export { Normalizer } from './normalize';
|
|
26
|
+
export * from './result';
|
|
27
|
+
export * from './utils';
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,217 @@
|
|
|
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 { MessageAggregator } from './messageAggregator';
|
|
23
|
+
import { fail, isDeferredResult, succeed } from './result';
|
|
24
|
+
/**
|
|
25
|
+
* Aggregates successful result values from a collection of {@link Result | Result<T>}.
|
|
26
|
+
* @param results - The collection of {@link Result | Result<T>} to be mapped.
|
|
27
|
+
* @param aggregatedErrors - Optional string array to which any error messages will be
|
|
28
|
+
* appended. Each error is appended as an individual string.
|
|
29
|
+
* @returns If all {@link Result | results} are successful, returns {@link Success} with an
|
|
30
|
+
* array containing all returned values. If any {@link Result | results} failed, returns
|
|
31
|
+
* {@link Failure} with a concatenated summary of all error messages.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export function mapResults(results, aggregatedErrors) {
|
|
35
|
+
const errors = [];
|
|
36
|
+
const elements = [];
|
|
37
|
+
for (const result of results) {
|
|
38
|
+
if (result.isSuccess()) {
|
|
39
|
+
elements.push(result.value);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
errors.push(result.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (errors.length > 0) {
|
|
46
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
|
|
47
|
+
return fail(errors.join('\n'));
|
|
48
|
+
}
|
|
49
|
+
return succeed(elements);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
|
|
53
|
+
* optionally ignoring certain error details.
|
|
54
|
+
* @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
|
|
55
|
+
* @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
|
|
56
|
+
* @param aggregatedErrors - Optional string array to which any non-ignorable error messages will be
|
|
57
|
+
* appended. Each error is appended as an individual string.
|
|
58
|
+
* @returns {@link Success} with an array containing all successful results if all results either
|
|
59
|
+
* succeeded or returned error details listed in `ignore`. If any results failed with details
|
|
60
|
+
* that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
|
|
61
|
+
* error messages.
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export function mapDetailedResults(results, ignore, aggregatedErrors) {
|
|
65
|
+
const errors = [];
|
|
66
|
+
const elements = [];
|
|
67
|
+
for (const result of results) {
|
|
68
|
+
if (result.isSuccess()) {
|
|
69
|
+
elements.push(result.value);
|
|
70
|
+
}
|
|
71
|
+
else if (result.detail && !ignore.includes(result.detail)) {
|
|
72
|
+
errors.push(result.message);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (errors.length > 0) {
|
|
76
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
|
|
77
|
+
return fail(errors.join('\n'));
|
|
78
|
+
}
|
|
79
|
+
return succeed(elements);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Aggregates successful results from a a collection of {@link Result | Result<T>}.
|
|
83
|
+
* @param results - An `Iterable` of {@link Result | Result<T>} from which success
|
|
84
|
+
* results are to be aggregated.
|
|
85
|
+
* @param aggregatedErrors - Optional string array to which any returned error messages will be
|
|
86
|
+
* appended. Each error is appended as an individual string.
|
|
87
|
+
* @returns {@link Success} with an array of `<T>` if any results were successful. If
|
|
88
|
+
* all {@link Result | results} failed, returns {@link Failure} with a concatenated
|
|
89
|
+
* summary of all error messages.
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export function mapSuccess(results, aggregatedErrors) {
|
|
93
|
+
const errors = [];
|
|
94
|
+
const elements = [];
|
|
95
|
+
for (const result of results) {
|
|
96
|
+
if (result.isSuccess()) {
|
|
97
|
+
elements.push(result.value);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
errors.push(result.message);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (elements.length === 0 && errors.length > 0) {
|
|
104
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
|
|
105
|
+
return fail(errors.join('\n'));
|
|
106
|
+
}
|
|
107
|
+
return succeed(elements);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Aggregates error messages from a collection of {@link Result | Result<T>}.
|
|
111
|
+
* @param results - An iterable collection of {@link Result | Result<T>} for which
|
|
112
|
+
* error messages are aggregated.
|
|
113
|
+
* @param aggregatedErrors - Optional string array to which any returned error messages will be
|
|
114
|
+
* appended. Each error is appended as an individual string.
|
|
115
|
+
* @returns An array of strings consisting of all error messages returned by
|
|
116
|
+
* {@link Result | results} in the source collection. Ignores {@link Success}
|
|
117
|
+
* results and returns an empty array if there were no errors.
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
export function mapFailures(results, aggregatedErrors) {
|
|
121
|
+
const errors = [];
|
|
122
|
+
for (const result of results) {
|
|
123
|
+
if (result.isFailure()) {
|
|
124
|
+
errors.push(result.message);
|
|
125
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessage(result.message);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return errors;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Determines if an iterable collection of {@link Result | Result<T>} were all successful.
|
|
132
|
+
* @param results - The collection of {@link Result | Result<T>} to be tested.
|
|
133
|
+
* @param successValue - The value to be returned if results are successful.
|
|
134
|
+
* @param aggregatedErrors - Optional string array to which any returned error messages will be
|
|
135
|
+
* appended. Each error is appended as an individual string.
|
|
136
|
+
* @returns Returns {@link Success} with `successValue` if all {@link Result | results} are successful.
|
|
137
|
+
* If any are unsuccessful, returns {@link Failure} with a concatenated summary of the error
|
|
138
|
+
* messages from all failed elements.
|
|
139
|
+
* @public
|
|
140
|
+
*/
|
|
141
|
+
export function allSucceed(results, successValue, aggregatedErrors) {
|
|
142
|
+
const errors = [];
|
|
143
|
+
if (results !== undefined) {
|
|
144
|
+
for (const result of results) {
|
|
145
|
+
if (result.isFailure()) {
|
|
146
|
+
errors.push(result.message);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (errors.length > 0) {
|
|
151
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
|
|
152
|
+
return fail(errors.join('\n'));
|
|
153
|
+
}
|
|
154
|
+
return succeed(successValue);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Returns the first successful result from a collection of {@link Result | Result<T>} or {@link DeferredResult | DeferredResult<T>}.
|
|
158
|
+
* @param results - The collection of {@link Result | Result<T>} or {@link DeferredResult | DeferredResult<T>} to be tested.
|
|
159
|
+
* @returns The first successful result, or {@link Failure} with a concatenated summary of all error messages.
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
export function firstSuccess(results) {
|
|
163
|
+
const errors = new MessageAggregator();
|
|
164
|
+
for (const r of results) {
|
|
165
|
+
const result = isDeferredResult(r) ? r() : r;
|
|
166
|
+
if (result.isSuccess()) {
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
errors.addMessage(result.message);
|
|
170
|
+
}
|
|
171
|
+
return errors.returnOrReport(fail('no results found'));
|
|
172
|
+
}
|
|
173
|
+
export function populateObject(initializers, optionsOrOrder, aggregatedErrors) {
|
|
174
|
+
var _a;
|
|
175
|
+
const options = optionsOrOrder
|
|
176
|
+
? Array.isArray(optionsOrOrder)
|
|
177
|
+
? { order: optionsOrOrder }
|
|
178
|
+
: optionsOrOrder
|
|
179
|
+
: {};
|
|
180
|
+
const state = {};
|
|
181
|
+
const errors = [];
|
|
182
|
+
const keys = Array.from((_a = options.order) !== null && _a !== void 0 ? _a : []);
|
|
183
|
+
const foundKeys = new Set(options.order);
|
|
184
|
+
// start with the supplied order then append anything else we find
|
|
185
|
+
for (const key in initializers) {
|
|
186
|
+
if (!foundKeys.has(key)) {
|
|
187
|
+
keys.push(key);
|
|
188
|
+
foundKeys.add(key);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
for (const key of keys) {
|
|
192
|
+
if (initializers[key]) {
|
|
193
|
+
const result = initializers[key](state);
|
|
194
|
+
if (result.isSuccess()) {
|
|
195
|
+
if (result.value === undefined) {
|
|
196
|
+
if (options.suppressUndefined === true ||
|
|
197
|
+
(Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
state[key] = result.value;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
errors.push(result.message);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (errors.length > 0) {
|
|
212
|
+
aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
|
|
213
|
+
return fail(errors.join('\n'));
|
|
214
|
+
}
|
|
215
|
+
return succeed(state);
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=mapResults.js.map
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 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 { fail } from './result';
|
|
23
|
+
/**
|
|
24
|
+
* A simple error aggregator to simplify collecting and reporting all errors in
|
|
25
|
+
* a flow.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class MessageAggregator {
|
|
29
|
+
/**
|
|
30
|
+
* Constructs a new {@link MessageAggregator | ErrorAggregator} with an
|
|
31
|
+
* optionally specified initial set of error messages.
|
|
32
|
+
* @param errors - optional array of errors to be included
|
|
33
|
+
* in the aggregation.
|
|
34
|
+
*/
|
|
35
|
+
constructor(errors) {
|
|
36
|
+
this._messages = errors ? Array.from(errors) : [];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* {@inheritdoc IMessageAggregator.hasMessages}
|
|
40
|
+
*/
|
|
41
|
+
get hasMessages() {
|
|
42
|
+
return this._messages.length > 0;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* {@inheritdoc IMessageAggregator.numMessages}
|
|
46
|
+
*/
|
|
47
|
+
get numMessages() {
|
|
48
|
+
return this._messages.length;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritdoc IMessageAggregator.messages}
|
|
52
|
+
*/
|
|
53
|
+
get messages() {
|
|
54
|
+
return this._messages;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritdoc IMessageAggregator.addMessage}
|
|
58
|
+
*/
|
|
59
|
+
addMessage(message) {
|
|
60
|
+
if (message) {
|
|
61
|
+
this._messages.push(message);
|
|
62
|
+
}
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* {@inheritdoc IMessageAggregator.addMessages}
|
|
67
|
+
*/
|
|
68
|
+
addMessages(messages) {
|
|
69
|
+
if (messages && messages.length > 0) {
|
|
70
|
+
this._messages.push(...messages);
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritdoc IMessageAggregator.toString}
|
|
76
|
+
*/
|
|
77
|
+
toString(separator) {
|
|
78
|
+
return this._messages.join(separator !== null && separator !== void 0 ? separator : '\n');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* If any error messages have been aggregated, returns
|
|
82
|
+
* {@link Failure | Failure<T>} with the aggregated
|
|
83
|
+
* messages concatenated using the optionally-supplied
|
|
84
|
+
* separator, or newline. If the supplied {@link Result | Result<T>}
|
|
85
|
+
* contains an error message that has not already been aggregated,
|
|
86
|
+
* it will be included in the aggregated messages.
|
|
87
|
+
*
|
|
88
|
+
* If no error messages have been aggregated, returns
|
|
89
|
+
* the supplied {@link Result | Result<T>}.
|
|
90
|
+
* @param result - The {@link Result | Result<T>} to be returned
|
|
91
|
+
* if no messages have been aggregated.
|
|
92
|
+
* @param separator - Optional string separator used to construct
|
|
93
|
+
* the error message.
|
|
94
|
+
* @returns {@link Failure | Failure<T>} with an aggregated message
|
|
95
|
+
* if any error messages were collected, the supplied
|
|
96
|
+
* {@link Result | Result<T>} otherwise.
|
|
97
|
+
*/
|
|
98
|
+
returnOrReport(result, separator) {
|
|
99
|
+
if (!this.hasMessages) {
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
if (!result.success) {
|
|
103
|
+
if (!this._messages.find((s) => s === result.message)) {
|
|
104
|
+
return fail([...this._messages, result.message].join(separator !== null && separator !== void 0 ? separator : '\n'));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return fail(this.toString(separator));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=messageAggregator.js.map
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021 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 { mapResults } from './mapResults';
|
|
23
|
+
import { fail, succeed } from './result';
|
|
24
|
+
/**
|
|
25
|
+
* Normalizes an arbitrary JSON object
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class Normalizer {
|
|
29
|
+
/**
|
|
30
|
+
* Normalizes the supplied value
|
|
31
|
+
*
|
|
32
|
+
* @param from - The value to be normalized
|
|
33
|
+
* @returns A normalized version of the value
|
|
34
|
+
*/
|
|
35
|
+
normalize(from) {
|
|
36
|
+
switch (typeof from) {
|
|
37
|
+
case 'string':
|
|
38
|
+
case 'bigint':
|
|
39
|
+
case 'boolean':
|
|
40
|
+
case 'number':
|
|
41
|
+
case 'symbol':
|
|
42
|
+
case 'undefined':
|
|
43
|
+
return this.normalizeLiteral(from);
|
|
44
|
+
case 'object':
|
|
45
|
+
if (from === null || from instanceof Date || from instanceof RegExp) {
|
|
46
|
+
return this.normalizeLiteral(from);
|
|
47
|
+
}
|
|
48
|
+
else if (Array.isArray(from)) {
|
|
49
|
+
return this._normalizeArray(from);
|
|
50
|
+
}
|
|
51
|
+
else if (from instanceof Map) {
|
|
52
|
+
return succeed(new Map(this.normalizeEntries(from.entries())));
|
|
53
|
+
}
|
|
54
|
+
else if (from instanceof Set) {
|
|
55
|
+
return succeed(new Set(this.normalizeEntries(from.entries())));
|
|
56
|
+
}
|
|
57
|
+
const obj = {};
|
|
58
|
+
for (const e of this.normalizeEntries(Object.entries(from))) {
|
|
59
|
+
obj[e[0]] = e[1];
|
|
60
|
+
}
|
|
61
|
+
return succeed(obj);
|
|
62
|
+
}
|
|
63
|
+
return fail(`normalize: Unexpected type - cannot normalize '${typeof from}'`);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Compares two property names from some object being normalized.
|
|
67
|
+
* @param k1 - First key to be compared.
|
|
68
|
+
* @param k2 - Second key to be compared.
|
|
69
|
+
* @returns `1` if `k1` is greater, `-1` if `k2` is greater and
|
|
70
|
+
* `0` if they are equal.
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
_compareKeys(k1, k2) {
|
|
74
|
+
const cs1 = String(k1);
|
|
75
|
+
const cs2 = String(k2);
|
|
76
|
+
if (cs1 > cs2) {
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
if (cs2 > cs1) {
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
/* c8 ignore next 2 */
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
|
|
87
|
+
* @remarks
|
|
88
|
+
* Converts property names (entry key) to string and then sorts as string.
|
|
89
|
+
* @param entries - The entries to be normalized.
|
|
90
|
+
* @returns A normalized sorted array of entries.
|
|
91
|
+
*/
|
|
92
|
+
normalizeEntries(entries) {
|
|
93
|
+
return Array.from(entries)
|
|
94
|
+
.sort((e1, e2) => this._compareKeys(e1[0], e2[0]))
|
|
95
|
+
.map((e) => [e[0], this.normalize(e[1])])
|
|
96
|
+
.filter((e) => e[1].isSuccess())
|
|
97
|
+
.map((e) => [e[0], e[1].orThrow()]);
|
|
98
|
+
}
|
|
99
|
+
_normalizeArray(from) {
|
|
100
|
+
return mapResults(from.map((v) => this.normalize(v)));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Normalizes the supplied literal value
|
|
104
|
+
* @param from - The literal value to be normalized.
|
|
105
|
+
* @returns A normalized value for the literal.
|
|
106
|
+
*/
|
|
107
|
+
normalizeLiteral(from) {
|
|
108
|
+
// TODO: Apply configurable normalization rules
|
|
109
|
+
switch (typeof from) {
|
|
110
|
+
case 'string':
|
|
111
|
+
return succeed(from);
|
|
112
|
+
case 'bigint':
|
|
113
|
+
case 'boolean':
|
|
114
|
+
case 'number':
|
|
115
|
+
case 'symbol':
|
|
116
|
+
case 'undefined':
|
|
117
|
+
return succeed(from);
|
|
118
|
+
}
|
|
119
|
+
if (from === null) {
|
|
120
|
+
return succeed(from);
|
|
121
|
+
}
|
|
122
|
+
if (from instanceof Date) {
|
|
123
|
+
return succeed(from);
|
|
124
|
+
}
|
|
125
|
+
if (from instanceof RegExp) {
|
|
126
|
+
return succeed(from);
|
|
127
|
+
}
|
|
128
|
+
/* c8 ignore next 2 */
|
|
129
|
+
return fail(`cannot normalize ${JSON.stringify(from)}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=normalize.js.map
|