ts-results
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0
@@ -37,1 +37,5 @@ export declare class Err<E> { | ||
export declare type ResultErrType<T extends Result<any, any>> = T extends Result<any, infer U> ? U : never; | ||
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>; |
12
index.js
@@ -69,2 +69,14 @@ export class Err { | ||
} | ||
export function Results(...results) { | ||
const okResult = []; | ||
for (let result of results) { | ||
if (result.ok) { | ||
okResult.push(result.val); | ||
} | ||
else { | ||
return new Err(result.val); | ||
} | ||
} | ||
return new Ok(okResult); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "ts-results", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0", | ||
"description": "A typescript implementation of Rust's Result object.", | ||
@@ -14,6 +14,3 @@ "main": "index.js", | ||
}, | ||
"peerDependencies": { | ||
"rxjs": "^6.1.0" | ||
}, | ||
"readme": "README.md" | ||
} |
155
README.md
# ts-results | ||
A typescript implementation of [Rust's Result](https://doc.rust-lang.org/std/result/) object. | ||
A typescript implementation of [Rust's Result](https://doc.rust-lang.org/std/result/) object. Brings compile-time error checking to typescript. | ||
*Note: This is the documentation for the newly released `ts-results@2.0.0` with breaking changes. To see breaking changes, go to [CHANGELOG.md](https://github.com/vultix/ts-results/blob/master/CHANGELOG.md)* | ||
## Contents | ||
* [Installation](#installation) | ||
* [Example](#example) | ||
* [Usage](#usage) | ||
@@ -12,3 +14,3 @@ * [Creation](#creation) | ||
* [Expect](#expect) | ||
* [Map](#map) | ||
* [Map, MapErr](#map-and-maperr) | ||
* [Else](#else) | ||
@@ -18,6 +20,10 @@ * [Combining Results](#combining-results) | ||
* [resultMap](#resultmap) | ||
* [resultMapErr](#resultmaperr) | ||
* [resultMapTo](#resultmapto) | ||
* [resultMapErrTo](#resultmapto) | ||
* [elseMap](#elsemap) | ||
* [elseMapTo](#elsemapto) | ||
* [resultSwitchMap, resultMergeMap](#resultswitchmap-and-resultmergemap) | ||
* [filterResultOk](#filterresultok) | ||
* [filterResultErr](#filterresulterr) | ||
@@ -33,2 +39,43 @@ ## Installation | ||
## Example | ||
Convert this: | ||
```typescript | ||
import {existsSync, readFileSync} from 'fs'; | ||
function readFile(path: string): string { | ||
if (existsSync(path)) { | ||
return readFileSync(path); | ||
} else { | ||
// Callers of readFile have no way of knowing the function can fail | ||
throw new Error('invalid path'); | ||
} | ||
} | ||
// This line may fail unexpectedly without warnings from typescript | ||
const text = readFile('test.txt'); | ||
``` | ||
To this: | ||
```typescript | ||
import {existsSync, readFileSync} from 'fs'; | ||
import {Ok, Err, Result} from 'ts-results'; | ||
function readFile(path: string): Result<string, 'invalid path'> { | ||
if (existsSync(path)) { | ||
return new Ok(readFileSync(path)); | ||
} else { | ||
return new Err("invalid path"); | ||
} | ||
} | ||
// Typescript now forces you to check whether you have a valid result at compile time. | ||
const result = readFile('test.txt'); | ||
if (result.ok) { | ||
// text contains the file's content | ||
const text = result.val; | ||
} else { | ||
// err equals 'invalid path' | ||
const err = result.val; | ||
} | ||
``` | ||
## Usage | ||
@@ -40,6 +87,6 @@ ```typescript | ||
```typescript | ||
let okResult: Result<number, Error> = Ok(10); | ||
let okResult: Result<number, Error> = new Ok(10); | ||
let okResult2 = Ok<number, Error>(10); // Exact same as above | ||
let errorResult: Result<number, Error> = Ok(new Error('bad number!')); | ||
let errorResult: Result<number, Error> = new Ok(new Error('bad number!')); | ||
let errorResult2 = Ok<number, Error>(new Error('bad number!')); // Exact same as above | ||
@@ -71,4 +118,4 @@ | ||
```typescript | ||
let goodResult = Ok<number, Error>(1); | ||
let badResult = Err<number, Error>(new Error("something went wrong")); | ||
let goodResult = new Ok(1); | ||
let badResult = new Err(new Error("something went wrong")); | ||
@@ -88,6 +135,6 @@ goodResult.unwrap(); // 1 | ||
#### Map | ||
#### Map and MapErr | ||
```typescript | ||
let goodResult = Ok<number, Error>(1); | ||
let badResult = Err<number, Error>(new Error("something went wrong")); | ||
let goodResult = new Ok(1); | ||
let badResult = new Err(new Error("something went wrong")); | ||
@@ -97,7 +144,4 @@ goodResult.map(num => num + 1).unwrap(); // 2 | ||
goodResult.map(num => num + 1, err => new Error('mapped')).unwrap(); // 2 | ||
badResult.map(num => num + 1, err => new Error('mapped')).unwrap(); // throws Error("mapped") | ||
goodResult.map(null, err => new Error('mapped')).unwrap(); // 1 | ||
badResult.map(null, err => new Error('mapped')).unwrap(); // throws Error("mapped") | ||
goodResult.map(num => num + 1).mapErr(err => new Error('mapped')).unwrap(); // 2 | ||
badResult.map(num => num + 1).mapErr(err => new Error('mapped')).unwrap(); // throws Error("mapped") | ||
``` | ||
@@ -107,4 +151,4 @@ | ||
```typescript | ||
let goodResult = Ok<number, Error>(1); | ||
let badResult = Err<number, Error>(new Error("something went wrong")); | ||
let goodResult = new Ok(1); | ||
let badResult = new Err(new Error("something went wrong")); | ||
@@ -137,12 +181,11 @@ goodResult.else(5); // 1 | ||
const obs$: Observable<Result<number, Error>> = of(Ok(5), Err(new Error('uh oh'))); | ||
const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err('uh oh')); | ||
const doubled = obs$.pipe( | ||
resultMap(number => number * 2), // Doubles the value | ||
resultMap(null, err => err.message), // You can also map the error | ||
); // Has type Observable<Result<number, string>> | ||
const greaterThanZero = obs$.pipe( | ||
resultMap(number => number > 0), // Doubles the value | ||
); // Has type Observable<Result<boolean, 'uh oh'>> | ||
doubled.subscribe(result => { | ||
greaterThanZero.subscribe(result => { | ||
if (result.ok) { | ||
console.log('Got number: ' + result.val); | ||
console.log('Was greater than zero: ' + result.val); | ||
} else { | ||
@@ -157,2 +200,9 @@ console.log('Got Error Message: ' + result.val); | ||
``` | ||
#### resultMapErr | ||
```typescript | ||
import {resultMapErr} from 'ts-results/rxjs-operators'; | ||
``` | ||
Behaves exactly the same as [resultMap](#resultmap), but maps the error value. | ||
#### resultMapTo | ||
@@ -164,2 +214,8 @@ ```typescript | ||
#### resultMapErrTo | ||
```typescript | ||
import {resultMapErrTo} from 'ts-results/rxjs-operators'; | ||
``` | ||
Behaves the same as [resultMapErr](#resultmaperr), but takes a value instead of a function. | ||
#### elseMap | ||
@@ -174,3 +230,3 @@ Allows you to turn a stream of Result objects into a stream of values, transforming any errors into a value. | ||
const obs$: Observable<Result<number, Error>> = of(Ok(5), Err(new Error('uh oh'))); | ||
const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err(new Error('uh oh'))); | ||
@@ -212,5 +268,5 @@ const doubled = obs$.pipe( | ||
const obs$: Observable<Result<number, Error>> = of(Ok(5), Err(new Error('uh oh'))); | ||
const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err(new Error('uh oh'))); | ||
const obs2$: Observable<Result<string, CustomError>> = of(Ok('hi'), Err(new CustomError('custom error'))); | ||
const obs2$: Observable<Result<string, CustomError>> = of(new Ok('hi'), new Err(new CustomError('custom error'))); | ||
@@ -239,1 +295,48 @@ const test$ = obs$.pipe( | ||
``` | ||
#### filterResultOk | ||
Converts an `Observable<Result<T, E>>` to an `Observble<T>` by filtering out the Errs and mapping to the Ok values. | ||
```typescript | ||
import {of, Observable} from 'rxjs'; | ||
import {Ok, Err, Result} from 'ts-results'; | ||
import {filterResultOk} from 'ts-results/rxjs-operators'; | ||
const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err(new Error('uh oh'))); | ||
const test$ = obs$.pipe( | ||
filterResultOk() | ||
); // Has type Observable<number> | ||
test$.subscribe(result => { | ||
console.log('Got number: ' + result); | ||
}); | ||
// Logs the following: | ||
// Got number: 5 | ||
``` | ||
#### filterResultErr | ||
Converts an `Observable<Result<T, E>>` to an `Observble<T>` by filtering out the Oks and mapping to the error values. | ||
```typescript | ||
import {of, Observable} from 'rxjs'; | ||
import {Ok, Err, Result} from 'ts-results'; | ||
import {filterResultOk} from 'ts-results/rxjs-operators'; | ||
const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err(new Error('uh oh'))); | ||
const test$ = obs$.pipe( | ||
filterResultOk() | ||
); // Has type Observable<number> | ||
test$.subscribe(result => { | ||
console.log('Got number: ' + result); | ||
}); | ||
// Logs the following: | ||
// Got number: 5 | ||
``` |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
24451
0
229
1
329