Package migrated from ts-results
to @utx/result
for continued development.
- Added
Result.match
method. Usage:function sayHello(result: Result<string, Error>) { const phrase = result.match({ Ok: (name) => `Hello ${name}!`, Err: (err) => `Would say hello but an error occurred`, }) return phrase; }
- Support Native Node ESM with MJS files
- (Internal) Switch to using Rollup to build CJS and ESM bundles
Big thank you to @petehunt for all his work adding stack traces to Err
.
- Added a
stack
property to allErr
objects. Can be used to pull a stack trace - Added
toOption
andtoResult
methods for converting betweenOption
andResult
objects
- Fix regression found in Issue#24
- Fixes for Typescript 4.2
Big thank you to @petehunt for all his work adding the Option
type.
-
Added new
Option<T>
,Some<T>
, andNone
types!-
You should feel at home if you're used to working with Rust:
import { Option, Some, None } from '@utx/result'; const optionalNum: Option<number> = Some(3).map((num) => num * 2); if (optionalNum.some) { console.log(optionalNum.val === 6); // prints `true` } const noneNum: Option<number> = None; if (noneNum.some) { // You'll never get in here }
-
-
Added new
Option.isOption
andResult.isResult
helper functions.
- Got to 100% test coverage on all code!
- Removed uses of
@ts-ignore
Huge shout out to @Jack-Works for helping get this release out. Most of the work was his, and it would not have happened without him.
Ok<T>
andErr<T>
are now callable withoutnew
!- No longer breaks when calling from node
- Tree-shakable when using tools like rollup or webpack
- Fully unit tested
- Added these helper functions:
Result.all(...)
- Same asResults
from previous releases. Collects allOk
values, or returns the firstErr
value.Results.any(...)
- Returns the firstOk
value, or all of theErr
values.Result.wrap<T, E>(() => ...)
- Wraps an operation that may throw an error, uses try / catch to return aResult<T, E>
Result.wrapAsync<T, E>(() => ...)
- Same as the above, but async
- Deprecated
else
in favor ofunwrapOr
to prefer api parity with Rust
- core: Added
reaonly static EMPTY: Ok<void>;
toOk
class. - core: Added
reaonly static EMPTY: Err<void>;
toErr
class.
This release features a complete rewrite of most of the library with one focus in mind: simpler types.
The entire library now consists of only the following:
- Two classes:
Ok<T>
andErr<E>
. - A
Result<T, E>
type that is a simple or type between the two classes. - A simple
Results
function that allows combining multiple results.
- core: much simpler Typescript types
- rxjs: added new
filterResultOk
andfilterResultErr
operators - rxjs: added new
resultMapErrTo
operator
- core:
Err
andOk
now requirenew
:- Before:
let result = Ok(value); let error = Err(message);
- After:
let result = new Ok(value); let error = new Err(message);
- Before:
- core:
map
function broken into two functions:map
andmapErr
- before:
result.map(value => "new value", error => "new error")
- after:
result.map(value => "newValue").mapError(error => "newError")
- before:
- rxjs:
resultMap
operator broken into two operators:resultMap
andresultMapErr
- before:
obs.pipe(resultMap(value => "new value", error => "new error"))
- after:
result.pipe(resultMap(value => "newValue"), resultMapError(error => "newError"))
- before: