Skip to content

Commit 2d6e5e9

Browse files
committed
feat: add mergeWithConfig
1 parent 5551d66 commit 2d6e5e9

File tree

4 files changed

+349
-167
lines changed

4 files changed

+349
-167
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const user = getUser(1).map(({ email }) => email);
3939

4040
- [`chain`](#chain)
4141
- [`merge`](#merge)
42+
- [`mergeWithConfig`](#mergewithconfig)
4243
- [`mergeInOne`](#mergeinone)
4344
- [`mergeInMany`](#mergeinmany)
4445
- [`initial`](#initial)
@@ -155,6 +156,37 @@ const r2 = merge([v2, v5]); // Result<Error, [never, boolean]>.Pending
155156
const r3 = merge([v3, v4]); // Result<TypeError | ReferenceError, [number, string]>.Success
156157
const r4 = merge([v3, v4, v5]); // Result<TypeError | Error | ReferenceError, [number, string, boolean]>.Failure
157158
```
159+
#### `mergeWithConfig`
160+
161+
```typescript
162+
function mergeWithConfig<F, S>(values: Result<F, S>[], config: { priority: 'failure' | 'pending' }): Result<F, S[]>;
163+
```
164+
165+
Merges an array of Results with configurable priority between states.
166+
167+
Default behavior (priority: 'pending'):
168+
1. Returns Initial if ANY Result is Initial
169+
2. Returns Pending if ANY Result is Pending (and none are Initial)
170+
3. Returns Failure if ANY Result is Failure (and none are Initial/Pending)
171+
4. Returns Success only if ALL Results are Success
172+
173+
Failure priority (priority: 'failure'):
174+
1. Returns Failure if ANY Result is Failure
175+
2. Returns Initial if ANY Result is Initial
176+
3. Returns Pending if ANY Result is Pending
177+
4. Returns Success only if ALL Results are Success
178+
179+
```typescript
180+
const v1 = success<string, number>(1);
181+
const v2 = failure('error');
182+
const v3 = pending as Result<string, number>;
183+
184+
// Default behavior - Pending takes precedence
185+
mergeWithConfig([v1, v2, v3], { priority: 'pending' }); // Result.Pending
186+
187+
// Failure priority
188+
mergeWithConfig([v1, v2, v3], { priority: 'failure' }); // Result.Failure('error')
189+
```
158190

159191
#### `mergeInOne`
160192

packages/ts-result/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lonli-lokli/ts-result",
3-
"version": "2.5.0",
3+
"version": "2.6.0",
44
"private": false,
55
"sideEffects": false,
66
"main": "dist/index.js",

0 commit comments

Comments
 (0)