Skip to content

laggingreflex/chain-free

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chain-free

Create chains with any arbitrary properties.

Uses ES Proxy, use only where available.

Install

npm install chain-free

Usage

API

chain(all, {...custom}, {...opts})
  • all function that gets called on every property access with all previous properties as args in latest-to-oldest order.

  • {...custom} object|Map whose keys (function) get called whenever that key is accessed.

    Keys may be of the form:

    • single key
    • key.chain
    • regex
  • opts.base Base 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 like inspect to 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

About

A minimal chaining library using ES Proxy

Resources

Stars

Watchers

Forks

Packages

No packages published