@fgv/ts-utils 3.0.1-alpha.3 → 3.0.1-alpha.5

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.
@@ -1,5 +1,7 @@
1
1
  import * as Logging from './logger';
2
2
  export * from './brand';
3
+ export * from './mapResults';
4
+ export * from './messageAggregator';
3
5
  export { Normalizer } from './normalize';
4
6
  export * from './result';
5
7
  export * from './utils';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,OAAO,MAAM,UAAU,CAAC;AAEpC,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,OAAO,MAAM,UAAU,CAAC;AAEpC,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -51,6 +51,8 @@ exports.Logging = exports.Normalizer = void 0;
51
51
  const Logging = __importStar(require("./logger"));
52
52
  exports.Logging = Logging;
53
53
  __exportStar(require("./brand"), exports);
54
+ __exportStar(require("./mapResults"), exports);
55
+ __exportStar(require("./messageAggregator"), exports);
54
56
  var normalize_1 = require("./normalize");
55
57
  Object.defineProperty(exports, "Normalizer", { enumerable: true, get: function () { return normalize_1.Normalizer; } });
56
58
  __exportStar(require("./result"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/base/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,kDAAoC;AAM3B,0BAAO;AAJhB,0CAAwB;AACxB,yCAAyC;AAAhC,uGAAA,UAAU,OAAA;AACnB,2CAAyB;AACzB,0CAAwB","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport * as Logging from './logger';\n\nexport * from './brand';\nexport { Normalizer } from './normalize';\nexport * from './result';\nexport * from './utils';\nexport { Logging };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/base/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,kDAAoC;AAQ3B,0BAAO;AANhB,0CAAwB;AACxB,+CAA6B;AAC7B,sDAAoC;AACpC,yCAAyC;AAAhC,uGAAA,UAAU,OAAA;AACnB,2CAAyB;AACzB,0CAAwB","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport * as Logging from './logger';\n\nexport * from './brand';\nexport * from './mapResults';\nexport * from './messageAggregator';\nexport { Normalizer } from './normalize';\nexport * from './result';\nexport * from './utils';\nexport { Logging };\n"]}
@@ -0,0 +1,120 @@
1
+ import { DetailedResult, IMessageAggregator, Result } from './result';
2
+ /**
3
+ * Aggregates successful result values from a collection of {@link Result | Result<T>}.
4
+ * @param results - The collection of {@link Result | Result<T>} to be mapped.
5
+ * @param aggregatedErrors - Optional string array to which any error messages will be
6
+ * appended. Each error is appended as an individual string.
7
+ * @returns If all {@link Result | results} are successful, returns {@link Success} with an
8
+ * array containing all returned values. If any {@link Result | results} failed, returns
9
+ * {@link Failure} with a concatenated summary of all error messages.
10
+ * @public
11
+ */
12
+ export declare function mapResults<T>(results: Iterable<Result<T>>, aggregatedErrors?: IMessageAggregator): Result<T[]>;
13
+ /**
14
+ * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
15
+ * optionally ignoring certain error details.
16
+ * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
17
+ * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
18
+ * @param aggregatedErrors - Optional string array to which any non-ignorable error messages will be
19
+ * appended. Each error is appended as an individual string.
20
+ * @returns {@link Success} with an array containing all successful results if all results either
21
+ * succeeded or returned error details listed in `ignore`. If any results failed with details
22
+ * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
23
+ * error messages.
24
+ * @public
25
+ */
26
+ export declare function mapDetailedResults<T, TD>(results: Iterable<DetailedResult<T, TD>>, ignore: TD[], aggregatedErrors?: IMessageAggregator): Result<T[]>;
27
+ /**
28
+ * Aggregates successful results from a a collection of {@link Result | Result<T>}.
29
+ * @param results - An `Iterable` of {@link Result | Result<T>} from which success
30
+ * results are to be aggregated.
31
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
32
+ * appended. Each error is appended as an individual string.
33
+ * @returns {@link Success} with an array of `<T>` if any results were successful. If
34
+ * all {@link Result | results} failed, returns {@link Failure} with a concatenated
35
+ * summary of all error messages.
36
+ * @public
37
+ */
38
+ export declare function mapSuccess<T>(results: Iterable<Result<T>>, aggregatedErrors?: IMessageAggregator): Result<T[]>;
39
+ /**
40
+ * Aggregates error messages from a collection of {@link Result | Result<T>}.
41
+ * @param results - An iterable collection of {@link Result | Result<T>} for which
42
+ * error messages are aggregated.
43
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
44
+ * appended. Each error is appended as an individual string.
45
+ * @returns An array of strings consisting of all error messages returned by
46
+ * {@link Result | results} in the source collection. Ignores {@link Success}
47
+ * results and returns an empty array if there were no errors.
48
+ * @public
49
+ */
50
+ export declare function mapFailures<T>(results: Iterable<Result<T>>, aggregatedErrors?: IMessageAggregator): string[];
51
+ /**
52
+ * Determines if an iterable collection of {@link Result | Result<T>} were all successful.
53
+ * @param results - The collection of {@link Result | Result<T>} to be tested.
54
+ * @param successValue - The value to be returned if results are successful.
55
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
56
+ * appended. Each error is appended as an individual string.
57
+ * @returns Returns {@link Success} with `successValue` if all {@link Result | results} are successful.
58
+ * If any are unsuccessful, returns {@link Failure} with a concatenated summary of the error
59
+ * messages from all failed elements.
60
+ * @public
61
+ */
62
+ export declare function allSucceed<T>(results: Iterable<Result<unknown>>, successValue: T, aggregatedErrors?: IMessageAggregator): Result<T>;
63
+ /**
64
+ * String-keyed record of initialization functions to be passed to {@link (populateObject:1)}
65
+ * or {@link (populateObject:2)}.
66
+ * @public
67
+ */
68
+ export type FieldInitializers<T> = {
69
+ [key in keyof T]: (state: Partial<T>) => Result<T[key]>;
70
+ };
71
+ /**
72
+ * Options for the {@link (populateObject:1)} function.
73
+ * @public
74
+ */
75
+ export interface PopulateObjectOptions<T> {
76
+ /**
77
+ * If present, specifies the order in which property values should
78
+ * be evaluated. Any keys not listed are evaluated after all listed
79
+ * keys in indeterminate order. If 'order' is not present, keys
80
+ * are evaluated in indeterminate order.
81
+ */
82
+ order?: (keyof T)[];
83
+ /**
84
+ * Specify handling of `undefined` values. By default, successful
85
+ * `undefined` results are written to the result object. If this value
86
+ * is `true` then `undefined` results are suppressed for all properties.
87
+ * If this value is an array of property keys then `undefined` results
88
+ * are suppressed for those properties only.
89
+ */
90
+ suppressUndefined?: boolean | (keyof T)[];
91
+ }
92
+ /**
93
+ * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.
94
+ * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a
95
+ * concatenated list of all error messages.
96
+ * @param initializers - An object with the shape of the target but with initializer functions for
97
+ * each property.
98
+ * @param options - An optional {@link PopulateObjectOptions | set of options} which
99
+ * modify the behavior of this call.
100
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
101
+ * appended. Each error is appended as an individual string.
102
+ * {@label WITH_OPTIONS}
103
+ * @public
104
+ */
105
+ export declare function populateObject<T>(initializers: FieldInitializers<T>, options?: PopulateObjectOptions<T>, aggregatedErrors?: IMessageAggregator): Result<T>;
106
+ /**
107
+ * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.
108
+ * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a
109
+ * concatenated list of all error messages.
110
+ * @param initializers - An object with the shape of the target but with initializer functions for
111
+ * each property.
112
+ * @param order - Optional order in which keys should be written.
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
+ * @public
116
+ * {@label WITH_ORDER}
117
+ * @deprecated Pass {@link PopulateObjectOptions} instead.
118
+ */
119
+ export declare function populateObject<T>(initializers: FieldInitializers<T>, order: (keyof T)[] | undefined, aggregatedErrors?: IMessageAggregator): Result<T>;
120
+ //# sourceMappingURL=mapResults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapResults.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/mapResults.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,EAAiB,MAAM,UAAU,CAAC;AAErF;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC1B,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAC5B,gBAAgB,CAAC,EAAE,kBAAkB,GACpC,MAAM,CAAC,CAAC,EAAE,CAAC,CAiBb;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,EAAE,EACtC,OAAO,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EACxC,MAAM,EAAE,EAAE,EAAE,EACZ,gBAAgB,CAAC,EAAE,kBAAkB,GACpC,MAAM,CAAC,CAAC,EAAE,CAAC,CAiBb;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,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CAAC,CAAC,EAC9B,YAAY,EAAE,iBAAiB,CAAC,CAAC,CAAC,EAClC,KAAK,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,SAAS,EAC9B,gBAAgB,CAAC,EAAE,kBAAkB,GACpC,MAAM,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2020 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.populateObject = exports.allSucceed = exports.mapFailures = exports.mapSuccess = exports.mapDetailedResults = exports.mapResults = void 0;
25
+ const result_1 = require("./result");
26
+ /**
27
+ * Aggregates successful result values from a collection of {@link Result | Result<T>}.
28
+ * @param results - The collection of {@link Result | Result<T>} to be mapped.
29
+ * @param aggregatedErrors - Optional string array to which any error messages will be
30
+ * appended. Each error is appended as an individual string.
31
+ * @returns If all {@link Result | results} are successful, returns {@link Success} with an
32
+ * array containing all returned values. If any {@link Result | results} failed, returns
33
+ * {@link Failure} with a concatenated summary of all error messages.
34
+ * @public
35
+ */
36
+ function mapResults(results, aggregatedErrors) {
37
+ const errors = [];
38
+ const elements = [];
39
+ for (const result of results) {
40
+ if (result.isSuccess()) {
41
+ elements.push(result.value);
42
+ }
43
+ else {
44
+ errors.push(result.message);
45
+ }
46
+ }
47
+ if (errors.length > 0) {
48
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
49
+ return (0, result_1.fail)(errors.join('\n'));
50
+ }
51
+ return (0, result_1.succeed)(elements);
52
+ }
53
+ exports.mapResults = mapResults;
54
+ /**
55
+ * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
56
+ * optionally ignoring certain error details.
57
+ * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
58
+ * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
59
+ * @param aggregatedErrors - Optional string array to which any non-ignorable error messages will be
60
+ * appended. Each error is appended as an individual string.
61
+ * @returns {@link Success} with an array containing all successful results if all results either
62
+ * succeeded or returned error details listed in `ignore`. If any results failed with details
63
+ * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
64
+ * error messages.
65
+ * @public
66
+ */
67
+ function mapDetailedResults(results, ignore, aggregatedErrors) {
68
+ const errors = [];
69
+ const elements = [];
70
+ for (const result of results) {
71
+ if (result.isSuccess()) {
72
+ elements.push(result.value);
73
+ }
74
+ else if (!ignore.includes(result.detail)) {
75
+ errors.push(result.message);
76
+ }
77
+ }
78
+ if (errors.length > 0) {
79
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
80
+ return (0, result_1.fail)(errors.join('\n'));
81
+ }
82
+ return (0, result_1.succeed)(elements);
83
+ }
84
+ exports.mapDetailedResults = mapDetailedResults;
85
+ /**
86
+ * Aggregates successful results from a a collection of {@link Result | Result<T>}.
87
+ * @param results - An `Iterable` of {@link Result | Result<T>} from which success
88
+ * results are to be aggregated.
89
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
90
+ * appended. Each error is appended as an individual string.
91
+ * @returns {@link Success} with an array of `<T>` if any results were successful. If
92
+ * all {@link Result | results} failed, returns {@link Failure} with a concatenated
93
+ * summary of all error messages.
94
+ * @public
95
+ */
96
+ function mapSuccess(results, aggregatedErrors) {
97
+ const errors = [];
98
+ const elements = [];
99
+ for (const result of results) {
100
+ if (result.isSuccess()) {
101
+ elements.push(result.value);
102
+ }
103
+ else {
104
+ errors.push(result.message);
105
+ }
106
+ }
107
+ if (elements.length === 0 && errors.length > 0) {
108
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
109
+ return (0, result_1.fail)(errors.join('\n'));
110
+ }
111
+ return (0, result_1.succeed)(elements);
112
+ }
113
+ exports.mapSuccess = mapSuccess;
114
+ /**
115
+ * Aggregates error messages from a collection of {@link Result | Result<T>}.
116
+ * @param results - An iterable collection of {@link Result | Result<T>} for which
117
+ * error messages are aggregated.
118
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
119
+ * appended. Each error is appended as an individual string.
120
+ * @returns An array of strings consisting of all error messages returned by
121
+ * {@link Result | results} in the source collection. Ignores {@link Success}
122
+ * results and returns an empty array if there were no errors.
123
+ * @public
124
+ */
125
+ function mapFailures(results, aggregatedErrors) {
126
+ const errors = [];
127
+ for (const result of results) {
128
+ if (result.isFailure()) {
129
+ errors.push(result.message);
130
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessage(result.message);
131
+ }
132
+ }
133
+ return errors;
134
+ }
135
+ exports.mapFailures = mapFailures;
136
+ /**
137
+ * Determines if an iterable collection of {@link Result | Result<T>} were all successful.
138
+ * @param results - The collection of {@link Result | Result<T>} to be tested.
139
+ * @param successValue - The value to be returned if results are successful.
140
+ * @param aggregatedErrors - Optional string array to which any returned error messages will be
141
+ * appended. Each error is appended as an individual string.
142
+ * @returns Returns {@link Success} with `successValue` if all {@link Result | results} are successful.
143
+ * If any are unsuccessful, returns {@link Failure} with a concatenated summary of the error
144
+ * messages from all failed elements.
145
+ * @public
146
+ */
147
+ function allSucceed(results, successValue, aggregatedErrors) {
148
+ const errors = [];
149
+ if (results !== undefined) {
150
+ for (const result of results) {
151
+ if (result.isFailure()) {
152
+ errors.push(result.message);
153
+ }
154
+ }
155
+ }
156
+ if (errors.length > 0) {
157
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
158
+ return (0, result_1.fail)(errors.join('\n'));
159
+ }
160
+ return (0, result_1.succeed)(successValue);
161
+ }
162
+ exports.allSucceed = allSucceed;
163
+ function populateObject(initializers, optionsOrOrder, aggregatedErrors) {
164
+ var _a;
165
+ const options = optionsOrOrder
166
+ ? Array.isArray(optionsOrOrder)
167
+ ? { order: optionsOrOrder }
168
+ : optionsOrOrder
169
+ : {};
170
+ const state = {};
171
+ const errors = [];
172
+ const keys = Array.from((_a = options.order) !== null && _a !== void 0 ? _a : []);
173
+ const foundKeys = new Set(options.order);
174
+ // start with the supplied order then append anything else we find
175
+ for (const key in initializers) {
176
+ if (!foundKeys.has(key)) {
177
+ keys.push(key);
178
+ foundKeys.add(key);
179
+ }
180
+ }
181
+ for (const key of keys) {
182
+ if (initializers[key]) {
183
+ const result = initializers[key](state);
184
+ if (result.isSuccess()) {
185
+ if (result.value === undefined) {
186
+ if (options.suppressUndefined === true ||
187
+ (Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))) {
188
+ continue;
189
+ }
190
+ }
191
+ state[key] = result.value;
192
+ }
193
+ else {
194
+ errors.push(result.message);
195
+ }
196
+ }
197
+ else {
198
+ errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);
199
+ }
200
+ }
201
+ if (errors.length > 0) {
202
+ aggregatedErrors === null || aggregatedErrors === void 0 ? void 0 : aggregatedErrors.addMessages(errors);
203
+ return (0, result_1.fail)(errors.join('\n'));
204
+ }
205
+ return (0, result_1.succeed)(state);
206
+ }
207
+ exports.populateObject = populateObject;
208
+ //# sourceMappingURL=mapResults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapResults.js","sourceRoot":"","sources":["../../../src/packlets/base/mapResults.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,qCAAqF;AAErF;;;;;;;;;GASG;AACH,SAAgB,UAAU,CACxB,OAA4B,EAC5B,gBAAqC;IAErC,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,QAAQ,GAAQ,EAAE,CAAC;IAEzB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,CAAC;YACvB,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,gBAAgB,aAAhB,gBAAgB,uBAAhB,gBAAgB,CAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QACtC,OAAO,IAAA,aAAI,EAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACjC,CAAC;IACD,OAAO,IAAA,gBAAO,EAAC,QAAQ,CAAC,CAAC;AAC3B,CAAC;AApBD,gCAoBC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,kBAAkB,CAChC,OAAwC,EACxC,MAAY,EACZ,gBAAqC;IAErC,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,QAAQ,GAAQ,EAAE,CAAC;IAEzB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,IAA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MAAM,CAAC,KAAK,CAAC;YAC5B,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,IAAI,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;QACtF,CAAC;IACH,CAAC;IAED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,gBAAgB,aAAhB,gBAAgB,uBAAhB,gBAAgB,CAAE,WAAW,CAAC,MAAM,CAAC,CAAC;QACtC,OAAO,IAAA,aAAI,EAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACjC,CAAC;IACD,OAAO,IAAA,gBAAO,EAAC,KAAU,CAAC,CAAC;AAC7B,CAAC;AAjDD,wCAiDC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { DetailedResult, IMessageAggregator, Result, fail, succeed } from './result';\n\n/**\n * Aggregates successful result values from a collection of {@link Result | Result<T>}.\n * @param results - The collection of {@link Result | Result<T>} to be mapped.\n * @param aggregatedErrors - Optional string array to which any error messages will be\n * appended. Each error is appended as an individual string.\n * @returns If all {@link Result | results} are successful, returns {@link Success} with an\n * array containing all returned values. If any {@link Result | results} failed, returns\n * {@link Failure} with a concatenated summary of all error messages.\n * @public\n */\nexport function mapResults<T>(\n results: Iterable<Result<T>>,\n aggregatedErrors?: IMessageAggregator\n): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else {\n errors.push(result.message);\n }\n }\n\n if (errors.length > 0) {\n aggregatedErrors?.addMessages(errors);\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates successful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},\n * optionally ignoring certain error details.\n * @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.\n * @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.\n * @param aggregatedErrors - Optional string array to which any non-ignorable error messages will be\n * appended. Each error is appended as an individual string.\n * @returns {@link Success} with an array containing all successful results if all results either\n * succeeded or returned error details listed in `ignore`. If any results failed with details\n * that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable\n * error messages.\n * @public\n */\nexport function mapDetailedResults<T, TD>(\n results: Iterable<DetailedResult<T, TD>>,\n ignore: TD[],\n aggregatedErrors?: IMessageAggregator\n): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else if (!ignore.includes(result.detail)) {\n errors.push(result.message);\n }\n }\n\n if (errors.length > 0) {\n aggregatedErrors?.addMessages(errors);\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates successful results from a a collection of {@link Result | Result<T>}.\n * @param results - An `Iterable` of {@link Result | Result<T>} from which success\n * results are to be aggregated.\n * @param aggregatedErrors - Optional string array to which any returned error messages will be\n * appended. Each error is appended as an individual string.\n * @returns {@link Success} with an array of `<T>` if any results were successful. If\n * all {@link Result | results} failed, returns {@link Failure} with a concatenated\n * summary of all error messages.\n * @public\n */\nexport function mapSuccess<T>(\n results: Iterable<Result<T>>,\n aggregatedErrors?: IMessageAggregator\n): Result<T[]> {\n const errors: string[] = [];\n const elements: T[] = [];\n\n for (const result of results) {\n if (result.isSuccess()) {\n elements.push(result.value);\n } else {\n errors.push(result.message);\n }\n }\n\n if (elements.length === 0 && errors.length > 0) {\n aggregatedErrors?.addMessages(errors);\n return fail(errors.join('\\n'));\n }\n return succeed(elements);\n}\n\n/**\n * Aggregates error messages from a collection of {@link Result | Result<T>}.\n * @param results - An iterable collection of {@link Result | Result<T>} for which\n * error messages are aggregated.\n * @param aggregatedErrors - Optional string array to which any returned error messages will be\n * appended. Each error is appended as an individual string.\n * @returns An array of strings consisting of all error messages returned by\n * {@link Result | results} in the source collection. Ignores {@link Success}\n * results and returns an empty array if there were no errors.\n * @public\n */\nexport function mapFailures<T>(\n results: Iterable<Result<T>>,\n aggregatedErrors?: IMessageAggregator\n): string[] {\n const errors: string[] = [];\n for (const result of results) {\n if (result.isFailure()) {\n errors.push(result.message);\n aggregatedErrors?.addMessage(result.message);\n }\n }\n return errors;\n}\n\n/**\n * Determines if an iterable collection of {@link Result | Result<T>} were all successful.\n * @param results - The collection of {@link Result | Result<T>} to be tested.\n * @param successValue - The value to be returned if results are successful.\n * @param aggregatedErrors - Optional string array to which any returned error messages will be\n * appended. Each error is appended as an individual string.\n * @returns Returns {@link Success} with `successValue` if all {@link Result | results} are successful.\n * If any are unsuccessful, returns {@link Failure} with a concatenated summary of the error\n * messages from all failed elements.\n * @public\n */\nexport function allSucceed<T>(\n results: Iterable<Result<unknown>>,\n successValue: T,\n aggregatedErrors?: IMessageAggregator\n): Result<T> {\n const errors: string[] = [];\n\n if (results !== undefined) {\n for (const result of results) {\n if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n }\n\n if (errors.length > 0) {\n aggregatedErrors?.addMessages(errors);\n return fail(errors.join('\\n'));\n }\n return succeed(successValue);\n}\n\n/**\n * String-keyed record of initialization functions to be passed to {@link (populateObject:1)}\n * or {@link (populateObject:2)}.\n * @public\n */\nexport type FieldInitializers<T> = { [key in keyof T]: (state: Partial<T>) => Result<T[key]> };\n\n/**\n * Options for the {@link (populateObject:1)} function.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface PopulateObjectOptions<T> {\n /**\n * If present, specifies the order in which property values should\n * be evaluated. Any keys not listed are evaluated after all listed\n * keys in indeterminate order. If 'order' is not present, keys\n * are evaluated in indeterminate order.\n */\n order?: (keyof T)[];\n\n /**\n * Specify handling of `undefined` values. By default, successful\n * `undefined` results are written to the result object. If this value\n * is `true` then `undefined` results are suppressed for all properties.\n * If this value is an array of property keys then `undefined` results\n * are suppressed for those properties only.\n */\n suppressUndefined?: boolean | (keyof T)[];\n}\n\n/**\n * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.\n * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a\n * concatenated list of all error messages.\n * @param initializers - An object with the shape of the target but with initializer functions for\n * each property.\n * @param options - An optional {@link PopulateObjectOptions | set of options} which\n * modify the behavior of this call.\n * @param aggregatedErrors - Optional string array to which any returned error messages will be\n * appended. Each error is appended as an individual string.\n * {@label WITH_OPTIONS}\n * @public\n */\nexport function populateObject<T>(\n initializers: FieldInitializers<T>,\n options?: PopulateObjectOptions<T>,\n aggregatedErrors?: IMessageAggregator\n): Result<T>;\n\n/**\n * Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.\n * Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a\n * concatenated list of all error messages.\n * @param initializers - An object with the shape of the target but with initializer functions for\n * each property.\n * @param order - Optional order in which keys should be written.\n * @param aggregatedErrors - Optional string array to which any returned error messages will be\n * appended. Each error is appended as an individual string.\n * @public\n * {@label WITH_ORDER}\n * @deprecated Pass {@link PopulateObjectOptions} instead.\n */\nexport function populateObject<T>(\n initializers: FieldInitializers<T>,\n order: (keyof T)[] | undefined,\n aggregatedErrors?: IMessageAggregator\n): Result<T>;\n\nexport function populateObject<T>(\n initializers: FieldInitializers<T>,\n optionsOrOrder?: PopulateObjectOptions<T> | (keyof T)[],\n aggregatedErrors?: IMessageAggregator\n): Result<T> {\n const options: PopulateObjectOptions<T> = optionsOrOrder\n ? Array.isArray(optionsOrOrder)\n ? { order: optionsOrOrder }\n : optionsOrOrder\n : {};\n const state = {} as { [key in keyof T]: T[key] };\n const errors: string[] = [];\n const keys: (keyof T)[] = Array.from(options.order ?? []);\n const foundKeys = new Set<keyof T>(options.order);\n\n // start with the supplied order then append anything else we find\n for (const key in initializers) {\n if (!foundKeys.has(key)) {\n keys.push(key);\n foundKeys.add(key);\n }\n }\n\n for (const key of keys) {\n if (initializers[key]) {\n const result = initializers[key](state);\n if (result.isSuccess()) {\n if (result.value === undefined) {\n if (\n options.suppressUndefined === true ||\n (Array.isArray(options.suppressUndefined) && options.suppressUndefined.includes(key))\n ) {\n continue;\n }\n }\n state[key] = result.value;\n } else {\n errors.push(result.message);\n }\n } else {\n errors.push(`populateObject: Key ${String(key)} is present but has no initializer`);\n }\n }\n\n if (errors.length > 0) {\n aggregatedErrors?.addMessages(errors);\n return fail(errors.join('\\n'));\n }\n return succeed(state as T);\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import { IMessageAggregator, Result } from './result';
2
+ /**
3
+ * A simple error aggregator to simplify collecting and reporting all errors in
4
+ * a flow.
5
+ * @public
6
+ */
7
+ export declare class MessageAggregator implements IMessageAggregator {
8
+ private readonly _messages;
9
+ /**
10
+ * Constructs a new {@link MessageAggregator | ErrorAggregator} with an
11
+ * optionally specified initial set of error messages.
12
+ * @param errors - optional array of errors to be included
13
+ * in the aggregation.
14
+ */
15
+ constructor(errors?: string[]);
16
+ /**
17
+ * {@inheritdoc IMessageAggregator.hasMessages}
18
+ */
19
+ get hasMessages(): boolean;
20
+ /**
21
+ * {@inheritdoc IMessageAggregator.messages}
22
+ */
23
+ get messages(): string[];
24
+ /**
25
+ * {@inheritdoc IMessageAggregator.addMessage}
26
+ */
27
+ addMessage(message: string | undefined): this;
28
+ /**
29
+ * {@inheritdoc IMessageAggregator.addMessages}
30
+ */
31
+ addMessages(messages: string[] | undefined): this;
32
+ /**
33
+ * {@inheritdoc IMessageAggregator.toString}
34
+ */
35
+ toString(separator?: string): string;
36
+ /**
37
+ * If any error messages have been aggregated, returns
38
+ * {@link Failure | Failure<T>} with the aggregated
39
+ * messages concatenated using the optionally-supplied
40
+ * separator, or newline. If the supplied {@link Result | Result<T>}
41
+ * contains an error message that has not already been aggregated,
42
+ * it will be included in the aggregated messages.
43
+ *
44
+ * If no error messages have been aggregated, returns
45
+ * the supplied {@link Result | Result<T>}.
46
+ * @param result - The {@link Result | Result<T>} to be returned
47
+ * if no messages have been aggregated.
48
+ * @param separator - Optional string separator used to construct
49
+ * the error message.
50
+ * @returns {@link Failure | Failure<T>} with an aggregated message
51
+ * if any error messages were collected, the supplied
52
+ * {@link Result | Result<T>} otherwise.
53
+ */
54
+ returnOrReport<T>(result: Result<T>, separator?: string): Result<T>;
55
+ }
56
+ //# sourceMappingURL=messageAggregator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messageAggregator.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/messageAggregator.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAQ,MAAM,UAAU,CAAC;AAE5D;;;;GAIG;AACH,qBAAa,iBAAkB,YAAW,kBAAkB;IAC1D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IAErC;;;;;OAKG;gBACgB,MAAM,CAAC,EAAE,MAAM,EAAE;IAIpC;;OAEG;IACH,IAAW,WAAW,IAAI,OAAO,CAEhC;IAED;;OAEG;IACH,IAAW,QAAQ,IAAI,MAAM,EAAE,CAE9B;IAED;;OAEG;IACI,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;IAOpD;;OAEG;IACI,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,IAAI;IAOxD;;OAEG;IACI,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3C;;;;;;;;;;;;;;;;;OAiBG;IACI,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;CAW3E"}
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.MessageAggregator = void 0;
25
+ const result_1 = require("./result");
26
+ /**
27
+ * A simple error aggregator to simplify collecting and reporting all errors in
28
+ * a flow.
29
+ * @public
30
+ */
31
+ class MessageAggregator {
32
+ /**
33
+ * Constructs a new {@link MessageAggregator | ErrorAggregator} with an
34
+ * optionally specified initial set of error messages.
35
+ * @param errors - optional array of errors to be included
36
+ * in the aggregation.
37
+ */
38
+ constructor(errors) {
39
+ this._messages = errors ? Array.from(errors) : [];
40
+ }
41
+ /**
42
+ * {@inheritdoc IMessageAggregator.hasMessages}
43
+ */
44
+ get hasMessages() {
45
+ return this._messages.length > 0;
46
+ }
47
+ /**
48
+ * {@inheritdoc IMessageAggregator.messages}
49
+ */
50
+ get messages() {
51
+ return this._messages;
52
+ }
53
+ /**
54
+ * {@inheritdoc IMessageAggregator.addMessage}
55
+ */
56
+ addMessage(message) {
57
+ if (message) {
58
+ this._messages.push(message);
59
+ }
60
+ return this;
61
+ }
62
+ /**
63
+ * {@inheritdoc IMessageAggregator.addMessages}
64
+ */
65
+ addMessages(messages) {
66
+ if (messages && messages.length > 0) {
67
+ this._messages.push(...messages);
68
+ }
69
+ return this;
70
+ }
71
+ /**
72
+ * {@inheritdoc IMessageAggregator.toString}
73
+ */
74
+ toString(separator) {
75
+ return this._messages.join(separator !== null && separator !== void 0 ? separator : '\n');
76
+ }
77
+ /**
78
+ * If any error messages have been aggregated, returns
79
+ * {@link Failure | Failure<T>} with the aggregated
80
+ * messages concatenated using the optionally-supplied
81
+ * separator, or newline. If the supplied {@link Result | Result<T>}
82
+ * contains an error message that has not already been aggregated,
83
+ * it will be included in the aggregated messages.
84
+ *
85
+ * If no error messages have been aggregated, returns
86
+ * the supplied {@link Result | Result<T>}.
87
+ * @param result - The {@link Result | Result<T>} to be returned
88
+ * if no messages have been aggregated.
89
+ * @param separator - Optional string separator used to construct
90
+ * the error message.
91
+ * @returns {@link Failure | Failure<T>} with an aggregated message
92
+ * if any error messages were collected, the supplied
93
+ * {@link Result | Result<T>} otherwise.
94
+ */
95
+ returnOrReport(result, separator) {
96
+ if (!this.hasMessages) {
97
+ return result;
98
+ }
99
+ if (!result.success) {
100
+ if (!this._messages.find((s) => s === result.message)) {
101
+ return (0, result_1.fail)([...this._messages, result.message].join(separator !== null && separator !== void 0 ? separator : '\n'));
102
+ }
103
+ }
104
+ return (0, result_1.fail)(this.toString(separator));
105
+ }
106
+ }
107
+ exports.MessageAggregator = MessageAggregator;
108
+ //# sourceMappingURL=messageAggregator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"messageAggregator.js","sourceRoot":"","sources":["../../../src/packlets/base/messageAggregator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,qCAA4D;AAE5D;;;;GAIG;AACH,MAAa,iBAAiB;IAG5B;;;;;OAKG;IACH,YAAmB,MAAiB;QAClC,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,IAAW,WAAW;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;OAEG;IACI,UAAU,CAAC,OAA2B;QAC3C,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,QAA8B;QAC/C,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,QAAQ,CAAC,SAAkB;QAChC,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,IAAI,CAAC,CAAC;IAChD,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACI,cAAc,CAAI,MAAiB,EAAE,SAAkB;QAC5D,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACtB,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;gBACtD,OAAO,IAAA,aAAI,EAAC,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,IAAI,CAAC,CAAC,CAAC;YAC3E,CAAC;QACH,CAAC;QACD,OAAO,IAAA,aAAI,EAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;IACxC,CAAC;CACF;AAnFD,8CAmFC","sourcesContent":["/*\n * Copyright (c) 2024 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { IMessageAggregator, Result, fail } from './result';\n\n/**\n * A simple error aggregator to simplify collecting and reporting all errors in\n * a flow.\n * @public\n */\nexport class MessageAggregator implements IMessageAggregator {\n private readonly _messages: string[];\n\n /**\n * Constructs a new {@link MessageAggregator | ErrorAggregator} with an\n * optionally specified initial set of error messages.\n * @param errors - optional array of errors to be included\n * in the aggregation.\n */\n public constructor(errors?: string[]) {\n this._messages = errors ? Array.from(errors) : [];\n }\n\n /**\n * {@inheritdoc IMessageAggregator.hasMessages}\n */\n public get hasMessages(): boolean {\n return this._messages.length > 0;\n }\n\n /**\n * {@inheritdoc IMessageAggregator.messages}\n */\n public get messages(): string[] {\n return this._messages;\n }\n\n /**\n * {@inheritdoc IMessageAggregator.addMessage}\n */\n public addMessage(message: string | undefined): this {\n if (message) {\n this._messages.push(message);\n }\n return this;\n }\n\n /**\n * {@inheritdoc IMessageAggregator.addMessages}\n */\n public addMessages(messages: string[] | undefined): this {\n if (messages && messages.length > 0) {\n this._messages.push(...messages);\n }\n return this;\n }\n\n /**\n * {@inheritdoc IMessageAggregator.toString}\n */\n public toString(separator?: string): string {\n return this._messages.join(separator ?? '\\n');\n }\n\n /**\n * If any error messages have been aggregated, returns\n * {@link Failure | Failure<T>} with the aggregated\n * messages concatenated using the optionally-supplied\n * separator, or newline. If the supplied {@link Result | Result<T>}\n * contains an error message that has not already been aggregated,\n * it will be included in the aggregated messages.\n *\n * If no error messages have been aggregated, returns\n * the supplied {@link Result | Result<T>}.\n * @param result - The {@link Result | Result<T>} to be returned\n * if no messages have been aggregated.\n * @param separator - Optional string separator used to construct\n * the error message.\n * @returns {@link Failure | Failure<T>} with an aggregated message\n * if any error messages were collected, the supplied\n * {@link Result | Result<T>} otherwise.\n */\n public returnOrReport<T>(result: Result<T>, separator?: string): Result<T> {\n if (!this.hasMessages) {\n return result;\n }\n if (!result.success) {\n if (!this._messages.find((s) => s === result.message)) {\n return fail([...this._messages, result.message].join(separator ?? '\\n'));\n }\n }\n return fail(this.toString(separator));\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/normalize.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAA6B,MAAM,UAAU,CAAC;AAE7D;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEnE;;;GAGG;AACH,qBAAa,UAAU;IACrB;;;;;OAKG;IACI,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IA4BvC;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM;IAaxD;;;;;;OAMG;IACI,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE;IAQ7E,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IAI7D;;;;OAIG;IACI,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;CAwB/C"}
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../../src/packlets/base/normalize.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,MAAM,EAAiB,MAAM,UAAU,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC,CAAC;AAErD;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;AAEnE;;;GAGG;AACH,qBAAa,UAAU;IACrB;;;;;OAKG;IACI,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IA4BvC;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM;IAaxD;;;;;;OAMG;IACI,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE;IAQ7E,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IAI7D;;;;OAIG;IACI,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;CAwB/C"}
@@ -22,6 +22,7 @@
22
22
  */
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.Normalizer = void 0;
25
+ const mapResults_1 = require("./mapResults");
25
26
  const result_1 = require("./result");
26
27
  /**
27
28
  * Normalizes an arbitrary JSON object
@@ -99,7 +100,7 @@ class Normalizer {
99
100
  .map((e) => [e[0], e[1].orThrow()]);
100
101
  }
101
102
  _normalizeArray(from) {
102
- return (0, result_1.mapResults)(from.map((v) => this.normalize(v)));
103
+ return (0, mapResults_1.mapResults)(from.map((v) => this.normalize(v)));
103
104
  }
104
105
  /**
105
106
  * Normalizes the supplied literal value