You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
685 B

4 years ago
  1. # wrappy
  2. Callback wrapping utility
  3. ## USAGE
  4. ```javascript
  5. var wrappy = require("wrappy")
  6. // var wrapper = wrappy(wrapperFunction)
  7. // make sure a cb is called only once
  8. // See also: http://npm.im/once for this specific use case
  9. var once = wrappy(function (cb) {
  10. var called = false
  11. return function () {
  12. if (called) return
  13. called = true
  14. return cb.apply(this, arguments)
  15. }
  16. })
  17. function printBoo () {
  18. console.log('boo')
  19. }
  20. // has some rando property
  21. printBoo.iAmBooPrinter = true
  22. var onlyPrintOnce = once(printBoo)
  23. onlyPrintOnce() // prints 'boo'
  24. onlyPrintOnce() // does nothing
  25. // random property is retained!
  26. assert.equal(onlyPrintOnce.iAmBooPrinter, true)
  27. ```