Ƭ Buildable<a
>: (a?
: Partial
<a
>) => a
Name |
---|
a |
▸ (a?
): a
Represents a builder (or factory) function.
It accepts a Partial<a>
and must return a a
type.
Example
import { Functors } from 'tiinvo';
export interface User {
name: string;
surname: string;
}
export type t = User;
export const make: Functors.Buildable<t> = (p = {}) => ({
name: p.name ?? '',
surname: p.surname ?? '',
})
make()
// { name: '', surname: '' }
make({ name: 'John' })
// { name: 'John', surname: '' }
make({ name: 'John', surname: 'Doe' })
// { name: 'John', surname: 'Doe' }
Since
4.0.0
Name | Type |
---|---|
a? |
Partial <a > |
a
Ƭ BuildableMobule<a
>: Object
Represents a builder (or factory) module.
This module must have a Buildable<a>
function called make
.
Example
////// module User.ts
import { Functors } from 'tiinvo';
export interface User {
name: string;
surname: string;
}
export type t = User;
export const make: Functors.Buildable<t> = (p = {}) => ({
name: p.name ?? '',
surname: p.surname ?? '',
})
/////// Module Foo.ts
import * as User from './User.ts';
import { Functors } from 'tiinvo';
export const greet = (x: Functors.BuildableMobule<User.t>, y: Partial<ReturnType<Functors.Buildable<User.t>>>) => {
const z = x.make(y);
return `hello ${z.name} ${z.surname}`
}
greet(User, { name: 'John', surname: 'Doe' }) // "hello John Doe"
greet(User, "" as any) // "hello "
Since
4.0.0
Name |
---|
a |
Name | Type |
---|---|
make |
Buildable <a > |
Ƭ DefaultableModule<a
>: Object
The default value module-like.
Example
////// module User.ts
export interface User {
name: string;
surnname: string;
}
export type t = User;
export const toString = (t: t) => `hello ${t.name} ${t.surname}`;
export default {
name: "name",
surname: "surname",
}
////// module Animal.ts
export interface Animal {
name: string;
legs: number;
}
export type t = Animal;
export const toString = (t: t) => `a ${t.name} has ${t.legs} legs`;
export default {
name: "raccoon",
legs: 4,
}
////// module X.ts
import * as Animal.ts
import * as User.ts
import { Fn, Functors } from 'tiinvo'
type Stringifiable<a> = Record<'toString', Fn.Unary<a, string>> & Functors.DefaultableModule<a>
const describe = <a>(x: Stringifiable<a>) => x.toString(x.default);
describe(Animal) // "a raccoon has 4 legs"
describe(User) // "hello name surname"
Since
4.0.0
Name |
---|
a |
Name | Type |
---|---|
default |
a |
Ƭ Catchable<f
>: Object
Represents a catchable functor
Since
4.0.0
Name | Type |
---|---|
f |
extends AnyFn |
Name | Type |
---|---|
catch |
(error : Error , args : Parameters <f >) => ReturnType <f > |
func |
f |
Ƭ CatchableAsyncModule<f
>: Object
Represents a catchable async module functor
Since
4.0.0
Name | Type |
---|---|
f |
extends AnyAsyncFn |
Name | Type |
---|---|
[catchableAsync] |
() => Catchable <f > |
Ƭ CatchableSyncModule<f
>: Object
Represents a catchable sync module functor
Since
4.0.0
Name | Type |
---|---|
f |
extends AnyFn |
Name | Type |
---|---|
[catchableSync] |
() => Catchable <f > |
Ƭ CatchableModule<f
>: CatchableAsyncModule
<f
> | CatchableSyncModule
<f
>
Represents a catchable module functor
Since
4.0.0
Name | Type |
---|---|
f |
extends AnyFn |
Ƭ ComparableResult: -1
| 0
| 1
Is the result of a comparison made by a Comparable<A>
functor
Since
4.0.0
Ƭ Comparable<A
>: (a
: A
, b
: A
) => ComparableResult
(a
: A
) => (b
: A
) => ComparableResult
Name | Description |
---|---|
A |
is the type of the compared values |
▸ (a
, b
): ComparableResult
Is a comparable functor
Since
4.0.0
Name | Type | Description |
---|---|---|
a |
A |
the first value to compare |
b |
A |
the second value to compare |
▸ (a
): (b
: A
) => ComparableResult
Is a comparable functor
Since
4.0.0
Name | Type | Description |
---|---|---|
a |
A |
the first value to compare |
fn
▸ (b
): ComparableResult
Name | Type |
---|---|
b |
A |
Ƭ ComparableModule<a
>: Object
Is a comparable functor module
Since
4.0.0
Name | Description |
---|---|
a |
is the type of the compared values |
Name | Type |
---|---|
cmp |
Comparable <a > |
Ƭ Equatable<a
>: (a
: a
, b
: a
) => boolean
(a
: a
) => (b
: a
) => boolean
Name | Description |
---|---|
a |
is the type of the compared values |
▸ (a
, b
): boolean
Is a comparable functor
Example
import { Functors, Str, Num } from 'tiinvo';
module LivingBeen {
export type LivingBeen<A> = {
name: string;
} & A;
export type T<A> = LivingBeen<A>;
export const eq = <A>(a: T<A>, b: T<A>): boolean => {
return Str.eq(a.name, b.name);
}
}
module Animal {
export type Animal = LivingBeen.T<{
legs: number;
}>;
export type T = Animal;
export const make: Functors.Buildable<T> = (p = {}) => ({
name: p.name ?? '',
legs: p.legs ?? 0,
})
export const eq: Functors.Equatable<T> = (a, b) => {
return LivingBeen.eq(a, b) && Num.eq(a.legs, b.legs)
}
}
module Person {
export type Person = LivingBeen.T<{
surname: string;
}>
export type T = Person;
export const make: Functors.Buildable<T> = (p = {}) => ({
name: p.name ?? '',
surname: p.surname ?? '',
})
export const eq = (a: T, b: T) => {
return LivingBeen.eq(a, b) && Str.eq(a.surname, b.surname)
}
}
const duck = Animal.make({ legs: 2, name: 'duck' })
const donaldDuck = Animal.make({ legs: 2, name: 'Donald' })
const donald = Person.make({ name: 'Donald', surname: 'Duck' });
LivingBeen.eq(duck, donaldDuck) // false
LivingBeen.eq(donald, donaldDuck as LivingBeen.T<any>) // true
Since
4.0.0
Name | Type | Description |
---|---|---|
a |
a |
the first value to compare |
b |
a |
the second value to compare |
boolean
true if a and b are equal
▸ (a
): (b
: a
) => boolean
Is a comparable functor
Example
import { Functors, Str, Num } from 'tiinvo';
module LivingBeen {
export type LivingBeen<A> = {
name: string;
} & A;
export type T<A> = LivingBeen<A>;
export const eq = <A>(a: T<A>, b: T<A>): boolean => {
return Str.eq(a.name, b.name);
}
}
module Animal {
export type Animal = LivingBeen.T<{
legs: number;
}>;
export type T = Animal;
export const make: Functors.Buildable<T> = (p = {}) => ({
name: p.name ?? '',
legs: p.legs ?? 0,
})
export const eq: Functors.Equatable<T> = (a, b) => {
return LivingBeen.eq(a, b) && Num.eq(a.legs, b.legs)
}
}
module Person {
export type Person = LivingBeen.T<{
surname: string;
}>
export type T = Person;
export const make: Functors.Buildable<T> = (p = {}) => ({
name: p.name ?? '',
surname: p.surname ?? '',
})
export const eq = (a: T, b: T) => {
return LivingBeen.eq(a, b) && Str.eq(a.surname, b.surname)
}
}
const duck = Animal.make({ legs: 2, name: 'duck' })
const donaldDuck = Animal.make({ legs: 2, name: 'Donald' })
const donald = Person.make({ name: 'Donald', surname: 'Duck' });
LivingBeen.eq(duck, donaldDuck) // false
LivingBeen.eq(donald, donaldDuck as LivingBeen.T<any>) // true
Since
4.0.0
Name | Type | Description |
---|---|---|
a |
a |
the first value to compare |
fn
true if a and b are equal
▸ (b
): boolean
Name | Type |
---|---|
b |
a |
boolean
Ƭ EquatableModule<a
>: Object
Is an equatable functor module
Since
4.0.0
Name | Description |
---|---|
a |
the value type |
Name | Type |
---|---|
eq |
Equatable <a > |
Ƭ Guardable<a
>: (x
: unknown
) => x is a
Name |
---|
a |
▸ (x
): x is a
Express a function which guards if a passed parameter x
is of a certain type a
.
Read more about typeguards on ts doc pages.
Example
import { Functors } from 'tiinvo';
let is0: Functors.Guardable<0> = (x: unknown): x is 0 => x === 0;
is0(10) // false
is0("") // false
is0(-1) // false
is0(0) // true
Since
4.0.0
Name | Type |
---|---|
x |
unknown |
x is a
Ƭ GuardableModule<a
>: Object
Represents a module which have an exported function guard
of type Guardable<a>
Example
import { Fn, Num, Predicate, Functors } from 'tiinvo';
module Int8 {
export type T = number;
export const guard = Predicate.and(Num.guard, Num.gte(0), Num.lte(2**8 - 1)) as Functors.Guardable<T>;
}
module Int16 {
export type T = number;
export const guard = Predicate.and(Num.guard, Num.gte(0), Num.lte(2**16 - 1)) as Functors.Guardable<T>;
}
const makeeq = <A extends number>(mod: Functors.GuardableModule<A>): Functors.Equatable<A> => {
function eq(a: A, b: A): boolean
function eq(a: A): Fn.Unary<A, boolean>
function eq(a: A, b?: A): any
{
const _eq = (x: A, y: A) => {
return mod.guard(x) && mod.guard(y) && Num.eq(x, y)
}
if (Num.guard(a) && Num.guard(b)) {
return _eq(a, b)
}
return (b: A) => _eq(a, b)
}
return eq;
}
const eqInt8 = makeeq(Int8)
const eqInt16 = makeeq(Int16)
eqInt8(255, 255) // true
eqInt8(256, 256) // false
eqInt16(255, 255) // true
eqInt16(256, 256) // true
Since
4.0.0
Name |
---|
a |
Name | Type |
---|---|
guard |
Guardable <a > |
Ƭ Filterable<a
>: (a
: a
) => boolean
Name |
---|
a |
▸ (a
): boolean
Is a filter predicate.
Since
4.0.0
Name | Type |
---|---|
a |
a |
boolean
Ƭ FilterableModule<a
>: Object
Since
4.0.0
Name |
---|
a |
Name | Type |
---|---|
filter |
Filterable <a > |
Ƭ FilterMappableModule<a
, b
>: FilterableModule
<a
> & { map
: Mappable
<a
, b
> }
Compound module of ModuleFilterable<a>
and ModuleMappable<a, b>
Since
4.0.0
Name |
---|
a |
b |
Ƭ FilterReduceableModule<a
, b
>: FilterableModule
<a
> & ReduceableModule
<a
, b
> & DefaultableModule
<b
>
Compound module of ModuleFilterable<a>
and ModuleMappable<a, b>
with a default value b
.
Since
4.0.0
Name |
---|
a |
b |
• Const
catchableAsync: typeof catchableAsync
Is the symbol used to express the presence of an async catchable function
Since
4.0.0
• Const
catchableSync: typeof catchableSync
Is the symbol used to express the presence of a sync catchable function
Since
4.0.0
Ƭ GuardReturnType<A
>: A
extends Guardable
<infer U> ? U
: A
extends GuardableModule
<infer U> ? U
: never
The checked type of a Guardable or a GuardableModule
Example
import type { Functors, Str } from 'tiinvo'
type x = Functors.GuardReturnType<Str>
type x: x = "" // ok for the compiler
type y: x = 10 // error for the compiler
Since
4.0.0
Name | Type |
---|---|
A |
extends Guardable <any > | GuardableModule <any > |
Ƭ GuardArrayReturnType<A
>: { [key in keyof A]: GuardReturnType<A[key]> } & RelativeIndexable
<any
>
Returns an array of types from an array of Guardable<any>
or GuardableModule<any>
Example
import type { Functors, Str, Num, Bool } from 'tiinvo'
type x: Functors.GuardArrayReturnType<[Str, Num, Bool]>
const x: x = ["hello", 10, true] // ok for the compiler
const y: x = ["hello", "world", 0] // error for the compiler
Since
4.0.0
Name | Type |
---|---|
A |
extends (Guardable <any > | GuardableModule <any >)[] |
Ƭ Mappable<A
, B
>: (a
: A
) => B
Name | Description |
---|---|
A |
the starting value |
B |
the mapped value |
▸ (a
): B
A map function. Maps a value A
to a value B
Since
4.0.0
Name | Type |
---|---|
a |
A |
B
Ƭ MappableModule<A
, B
>: Object
A module with a map function Mappable<A, B>
.
Example
import { Fn, Functors, Num } from 'tiinvo';
function makemap<T extends number>(guard: Functors.Guardable<T>, tn: string) {
function map<A>(m: Functors.Mappable<T, A>, t: T): A
function map<A>(m: Functors.Mappable<T, A>): Fn.Unary<T, A>
function map<A>(m: Functors.Mappable<T, A>, t?: T): any
{
const _map = (x: T) => guard(x) ? m(x) : new TypeError("Value not " + tn)
if (Num.guard(t)) {
return _map(t)
}
return (b: T) => _map(b)
}
return map;
}
module Int8 {
export type T = number;
export const guard = Predicate.and(
Num.guard,
Num.gte(0),
Num.lte(2 ** 8 - 1)
) as Functors.Guardable<T>;
export const map = makemap(guard, "Int8");
}
module Int16 {
export type T = number;
export const guard = Predicate.and(
Num.guard,
Num.gte(0),
Num.lte(2 ** 16 - 1)
) as Functors.Guardable<T>;
export const map = makemap(guard, "Int16");
}
const toHex = <A extends number>(
mod: Functors.MappableModule<A, string>,
a: A,
) => mod.map(Num.toHex as Functors.Mappable<A, string>, a)
toHex(Int8, 255) // "0xff"
toHex(Int8, 256) // TypeError("Value not Int8")
toHex(Int16, 256) // "0x100"
toHex(Int16, 2 ** 16) // TypeError("Value not Int16")
Since
4.0.0
Name | Description |
---|---|
A |
the starting value |
B |
the mapped value |
Name | Type |
---|---|
map |
(m : Mappable <A , B >, a : A ) => B (m : Mappable <A , B >) => (a : A ) => B |
Ƭ MappableParameter<A
>: A
extends Mappable
<infer U, any
> ? U
: A
extends MappableModule
<infer U, any
> ? U
: never
Gets the parameter type of a Mappable<any, any> or a MappableModule<any, any>
Example
import type { Functors } from 'tiinvo'
type MyMap = (x: string) => number;
type x = Functors.MappableParameter<MyMap>
let x: x = "hello" // compiler gives ok
// @ts-ignore
let y: x = 10 // compiler gives error
Since
4.0.0
Name | Type |
---|---|
A |
extends Mappable <any , any > | MappableModule <any , any > |
Ƭ MappableParameters<A
>: { [key in keyof A]: MappableParameter<A[key]> } & RelativeIndexable
<any
>
Gets the parameters type of an array of Mappable
or MappableModule
s.
Example
import type { Functors } from 'tiinvo'
type MyMap0 = (x: string) => number;
type MyMap1 = (x: number) => Date;
type x = Functors.MappableParameters<[MyMap0, MyMap1]>
let x: x = ["hello", 10] // compiler gives ok
// @ts-ignore
let y: x = [10, new Date()] // compiler gives error
Since
4.0.0
Name | Type |
---|---|
A |
extends (Mappable <any , any > | MappableModule <any , any >)[] |
Ƭ MappableReturnType<A
>: A
extends Mappable
<any
, infer U> ? U
: A
extends MappableModule
<any
, infer U> ? U
: never
Gets the returning type of a Mappable<any, any> or a MappableModule<any, any>
Example
import type { Functors } from 'tiinvo'
type MyMap = (x: string) => number;
type x = Functors.MappableReturnType<MyMap>
let x: x = 10 // compiler gives ok
// @ts-ignore
let y: x = "hello" // compiler gives error
Since
4.0.0
Name | Type |
---|---|
A |
extends Mappable <any , any > | MappableModule <any , any > |
Ƭ MappableReturnTypes<A
>: { [key in keyof A]: MappableReturnType<A[key]> } & RelativeIndexable
<any
>
Gets the return types of an array of Mappable
or MappableModule
s.
Example
import type { Functors } from 'tiinvo'
type MyMap0 = (x: string) => number;
type MyMap1 = (x: number) => Date;
type x = Functors.MappableParameters<[MyMap0, MyMap1]>
let x: x = [10, new Date()] // compiler gives ok
// @ts-ignore
let y: x = ["hello", 10] // compiler gives error
Since
4.0.0
Name | Type |
---|---|
A |
extends (Mappable <any , any > | MappableModule <any , any >)[] |
Ƭ Reduceable<a
, b
>: (p
: b
, c
: a
) => b
Name |
---|
a |
b |
▸ (p
, c
): b
Reduce a value A
to a value B
aggregating the previous value B
to the current.
Since
4.0.0
Name | Type |
---|---|
p |
b |
c |
a |
b
Ƭ ReduceableModule<a
, b
>: Object
A module with a reduce function exposed.
Since
4.0.0
Name |
---|
a |
b |
Name | Type |
---|---|
reduce |
Reduceable <a , b > |