ts-results
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,9 +0,9 @@ | ||
interface BaseResult<S, E> { | ||
map<T>(fn: (ok: S) => T): Result<T, E>; | ||
map<T2>(fn: null | undefined, errFn: (err: E) => T2): Result<S, T2>; | ||
map<T, T2>(fn: (ok: S) => T, errFn: (err: E) => T2): Result<T, T2>; | ||
unwrap(): S; | ||
expect(msg: string): S; | ||
interface BaseResult<T, E> { | ||
map<T>(fn: (ok: T) => T): Result<T, E>; | ||
map<T2>(fn: null | undefined, errFn: (err: E) => T2): Result<T, T2>; | ||
map<T, T2>(fn: (ok: T) => T, errFn: (err: E) => T2): Result<T, T2>; | ||
unwrap(): T; | ||
expect(msg: string): T; | ||
} | ||
interface OkResult<S, E> extends BaseResult<S, E> { | ||
interface OkResult<T, E> extends BaseResult<T, E> { | ||
readonly ok: false; | ||
@@ -13,10 +13,14 @@ readonly err: true; | ||
} | ||
interface ErrorResult<S, E> extends BaseResult<S, E> { | ||
interface ErrorResult<T, E> extends BaseResult<T, E> { | ||
readonly ok: true; | ||
readonly err: false; | ||
readonly val: S; | ||
readonly val: T; | ||
} | ||
export declare type Result<S, E> = OkResult<S, E> | ErrorResult<S, E>; | ||
export declare function Ok<S, E>(val: S): Result<S, E>; | ||
export declare function Err<S, E>(val: E): Result<S, E>; | ||
export declare type Result<T, E> = OkResult<T, E> | ErrorResult<T, E>; | ||
export declare function Ok<T, E>(val: T): Result<T, E>; | ||
export declare function Err<T, E>(val: E): Result<T, E>; | ||
export declare function Results<T1, E1, T2, E2>(result1: Result<T1, E1>, result2: Result<T2, E2>): Result<[T1, T2], E1 | E2>; | ||
export declare function Results<T1, E1, T2, E2, T3, E3>(result1: Result<T1, E1>, result2: Result<T2, E2>, result3: Result<T3, E3>): Result<[T1, T2, T3], E1 | E2 | E3>; | ||
export declare function Results<T1, E1, T2, E2, T3, E3, T4, E4>(result1: Result<T1, E1>, result2: Result<T2, E2>, result3: Result<T3, E3>, result4: Result<T4, E4>): Result<[T1, T2, T3, T4], E1 | E2 | E3 | E4>; | ||
export declare function Results(...results: Result<any, any>[]): Result<any[], any>; | ||
export {}; |
12
index.js
@@ -49,2 +49,14 @@ class ResultImpl { | ||
} | ||
export function Results(...results) { | ||
const okResult = []; | ||
for (let result of results) { | ||
if (result.ok) { | ||
okResult.push(result.val); | ||
} | ||
else { | ||
return Err(result.val); | ||
} | ||
} | ||
return Ok(okResult); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ts-results", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A typescript implementation of Rust's Result object.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,3 +15,3 @@ # ts-results | ||
```typescript | ||
import { Result, Err, Ok } from 'ts-results'; | ||
import { Result, Err, Ok, Results } from 'ts-results'; | ||
``` | ||
@@ -80,1 +80,14 @@ ### Creation | ||
``` | ||
### Combining Results | ||
There may be some cases where we have two or more separate `Result` objects and we want to do something with both values. | ||
This is handled by using the `Results` function to combine them. | ||
```typescript | ||
let pizzaResult: Result<Pizza, GetPizzaError> = getPizzaSomehow(); | ||
let toppingsResult: Result<Toppings, GetToppingsError> = getToppingsSomehow(); | ||
let result = Results(pizzaResult, toppingsResult); // Result<[Pizza, Toppings], GetPizzaError | GetToppingsError> | ||
let [pizza, toppings] = result.unwrap(); // pizza is a Pizza, toppings is a Toppings. Could throw GetPizzaError or GetToppingsError. | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8416
85
92