Helpers for working with types; specifically designed to allow null/undefined to be treated the same as any other value.
- Source:
Methods
(inner) isdef(v) → {Boolean}
- Source:
Checks whether a value is null or undefined
const {isdef} = require('ferrum');
isdef(null) # => false
isdef(undefined) # => false
isdef(0) # => true
isdef(false) # => true
This function considers all values that are not null and not undefined to be defined.
Parameters:
| Name | Type | Description |
|---|---|---|
v |
T |
Returns:
- Type
- Boolean
(inner) ifdef(v, fn)
- Source:
Apply the given function to the value only if the value is defined (not null or undefined).
This basically implements Optional semantics using null/undefined.
const {plus, pipe, isdef, ifdef} = require('ferrum');
const o = {
foo: 42
};
ifdef(o['foo'], plus(2)); // 44
ifdef(o['bar'], plus(2)); // undefined
// This is particularly useful for map or curry
pipe(
[1,2,null,3],
map(ifdef(x => x*3))
list);
// yields [3,6,null,9]
// Without ifdef the pipe above would have to be manually written,
// which is a bit harder to read
pipe(
[1,2,null,3],
map(x => isdef(x) ? x : x*3)
list);
Parameters:
| Name | Type | Description |
|---|---|---|
v |
T | |
fn |
function |
Returns:
null | undefined | typeof(fn())
(inner) type(v) → {function|null|undefined}
- Source:
Determine type of an object.
const {type} = require('ferrum');a
class Bar {};
type(null) # => null
type(undefined) # => undefined
type(42) # => Number
type(new Number(42)) # => Number
type(new Bar()) # => Bar
// The usual strategy to get the type is this
new Bar().constructor
// Which fails for null and undefined...
null.constructor
// Thrown:
// TypeError: Cannot read property 'constructor' of null
Like obj.constructor, but won't fail for null/undefined and just returns the value itself for those. This is a useful feature for code that is supposed to be null/undefined-safe since those need not be special cased.
Parameters:
| Name | Type | Description |
|---|---|---|
v |
T |
Returns:
The type of the given parameter
- Type
- function | null | undefined
(inner) typename(The) → {String}
- Source:
Given a type, determine it's name.
const {type, typename} = require('ferrum');
class Bar {};
typename(type(null)) # => "null"
typename(type(undefined)) # => "undefined"
typename(type(42)) # => "Number"
typename(Bar) # => "Bar"
// The usual strategy to get the name of a value's type is this
new Bar().constructor.name
// But this obviously fails for null & undefined
null.constructor.name
null.name // still throws
This is useful as a replacement for val.constructor.name, since this can deal with null and undefined.
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 primitive
const {isPrimitive} = require('ferrum');
isPrimitive(null) # => true
isPrimitive(undefined) # => true
isPrimitive(42) # => true
isPrimitive({}) # => false
isPrimitive(new Number(42)) # => false
Parameters:
| Name | Type | Description |
|---|---|---|
v |
T |
Returns:
- Type
- Boolean