# guardfn

> Conditional function execution.

Latest version **1.0.0** (published 2014-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install guardfn
pnpm add guardfn
yarn add guardfn
bun add guardfn
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2014-06-17 |
| First published | 2014-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Tim Oxley |
| Maintainers | timoxley |

## Links

- npm: https://www.npmjs.com/package/guardfn
- Repository: https://github.com/timoxley/guardfn
- Issues: https://github.com/timoxley/guardfn/issues
- npm.io page: https://npm.io/package/guardfn

## Dependencies (1)

- [sliced](https://npm.io/package/sliced.md) 0.0.5

## Recent versions

- 1.0.0 (latest) — 2014-06-17

## README

# guardfn

#### Conditionally execute a function

## Example

```js
var guard = require('guardfn')

var user = {
  name: 'Tim Oxley',
  save: function() {
    // save routine
    console.log('Saved "%s"!', this.name)
  },
  validate: function() {
    return this.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.

## See Also

* [timoxley/beforefn](http://github.com/timoxley/beforefn)
* [timoxley/afterfn](http://github.com/timoxley/afterfn)

## License

MIT

---
_Source: https://npm.io/package/guardfn · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
