@fgv/ts-utils 5.0.2 → 5.1.0-1
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/README.md +152 -84
- package/dist/index.js +2 -2
- package/dist/packlets/base/messageAggregator.js +6 -6
- package/dist/packlets/base/result.js +39 -39
- package/dist/packlets/base/utils.js +47 -0
- package/dist/packlets/collections/aggregatedResultMap.js +722 -0
- package/dist/packlets/collections/collector.js +9 -9
- package/dist/packlets/collections/collectorValidator.js +4 -4
- package/dist/packlets/collections/converters.js +39 -0
- package/dist/packlets/collections/convertingCollectorValidator.js +3 -3
- package/dist/packlets/collections/convertingResultMap.js +208 -0
- package/dist/packlets/collections/index.js +4 -0
- package/dist/packlets/collections/keyValueConverters.js +1 -4
- package/dist/packlets/collections/readOnlyConvertingResultMap.js +193 -0
- package/dist/packlets/collections/resultMapValidator.js +50 -6
- package/dist/packlets/collections/validatingConvertingResultMap.js +78 -0
- package/dist/packlets/conversion/advancedConverters.js +249 -0
- package/dist/packlets/conversion/baseConverter.js +23 -16
- package/dist/packlets/conversion/basicConverters.js +571 -0
- package/dist/packlets/conversion/converters.js +3 -617
- package/dist/packlets/conversion/defaultingConverter.js +21 -15
- package/dist/packlets/logging/bootLogger.js +122 -0
- package/dist/packlets/logging/index.js +1 -0
- package/dist/packlets/logging/logReporter.js +32 -1
- package/dist/packlets/logging/logger.js +25 -5
- package/dist/packlets/validation/classes.js +1 -0
- package/dist/packlets/validation/compositeId.js +60 -0
- package/dist/packlets/validation/field.js +1 -1
- package/dist/packlets/validation/genericValidator.js +24 -10
- package/dist/packlets/validation/object.js +3 -6
- package/dist/packlets/validation/validators.js +12 -0
- package/dist/ts-utils.d.ts +1633 -200
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/packlets/base/mapResults.d.ts +3 -3
- package/lib/packlets/base/messageAggregator.d.ts +6 -6
- package/lib/packlets/base/messageAggregator.js +6 -6
- package/lib/packlets/base/result.d.ts +74 -51
- package/lib/packlets/base/result.js +39 -39
- package/lib/packlets/base/utils.d.ts +42 -0
- package/lib/packlets/base/utils.js +52 -0
- package/lib/packlets/collections/aggregatedResultMap.d.ts +433 -0
- package/lib/packlets/collections/aggregatedResultMap.js +728 -0
- package/lib/packlets/collections/collector.d.ts +11 -10
- package/lib/packlets/collections/collector.js +9 -9
- package/lib/packlets/collections/collectorValidator.d.ts +12 -10
- package/lib/packlets/collections/collectorValidator.js +4 -4
- package/lib/packlets/collections/converters.d.ts +18 -0
- package/lib/packlets/collections/converters.js +42 -0
- package/lib/packlets/collections/convertingCollector.d.ts +4 -4
- package/lib/packlets/collections/convertingCollectorValidator.d.ts +10 -7
- package/lib/packlets/collections/convertingCollectorValidator.js +3 -3
- package/lib/packlets/collections/convertingResultMap.d.ts +176 -0
- package/lib/packlets/collections/convertingResultMap.js +213 -0
- package/lib/packlets/collections/index.d.ts +4 -0
- package/lib/packlets/collections/index.js +4 -0
- package/lib/packlets/collections/keyValueConverters.d.ts +1 -4
- package/lib/packlets/collections/keyValueConverters.js +1 -4
- package/lib/packlets/collections/readOnlyConvertingResultMap.d.ts +153 -0
- package/lib/packlets/collections/readOnlyConvertingResultMap.js +197 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts +7 -7
- package/lib/packlets/collections/resultMap.d.ts +108 -2
- package/lib/packlets/collections/resultMapValidator.d.ts +45 -11
- package/lib/packlets/collections/resultMapValidator.js +52 -7
- package/lib/packlets/collections/validatingCollector.d.ts +5 -5
- package/lib/packlets/collections/validatingConvertingCollector.d.ts +3 -3
- package/lib/packlets/collections/validatingConvertingResultMap.d.ts +102 -0
- package/lib/packlets/collections/validatingConvertingResultMap.js +83 -0
- package/lib/packlets/collections/validatingResultMap.d.ts +1 -1
- package/lib/packlets/conversion/advancedConverters.d.ts +170 -0
- package/lib/packlets/conversion/advancedConverters.js +258 -0
- package/lib/packlets/conversion/baseConverter.d.ts +39 -23
- package/lib/packlets/conversion/baseConverter.js +23 -16
- package/lib/packlets/conversion/basicConverters.d.ts +470 -0
- package/lib/packlets/conversion/basicConverters.js +595 -0
- package/lib/packlets/conversion/converter.d.ts +7 -0
- package/lib/packlets/conversion/converters.d.ts +2 -535
- package/lib/packlets/conversion/converters.js +17 -639
- package/lib/packlets/conversion/defaultingConverter.d.ts +20 -16
- package/lib/packlets/conversion/defaultingConverter.js +21 -15
- package/lib/packlets/logging/bootLogger.d.ts +84 -0
- package/lib/packlets/logging/bootLogger.js +126 -0
- package/lib/packlets/logging/index.d.ts +1 -0
- package/lib/packlets/logging/index.js +1 -0
- package/lib/packlets/logging/logReporter.d.ts +18 -3
- package/lib/packlets/logging/logReporter.js +31 -0
- package/lib/packlets/logging/logger.d.ts +39 -6
- package/lib/packlets/logging/logger.js +26 -5
- package/lib/packlets/validation/classes.d.ts +1 -0
- package/lib/packlets/validation/classes.js +3 -1
- package/lib/packlets/validation/compositeId.d.ts +29 -0
- package/lib/packlets/validation/compositeId.js +64 -0
- package/lib/packlets/validation/field.d.ts +1 -1
- package/lib/packlets/validation/field.js +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +15 -11
- package/lib/packlets/validation/genericValidator.js +24 -10
- package/lib/packlets/validation/object.d.ts +3 -6
- package/lib/packlets/validation/object.js +3 -6
- package/lib/packlets/validation/traits.d.ts +3 -3
- package/lib/packlets/validation/validator.d.ts +7 -0
- package/lib/packlets/validation/validators.d.ts +10 -0
- package/lib/packlets/validation/validators.js +13 -0
- package/package.json +27 -27
package/README.md
CHANGED
|
@@ -1,132 +1,200 @@
|
|
|
1
|
-
|
|
2
|
-
<h1>ts-utils</h1>
|
|
3
|
-
Assorted Typescript Utilities
|
|
4
|
-
</div>
|
|
1
|
+
# @fgv/ts-utils
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
## Summary
|
|
9
|
-
|
|
10
|
-
Assorted typescript utilities that I'm tired of copying from project to project. Most notable and closest to production-ready are:
|
|
11
|
-
* Result\<T\> - Easily combine inline and exception-based error handling
|
|
12
|
-
* Converter\<T\> - Conversion framework especially useful for type-safe processing of JSON
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
- [Summary](#summary)
|
|
17
|
-
- [Installation](#installation)
|
|
18
|
-
- [API Documentation](#api-documentation)
|
|
19
|
-
- [Overview](#overview)
|
|
20
|
-
- [The Result Pattern](#the-result-pattern)
|
|
21
|
-
- [Converters](#converters)
|
|
22
|
-
- [API](#api)
|
|
23
|
-
- [Result\<T\>](#resultt)
|
|
24
|
-
- [Converter\<T\>](#convertert)
|
|
3
|
+
Type-safe utilities for TypeScript — Result pattern, composable converters and validators, and Result-aware collections.
|
|
25
4
|
|
|
26
5
|
## Installation
|
|
27
6
|
|
|
28
|
-
With npm:
|
|
29
7
|
```sh
|
|
30
8
|
npm install @fgv/ts-utils
|
|
31
9
|
```
|
|
32
10
|
|
|
33
|
-
## API Documentation
|
|
34
|
-
Extracted API documentation is [here](./docs/ts-utils.md).
|
|
35
|
-
|
|
36
11
|
## Overview
|
|
37
|
-
### The Result Pattern
|
|
38
12
|
|
|
39
|
-
|
|
13
|
+
`@fgv/ts-utils` provides a cohesive set of utilities built around the Result pattern. Instead of throwing exceptions for expected error conditions, all fallible operations return `Result<T>` — making errors explicit in the type system and enabling safe, composable error handling.
|
|
14
|
+
|
|
15
|
+
The library is organized into six packlets, each with its own detailed documentation.
|
|
16
|
+
|
|
17
|
+
## The Result Pattern
|
|
18
|
+
|
|
19
|
+
A `Result<T>` is either a `Success<T>` containing a value, or a `Failure<T>` containing an error message. This replaces exception-based error handling with explicit, chainable results.
|
|
20
|
+
|
|
21
|
+
### Creating Results
|
|
40
22
|
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
```typescript
|
|
24
|
+
import { Result, succeed, fail, captureResult } from '@fgv/ts-utils';
|
|
25
|
+
|
|
26
|
+
function divide(a: number, b: number): Result<number> {
|
|
27
|
+
if (b === 0) {
|
|
28
|
+
return fail('Division by zero');
|
|
29
|
+
}
|
|
30
|
+
return succeed(a / b);
|
|
45
31
|
}
|
|
46
|
-
|
|
47
|
-
|
|
32
|
+
|
|
33
|
+
// Wrap throwing code
|
|
34
|
+
function parseJson(text: string): Result<unknown> {
|
|
35
|
+
return captureResult(() => JSON.parse(text));
|
|
48
36
|
}
|
|
49
37
|
```
|
|
50
38
|
|
|
51
|
-
|
|
39
|
+
### Extracting Values
|
|
52
40
|
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
41
|
+
```typescript
|
|
42
|
+
// In setup/initialization — converts failure to exception
|
|
43
|
+
const config = loadConfig().orThrow();
|
|
44
|
+
|
|
45
|
+
// Safe fallback
|
|
46
|
+
const port = getPort().orDefault(3000);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Chaining Operations
|
|
57
50
|
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
```typescript
|
|
52
|
+
function processData(input: string): Result<ProcessedData> {
|
|
53
|
+
return parseInput(input)
|
|
54
|
+
.onSuccess((parsed) => validate(parsed))
|
|
55
|
+
.onSuccess((valid) => transform(valid));
|
|
60
56
|
}
|
|
61
57
|
```
|
|
62
58
|
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
// returns undefined on failure
|
|
66
|
-
const value1: string|undefined = functionReturningResult('whatever').orDefault();
|
|
59
|
+
### Error Context
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
61
|
+
```typescript
|
|
62
|
+
function loadUser(id: string): Result<User> {
|
|
63
|
+
return fetchUser(id)
|
|
64
|
+
.withErrorFormat((msg) => `Failed to load user ${id}: ${msg}`);
|
|
65
|
+
}
|
|
70
66
|
```
|
|
71
67
|
|
|
72
|
-
|
|
68
|
+
### Error Aggregation
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { mapResults, MessageAggregator } from '@fgv/ts-utils';
|
|
72
|
+
|
|
73
|
+
// Aggregate array of results
|
|
74
|
+
const results = items.map((item) => processItem(item));
|
|
75
|
+
return mapResults(results); // All must succeed, or errors are aggregated
|
|
73
76
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
// Collect errors manually
|
|
78
|
+
const aggregator = new MessageAggregator();
|
|
79
|
+
validateA(data).aggregateError(aggregator);
|
|
80
|
+
validateB(data).aggregateError(aggregator);
|
|
81
|
+
if (aggregator.hasMessages) {
|
|
82
|
+
return fail(aggregator.toString('; '));
|
|
77
83
|
}
|
|
78
84
|
```
|
|
79
85
|
|
|
80
|
-
|
|
86
|
+
### Factory Pattern
|
|
81
87
|
|
|
82
|
-
```
|
|
83
|
-
class
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
```typescript
|
|
89
|
+
class ResourceManager {
|
|
90
|
+
private constructor(private config: Config) { /* may throw */ }
|
|
91
|
+
|
|
92
|
+
public static create(params: Params): Result<ResourceManager> {
|
|
93
|
+
return validateParams(params)
|
|
94
|
+
.onSuccess((valid) => loadConfig(valid))
|
|
95
|
+
.onSuccess((config) => captureResult(() => new ResourceManager(config)));
|
|
96
|
+
}
|
|
87
97
|
}
|
|
88
98
|
```
|
|
89
99
|
|
|
90
|
-
|
|
100
|
+
### Best Practices
|
|
101
|
+
|
|
102
|
+
- Return `Result<T>` from fallible operations — never throw in business logic
|
|
103
|
+
- Use `orThrow()` only in setup/initialization
|
|
104
|
+
- Prefer chaining over intermediate variables
|
|
105
|
+
- Add context with `withErrorFormat()` as errors propagate
|
|
106
|
+
- Use `MessageAggregator` for collecting multiple errors
|
|
91
107
|
|
|
92
|
-
|
|
108
|
+
[Full Result pattern documentation →](./src/packlets/base/README.md)
|
|
93
109
|
|
|
94
|
-
|
|
110
|
+
## Type-Safe Conversion
|
|
95
111
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
112
|
+
`Converter<T>` converts `unknown` to `T`, returning `Result<T>`. Converters compose to build type-safe pipelines for JSON parsing and data transformation.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { Converters } from '@fgv/ts-utils';
|
|
116
|
+
|
|
117
|
+
interface IConfig {
|
|
118
|
+
host: string;
|
|
119
|
+
port: number;
|
|
120
|
+
debug: boolean;
|
|
121
|
+
tags: string[];
|
|
99
122
|
}
|
|
123
|
+
|
|
124
|
+
const configConverter = Converters.object<IConfig>({
|
|
125
|
+
host: Converters.string,
|
|
126
|
+
port: Converters.number,
|
|
127
|
+
debug: Converters.boolean,
|
|
128
|
+
tags: Converters.arrayOf(Converters.string),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const config = configConverter.convert(json).orThrow();
|
|
100
132
|
```
|
|
101
133
|
|
|
102
|
-
|
|
134
|
+
Converters support a fluent modifier API:
|
|
103
135
|
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
136
|
+
```typescript
|
|
137
|
+
Converters.number
|
|
138
|
+
.withConstraint((n) => n > 0 && n < 65536, { description: 'valid port' })
|
|
139
|
+
.withBrand<'Port'>('Port');
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
[Full conversion documentation →](./src/packlets/conversion/README.md)
|
|
143
|
+
|
|
144
|
+
## In-Place Validation
|
|
145
|
+
|
|
146
|
+
`Validator<T>` verifies a value is of type `T` without creating a new object — preserving object identity and prototype chains.
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { Validators } from '@fgv/ts-utils';
|
|
111
150
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
hints: Converters.array(Converters.string),
|
|
151
|
+
const userValidator = Validators.object<IUser>({
|
|
152
|
+
name: Validators.string,
|
|
153
|
+
age: Validators.number,
|
|
154
|
+
active: Validators.boolean,
|
|
117
155
|
});
|
|
118
156
|
|
|
119
|
-
|
|
120
|
-
const thing: Things = thingConverter.convert(json).orThrow();
|
|
157
|
+
const result = userValidator.validate(input); // Result<IUser>
|
|
121
158
|
```
|
|
122
159
|
|
|
123
|
-
|
|
160
|
+
Converters and Validators interoperate — use either as field definitions in object converters.
|
|
124
161
|
|
|
125
|
-
|
|
162
|
+
[Full validation documentation →](./src/packlets/validation/README.md)
|
|
126
163
|
|
|
127
|
-
##
|
|
164
|
+
## Type-Safe Collections
|
|
128
165
|
|
|
129
|
-
|
|
166
|
+
`ResultMap` provides a `Map`-like API where all operations return `DetailedResult` with discriminated details (`'added'`, `'exists'`, `'not-found'`, etc.):
|
|
130
167
|
|
|
131
|
-
|
|
168
|
+
```typescript
|
|
169
|
+
import { ResultMap } from '@fgv/ts-utils';
|
|
132
170
|
|
|
171
|
+
const map = new ResultMap<string, number>();
|
|
172
|
+
map.add('key', 42); // detail: 'added'
|
|
173
|
+
map.add('key', 99); // detail: 'exists' (failure)
|
|
174
|
+
map.get('key'); // detail: 'exists', value: 42
|
|
175
|
+
map.get('missing'); // detail: 'not-found' (failure)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The collections packlet also provides `Collector` (append-only with write-once indexing), `ValidatingResultMap` (weakly-typed access with validation), `ConvertingResultMap` (lazy conversion with caching), and `AggregatedResultMap` (multi-collection composite IDs).
|
|
179
|
+
|
|
180
|
+
[Full collections documentation →](./src/packlets/collections/README.md)
|
|
181
|
+
|
|
182
|
+
## Additional Utilities
|
|
183
|
+
|
|
184
|
+
- **[Logging](./src/packlets/logging/README.md)** — Level-based logging with `ConsoleLogger`, `InMemoryLogger` (for testing), and `LogReporter` for Result-aware reporting.
|
|
185
|
+
- **[Hashing](./src/packlets/hash/README.md)** — Deterministic hashing of nested objects with `Crc32Normalizer`, producing consistent hashes regardless of property order.
|
|
186
|
+
|
|
187
|
+
## Packlet Reference
|
|
188
|
+
|
|
189
|
+
| Packlet | Description | Documentation |
|
|
190
|
+
|---------|-------------|---------------|
|
|
191
|
+
| `base` | Result pattern, error aggregation, branded types | [README](./src/packlets/base/README.md) |
|
|
192
|
+
| `conversion` | Type-safe converters (`unknown` → `T`) | [README](./src/packlets/conversion/README.md) |
|
|
193
|
+
| `validation` | In-place validators (verify without transforming) | [README](./src/packlets/validation/README.md) |
|
|
194
|
+
| `collections` | Result-aware Map/Collection with validation layers | [README](./src/packlets/collections/README.md) |
|
|
195
|
+
| `logging` | Level-based logging with Result integration | [README](./src/packlets/logging/README.md) |
|
|
196
|
+
| `hash` | Deterministic object hashing | [README](./src/packlets/hash/README.md) |
|
|
197
|
+
|
|
198
|
+
## API Documentation
|
|
199
|
+
|
|
200
|
+
Extracted API documentation is [here](./docs/ts-utils.md).
|
package/dist/index.js
CHANGED
|
@@ -24,9 +24,9 @@ import * as Conversion from './packlets/conversion';
|
|
|
24
24
|
import * as Hash from './packlets/hash';
|
|
25
25
|
import * as Logging from './packlets/logging';
|
|
26
26
|
import * as Validation from './packlets/validation';
|
|
27
|
-
import { Collector, ConvertingCollector, ResultMap, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap } from './packlets/collections';
|
|
27
|
+
import { AggregatedResultMap, Collector, ConvertingCollector, ResultMap, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap } from './packlets/collections';
|
|
28
28
|
import { Converters, ObjectConverter, StringConverter } from './packlets/conversion';
|
|
29
29
|
import { Validators } from './packlets/validation';
|
|
30
30
|
export * from './packlets/base';
|
|
31
|
-
export { Collections, Collector, ConvertingCollector, Conversion, Converters, Hash, Logging, ObjectConverter, ResultMap, StringConverter, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap, Validation, Validators };
|
|
31
|
+
export { AggregatedResultMap, Collections, Collector, ConvertingCollector, Conversion, Converters, Hash, Logging, ObjectConverter, ResultMap, StringConverter, ValidatingCollector, ValidatingConvertingCollector, ValidatingResultMap, Validation, Validators };
|
|
32
32
|
//# sourceMappingURL=index.js.map
|
|
@@ -36,25 +36,25 @@ export class MessageAggregator {
|
|
|
36
36
|
this._messages = errors ? Array.from(errors) : [];
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
* {@
|
|
39
|
+
* {@inheritDoc IMessageAggregator.hasMessages}
|
|
40
40
|
*/
|
|
41
41
|
get hasMessages() {
|
|
42
42
|
return this._messages.length > 0;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
* {@
|
|
45
|
+
* {@inheritDoc IMessageAggregator.numMessages}
|
|
46
46
|
*/
|
|
47
47
|
get numMessages() {
|
|
48
48
|
return this._messages.length;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
* {@
|
|
51
|
+
* {@inheritDoc IMessageAggregator.messages}
|
|
52
52
|
*/
|
|
53
53
|
get messages() {
|
|
54
54
|
return this._messages;
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
|
-
* {@
|
|
57
|
+
* {@inheritDoc IMessageAggregator.addMessage}
|
|
58
58
|
*/
|
|
59
59
|
addMessage(message) {
|
|
60
60
|
if (message) {
|
|
@@ -63,7 +63,7 @@ export class MessageAggregator {
|
|
|
63
63
|
return this;
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
|
-
* {@
|
|
66
|
+
* {@inheritDoc IMessageAggregator.addMessages}
|
|
67
67
|
*/
|
|
68
68
|
addMessages(messages) {
|
|
69
69
|
if (messages && messages.length > 0) {
|
|
@@ -72,7 +72,7 @@ export class MessageAggregator {
|
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
* {@
|
|
75
|
+
* {@inheritDoc IMessageAggregator.toString}
|
|
76
76
|
*/
|
|
77
77
|
toString(separator) {
|
|
78
78
|
return this._messages.join(separator !== null && separator !== void 0 ? separator : '\n');
|
|
@@ -40,7 +40,7 @@ export class Success {
|
|
|
40
40
|
*/
|
|
41
41
|
constructor(value) {
|
|
42
42
|
/**
|
|
43
|
-
* {@
|
|
43
|
+
* {@inheritDoc IResult.success}
|
|
44
44
|
*/
|
|
45
45
|
this.success = true;
|
|
46
46
|
/**
|
|
@@ -56,13 +56,13 @@ export class Success {
|
|
|
56
56
|
return this._value;
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
* {@
|
|
59
|
+
* {@inheritDoc IResult.isSuccess}
|
|
60
60
|
*/
|
|
61
61
|
isSuccess() {
|
|
62
62
|
return true;
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
-
* {@
|
|
65
|
+
* {@inheritDoc IResult.isFailure}
|
|
66
66
|
*/
|
|
67
67
|
isFailure() {
|
|
68
68
|
return false;
|
|
@@ -75,58 +75,58 @@ export class Success {
|
|
|
75
75
|
return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
|
-
* {@
|
|
79
|
-
* @deprecated Use {@link Success.
|
|
78
|
+
* {@inheritDoc IResult.getValueOrThrow}
|
|
79
|
+
* @deprecated Use {@link Success.orThrow | orThrow(logger)} or {@link Success.orThrow | orThrow(formatter)} instead.
|
|
80
80
|
*/
|
|
81
81
|
getValueOrThrow(__logger) {
|
|
82
82
|
return this._value;
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
|
-
* {@
|
|
86
|
-
* @deprecated Use {@link Success.
|
|
85
|
+
* {@inheritDoc IResult.getValueOrDefault}
|
|
86
|
+
* @deprecated Use {@link Success.orDefault | orDefault(T)} or {@link Success.orDefault | orDefault()} instead.
|
|
87
87
|
*/
|
|
88
88
|
getValueOrDefault(dflt) {
|
|
89
89
|
var _a;
|
|
90
90
|
return (_a = this._value) !== null && _a !== void 0 ? _a : dflt;
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
93
|
-
* {@
|
|
93
|
+
* {@inheritDoc IResult.onSuccess}
|
|
94
94
|
*/
|
|
95
95
|
onSuccess(cb) {
|
|
96
96
|
return cb(this._value);
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
99
|
-
* {@
|
|
99
|
+
* {@inheritDoc IResult.onFailure}
|
|
100
100
|
*/
|
|
101
101
|
onFailure(__) {
|
|
102
102
|
return this;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
* {@
|
|
105
|
+
* {@inheritDoc IResult.withErrorFormat}
|
|
106
106
|
*/
|
|
107
107
|
withErrorFormat(__cb) {
|
|
108
108
|
return this;
|
|
109
109
|
}
|
|
110
110
|
/**
|
|
111
|
-
* {@
|
|
111
|
+
* {@inheritDoc IResult.withFailureDetail}
|
|
112
112
|
*/
|
|
113
113
|
withFailureDetail(__detail) {
|
|
114
114
|
return succeedWithDetail(this._value);
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
117
|
-
* {@
|
|
117
|
+
* {@inheritDoc IResult.withDetail}
|
|
118
118
|
*/
|
|
119
119
|
withDetail(detail, successDetail) {
|
|
120
120
|
return succeedWithDetail(this._value, successDetail !== null && successDetail !== void 0 ? successDetail : detail);
|
|
121
121
|
}
|
|
122
122
|
/**
|
|
123
|
-
* {@
|
|
123
|
+
* {@inheritDoc IResult.aggregateError}
|
|
124
124
|
*/
|
|
125
125
|
aggregateError(__errors, __formatter) {
|
|
126
126
|
return this;
|
|
127
127
|
}
|
|
128
128
|
/**
|
|
129
|
-
* {@
|
|
129
|
+
* {@inheritDoc IResult.report}
|
|
130
130
|
*/
|
|
131
131
|
report(reporter, options) {
|
|
132
132
|
var _a;
|
|
@@ -156,7 +156,7 @@ export class Failure {
|
|
|
156
156
|
*/
|
|
157
157
|
constructor(message) {
|
|
158
158
|
/**
|
|
159
|
-
* {@
|
|
159
|
+
* {@inheritDoc IResult.success}
|
|
160
160
|
*/
|
|
161
161
|
this.success = false;
|
|
162
162
|
/**
|
|
@@ -172,13 +172,13 @@ export class Failure {
|
|
|
172
172
|
return this._message;
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
|
-
* {@
|
|
175
|
+
* {@inheritDoc IResult.isSuccess}
|
|
176
176
|
*/
|
|
177
177
|
isSuccess() {
|
|
178
178
|
return false;
|
|
179
179
|
}
|
|
180
180
|
/**
|
|
181
|
-
* {@
|
|
181
|
+
* {@inheritDoc IResult.isFailure}
|
|
182
182
|
*/
|
|
183
183
|
isFailure() {
|
|
184
184
|
return true;
|
|
@@ -198,8 +198,8 @@ export class Failure {
|
|
|
198
198
|
return dflt;
|
|
199
199
|
}
|
|
200
200
|
/**
|
|
201
|
-
* {@
|
|
202
|
-
* @deprecated Use {@link Failure.
|
|
201
|
+
* {@inheritDoc IResult.getValueOrThrow}
|
|
202
|
+
* @deprecated Use {@link Failure.orThrow | orThrow(logger)} or {@link Failure.orThrow | orThrow(formatter)} instead.
|
|
203
203
|
*/
|
|
204
204
|
getValueOrThrow(logger) {
|
|
205
205
|
if (logger !== undefined) {
|
|
@@ -208,44 +208,44 @@ export class Failure {
|
|
|
208
208
|
throw new Error(this._message);
|
|
209
209
|
}
|
|
210
210
|
/**
|
|
211
|
-
* {@
|
|
212
|
-
* @deprecated Use {@link Failure.
|
|
211
|
+
* {@inheritDoc IResult.getValueOrDefault}
|
|
212
|
+
* @deprecated Use {@link Failure.orDefault | orDefault(T)} or {@link Failure.orDefault | orDefault()} instead.
|
|
213
213
|
*/
|
|
214
214
|
getValueOrDefault(dflt) {
|
|
215
215
|
return dflt;
|
|
216
216
|
}
|
|
217
217
|
/**
|
|
218
|
-
* {@
|
|
218
|
+
* {@inheritDoc IResult.onSuccess}
|
|
219
219
|
*/
|
|
220
220
|
onSuccess(__) {
|
|
221
221
|
return new Failure(this._message);
|
|
222
222
|
}
|
|
223
223
|
/**
|
|
224
|
-
* {@
|
|
224
|
+
* {@inheritDoc IResult.onFailure}
|
|
225
225
|
*/
|
|
226
226
|
onFailure(cb) {
|
|
227
227
|
return cb(this._message);
|
|
228
228
|
}
|
|
229
229
|
/**
|
|
230
|
-
* {@
|
|
230
|
+
* {@inheritDoc IResult.withErrorFormat}
|
|
231
231
|
*/
|
|
232
232
|
withErrorFormat(cb) {
|
|
233
233
|
return fail(cb(this._message));
|
|
234
234
|
}
|
|
235
235
|
/**
|
|
236
|
-
* {@
|
|
236
|
+
* {@inheritDoc IResult.withFailureDetail}
|
|
237
237
|
*/
|
|
238
238
|
withFailureDetail(detail) {
|
|
239
239
|
return failWithDetail(this._message, detail);
|
|
240
240
|
}
|
|
241
241
|
/**
|
|
242
|
-
* {@
|
|
242
|
+
* {@inheritDoc IResult.withDetail}
|
|
243
243
|
*/
|
|
244
244
|
withDetail(detail, __successDetail) {
|
|
245
245
|
return failWithDetail(this._message, detail);
|
|
246
246
|
}
|
|
247
247
|
/**
|
|
248
|
-
* {@
|
|
248
|
+
* {@inheritDoc IResult.aggregateError}
|
|
249
249
|
*/
|
|
250
250
|
aggregateError(errors, formatter) {
|
|
251
251
|
const message = formatter ? formatter(this._message) : this._message;
|
|
@@ -253,7 +253,7 @@ export class Failure {
|
|
|
253
253
|
return this;
|
|
254
254
|
}
|
|
255
255
|
/**
|
|
256
|
-
* {@
|
|
256
|
+
* {@inheritDoc IResult.report}
|
|
257
257
|
*/
|
|
258
258
|
report(reporter, options) {
|
|
259
259
|
var _a, _b, _c;
|
|
@@ -294,7 +294,7 @@ export function succeed(value) {
|
|
|
294
294
|
return new Success(value);
|
|
295
295
|
}
|
|
296
296
|
/**
|
|
297
|
-
* {@
|
|
297
|
+
* {@inheritDoc succeed}
|
|
298
298
|
* @public
|
|
299
299
|
*/
|
|
300
300
|
export function succeeds(value) {
|
|
@@ -312,7 +312,7 @@ export function fail(message) {
|
|
|
312
312
|
return new Failure(message);
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
-
* {@
|
|
315
|
+
* {@inheritDoc fail}
|
|
316
316
|
* @public
|
|
317
317
|
*/
|
|
318
318
|
export function fails(message) {
|
|
@@ -378,7 +378,7 @@ export class DetailedSuccess extends Success {
|
|
|
378
378
|
* Propagates this {@link DetailedSuccess}.
|
|
379
379
|
* @remarks
|
|
380
380
|
* Failure does not mutate return type so we can return this event directly.
|
|
381
|
-
* @param
|
|
381
|
+
* @param __cb - {@link DetailedFailureContinuation | Failure callback} to be called
|
|
382
382
|
* on a {@link DetailedResult} in case of failure (ignored).
|
|
383
383
|
* @returns `this`
|
|
384
384
|
*/
|
|
@@ -386,13 +386,13 @@ export class DetailedSuccess extends Success {
|
|
|
386
386
|
return this;
|
|
387
387
|
}
|
|
388
388
|
/**
|
|
389
|
-
* {@
|
|
389
|
+
* {@inheritDoc Success.withErrorFormat}
|
|
390
390
|
*/
|
|
391
391
|
withErrorFormat(cb) {
|
|
392
392
|
return this;
|
|
393
393
|
}
|
|
394
394
|
/**
|
|
395
|
-
* {@
|
|
395
|
+
* {@inheritDoc IResult.report}
|
|
396
396
|
*/
|
|
397
397
|
report(reporter, options) {
|
|
398
398
|
var _a;
|
|
@@ -455,7 +455,7 @@ export class DetailedFailure extends Failure {
|
|
|
455
455
|
* @remarks
|
|
456
456
|
* Mutates the success type as the success callback would have, but does not
|
|
457
457
|
* call the success callback.
|
|
458
|
-
* @param
|
|
458
|
+
* @param __cb - {@link DetailedSuccessContinuation | Success callback} to be called
|
|
459
459
|
* on a {@link DetailedResult} in case of success (ignored).
|
|
460
460
|
* @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
|
|
461
461
|
* the error message and detail from this one.
|
|
@@ -473,13 +473,13 @@ export class DetailedFailure extends Failure {
|
|
|
473
473
|
return cb(this._message, this._detail);
|
|
474
474
|
}
|
|
475
475
|
/**
|
|
476
|
-
* {@
|
|
476
|
+
* {@inheritDoc IResult.withErrorFormat}
|
|
477
477
|
*/
|
|
478
478
|
withErrorFormat(cb) {
|
|
479
479
|
return failWithDetail(cb(this._message, this._detail), this._detail);
|
|
480
480
|
}
|
|
481
481
|
/**
|
|
482
|
-
* {@
|
|
482
|
+
* {@inheritDoc IResult.aggregateError}
|
|
483
483
|
*/
|
|
484
484
|
aggregateError(errors, formatter) {
|
|
485
485
|
const message = formatter ? formatter(this._message, this._detail) : this._message;
|
|
@@ -487,7 +487,7 @@ export class DetailedFailure extends Failure {
|
|
|
487
487
|
return this;
|
|
488
488
|
}
|
|
489
489
|
/**
|
|
490
|
-
* {@
|
|
490
|
+
* {@inheritDoc IResult.report}
|
|
491
491
|
*/
|
|
492
492
|
report(reporter, options) {
|
|
493
493
|
var _a, _b;
|
|
@@ -548,7 +548,7 @@ export function succeedWithDetail(value, detail) {
|
|
|
548
548
|
return new DetailedSuccess(value, detail);
|
|
549
549
|
}
|
|
550
550
|
/**
|
|
551
|
-
* {@
|
|
551
|
+
* {@inheritDoc succeedWithDetail}
|
|
552
552
|
* @public
|
|
553
553
|
*/
|
|
554
554
|
export function succeedsWithDetail(value, detail) {
|
|
@@ -569,7 +569,7 @@ export function failWithDetail(message, detail) {
|
|
|
569
569
|
return new DetailedFailure(message, detail);
|
|
570
570
|
}
|
|
571
571
|
/**
|
|
572
|
-
* {@
|
|
572
|
+
* {@inheritDoc failWithDetail}
|
|
573
573
|
* @public
|
|
574
574
|
*/
|
|
575
575
|
export function failsWithDetail(message, detail) {
|