Ƭ Err: Error
Represents an error
Ƭ Ok<A>: A extends Error ? never : A
Represents the successful result of an operation
| Name |
|---|
A |
Could represent both an Error Err or a successful result of an operation Ok<a>
| Name |
|---|
A |
▸ err(a): Error
Returns an Err
Example
import { Result } from 'tiinvo';
Result.err(10) instanceof Error // true
Result.err(new TypeError('aaaa')) instanceof Error // true
Result.err({}) instanceof Error // true
Result.err(10n) instanceof Error // true
Result.err([10, 20, 30]) instanceof Error // true
Since
3.8.0
| Name | Type |
|---|---|
a |
unknown |
Error
▸ isErr(x): x is Error
Checks if a value is Err
Example
import { Result } from 'tiinvo';
Result.isErr(10) // false
Result.isErr(new TypeError('aaaa')) // true
Since
4.0.0
| Name | Type | Description |
|---|---|---|
x |
unknown |
the value to check |
x is Error
true if x is Err, false otherwise
▸ isOk<A>(x): x is Ok<A>
Checks if a value is Ok
Example
import { Result } from 'tiinvo';
Result.isOk(10) // true
Result.isOk(new TypeError('aaaa')) // false
Since
4.0.0
| Name |
|---|
A |
| Name | Type |
|---|---|
x |
T<A> |
x is Ok<A>
true if x is Ok<A>, false otherwise
▸ isOkOf<A>(g): (x: unknown) => x is A
Checks if a value is Ok<A>
import { Num, Result } from 'tiinvo';
const guard = Result.isOkOf(Num.guard);
guard(10) // true
guard("hello") // false
guard(new TypeError('aaaa')) // false
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
g |
Guardable<A> |
the type guard |
fn
the Guard
▸ (x): x is A
| Name | Type |
|---|---|
x |
unknown |
x is A
▸ cmp<A>(cmp, a, b): ComparableResult
Compares two results T<A> by a given Comparable<A>.
a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1Example
import { Str, Result } from 'tiinvo';
Result.cmp(Str.cmp, "a", "a") // 0
Result.cmp(Str.cmp, "a", "b") // -1
Result.cmp(Str.cmp, "b", "a") // 1
Result.cmp(Str.cmp, new Error(), new Error()) // 0
Result.cmp(Str.cmp, new Error(), "a") // -1
Result.cmp(Str.cmp, "a", new Error()) // 1
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
Comparable<A> |
the comparer function |
a |
T<A> |
first value |
b |
T<A> |
last value |
a is less than ba is same of ba is greater than b.▸ cmp<A>(cmp, a, b): ComparableResult
Compares two results T<A> by a given ComparableModule<A>.
a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1Example
import { Str, Result } from 'tiinvo';
Result.cmp(Str, "a", "a") // 0
Result.cmp(Str, "a", "b") // -1
Result.cmp(Str, "b", "a") // 1
Result.cmp(Str, new Error(), new Error()) // 0
Result.cmp(Str, new Error(), "a") // -1
Result.cmp(Str, "a", new Error()) // 1
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
ComparableModule<A> |
the comparer function |
a |
T<A> |
first value |
b |
T<A> |
last value |
a is less than ba is same of ba is greater than b.▸ cmp<A>(cmp, a): Unary<T<A>, ComparableResult>
Returns a unary function which compares two results T<A> by a given Comparable<A>.
If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1
Example
import { Str, Result } from 'tiinvo';
const cmp = Result.cmp(Str.cmp, "a");
cmp("a") // 0
cmp("b") // -1
cmp("a") // 1
cmp(new Error()) // 1
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
Comparable<A> |
the comparer function |
a |
T<A> |
first value |
Unary<T<A>, ComparableResult>
the unary function which returns
a is less than ba is same of ba is greater than b.▸ cmp<A>(cmp, a): Unary<T<A>, ComparableResult>
Returns a unary function which compares two results T<A> by a given ComparableModule<A>.
If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1
Example
import { Str, Result } from 'tiinvo';
const cmp = Result.cmp(Str, "a");
cmp("a") // 0
cmp("b") // -1
cmp("a") // 1
cmp(new Error()) // 1
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
ComparableModule<A> |
the comparer module |
a |
T<A> |
first value |
Unary<T<A>, ComparableResult>
the unary function which returns
a is less than ba is same of ba is greater than b.▸ cmp<A>(cmp): Binary<T<A>, T<A>, ComparableResult>
Returns a binary function which compares two results T<A> by a given Comparable<A>.
If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1
Example
import { Str, Result } from 'tiinvo';
const cmp = Result.cmp(Str.cmp);
cmp("a", "a") // 0
cmp("a", "b") // -1
cmp("b", "a") // 1
cmp("a", new Error()) // 1
cmp(new Error(), "a") // -1
cmp(new Error(), new Error()) // 0
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
Comparable<A> |
the comparer function |
Binary<T<A>, T<A>, ComparableResult>
the binary function which returns
a is less than ba is same of ba is greater than b.▸ cmp<A>(cmp): Binary<T<A>, T<A>, ComparableResult>
Returns a binary function which compares two results T<A> by a given ComparableModule<A>.
If a is Err and b is Ok returns -1, else if both are Err returns 0, else returns 1
Example
import { Str, Result } from 'tiinvo';
const cmp = Result.cmp(Str);
cmp("a", "a") // 0
cmp("a", "b") // -1
cmp("b", "a") // 1
cmp("a", new Error()) // 1
cmp(new Error(), "a") // -1
cmp(new Error(), new Error()) // 0
Since
4.0.0
| Name | Description |
|---|---|
A |
the type |
| Name | Type | Description |
|---|---|---|
cmp |
ComparableModule<A> |
the comparer module |
Binary<T<A>, T<A>, ComparableResult>
the binary function which returns
a is less than ba is same of ba is greater than b.▸ eq<A>(eq, a, b): boolean
Returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
Result.eq(Num.eq, 0, 0) // true
Result.eq(Num.eq, new Error(), new TypeError()) // true
Result.eq(Num.eq, new Error(), 0) // false
Result.eq(Num.eq, 0, new Error()) // false
Result.eq(Num.eq, 1_000_000, 0) // false
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
eq |
Equatable<A> |
the Equatable functor |
a |
T<A> |
the left compared value |
b |
T<A> |
the right compared value |
boolean
true if a equals to b
▸ eq<A>(eq, a, b): boolean
Returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
Result.eq(Num, 0, 0) // true
Result.eq(Num, new Error(), new TypeError()) // true
Result.eq(Num, new Error(), 0) // false
Result.eq(Num, 0, new Error()) // false
Result.eq(Num, 1_000_000, 0) // false
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
eq |
EquatableModule<A> |
the Equatable module |
a |
T<A> |
the left compared value |
b |
T<A> |
the right compared value |
boolean
true if a equals to b
▸ eq<A>(eq, a): Unary<T<A>, boolean>
Returns a unary function which returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
const is0 = Result.eq(Num.eq, 0)
Result.eq(0) // true
Result.eq(new TypeError()) // false
Result.eq(new Error()) // false
Result.eq(1_000_000) // false
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
eq |
Equatable<A> |
the Equatable functor |
a |
T<A> |
the left compared value |
the unary function
▸ eq<A>(eq, a): Unary<T<A>, boolean>
Returns a unary function which returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
const is0 = Result.eq(Num, 0)
Result.eq(0) // true
Result.eq(new TypeError()) // false
Result.eq(new Error()) // false
Result.eq(1_000_000) // false
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
eq |
EquatableModule<A> |
the Equatable functor |
a |
T<A> |
the left compared value |
the unary function
▸ eq<a>(eq): Binary<T<a>, T<a>, boolean>
Returns a binary function which returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
const is0 = Result.eq(Num.eq)
Result.eq(0, 0) // true
Result.eq(new TypeError(), new Error()) // true
Result.eq(0, new Error()) // false
Result.eq(1_000, 1_000) // true
Since
4.0.0
| Name |
|---|
a |
| Name | Type | Description |
|---|---|---|
eq |
Equatable<a> |
the Equatable functor |
the binary function
▸ eq<a>(eq): Binary<T<a>, T<a>, boolean>
Returns a binary function which returns true if two results are equal, false otherwise.
import { Num, Result } from 'tiinvo';
const is0 = Result.eq(Num)
Result.eq(0, 0) // true
Result.eq(new TypeError(), new Error()) // true
Result.eq(0, new Error()) // false
Result.eq(1_000, 1_000) // true
Since
4.0.0
| Name |
|---|
a |
| Name | Type | Description |
|---|---|---|
eq |
EquatableModule<a> |
the Equatable module |
the binary function
▸ filter<A>(f, a): T<A>
Returns Ok<A> if the value a is Ok<A> and the predicate is satisfied, otherwise returns Err.
import { Result, Num } from 'tiinvo';
Result.filter(Num.gt(1), 1) // Error("Value did not pass filter")
Result.filter(Num.gt(1), 2) // 2
Result.filter(Num.gt(1), new TypeError()) // Error("Value did not pass filter")
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
f |
Filterable<A> |
the Filterable functor |
a |
T<A> |
the value to filter |
T<A>
T<A> if the Filterable has been satisfied
▸ filter<A>(f): Unary<T<A>, T<A>>
Returns a unary function which checks if Result.T<A> is Ok<A> and the predicate is satisfied, otherwise returns Err.
import { Result, Num } from 'tiinvo';
const f = Result.filter(Num.gt(1));
f(1) // Error("Value did not pass filter")
f(2) // 2
f(new TypeError()) // Error("Value did not pass filter")
Since
4.0.0
| Name | Description |
|---|---|
A |
the value type |
| Name | Type | Description |
|---|---|---|
f |
Filterable<A> |
the Filterable functor |
the unary function
▸ map<A, B>(m): (a: T<A>) => T<B>
Maps Result.t<a> to Result.t<b> if ok, otherwise returns err
import { Num, Result } from 'tiinvo';
const m = Result.map(Num.add(10))
m(10) // 20
m(new Error('foobar!')) // Error('foobar!')
Since
4.0.0
| Name |
|---|
A |
B |
| Name | Type | Description |
|---|---|---|
m |
Mappable<A, B> |
the Mappable functor |
fn
▸ (a): T<B>
| Name | Type |
|---|---|
a |
T<A> |
T<B>
▸ mapOr<a, b>(m, b): (a: T<a>) => b
Maps Result.t<a> to Result.t<b> if ok, otherwise returns b
import { Str, Result } from 'tiinvo';
const map = Result.mapOr(Str.length, 0);
map('hello') // 5
map(new Error()) // 0
Since
4.0.0
| Name |
|---|
a |
b |
| Name | Type |
|---|---|
m |
Mappable<a, b> |
b |
b |
fn
▸ (a): b
| Name | Type |
|---|---|
a |
T<a> |
b
▸ tryAsync<f>(f): (…args: Parameters<f>) => Promise<T<ReturnType<f>>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => Promise<T<ReturnType<f>>>
Calls a function f with it’s arguments and returns a Promise<Result.t<ReturnType<f>>>
import { Result, Num } from 'tiinvo';
const fn = async (arg: number) => {
if (Num.isEven(arg)) {
return arg * 2;
}
throw new Error(`${arg} is not even`);
}
const safe = Result.tryAsync(fn);
await safe(2) // 4
await safe(3) // Error("3 is not even")
Result.isOk(await safe(2)) // true
Result.isErr(await safe(3)) // true
Since
4.0.0
| Name | Type |
|---|---|
f |
extends AnyAsyncFn |
| Name | Type |
|---|---|
f |
f |
(…args: Parameters<f>) => Promise<T<ReturnType<f>>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => Promise<T<ReturnType<f>>>
▸ trySync<f>(f): (…args: Parameters<f>) => T<ReturnType<f>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => T<ReturnType<f>>
Calls a function f with it’s arguments and returns a Promise<Result.t<ReturnType<f>>>
import { Result, Num } from 'tiinvo';
const fn = (arg: number) => {
if (Num.isEven(arg)) {
return arg * 2;
}
throw new Error(`${arg} is not even`);
}
const safe = Result.trySync(fn);
safe(2) // 4
safe(3) // Error("3 is not even")
Result.isOk(safe(2)) // true
Result.isErr(safe(3)) // true
Since
4.0.0
| Name | Type |
|---|---|
f |
extends AnyFn |
| Name | Type |
|---|---|
f |
f |
(…args: Parameters<f>) => T<ReturnType<f>> extends AnyAsyncFn ? (…args: Parameters<f>) => Promise<Error | Awaited<Ok<ReturnType<f>>>> : (…args: Parameters<f>) => T<ReturnType<f>>