safe
Verifies argument types.
Installation
Run npm install --save @smockle/safe
to add safe
to your project.
Usage
safe
can be called within a function to verify argument types:
const safe = require('./lib/safe').safe
function toUpperAndNotEmpty (xs, x) {
const unsafe = safe([
{ name: 'xs', type: 'array' },
{ name: 'x', type: 'string' }
], arguments)
if (unsafe) throw unsafe
return xs.concat(x ? x.toUpperCase() : [])
}
const a = [ 'aaa', 222, '', 'ccc', '' ]
a.reduce(toUpperAndNotEmpty, [])
// Throws TypeError('x must be a string')
const b = [ 'aaa', 'bbb', '', 'ccc', '' ]
b.reduce(toUpperAndNotEmpty, [])
// [ 'AAA', 'BBB', 'CCC' ]
safe
also includes a wrapper that accepts functions as a callback:
const safe = require('./lib/wrapper').safe
const toUpperAndNotEmpty = safe([
{ name: 'xs', type: 'array' },
{ name: 'x', type: 'string' }
])(
function (xs, x) {
return xs.concat(x ? x.toUpperCase() : [])
}
)
const a = [ 'aaa', 222, '', 'ccc', '' ]
a.reduce(toUpperAndNotEmpty, [])
// Throws TypeError('x must be a string')
const b = [ 'aaa', 'bbb', '', 'ccc', '' ]
b.reduce(toUpperAndNotEmpty, [])
// [ 'AAA', 'BBB', 'CCC' ]
API Reference
-
safe
-
.IDENTITY :
Object.<string, string>
-
.identifyTypeClass(type) ⇒
string
|null
-
.isSafe(type, value) ⇒
Boolean
-
.safe(types, value) ⇒
TypeError
-
.IDENTITY :
Object.<string, string>
safe.IDENTITY : Dictionary of type classes.
Kind: static constant of safe
string
| null
safe.identifyTypeClass(type) ⇒ Select type class matching the provided parameter.
Kind: static method of safe
Returns: string
| null
- Type class.
Param | Type | Description |
---|---|---|
type | string |
String representation of a primitive or instantiable type. |
Boolean
safe.isSafe(type, value) ⇒ Whether value is of provided type.
Kind: static method of safe
Returns: Boolean
- Value is of provided type.
Param | Type | Description |
---|---|---|
type | string |
String representation of a primitive or instantiable type. |
value | * |
Value to check. |
TypeError
safe.safe(types, value) ⇒ Create TypeError when an argument doesn’t match provided type.
Kind: static method of safe
Returns: TypeError
- Argument doesn’t match provided type.
Param | Type | Description |
---|---|---|
types | Array.<Object.<string, string>> |
List of parameter names and types. |
value | * |
List of values to check. |
ERROR, Cannot find module.
-
instantiable
-
.instantiables :
Object.<string, (function()|Object|number)>
-
.instantiableKeys :
Array.<string>
-
.isInstantiable(type) ⇒
Boolean
-
.instanceOf(type, value) ⇒
Boolean
-
.instantiables :
Object.<string, (function()|Object|number)>
instantiable.instantiables : Dictionary of instantiable names to instantiable types.
Kind: static constant of instantiable
Array.<string>
instantiable.instantiableKeys : List of instantiable types.
Kind: static constant of instantiable
Boolean
instantiable.isInstantiable(type) ⇒ Whether parameter represents an instantiable type.
Kind: static method of instantiable
Returns: Boolean
- Parameter represents an instantiable type.
Param | Type | Description |
---|---|---|
type | string |
String representation of an instantiable type. |
Boolean
instantiable.instanceOf(type, value) ⇒ Whether value is of provided instantiable type.
Kind: static method of instantiable
Returns: Boolean
- Value is of provided instantiable type.
Param | Type | Description |
---|---|---|
type | string |
String representation of a instantiable type. |
value | * |
Value to check. |
Testing
safe
includes several unit tests. After cloning the safe
repo locally, run npm install
in the project folder to install dependencies. Run npm test
to execute the tests.