0.7.0 • Published 7 years ago
chain-free v0.7.0
chain-free
Create chains with any arbitrary properties.
Uses ES Proxy, use only where available.
Install
npm install chain-freeUsage
API
chain(all, {...custom}, {...opts})allfunctionthat gets called on every property access with all previous properties as args in latest-to-oldest order.{...custom}object|Mapwhose keys (function) get called whenever that key is accessed.Keys may be of the form:
- single key
key.chain- regex
opts.baseBase to use for proxy.opts.inherit[default:true]Whether to inherit existing properties.opts.exclude[default:[inspect]]Keys to exclude from proxying. For internal properties likeinspectto prevent infinite loops.opts.depth[default:Infinity]Depth to which properties should be accessible.
Example
const chained = chain(key => {
// called on all property lookups
console.log('Property accessed:', key)
// you may return a function here for it to be treated as such
return () => {
console.log(`chained.${key}() called`)
if (done) {
// The chain ends when you return something
return 'done'
// (either from this child function or the parent property lookup)
}
}
}, {
// custom functions called when accessed property name matches:
customKey: () => {
console.log('"customKey" property accessed')
// don't try to call `.customKey()` (that's next)
},
customFn: () => () => {
// ^ return function from property
console.log('"customFn" called')
return 'result'
},
})
const result = chained.a.b('c').d.e.customKey.customFn()
console.log('result =', result)Property accessed: a
Property accessed: b
chained.b() called
Property accessed: d
Property accessed: e
"customKey" property accessed
"customFn" called
result = result