krash
v0.1.3
Published
fluent assert for validation of invariants before assignment
Downloads
2,717
Maintainers
Readme
krash
fluent assert for validation of invariants before assignment.
motivation
bare-bones inline assertion of invariants when assigning a value to a variable (detailed example below)
var delicate = krash.unless(suspect, isDefined, 'suspect not defined')
particularly helpful together with your favorite library of predicates such as lodash/Lang, predicate, predicates, ...
note that it's easy to use a shorter alias for krash.unless
, for example:
var assert = require('krash').unless
...
var delicate = assert(suspect, isDefined, 'suspect not defined')
...
krash
also comes with a helper for throwing,
that helps with formating the error message: krash.now(...message)
usage
example
var krash = require('krash')
function fun (suspect) {
// assert the invariants:
// assign suspect to delicate if defined, otherwise throw 'suspect not defined'
var delicate = krash.unless(suspect, isDefined, 'suspect not defined')
}
// example of predicate that returns a boolean
function isDefined (val) {
return (typeof val !== 'undefined') && (val !== null)
}
fun(undefined) // throw 'suspect not defined'
signatures
krash.unless(val: any
, predicate: function
, ...message: string[]
)
- val
any
the value to check against the predicate and return if valid - predicate
function
givenval
, returns aboolean
- ...message
string[]
the remaining arguments are assembled byutil#format
to generate the error message - return
any
val
ifpredicate(val)
istrue
- throw
...message
ifpredicate(val)
isfalse
krash.now(...message: string[]
)
- ...message
string[]
the arguments are assembled byutil#format
to generate the error message - throw
...message
status
license
(C) Copyright 2015, bzb-stcnx, MIT