guardfn

1.0.0 • Public • Published

guardfn

Conditionally execute a function

Example

var guard = require('guardfn')

var user = {
  name: 'Tim Oxley',
  save: function() {
    // save routine
    console.log('Saved "%s"!', this.name)
  },
  validate: function() {
    return name.length < 10
  }
}

// always calls 'user.formatName' before 'user.save'
user.save = guard(user.save, user.validate)

user.save() // => Saved "Tim Oxley"!
user.name = 'Timothy Kevin Oxley'
user.save() // => undefined

API Facts

  • guardfn returns a new Function.
  • Function will only execute if guard function returns truthy value
  • Original arguments will be passed as regular arguments to the guard function.
  • Original this context is maintained.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i guardfn

Weekly Downloads

8

Version

1.0.0

License

MIT

Last publish

Collaborators

  • timoxley