typesafe

Help­ers for work­ing with types; spe­cific­ally de­signed to al­low null/​un­defined to be treated the same as any other value.

Source:

Methods

(inner) isdef(v) → {Boolean}

Source:

Checks whether a value is null or un­defined

const {is­def} = re­quire('fer­rum');

is­def(null) # => false
is­def(un­defined) # => false
is­def(0) # => true
is­def(false) # => true

This func­tion con­siders all val­ues that are not null and not un­defined to be defined.

Parameters:
Name Type Description
v T
Returns:
Type
Boolean

(inner) ifdef(v, fn)

Source:

Ap­ply the given func­tion to the value only if the value is defined (not null or un­defined).

This ba­sic­ally im­ple­ments Op­tional se­mantics us­ing null/​un­defined.

const {plus, pipe, is­def, if­def} = re­quire('fer­rum');

const o = {
  foo: 42
};

if­def(o['foo'], plus(2)); // 44
if­def(o['bar'], plus(2)); // un­defined

// This is par­tic­u­larly use­ful for map or curry
pipe(
  [1,2,null,3],
  map(if­def(x => x*3))
  list);
// yields [3,6,null,9]

// Without if­def the pipe above would have to be manu­ally writ­ten,
// which is a bit harder to read
pipe(
  [1,2,null,3],
  map(x => is­def(x) ? x : x*3)
  list);
Parameters:
Name Type Description
v T
fn function
Returns:

null | un­defined | typeof(fn())

(inner) type(v) → {function|null|undefined}

Source:

De­term­ine type of an ob­ject.

const {type} = re­quire('fer­rum');a

class Bar {};

type(null) # => null
type(un­defined) # => un­defined
type(42) # => Num­ber
type(new Num­ber(42)) # => Num­ber
type(new Bar()) # => Bar

// The usual strategy to get the type is this
new Bar().con­structor

// Which fails for null and un­defined...
null.con­structor
// Thrown:
// TypeEr­ror: Can­not read prop­erty 'con­struct­or' of null

Like obj.con­structor, but won't fail for null/​un­defined and just re­turns the value it­self for those. This is a use­ful fea­ture for code that is sup­posed to be null/​un­defined-safe since those need not be spe­cial cased.

Parameters:
Name Type Description
v T
Returns:

The type of the given para­meter

Type
function | null | undefined

(inner) typename(The) → {String}

Source:

Given a type, de­term­ine it's name.

const {type, type­name} = re­quire('fer­rum');

class Bar {};

type­name(type(null)) # => "null"
type­name(type(un­defined)) # => "un­defined"
type­name(type(42)) # => "Num­ber"
type­name(Bar) # => "Bar"

// The usual strategy to get the name of a value's type is this
new Bar().con­structor.name

// But this ob­vi­ously fails for null & un­defined
null.con­structor.name
null.name // still throws

This is use­ful as a re­place­ment for val.con­structor.name, since this can deal with null and un­defined.

Parameters:
Name Type Description
The function | null | undefined

type to get the name of

Returns:
Type
String

(inner) isPrimitive(v) → {Boolean}

Source:

Test if a value is prim­it­ive

const {isPrim­it­ive} = re­quire('fer­rum');

isPrim­it­ive(null) # => true
isPrim­it­ive(un­defined) # => true
isPrim­it­ive(42) # => true
isPrim­it­ive({}) # => false
isPrim­it­ive(new Num­ber(42)) # => false
Parameters:
Name Type Description
v T
Returns:
Type
Boolean