About
Properties with getter, validated setters and hooks.
Installation & Usage
npm i @lbzg/props
const { prop } = require('@lbzg/props')
const isNumber = x => typeof x === 'number'
const double = x => x * 2
const p = prop(isNumber, 100)
p.config.hooks.on(console.log)
p.set('asdf') # false # console logs 'asdf'
p.get() # 100
p.set(2) # true # console logs 2
p.apply(double) # true # console logs 4
p.call(double) # 8
p.get() # 4
Prototype
Exports prop
and safeProp
functions (and interfaces IProp
/ISafeProp
).
prop(validator?, value?):
Prop
~ get() returns T | undefined
safeProp(validator, value):Prop
~ get() returns T
Prop
get(): T | undefined ~ just T if safeProp
set(x): boolean ~ (validates and) assigns x
apply(fn
): boolean ~ (validates and) assigns fn(prop)
call(fn
): any ~ executes and returns fn(prop)
config:PropConfig
apply
and set
return true when valid input is provided
PropConfig
validator(
fn
?) ~ (un)set validation for set and apply methods
hooks:PropHooks
PropHooks
Hooks are triggered during set
and apply
if provided.
on(
fn
) ~ fn takes input which might become prop
success(fn
) ~ fn takes input which might become prop
change(fn
) ~ fn takes input which always becomes prop
fail(fn
) ~ fn takes input which never becomes prop
fn :: x -> any