Ƭ None: null
| undefined
None represents a value that is not present. Unfortunately in javascript, null and undefined are not the same thing, but we can use them interchangeably within the option type.
Since
4.0.0
Ƭ Some<A
>: A
extends None
? never
: A
Some<A>
represents a value which cannot be null
or undefined
Since
4.0.0
Name | Description |
---|---|
A |
the some type |
The type Option.T<A>
represents a value that could be both A
or null
or undefined
.
Since
4.0.0
Name | Description |
---|---|
A |
the option type |
▸ isNone(x
): x is None
Returns true
if the option is None
, false
otherwise.
import { Option } from 'tiinvo';
Option.isNone(1); // false
Option.isNone(null); // true
Option.isNone(undefined); // true
Since
4.0.0
Name | Type | Description |
---|---|---|
x |
unknown |
the argument to check if none |
x is None
true
if x
is None
false
otherwise▸ isSome(x
): x is unknown
Returns true
if the option is Some<unknown>
, false
otherwise.
import { Option } from 'tiinvo';
Option.isSome(1) // true
Option.isSome(null) // false
Option.isSome(undefined) // false
Since
4.0.0
Name | Type |
---|---|
x |
unknown |
x is unknown
true
if x
is Some<unknown>
false
otherwise▸ guardOf<A
>(f
): Guardable
<T
<A
>>
Returns true
if the option is Some<A>
and the value type is satisfied by the guard, otherwise false
.
If the option is None
, it will always return true
.
import { Num, Option } from 'tiinvo';
const x = 1
const y = null
const z = undefined
const w = `a`
const isnumsome = Option.guardOf(Num.guard);
isnumsome(x) // true
isnumsome(y) // true
isnumsome(z) // true
isnumsome(w) // false
Since
4.0.0
Name | Description |
---|---|
A |
the type |
Name | Type | Description |
---|---|---|
f |
Guardable <A > |
the guard |
the new Guardable<T<A>>
which returns
true
if x
is None
or is Some<A>
false
otherwise▸ tryAsync<F
>(f
): (…args
: Parameters
<F
>) => Promise
<T
<ReturnType
<F
>>> extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<None
| Awaited
<Some
<ReturnType
<F
>>>> : (…args
: Parameters
<F
>) => Promise
<T
<ReturnType
<F
>>>
Calls a function F
with it’s arguments and returns a Promise<Option.T<ReturnType<f>>>
import { Option, Num } from 'tiinvo';
const fn = async (arg: number) => {
if (Num.isEven(arg)) {
return arg * 2;
}
throw new Error(`${arg} is not even`);
}
const safe = Option.tryAsync(fn);
await safe(2) // 4
await safe(3) // null
Option.isSome(await safe(2)) // true
Option.isNone(await safe(3)) // true
Since
4.0.0
Name | Type | Description |
---|---|---|
F |
extends AnyAsyncFn |
the function type |
Name | Type | Description |
---|---|---|
f |
F |
the function to wrap |
(…args
: Parameters
<F
>) => Promise
<T
<ReturnType
<F
>>> extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<None
| Awaited
<Some
<ReturnType
<F
>>>> : (…args
: Parameters
<F
>) => Promise
<T
<ReturnType
<F
>>>
the wrapped function
Promise<None>
if the function catchedPromise<ReturnValue<F>>
▸ trySync<F
>(f
): (…args
: Parameters
<F
>) => T
<ReturnType
<F
>> extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<None
| Awaited
<Some
<ReturnType
<F
>>>> : (…args
: Parameters
<F
>) => T
<ReturnType
<F
>>
Calls a function F
with it’s arguments and returns a Option.T<ReturnType<f>>
import { Option, Num } from 'tiinvo';
const fn = (arg: number) => {
if (Num.isEven(arg)) {
return arg * 2;
}
throw new Error(`${arg} is not even`);
}
const safe = Option.trySync(fn);
safe(2) // 4
safe(3) // null
Option.isSome(safe(2)) // true
Option.isNone(safe(3)) // true
Since
4.0.0
Name | Type | Description |
---|---|---|
F |
extends AnyFn |
the function type |
Name | Type | Description |
---|---|---|
f |
F |
the function to wrap |
(…args
: Parameters
<F
>) => T
<ReturnType
<F
>> extends AnyAsyncFn
? (…args
: Parameters
<F
>) => Promise
<None
| Awaited
<Some
<ReturnType
<F
>>>> : (…args
: Parameters
<F
>) => T
<ReturnType
<F
>>
the wrapped function
None
if the function catchedReturnValue<F>
▸ cmp<A
>(c
, a
, b
): ComparableResult
Compares two options T<A>
by a given Comparable<A>
.
Example
import { Str, Option } from 'tiinvo';
Option.cmp(Str.cmp, "a", "a") // 0
Option.cmp(Str.cmp, "a", "b") // -1
Option.cmp(Str.cmp, "b", "a") // 1
Option.cmp(Str.cmp, null, undefined) // 0
Option.cmp(Str.cmp, null, "a") // -1
Option.cmp(Str.cmp, "a", undefined) // 1
Since
4.0.0
Name | Description |
---|---|
A |
the type |
Name | Type | Description |
---|---|---|
c |
Comparable <A > |
the Comparable<A> |
a |
T <A > |
the left-hand compared Option.T<A> |
b |
T <A > |
the right-hand compared Option.T<A> |
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ cmp<A
>(c
, a
, b
): ComparableResult
Compares two options T<A>
by a given ComparableModule<A>
.
Example
import { Str, Option } from 'tiinvo';
Option.cmp(Str, "a", "a") // 0
Option.cmp(Str, "a", "b") // -1
Option.cmp(Str, "b", "a") // 1
Option.cmp(Str, null, undefined) // 0
Option.cmp(Str, null, "a") // -1
Option.cmp(Str, "a", undefined) // 1
Since
4.0.0
Name | Description |
---|---|
A |
the type |
Name | Type | Description |
---|---|---|
c |
ComparableModule <A > |
the ComparableModule<A> |
a |
T <A > |
the left-hand compared Option.T<A> |
b |
T <A > |
the right-hand compared Option.T<A> |
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ cmp<A
>(c
, a
): Unary
<T
<A
>, ComparableResult
>
Compares two options T<A>
by a given Comparable<A>
.
Example
import { Str, Option } from 'tiinvo';
Option.cmp(Str.cmp, "a")("a") // 0
Option.cmp(Str.cmp, "a")("b") // 1
Option.cmp(Str.cmp, "b")("a") // -1
Option.cmp(Str.cmp, null)(undefined) // 0
Option.cmp(Str.cmp, null)("a") // 1
Option.cmp(Str.cmp, "a")(undefined) // -1
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
c |
Comparable <A > |
the Comparable<A> functor |
a |
T <A > |
the right-hand compared value |
Unary
<T
<A
>, ComparableResult
>
the unary comparer functor which takes the left-hand compared value and returns
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ cmp<A
>(c
, a
): Unary
<T
<A
>, ComparableResult
>
Compares two options T<A>
by a given ComparableModule<A>
.
Example
import { Str, Option } from 'tiinvo';
Option.cmp(Str, "a")("a") // 0
Option.cmp(Str, "a")("b") // 1
Option.cmp(Str, "b")("a") // -1
Option.cmp(Str, null)(undefined) // 0
Option.cmp(Str, null)("a") // 1
Option.cmp(Str, "a")(undefined) // -1
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
c |
ComparableModule <A > |
the ComparableModule<A> functor |
a |
T <A > |
the right-hand compared value |
Unary
<T
<A
>, ComparableResult
>
the unary comparer functor which takes the left-hand compared value and returns
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ cmp<A
>(c
): Binary
<T
<A
>, T
<A
>, ComparableResult
>
Compares two options T<A>
by a given Comparable<A>
.
Example
import { Str, Option } from 'tiinvo';
const cmp = Option.cmp(Str.cmp)
cmp("a", "a") // 0
cmp("a", "b") // -1
cmp("b", "a") // 1
cmp(null, undefined) // 0
cmp(null, "a") // -1
cmp("a", undefined) // 1
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
c |
Comparable <A > |
the Comparable<A> functor |
Binary
<T
<A
>, T
<A
>, ComparableResult
>
the binary comparer functor which takes two values a
and b
and returns
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ cmp<A
>(c
): Binary
<T
<A
>, T
<A
>, ComparableResult
>
Compares two options T<A>
by a given ComparableModule<A>
.
Example
import { Str, Option } from 'tiinvo';
const cmp = Option.cmp(Str)
cmp("a", "a") // 0
cmp("a", "b") // -1
cmp("b", "a") // 1
cmp(null, undefined) // 0
cmp(null, "a") // -1
cmp("a", undefined) // 1
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
c |
ComparableModule <A > |
the ComparableModule<A> functor |
Binary
<T
<A
>, T
<A
>, ComparableResult
>
the binary comparer functor which takes two values a
and b
and returns
0
if a
equals to b
or both a
and b
are None
1
if a
is greater than b
or a
is Some<A>
and b
is None
-1
if a
is less than b
or a
is None
and b
is Some<A>
▸ eq<A
>(e
, a
, b
): boolean
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
const eq = Option.eq(Num.eq);
Option.eq(Num.eq, 0, 0) // true
Option.eq(Num.eq, 1, 0) // false
Option.eq(Num.eq, 0, 1) // false
Option.eq(Num.eq, null, 1) // false
Option.eq(Num.eq, null, undefined) // true
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
Equatable <A > |
the equatable functor |
a |
T <A > |
the left-hand compared value |
b |
T <A > |
the right-hand compared value |
boolean
true
if a
equals to b
false
otherwise▸ eq<A
>(e
, a
, b
): boolean
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
Option.eq(Num.eq, 0, 0) // true
Option.eq(Num.eq, 1, 0) // false
Option.eq(Num.eq, 0, 1) // false
Option.eq(Num.eq, null, 1) // false
Option.eq(Num.eq, null, undefined) // true
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
EquatableModule <A > |
the equatable functor module |
a |
T <A > |
the left-hand compared value |
b |
T <A > |
the right-hand compared value |
boolean
true
if a
equals to b
false
otherwise▸ eq<A
>(e
, a
): Unary
<T
<A
>, boolean
>
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
Option.eq(Num.eq, 0)(0) // true
Option.eq(Num.eq, 0)(1) // false
Option.eq(Num.eq, null)(undefined) // true
Option.eq(Num.eq, null)(100000000) // false
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
Equatable <A > |
the equatable functor |
a |
T <A > |
the right-hand compared value |
the unary function which returns
true
if a
equals to b
false
otherwise▸ eq<A
>(e
, a
): Unary
<T
<A
>, boolean
>
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
Option.eq(Num, 0)(0) // true
Option.eq(Num, 0)(1) // false
Option.eq(Num, null)(undefined) // true
Option.eq(Num, null)(100000000) // false
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
EquatableModule <A > |
the equatable functor module |
a |
T <A > |
the right-hand compared value |
the unary function which returns
true
if a
equals to b
false
otherwise▸ eq<A
>(e
): Binary
<T
<A
>, T
<A
>, boolean
>
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
const eq = Option.eq(Num.eq);
eq(0, 0) // true
eq(null, undefined) // true
eq(null, 0) // false
eq(0, null) // false
eq(1_000_000, 0) // false
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
Equatable <A > |
the equatable functor |
the binary function which returns
true
if a
equals to b
false
otherwise▸ eq<A
>(e
): Binary
<T
<A
>, T
<A
>, boolean
>
Returns true if two options T<A>
are equal, false otherwise.
import { Num, Option } from 'tiinvo';
const eq = Option.eq(Num);
eq(0, 0) // true
eq(null, undefined) // true
eq(null, 0) // false
eq(0, null) // false
eq(1_000_000, 0) // false
Since
4.0.0
Name |
---|
A |
Name | Type | Description |
---|---|---|
e |
EquatableModule <A > |
the equatable functor module |
the binary function which returns
true
if a
equals to b
false
otherwise▸ filter<A
>(a
, b
): T
<A
>
Returns Some<A>
if the value is Some<A>
and the predicate returns true, otherwise returns None
.
import { Option, Num } from 'tiinvo';
Option.filter(Num.gt(1), 1) // null
Option.filter(Num.gt(1), 2) // 2
Option.filter(Num.gt(1), null) // null
Since
4.0.0
Name | Description |
---|---|
A |
the type to filter |
Name | Type | Description |
---|---|---|
a |
Mappable <A , boolean > |
the predicate |
b |
T <A > |
the value to filter |
T
<A
>
T<A>
if the predicate is satisfiedNone
if the predicate is not satisfied▸ filter<A
>(a
): Unary
<T
<A
>, T
<A
>>
Returns Some<A>
if the value is Some<A>
and the predicate returns true, otherwise returns None
.
import { Option, Num } from 'tiinvo';
const f = Option.filter(Num.gt(1));
f(1) // null
f(2) // 2
f(null) // null
Since
4.0.0
Name | Description |
---|---|
A |
the type to filter |
Name | Type | Description |
---|---|---|
a |
Mappable <A , boolean > |
the predicate |
the unary function which filters the value b
▸ map<A
, B
>(m
, a
): T
<B
>
Maps an Option.T<A>
to another Option.T<b>
if is Some<A>
, otherwise returns None
.
import { Option, Num } from 'tiinvo';
const m = Option.map(Num.add(1));
Option.map(Num.add(1), 1) // 2
Option.map(Num.add(1), null) // null
Since
4.0.0
Name | Description |
---|---|
A |
the starting type |
B |
the mapped type |
Name | Type | Description |
---|---|---|
m |
Mappable <A , B > |
the mappable |
a |
T <A > |
the value to map |
T
<B
>
the mapped option T<B>
▸ map<A
, B
>(m
): Mappable
<T
<A
>, T
<B
>>
Maps an Option.T<A>
to another Option.T<b>
if is Some<A>
, otherwise returns None
.
import { Option, Num } from 'tiinvo';
const m = Option.map(Num.add(1));
m(1) // 2
m(null) // null
Since
4.0.0
Name | Description |
---|---|
A |
the starting type |
B |
the mapped type |
Name | Type | Description |
---|---|---|
m |
Mappable <A , B > |
the mappable |
the mappable functor which maps T<A>
to T<B>
▸ mapOr<A
, B
>(m
, a
, b
): B
Maps an Option.T<A>
to another Option.T<b>
if is Some<A>
, otherwise returns b
.
import { Option, Num } from 'tiinvo';
Option.mapOr(Num.add(2), 1, 0) // 3
Option.mapOr(Num.add(2), null, 0) // 0
Since
4.0.0
Name | Description |
---|---|
A |
the starting type |
B |
the mapped type |
Name | Type | Description |
---|---|---|
m |
Mappable <A , B > |
the mappable |
a |
T <A > |
the value to map |
b |
B |
the fallback value |
B
the mapped value B
or the fallback value b
if a
is None
▸ mapOr<A
, B
>(m
, a
): Unary
<T
<A
>, B
>
Maps an Option.T<A>
to another Option.T<b>
if is Some<A>
, otherwise returns b
.
import { Option, Num } from 'tiinvo';
const m = Option.mapOr(Num.add(2), 0);
m(1) // 3
m(null) // 0
Since
4.0.0
Name | Description |
---|---|
A |
the starting type |
B |
the mapped type |
Name | Type | Description |
---|---|---|
m |
Mappable <A , B > |
the mappable |
a |
B |
the fallback value |
the mappable functor which maps A
to B
or returns the fallback value a
if b
is None