https://github.com/isaacs/wrappy
Callback wrapping utility
https://github.com/isaacs/wrappy
npm-cli
Last synced: over 1 year ago
JSON representation
Callback wrapping utility
- Host: GitHub
- URL: https://github.com/isaacs/wrappy
- Owner: isaacs
- License: isc
- Created: 2014-09-18T23:01:16.000Z (almost 12 years ago)
- Default Branch: main
- Last Pushed: 2024-02-10T05:58:21.000Z (over 2 years ago)
- Last Synced: 2025-03-10T05:09:51.373Z (over 1 year ago)
- Topics: npm-cli
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 55
- Watchers: 28
- Forks: 27
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wrappy
Callback wrapping utility
## USAGE
```javascript
var wrappy = require("wrappy")
// var wrapper = wrappy(wrapperFunction)
// make sure a cb is called only once
// See also: http://npm.im/once for this specific use case
var once = wrappy(function (cb) {
var called = false
return function () {
if (called) return
called = true
return cb.apply(this, arguments)
}
})
function printBoo () {
console.log('boo')
}
// has some rando property
printBoo.iAmBooPrinter = true
var onlyPrintOnce = once(printBoo)
onlyPrintOnce() // prints 'boo'
onlyPrintOnce() // does nothing
// random property is retained!
assert.equal(onlyPrintOnce.iAmBooPrinter, true)
```