Falit.js
Static-typing and optional parameters in native JavaScript.
npm install falitjs
var falit = req = falitrequired opt = falitoptional _ = ; // configure falit settingsfalit // common usage patternvar add = falit; // template usage patternvar tmpl = falit mul = tmpl add = tmpl; var dbl = falit; var add5 = _;
Returns
10
10
15
250
[Finished in 0.0s]
todo
- unit tests (nodejs)
- remove underscore.js (underscorejs.org) dependency
- multiple, valid, types:
falit.binder(oneOf(req.hex, req.posNum), ...)
- performance benchmarks
- ensure nodejs + webkit compatibility
- ensure crossbrowser compatibility
API
require('falitjs')
.binder([required parameters,] [optional parameters,] function)
Available Types
any
args
array
bool
char
date
element
finite
float
func
hex
int
NaN
negNum
null
num
posNum
obj
regex
str
undefined
.optional.type[(defaultValue)]
.required.type[(validationFn)]
Used as place holder objects in .binder
and .template
[opt
|req
].availableType will reserve the parameter slot for that given type.
falit
Calling the placeholder will allow one of two things to happen:
optional.availableType(..value of type..)
will initiate a default value when omitted.required.availableType(req.func)
will pass the supplied varable through a custom validation.
Example:
var add5 = falit; >>> 5 >>> 15
Example:
var { return slength < 10; }var shortString = falit >>> blake >>> Failed validation Supplied value did not pass Expected: str position: 0 null
Example:
var realExample = falit.binder(
opt.int(0),
opt.obj({debug: false}),
req.func,
function(delaySec, options, callback) {
callback(null, [delaySec, options]);
}
)
>>> realExample(console.log)
null [ 0, { debug: false } ]
>>> realExample(10, console.log)
null [ 10, { debug: false } ]
>>> realExample({debug: true}, console.log)
null [ null, { debug: true } ]
The following fails, because the first argument doesn't match the first two optionals
and then is validated against req.func
.
>>> Error: Invalid type Expected: func position: 2 Received: anyfinitefloatnumposNum value: 15
Changing the realExample
template to opt.any, opt.obj({debug: true}), req.func
fails in the following
example because the callback console.log
was matched against opt.any
and we're thrown an error
that says we're missing a required argument.
>>> Error: Missing required func in position: 1 position: 1 --------v>>> >>> null Function debug: true >>> null {} {}
.settings(req.obj)
debug
, default:false
- Determines whether debug messages should be printed to console or not.
enabled
, default:true
- Enable type-checking; disabling is useful in production environment to reduce overhead.
throwErrors
, default:true
throw new Error
on violation, when set tofalse
execution integrity is unknown.
.template([required parameters,] [optional parameters])
.template(..) works like .binder(..) except that the last parameter is not treated as the function being bound to. Instead, you can construct a pattern of required and optional parameters that can be used in multiple instances for similar functions.
Example:
var twoInts = falit; var add = twoInts mul = twoInts; >>> 10 >>> 25 >>> Invalid type Expected: int position: 0 Received: anycharstr value: a >>> Invalid type Expected: int position: 0 Received: anyfinitefloatnumposNum value: 15
.for(req.func)
Applies a required function to a template.
.whatIs(req.any)
Returns the meta-data associated with a given parameter. Useful for checking what types a known value will pass validation on.
Example:
>>> falit posTypes: 'any' 'array' bitMask: 5 value: 1 2 3 >>> falit posTypes: 'any' 'finite' 'negNum' 'num' bitMask: 41089 value: -15 >>> falit posTypes: 'any' 'undefined' bitMask: 1048577 value: undefined >>> falit posTypes: 'any' 'num' 'posNum' bitMask: 98305 value: Infinity >>> falit posTypes: 'any' 'obj' 'regex' bitMask: 393217 value: /\S+/