Bad Argument JS
Terse, pure JavaScript function argument type checker. Implements contract pre-conditions for functions. Built for speed.
var argsMustBe = ; { ; whooohooo;} ;
Throws:
BadArgumentError: arg 2 of myImportantFunction is not a function.
Call arguments will also be stored in the Error's .arguments
attribute.
Bad Argument uses Function.caller and will degrade gracefully if it is not available.
Default error conditions
function
orF
:typeof arg !== "function"
object
orO
:!(arg instanceof Object)
number
orN
:typeof arg !== "number"
string
orS
:typeof arg === "string" || arg instanceof String
array
orA
:!Array.isArray(arg)
truthy
ort
:!arg
defined
or*
:arg == null
ignored
or_
:false
As a guideline, single-char versions of conditions that test for type are uppercase letters.
Custom error conditions
var badargument = ;{} var myArgsMustBe = badargument; { ;} var apple = color: 'blue';;
Will throw BadArgumentError: arg 1 of picnic is not red.