tiinvo

tiinvo / Exports / Opt

Namespace: Opt

Table of contents

Type Aliases

Functions

Comparables

Filterables

Mappables

Type Aliases

None

Ƭ 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

Defined in

src/Option.ts:12


Some

Ƭ Some<A>: A extends None ? never : A

Some<A> represents a value which cannot be null or undefined

Since

4.0.0

Type parameters

Name Description
A the some type

Defined in

src/Option.ts:19


T

Ƭ T<A>: Some<A> | None

The type Option.T<A> represents a value that could be both A or null or undefined.

Since

4.0.0

Type parameters

Name Description
A the option type

Defined in

src/Option.ts:25

Functions

isNone

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

Parameters

Name Type Description
x unknown the argument to check if none

Returns

x is None

Defined in

src/Functors.ts:338


isSome

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

Parameters

Name Type
x unknown

Returns

x is unknown

Defined in

src/Functors.ts:338


guardOf

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

Type parameters

Name Description
A the type

Parameters

Name Type Description
f Guardable<A> the guard

Returns

Guardable<T<A>>

the new Guardable<T<A>> which returns

Defined in

src/Option.ts:92


tryAsync

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

Type parameters

Name Type Description
F extends AnyAsyncFn the function type

Parameters

Name Type Description
f F the function to wrap

Returns

(…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

Defined in

src/Option.ts:646


trySync

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

Type parameters

Name Type Description
F extends AnyFn the function type

Parameters

Name Type Description
f F the function to wrap

Returns

(…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

Defined in

src/Option.ts:689

Comparables

cmp

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

Type parameters

Name Description
A the type

Parameters

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>

Returns

ComparableResult

Defined in

src/Option.ts:123

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

Type parameters

Name Description
A the type

Parameters

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>

Returns

ComparableResult

Defined in

src/Option.ts:151

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

Type parameters

Name
A

Parameters

Name Type Description
c Comparable<A> the Comparable<A> functor
a T<A> the right-hand compared value

Returns

Unary<T<A>, ComparableResult>

the unary comparer functor which takes the left-hand compared value and returns

Defined in

src/Option.ts:177

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

Type parameters

Name
A

Parameters

Name Type Description
c ComparableModule<A> the ComparableModule<A> functor
a T<A> the right-hand compared value

Returns

Unary<T<A>, ComparableResult>

the unary comparer functor which takes the left-hand compared value and returns

Defined in

src/Option.ts:203

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

Type parameters

Name
A

Parameters

Name Type Description
c Comparable<A> the Comparable<A> functor

Returns

Binary<T<A>, T<A>, ComparableResult>

the binary comparer functor which takes two values a and b and returns

Defined in

src/Option.ts:230

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

Type parameters

Name
A

Parameters

Name Type Description
c ComparableModule<A> the ComparableModule<A> functor

Returns

Binary<T<A>, T<A>, ComparableResult>

the binary comparer functor which takes two values a and b and returns

Defined in

src/Option.ts:257


eq

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

Type parameters

Name
A

Parameters

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

Returns

boolean

Defined in

src/Option.ts:312

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

Type parameters

Name
A

Parameters

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

Returns

boolean

Defined in

src/Option.ts:335

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

Type parameters

Name
A

Parameters

Name Type Description
e Equatable<A> the equatable functor
a T<A> the right-hand compared value

Returns

Unary<T<A>, boolean>

the unary function which returns

Defined in

src/Option.ts:356

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

Type parameters

Name
A

Parameters

Name Type Description
e EquatableModule<A> the equatable functor module
a T<A> the right-hand compared value

Returns

Unary<T<A>, boolean>

the unary function which returns

Defined in

src/Option.ts:377

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

Type parameters

Name
A

Parameters

Name Type Description
e Equatable<A> the equatable functor

Returns

Binary<T<A>, T<A>, boolean>

the binary function which returns

Defined in

src/Option.ts:400

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

Type parameters

Name
A

Parameters

Name Type Description
e EquatableModule<A> the equatable functor module

Returns

Binary<T<A>, T<A>, boolean>

the binary function which returns

Defined in

src/Option.ts:423

Filterables

filter

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

Type parameters

Name Description
A the type to filter

Parameters

Name Type Description
a Mappable<A, boolean> the predicate
b T<A> the value to filter

Returns

T<A>

Defined in

src/Option.ts:471

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

Type parameters

Name Description
A the type to filter

Parameters

Name Type Description
a Mappable<A, boolean> the predicate

Returns

Unary<T<A>, T<A>>

the unary function which filters the value b

Defined in

src/Option.ts:491

Mappables

map

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

Type parameters

Name Description
A the starting type
B the mapped type

Parameters

Name Type Description
m Mappable<A, B> the mappable
a T<A> the value to map

Returns

T<B>

the mapped option T<B>

Defined in

src/Option.ts:526

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

Type parameters

Name Description
A the starting type
B the mapped type

Parameters

Name Type Description
m Mappable<A, B> the mappable

Returns

Mappable<T<A>, T<B>>

the mappable functor which maps T<A> to T<B>

Defined in

src/Option.ts:546


mapOr

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

Type parameters

Name Description
A the starting type
B the mapped type

Parameters

Name Type Description
m Mappable<A, B> the mappable
a T<A> the value to map
b B the fallback value

Returns

B

the mapped value B or the fallback value b if a is None

Defined in

src/Option.ts:578

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

Type parameters

Name Description
A the starting type
B the mapped type

Parameters

Name Type Description
m Mappable<A, B> the mappable
a B the fallback value

Returns

Unary<T<A>, B>

the mappable functor which maps A to B or returns the fallback value a if b is None

Defined in

src/Option.ts:599