ts-results-es
Version:
A TypeScript implementation of Rust's Result and Option objects.
244 lines • 7.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resultMap = resultMap;
exports.resultMapErr = resultMapErr;
exports.resultMapTo = resultMapTo;
exports.resultMapErrTo = resultMapErrTo;
exports.elseMap = elseMap;
exports.elseMapTo = elseMapTo;
exports.resultSwitchMap = resultSwitchMap;
exports.resultMergeMap = resultMergeMap;
exports.filterResultOk = filterResultOk;
exports.filterResultErr = filterResultErr;
exports.tapResultErr = tapResultErr;
exports.tapResultOk = tapResultOk;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var index_js_1 = require("../index.js");
/**
* Allows you to do the same actions as the normal rxjs `map` operator on a stream of Result objects.
*
* @example
* ```typescript
* import { of, Observable } from 'rxjs';
* import { Ok, Err, Result } from 'ts-results-es';
* import { resultMap } from 'ts-results-es/rxjs-operators';
*
* const obs$: Observable<Result<number, Error>> = of(Ok(5), Err('uh oh'));
*
* const greaterThanZero = obs$.pipe(
* resultMap((number) => number > 0), // Return true for values greater zero
* ); // Has type Observable<Result<boolean, 'uh oh'>>
*
* greaterThanZero.subscribe((result) => {
* if (result.isOk()) {
* console.log('Was greater than zero: ' + result.value);
* } else {
* console.log('Got Error Message: ' + result.error);
* }
* });
*
* // Logs the following:
* // Was greater than zero: true
* // Got Error Message: uh oh
* ```
*/
function resultMap(mapper) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) { return result.map(mapper); }));
};
}
/**
* Behaves exactly the same as `resultMap`, but maps the error value.
*/
function resultMapErr(mapper) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) { return result.mapErr(mapper); }));
};
}
/**
* Behaves the same as `resultMap`, but takes a value instead of a function.
*/
function resultMapTo(value) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) { return result.map(function () { return value; }); }));
};
}
/**
* Behaves the same as `resultMapErr`, but takes a value instead of a function.
*/
function resultMapErrTo(value) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) { return result.mapErr(function () { return value; }); }));
};
}
/**
* Allows you to turn a stream of Result objects into a stream of values,
* transforming any errors into a value.
*
* Similar to calling the `else` function, but works on a stream of Result objects.
*
* @example
* ```typescript
* import { of, Observable } from 'rxjs';
* import { Ok, Err, Result } from 'ts-results-es';
* import { elseMap } from 'ts-results-es/rxjs-operators';
*
* const obs$: Observable<Result<number, Error>> = of(Ok(5), Err(new Error('uh oh')));
*
* const doubled = obs$.pipe(
* elseMap((err) => {
* console.log('Got error: ' + err.message);
*
* return -1;
* }),
* ); // Has type Observable<number>
*
* doubled.subscribe((number) => {
* console.log('Got number: ' + number);
* });
*
* // Logs the following:
* // Got number: 5
* // Got error: uh oh
* // Got number: -1
* ```
*/
function elseMap(mapper) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) {
if (result.isErr()) {
return mapper(result.error);
}
else {
return result.value;
}
}));
};
}
/**
* Behaves the same as `elseMap`, but takes a value instead of a function.
*/
function elseMapTo(value) {
return function (source) {
return source.pipe((0, operators_1.map)(function (result) {
if (result.isErr()) {
return value;
}
else {
return result.value;
}
}));
};
}
function resultSwitchMap(mapper) {
return function (source) {
return source.pipe((0, operators_1.switchMap)(function (result) {
if (result.isOk()) {
return mapper(result.value);
}
else {
return (0, rxjs_1.of)(result);
}
}), (0, operators_1.map)(function (result) {
if (index_js_1.Result.isResult(result)) {
return result;
}
else {
return new index_js_1.Ok(result);
}
}));
};
}
function resultMergeMap(mapper) {
return function (source) {
return source.pipe((0, operators_1.mergeMap)(function (result) {
if (result.isOk()) {
return mapper(result.value);
}
else {
return (0, rxjs_1.of)(result);
}
}), (0, operators_1.map)(function (result) {
if (index_js_1.Result.isResult(result)) {
return result;
}
else {
return new index_js_1.Ok(result);
}
}));
};
}
/**
* Converts an `Observable<Result<T, E>>` to an `Observable<T>` by
* filtering out the Errs and mapping to the Ok values.
*
* @example
* ```typescript
* import { of, Observable } from 'rxjs';
* import { Ok, Err, Result } from 'ts-results-es';
* import { filterResultOk } from 'ts-results-es/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
* ```
*/
function filterResultOk() {
return function (source) {
return source.pipe((0, operators_1.filter)(function (result) { return result.isOk(); }), (0, operators_1.map)(function (result) { return result.value; }));
};
}
/**
* Converts an `Observable<Result<T, E>>` to an `Observable<E>` by
* filtering out the Oks and mapping to the error values.
*
* @example
* ```typescript
* import { of, Observable } from 'rxjs';
* import { Ok, Err, Result } from 'ts-results-es';
* import { filterResultErr } from 'ts-results-es/rxjs-operators';
*
* const obs$: Observable<Result<number, Error>> = of(new Ok(5), new Err(new Error('uh oh')));
*
* const test$ = obs$.pipe(filterResultErr()); // Has type Observable<Error>
*
* test$.subscribe((result) => {
* console.log('Got error: ' + result);
* });
*
* // Logs the following:
* // Got error: uh oh
* ```
*/
function filterResultErr() {
return function (source) {
return source.pipe((0, operators_1.filter)(function (result) { return result.isErr(); }), (0, operators_1.map)(function (result) { return result.error; }));
};
}
function tapResultErr(tapFn) {
return function (source) {
return source.pipe((0, operators_1.tap)(function (r) {
if (!r.isOk()) {
tapFn(r.error);
}
}));
};
}
function tapResultOk(tapFn) {
return function (source) {
return source.pipe((0, operators_1.tap)(function (r) {
if (r.isOk()) {
tapFn(r.value);
}
}));
};
}
//# sourceMappingURL=index.js.map