@resultsafe/core-fp-result 0.1.10 → 0.2.0
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 +317 -305
- package/docs/assets/logo.svg +0 -0
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/refiners/types/Handler.d.ts +9 -1
- package/types/refiners/types/Handler.d.ts.map +1 -1
- package/types/refiners/types/MatchBuilder.d.ts +20 -1
- package/types/refiners/types/MatchBuilder.d.ts.map +1 -1
- package/types/refiners/types/Matcher.d.ts +23 -1
- package/types/refiners/types/Matcher.d.ts.map +1 -1
- package/types/refiners/types/SyncRefinedResult.d.ts +23 -1
- package/types/refiners/types/SyncRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/SyncRefinedResultUnion.d.ts +24 -1
- package/types/refiners/types/SyncRefinedResultUnion.d.ts.map +1 -1
- package/types/refiners/types/SyncValidatorMap.d.ts +23 -1
- package/types/refiners/types/SyncValidatorMap.d.ts.map +1 -1
- package/types/refiners/types/UniversalAsyncRefinedResult.d.ts +27 -1
- package/types/refiners/types/UniversalAsyncRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/UniversalRefinedResult.d.ts +27 -1
- package/types/refiners/types/UniversalRefinedResult.d.ts.map +1 -1
- package/types/refiners/types/VariantOf.d.ts +20 -1
- package/types/refiners/types/VariantOf.d.ts.map +1 -1
- package/types/shared-types.d.ts +142 -0
- package/types/shared-types.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -1,305 +1,317 @@
|
|
|
1
|
-
# @resultsafe/core-fp-result
|
|
2
|
-
|
|
3
|
-
<a id="top"></a>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
[
|
|
6
|
+
|
|
7
|
+
## 🤖 AI Context
|
|
8
|
+
|
|
9
|
+
This package implements the `Result<T, E>` pattern for explicit error handling without `try/catch`.
|
|
10
|
+
|
|
11
|
+
- **Rust analogue:** `std::result::Result`
|
|
12
|
+
- **Scala analogue:** `Either`
|
|
13
|
+
- **Core principle:** Error is a value, not an exception.
|
|
14
|
+
|
|
15
|
+
[](https://www.npmjs.com/package/@resultsafe/core-fp-result)
|
|
16
|
+
[](https://www.npmjs.com/package/@resultsafe/core-fp-result)
|
|
17
|
+
[](./LICENSE)
|
|
18
|
+
[](https://www.typescriptlang.org/)
|
|
19
|
+
[](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/README.md)
|
|
20
|
+
[](./package.json)
|
|
21
|
+
[](https://github.com/Livooon/resultsafe)
|
|
22
|
+
|
|
23
|
+
A Rust-inspired Result package for explicit, composable, and type-friendly APIs in TypeScript and JavaScript.
|
|
24
|
+
|
|
25
|
+
**Language:** English | [Русский](https://github.com/resultsafe/monorepo/blob/main/packages/core/fp/result/README.ru.md)
|
|
26
|
+
|
|
27
|
+
**Documentation:** [API index](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/README.md) · [Modules](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/modules.md)
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Contents
|
|
32
|
+
|
|
33
|
+
- [Why this package](#why-this-package)
|
|
34
|
+
- [Monorepo context](#monorepo-context)
|
|
35
|
+
- [Key features](#key-features)
|
|
36
|
+
- [Package](#package)
|
|
37
|
+
- [Installation](#installation)
|
|
38
|
+
- [Quick start](#quick-start)
|
|
39
|
+
- [Core API overview](#core-api-overview)
|
|
40
|
+
- [Build and distribution formats](#build-and-distribution-formats)
|
|
41
|
+
- [Monorepo and package structure](#monorepo-and-package-structure)
|
|
42
|
+
- [When to use this project](#when-to-use-this-project)
|
|
43
|
+
- [Documentation links](#documentation-links)
|
|
44
|
+
- [License](#license)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Why this package
|
|
49
|
+
|
|
50
|
+
`@resultsafe/core-fp-result` provides explicit success/error flows instead of hidden control paths and exception-first branching.
|
|
51
|
+
|
|
52
|
+
Its core package, [`@resultsafe/core-fp-result`](https://www.npmjs.com/package/@resultsafe/core-fp-result), provides a Rust-inspired `Result` API for TypeScript and JavaScript with:
|
|
53
|
+
|
|
54
|
+
- explicit `Ok` / `Err`
|
|
55
|
+
- predictable functional composition
|
|
56
|
+
- safe branching through guards and matching
|
|
57
|
+
- disciplined extraction APIs
|
|
58
|
+
- advanced refinement utilities for typed variants and strict matching
|
|
59
|
+
|
|
60
|
+
The goal is not to imitate Rust mechanically, but to bring the same clarity of intent to Node.js libraries: explicit values, predictable branching, and APIs organized for long-term maintenance.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Monorepo context
|
|
65
|
+
|
|
66
|
+
`@resultsafe/core-fp-result` is the TypeScript/JavaScript package inside the multilingual `resultsafe/monorepo`.
|
|
67
|
+
|
|
68
|
+
The monorepo applies shared Rust-inspired design concepts across language-specific packages. TypeScript/JavaScript is the current production package track, while Python is planned as a separate package track with the same conceptual model.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Key features
|
|
73
|
+
|
|
74
|
+
- Rust-inspired `Result` model for TypeScript and JavaScript.
|
|
75
|
+
- Explicit constructors via `Ok` and `Err`.
|
|
76
|
+
- Composable transformations with APIs such as `map`, `mapErr`, `andThen`, and `orElse`.
|
|
77
|
+
- Safe branching through guards like `isOk`, `isErr`, `isOkAnd`, `isErrAnd`, and matching helpers.
|
|
78
|
+
- Controlled extraction with `unwrap`, `unwrapOr`, `unwrapErr`, `expect`, and `expectErr`.
|
|
79
|
+
- Advanced refinement layer for typed variants, strict matching, and result narrowing.
|
|
80
|
+
- Coherent module structure instead of a flat utility dump.
|
|
81
|
+
- Type output for TypeScript users for better DX and safer integrations.
|
|
82
|
+
- Flexible distribution formats with Types, ESM, CJS, and UMD builds.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Package
|
|
87
|
+
|
|
88
|
+
### `@resultsafe/core-fp-result`
|
|
89
|
+
|
|
90
|
+
A focused Result library for explicit error handling and FP-style composition.
|
|
91
|
+
|
|
92
|
+
It is designed for developers who want:
|
|
93
|
+
|
|
94
|
+
- clear success/error modeling
|
|
95
|
+
- predictable transformations
|
|
96
|
+
- explicit branching and extraction
|
|
97
|
+
- better readability in error-heavy flows
|
|
98
|
+
- a structured Rust-inspired API surface in TypeScript/JavaScript
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Installation
|
|
103
|
+
|
|
104
|
+
### Package
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pnpm add @resultsafe/core-fp-result
|
|
108
|
+
|
|
109
|
+
# Alternative
|
|
110
|
+
npm install @resultsafe/core-fp-result
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Monorepo
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pnpm install
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Quick start
|
|
122
|
+
|
|
123
|
+
A typical Result flow starts with explicit constructors and then composes through functions rather than implicit exception paths.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { Ok, map, unwrapOr } from '@resultsafe/core-fp-result';
|
|
127
|
+
|
|
128
|
+
const initial = Ok(21);
|
|
129
|
+
const doubled = map(initial, (value) => value * 2);
|
|
130
|
+
const finalValue = unwrapOr(doubled, 0);
|
|
131
|
+
|
|
132
|
+
console.log(finalValue); // 42
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Basic `Ok` / `Err` example
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { Ok, Err, match } from '@resultsafe/core-fp-result';
|
|
139
|
+
|
|
140
|
+
const parsePort = (input: string) => {
|
|
141
|
+
const port = Number(input);
|
|
142
|
+
return Number.isInteger(port) && port > 0 ? Ok(port) : Err('Invalid port');
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const result = parsePort('3000');
|
|
146
|
+
const message = match(
|
|
147
|
+
result,
|
|
148
|
+
(value) => `Port: ${value}`,
|
|
149
|
+
(error) => `Error: ${error}`,
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
console.log(message);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## API Reference
|
|
158
|
+
|
|
159
|
+
### Core Functions
|
|
160
|
+
|
|
161
|
+
| Function | Signature | Description | GitHub | Raw |
|
|
162
|
+
| ----------- | ----------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
|
|
163
|
+
| `Ok` | `(value: T) => Result<T, E>` | Success constructor | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/constructors/Ok.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/constructors/Ok.ts) |
|
|
164
|
+
| `Err` | `(error: E) => Result<T, E>` | Error constructor | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/constructors/Err.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/constructors/Err.ts) |
|
|
165
|
+
| `match` | `(r, onOk, onErr) => U` | Branch on result | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/match.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/match.ts) |
|
|
166
|
+
| `andThen` | `(r, fn) => Result<U, E>` | Chain (flatMap) | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/andThen.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/andThen.ts) |
|
|
167
|
+
| `map` | `(r, fn) => Result<U, E>` | Transform success value | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/map.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/map.ts) |
|
|
168
|
+
| `mapErr` | `(r, fn) => Result<T, U>` | Transform error value | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/mapErr.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/mapErr.ts) |
|
|
169
|
+
| `orElse` | `(r, fn) => Result<T, U>` | Recover from error | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/orElse.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/orElse.ts) |
|
|
170
|
+
| `unwrap` | `(r) => T` | Extract value or throw | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/unwrap.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/unwrap.ts) |
|
|
171
|
+
| `unwrapOr` | `(r, fallback: T) => T` | Extract value or default | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/unwrapOr.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/unwrapOr.ts) |
|
|
172
|
+
| `expect` | `(r, msg: string) => T` | Extract value or throw with message | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/expect.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/expect.ts) |
|
|
173
|
+
| `isOk` | `(r) => boolean` | Check if success | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/guards/isOk.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/guards/isOk.ts) |
|
|
174
|
+
| `isErr` | `(r) => boolean` | Check if error | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/guards/isErr.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/guards/isErr.ts) |
|
|
175
|
+
| `tap` | `(r, fn) => Result<T, E>` | Side effect on success | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/tap.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/tap.ts) |
|
|
176
|
+
| `tapErr` | `(r, fn) => Result<T, E>` | Side effect on error | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/tapErr.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/tapErr.ts) |
|
|
177
|
+
| `transpose` | `(r) => Option<Result<T, E>>` | Result<Option> to Option<Result> | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/transpose.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/transpose.ts) |
|
|
178
|
+
| `flatten` | `(r) => Result<T, E>` | Flatten nested Result | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/methods/flatten.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/methods/flatten.ts) |
|
|
179
|
+
|
|
180
|
+
### Advanced: Refiners
|
|
181
|
+
|
|
182
|
+
| Function | Description | GitHub | Raw |
|
|
183
|
+
| -------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
|
184
|
+
| `isTypedVariant` | Type guard for variant | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/refiners/isTypedVariant.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/refiners/isTypedVariant.ts) |
|
|
185
|
+
| `matchVariant` | Match variant with handlers | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/refiners/matchVariant.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/refiners/matchVariant.ts) |
|
|
186
|
+
| `matchVariantStrict` | Strict variant matching | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/refiners/matchVariantStrict.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/refiners/matchVariantStrict.ts) |
|
|
187
|
+
| `refineResult` | Sync result refinement | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/refiners/refineResult.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/refiners/refineResult.ts) |
|
|
188
|
+
| `refineAsyncResult` | Async result refinement | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/refiners/refineAsyncResult.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/refiners/refineAsyncResult.ts) |
|
|
189
|
+
|
|
190
|
+
### Type Aliases
|
|
191
|
+
|
|
192
|
+
| Type | Description | GitHub | Raw |
|
|
193
|
+
| ------------------ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
194
|
+
| `Result<T, E>` | Base success/error type | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/types/core/Result.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/types/core/Result.ts) |
|
|
195
|
+
| `Option<T>` | Optional value type | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/types/core/Option.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/types/core/Option.ts) |
|
|
196
|
+
| `VariantConfig` | Variant configuration type | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/types/refiners/VariantConfig.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/types/refiners/VariantConfig.ts) |
|
|
197
|
+
| `ValidatorFn<T>` | Sync validator function type | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/types/refiners/ValidatorFn.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/types/refiners/ValidatorFn.ts) |
|
|
198
|
+
| `AsyncValidatorFn` | Async validator function type | [🔗](https://github.com/Livooon/resultsafe/blob/main/packages/core/fp/result/src/types/refiners/AsyncValidatorFn.ts) | [📄](https://raw.githubusercontent.com/Livooon/resultsafe/main/packages/core/fp/result/src/types/refiners/AsyncValidatorFn.ts) |
|
|
199
|
+
|
|
200
|
+
> **Full API:** [All modules](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/modules.md) · [Constructors](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/constructors/index.md) · [Guards](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/guards/index.md) · [Methods](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/methods/index.md) · [Refiners](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/refiners/index.md)
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## Examples
|
|
205
|
+
|
|
206
|
+
### Example 1: Basic Usage
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
import { Ok, Err, match } from '@resultsafe/core-fp-result';
|
|
210
|
+
|
|
211
|
+
const result = Ok(42);
|
|
212
|
+
match(
|
|
213
|
+
result,
|
|
214
|
+
(value) => console.log(value),
|
|
215
|
+
(error) => console.error(error),
|
|
216
|
+
);
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Example 2: Error Handling
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
import { Ok, Err } from '@resultsafe/core-fp-result';
|
|
223
|
+
|
|
224
|
+
const divide = (a: number, b: number) =>
|
|
225
|
+
b === 0 ? Err('Division by zero') : Ok(a / b);
|
|
226
|
+
|
|
227
|
+
const result = divide(10, 0);
|
|
228
|
+
// Err("Division by zero")
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Example 3: Chaining Operations
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
import { Ok, andThen, map } from '@resultsafe/core-fp-result';
|
|
235
|
+
|
|
236
|
+
const result = Ok(5);
|
|
237
|
+
const doubled = andThen(result, (x) => Ok(x * 2));
|
|
238
|
+
const squared = map(doubled, (x) => x ** 2);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## Ecosystem
|
|
244
|
+
|
|
245
|
+
- `@resultsafe/core-fp-option` — Option/Maybe type
|
|
246
|
+
- `@resultsafe/core-fp-either` — Either type
|
|
247
|
+
- `@resultsafe/core-fp-result` — This package (Result type)
|
|
248
|
+
|
|
249
|
+
All packages follow the same API design pattern.
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## Build and distribution formats
|
|
254
|
+
|
|
255
|
+
- Types: `build:types`
|
|
256
|
+
- ESM: `build:esm`
|
|
257
|
+
- CJS: `build:cjs`
|
|
258
|
+
- UMD: `build:umd`
|
|
259
|
+
|
|
260
|
+
This package ships typed declarations and multiple runtime module formats for broad compatibility.
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Monorepo and package structure
|
|
265
|
+
|
|
266
|
+
```txt
|
|
267
|
+
src/
|
|
268
|
+
constructors/
|
|
269
|
+
guards/
|
|
270
|
+
methods/
|
|
271
|
+
refiners/
|
|
272
|
+
types/
|
|
273
|
+
utils/
|
|
274
|
+
internal/
|
|
275
|
+
index.ts
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## When to use this project
|
|
281
|
+
|
|
282
|
+
Use this project when you want:
|
|
283
|
+
|
|
284
|
+
- explicit success/error modeling
|
|
285
|
+
- predictable FP-style composition
|
|
286
|
+
- visible control flow
|
|
287
|
+
- stronger type-guided result handling in TypeScript
|
|
288
|
+
- advanced refinement tools for typed variants and strict result matching
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## Documentation links
|
|
293
|
+
|
|
294
|
+
- [API entry (README)](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/README.md)
|
|
295
|
+
- [API entry (index)](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/index.md)
|
|
296
|
+
- [Modules](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/modules.md)
|
|
297
|
+
- [Constructors module](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/constructors/index.md)
|
|
298
|
+
- [Guards module](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/guards/index.md)
|
|
299
|
+
- [Methods module](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/methods/index.md)
|
|
300
|
+
- [Refiners module](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/refiners/index.md)
|
|
301
|
+
- [Type aliases module](https://unpkg.com/@resultsafe/core-fp-result@latest/docs/api/type-aliases/index.md)
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
Back to top: [@resultsafe/core-fp-result](#top)
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## Author
|
|
310
|
+
|
|
311
|
+
Denis Savasteev
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## License
|
|
316
|
+
|
|
317
|
+
[MIT](./LICENSE)
|
package/docs/assets/logo.svg
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC"}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a handler entry for variant matching in internal matcher mechanisms.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam T - The variant union type.
|
|
7
|
+
* @typeParam R - The return type of the handler function.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
3
11
|
export type Handler<K extends string, T extends VariantOf<K>, R> = {
|
|
4
12
|
readonly variant: K;
|
|
5
13
|
readonly fn: (value: Extract<T, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Handler.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"Handler.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI;IACjE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,CAAC;CACpD,CAAC"}
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes the shape of a strict builder for variant matching.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The variant union type.
|
|
6
|
+
* @typeParam R - The return type of the match operation.
|
|
7
|
+
* @typeParam Handled - The union of already handled variant types.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { MatchBuilder } from '@resultsafe/core-fp-result';
|
|
12
|
+
*
|
|
13
|
+
* const builder: MatchBuilder<MyVariant, string> = {
|
|
14
|
+
* with: (variant, fn) => builder,
|
|
15
|
+
* run: () => 'result'
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @since 0.1.8
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
3
22
|
export type MatchBuilder<T extends VariantOf, R, Handled extends T['type'] = never> = {
|
|
4
23
|
readonly with: <K extends Exclude<T['type'], Handled>>(variant: K, fn: (value: Extract<T, {
|
|
5
24
|
type: K;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MatchBuilder.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/MatchBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"MatchBuilder.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/MatchBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,YAAY,CACtB,CAAC,SAAS,SAAS,EACnB,CAAC,EACD,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,KAAK,IAC/B;IACF,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,EACnD,OAAO,EAAE,CAAC,EACV,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,KACtC,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;IACrC,QAAQ,CAAC,GAAG,EAAE,OAAO,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,KAAK,CAAC;CAC3D,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { VariantOf } from './VariantOf.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes the shape of a non-strict builder for variant matching.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The variant union type.
|
|
6
|
+
* @typeParam R - The return type of the match operation.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Unlike {@link MatchBuilder}, this builder allows matching any variant
|
|
10
|
+
* and provides an `otherwise` fallback handler for unmatched cases.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Matcher } from '@resultsafe/core-fp-result';
|
|
15
|
+
*
|
|
16
|
+
* const matcher: Matcher<MyVariant, string> = {
|
|
17
|
+
* with: (variant, fn) => matcher,
|
|
18
|
+
* otherwise: (fn) => ({ run: () => 'default' })
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type Matcher<T extends VariantOf, R> = {
|
|
4
26
|
readonly with: <K extends T['type']>(variant: K, fn: (value: Extract<T, {
|
|
5
27
|
type: K;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Matcher.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Matcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD
|
|
1
|
+
{"version":3,"file":"Matcher.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/Matcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI;IAC5C,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,EACjC,OAAO,EAAE,CAAC,EACV,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,CAAC,KACtC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnB,QAAQ,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,KAAK;QAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;CACxE,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { PayloadKeys, ValidatorFn, VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a synchronously refined specific variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a refined variant with its payload fields
|
|
11
|
+
* validated synchronously. The `type` field discriminates the variant,
|
|
12
|
+
* and the remaining fields are the validated payload.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { SyncRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type MyVariant = SyncRefinedResult<'user', { user: { payload: 'name' } }, {}>;
|
|
19
|
+
* // { type: 'user'; name: unknown }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type SyncRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Partial<Record<PayloadKeys<TMap[K]>, ValidatorFn>>> = {
|
|
4
26
|
type: K;
|
|
5
27
|
} & Record<PayloadKeys<TMap[K]>, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B
|
|
1
|
+
{"version":3,"file":"SyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,iBAAiB,CAC3B,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,IACrE;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
2
|
import type { SyncRefinedResult } from './SyncRefinedResult.js';
|
|
3
3
|
import type { SyncValidatorMap } from './SyncValidatorMap.js';
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Describes a union of synchronously refined variants.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam TMap - The variant configuration map.
|
|
8
|
+
* @typeParam TValidators - The validator map for all variants.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This type represents the union of all refined variants in the map.
|
|
12
|
+
* Each variant is refined using {@link SyncRefinedResult}.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { SyncRefinedResultUnion, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type Variants = SyncRefinedResultUnion<
|
|
19
|
+
* { user: { payload: 'name' }; error: { payload: 'code' } },
|
|
20
|
+
* {}
|
|
21
|
+
* >;
|
|
22
|
+
* // { type: 'user'; name: unknown } | { type: 'error'; code: unknown }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @since 0.1.8
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
5
28
|
export type SyncRefinedResultUnion<TMap extends Record<string, VariantConfig>, TValidators extends SyncValidatorMap<TMap>> = {
|
|
6
29
|
[K in keyof TMap & string]: SyncRefinedResult<K, TMap, NonNullable<TValidators[K]>>;
|
|
7
30
|
}[keyof TMap & string];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncRefinedResultUnion.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResultUnion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D
|
|
1
|
+
{"version":3,"file":"SyncRefinedResultUnion.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncRefinedResultUnion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,sBAAsB,CAChC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,WAAW,SAAS,gBAAgB,CAAC,IAAI,CAAC,IACxC;KACD,CAAC,IAAI,MAAM,IAAI,GAAG,MAAM,GAAG,iBAAiB,CAC3C,CAAC,EACD,IAAI,EACJ,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAC5B;CACF,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC"}
|
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import type { PayloadKeys, ValidatorFn, VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes validator sets grouped by variant key.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam TMap - The variant configuration map.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* This type maps each variant to its optional validators for payload fields.
|
|
9
|
+
* Validators are used to refine and check variant data at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { SyncValidatorMap, VariantConfig } from '@resultsafe/core-fp-result';
|
|
14
|
+
*
|
|
15
|
+
* type Validators = SyncValidatorMap<{
|
|
16
|
+
* user: { payload: 'name' | 'age' };
|
|
17
|
+
* error: { payload: 'code' };
|
|
18
|
+
* }>;
|
|
19
|
+
* // { user?: { name?: ValidatorFn; age?: ValidatorFn }; error?: { code?: ValidatorFn } }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @since 0.1.8
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
3
25
|
export type SyncValidatorMap<TMap extends Record<string, VariantConfig>> = {
|
|
4
26
|
[K in keyof TMap]?: Partial<Record<PayloadKeys<TMap[K]>, ValidatorFn>>;
|
|
5
27
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SyncValidatorMap.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncValidatorMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B
|
|
1
|
+
{"version":3,"file":"SyncValidatorMap.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/SyncValidatorMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,gBAAgB,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,IAAI;KACxE,CAAC,IAAI,MAAM,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;CACvE,CAAC"}
|
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a generalized asynchronously refined variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a variant refined through async validation.
|
|
11
|
+
* The `type` field discriminates the variant, and additional fields
|
|
12
|
+
* are validated asynchronously.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { UniversalAsyncRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type AsyncVariant = UniversalAsyncRefinedResult<
|
|
19
|
+
* 'user',
|
|
20
|
+
* { user: { payload: 'name' } },
|
|
21
|
+
* {}
|
|
22
|
+
* >;
|
|
23
|
+
* // { type: 'user'; [key: string]: unknown }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.8
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
3
29
|
export type UniversalAsyncRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Record<string, unknown>> = {
|
|
4
30
|
type: K;
|
|
5
31
|
} & Record<string, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UniversalAsyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalAsyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D
|
|
1
|
+
{"version":3,"file":"UniversalAsyncRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalAsyncRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,2BAA2B,CACrC,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC1C;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
import type { VariantConfig } from '../../shared-types.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Describes a generalized synchronously refined variant value.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam K - The variant key type.
|
|
6
|
+
* @typeParam TMap - The variant configuration map.
|
|
7
|
+
* @typeParam _TValidators - The validator map for the variant.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This type represents a variant refined through sync validation.
|
|
11
|
+
* The `type` field discriminates the variant, and additional fields
|
|
12
|
+
* are validated synchronously.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { UniversalRefinedResult, VariantConfig } from '@resultsafe/core-fp-result';
|
|
17
|
+
*
|
|
18
|
+
* type SyncVariant = UniversalRefinedResult<
|
|
19
|
+
* 'user',
|
|
20
|
+
* { user: { payload: 'name' } },
|
|
21
|
+
* {}
|
|
22
|
+
* >;
|
|
23
|
+
* // { type: 'user'; [key: string]: unknown }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @since 0.1.8
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
3
29
|
export type UniversalRefinedResult<K extends keyof TMap & string, TMap extends Record<string, VariantConfig>, _TValidators extends Record<string, unknown>> = {
|
|
4
30
|
type: K;
|
|
5
31
|
} & Record<string, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UniversalRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D
|
|
1
|
+
{"version":3,"file":"UniversalRefinedResult.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/UniversalRefinedResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,MAAM,sBAAsB,CAChC,CAAC,SAAS,MAAM,IAAI,GAAG,MAAM,EAC7B,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAC1C,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC1C;IACF,IAAI,EAAE,CAAC,CAAC;CACT,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC"}
|
|
@@ -1,4 +1,23 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Describes the minimal contract for a discriminated union variant.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam K - The variant key type. Defaults to `string`.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This type is used as a constraint for variant unions. Each variant
|
|
8
|
+
* must have a `type` field that serves as the discriminator.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { VariantOf } from '@resultsafe/core-fp-result';
|
|
13
|
+
*
|
|
14
|
+
* type MyVariant = VariantOf<'user' | 'error'>;
|
|
15
|
+
* // { type: 'user' } | { type: 'error' }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @since 0.1.8
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
2
21
|
export type VariantOf<K extends string = string> = {
|
|
3
22
|
type: K;
|
|
4
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VariantOf.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/VariantOf.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"VariantOf.d.ts","sourceRoot":"","sources":["../../../../src/refiners/types/VariantOf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,CAAC"}
|
package/types/shared-types.d.ts
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the result of an operation that can either succeed with a
|
|
3
|
+
* value of type `T` or fail with an error of type `E`.
|
|
4
|
+
*
|
|
5
|
+
* @summary
|
|
6
|
+
* Represents the result of an operation that can either succeed with a
|
|
7
|
+
* value of type `T` or fail with an error of type `E`.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* `Result` is the core type of this library. Pattern match on the `ok`
|
|
11
|
+
* field to narrow the union, then access `value` or `error` safely.
|
|
12
|
+
*
|
|
13
|
+
* Unlike exceptions, `Result` makes the failure path explicit in the
|
|
14
|
+
* type signature, forcing the caller to handle both outcomes.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T - The type of the success value.
|
|
17
|
+
* @typeParam E - The type of the error.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Result, Ok, Err } from '@resultsafe/core-fp-result';
|
|
22
|
+
*
|
|
23
|
+
* function divide(a: number, b: number): Result<number, string> {
|
|
24
|
+
* if (b === 0) return Err("Division by zero");
|
|
25
|
+
* return Ok(a / b);
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const result = divide(10, 2);
|
|
29
|
+
*
|
|
30
|
+
* if (result.ok) {
|
|
31
|
+
* console.log(result.value); // 5
|
|
32
|
+
* } else {
|
|
33
|
+
* console.error(result.error);
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @since 0.1.8
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
1
40
|
export type Result<T, E> = {
|
|
2
41
|
readonly ok: true;
|
|
3
42
|
readonly value: T;
|
|
@@ -5,18 +44,121 @@ export type Result<T, E> = {
|
|
|
5
44
|
readonly ok: false;
|
|
6
45
|
readonly error: E;
|
|
7
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Represents an optional value that can either contain a value of type `T`
|
|
49
|
+
* or be empty.
|
|
50
|
+
*
|
|
51
|
+
* @summary
|
|
52
|
+
* Represents an optional value that can either contain a value of type `T`
|
|
53
|
+
* or be empty.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* `Option` is used to represent the presence or absence of a value. It is
|
|
57
|
+
* an alternative to `null` or `undefined` that is type-safe and explicit.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T - The type of the contained value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { Option } from '@resultsafe/core-fp-result';
|
|
64
|
+
*
|
|
65
|
+
* const someValue: Option<number> = { some: true, value: 42 };
|
|
66
|
+
* const noValue: Option<number> = { some: false };
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @since 0.1.8
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
8
72
|
export type Option<T> = {
|
|
9
73
|
readonly some: true;
|
|
10
74
|
readonly value: T;
|
|
11
75
|
} | {
|
|
12
76
|
readonly some: false;
|
|
13
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* Configuration options for variant validation.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* Used to configure how variants are validated, including payload keys
|
|
83
|
+
* and field strictness.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { VariantConfig } from '@resultsafe/core-fp-result';
|
|
88
|
+
*
|
|
89
|
+
* const config: VariantConfig = {
|
|
90
|
+
* payload: ['name', 'age'],
|
|
91
|
+
* forbidden: 'id',
|
|
92
|
+
* strictFields: true
|
|
93
|
+
* };
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @since 0.1.8
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
14
99
|
export interface VariantConfig {
|
|
15
100
|
readonly payload: 'never' | string | readonly string[];
|
|
16
101
|
readonly forbidden?: string | undefined;
|
|
17
102
|
readonly strictFields?: boolean | undefined;
|
|
18
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Extracts the payload keys from a `VariantConfig`.
|
|
106
|
+
*
|
|
107
|
+
* @typeParam T - The `VariantConfig` type to extract keys from.
|
|
108
|
+
* @returns The union of payload keys, or `never` if payload is `'never'`.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { PayloadKeys, VariantConfig } from '@resultsafe/core-fp-result';
|
|
113
|
+
*
|
|
114
|
+
* type Config = { payload: 'name' | 'age'; forbidden?: string };
|
|
115
|
+
* type Keys = PayloadKeys<Config>; // 'name' | 'age'
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @since 0.1.8
|
|
119
|
+
* @public
|
|
120
|
+
*/
|
|
19
121
|
export type PayloadKeys<T extends VariantConfig> = T['payload'] extends 'never' ? never : T['payload'] extends string ? T['payload'] : T['payload'] extends readonly string[] ? T['payload'][number] : never;
|
|
122
|
+
/**
|
|
123
|
+
* A synchronous validator function that checks if a value is of a specific type.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam T - The type the validator checks for.
|
|
126
|
+
* @param x - The value to validate.
|
|
127
|
+
* @returns `true` if `x` is of type `T`, `false` otherwise.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { ValidatorFn } from '@resultsafe/core-fp-result';
|
|
132
|
+
*
|
|
133
|
+
* const isNumber: ValidatorFn<number> = (x): x is number =>
|
|
134
|
+
* typeof x === 'number';
|
|
135
|
+
*
|
|
136
|
+
* console.log(isNumber(42)); // true
|
|
137
|
+
* console.log(isNumber('42')); // false
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @since 0.1.8
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
20
143
|
export type ValidatorFn<T = unknown> = (x: unknown) => x is T;
|
|
144
|
+
/**
|
|
145
|
+
* An asynchronous validator function that checks if a value is valid.
|
|
146
|
+
*
|
|
147
|
+
* @param value - The value to validate.
|
|
148
|
+
* @returns A promise that resolves to `true` if valid, `false` otherwise.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* import { AsyncValidatorFn } from '@resultsafe/core-fp-result';
|
|
153
|
+
*
|
|
154
|
+
* const isValidId: AsyncValidatorFn = async (value) => {
|
|
155
|
+
* // Simulate async database check
|
|
156
|
+
* return typeof value === 'string' && value.length > 0;
|
|
157
|
+
* };
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* @since 0.1.8
|
|
161
|
+
* @public
|
|
162
|
+
*/
|
|
21
163
|
export type AsyncValidatorFn = (value: unknown) => Promise<boolean>;
|
|
22
164
|
//# sourceMappingURL=shared-types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-types.d.ts","sourceRoot":"","sources":["../../src/shared-types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAE9C,MAAM,MAAM,MAAM,CAAC,CAAC,IAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAE7B,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,aAAa,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,OAAO,GAC3E,KAAK,GACL,CAAC,CAAC,SAAS,CAAC,SAAS,MAAM,GACzB,CAAC,CAAC,SAAS,CAAC,GACZ,CAAC,CAAC,SAAS,CAAC,SAAS,SAAS,MAAM,EAAE,GACpC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GACpB,KAAK,CAAC;AAEd,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;AAE9D,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"shared-types.d.ts","sourceRoot":"","sources":["../../src/shared-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAC1C;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC7C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,aAAa,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,OAAO,GAC3E,KAAK,GACL,CAAC,CAAC,SAAS,CAAC,SAAS,MAAM,GACzB,CAAC,CAAC,SAAS,CAAC,GACZ,CAAC,CAAC,SAAS,CAAC,SAAS,SAAS,MAAM,EAAE,GACpC,CAAC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GACpB,KAAK,CAAC;AAEd;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC,EAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC;AAE9D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
|