pax_global_header00006660000000000000000000000064127167231400014515gustar00rootroot0000000000000052 comment=71d91b6dc5bdeac37e218c2cf03f9ab55b60d214 wrappy-1.0.2/000077500000000000000000000000001271672314000130375ustar00rootroot00000000000000wrappy-1.0.2/.gitignore000066400000000000000000000000451271672314000150260ustar00rootroot00000000000000node_modules/ .nyc_output/ coverage/ wrappy-1.0.2/.travis.yml000066400000000000000000000002221271672314000151440ustar00rootroot00000000000000language: node_js sudo: false before_install: - "npm install -g npm@latest" node_js: - '0.8' - '0.10' - '0.12' - 'iojs' - '4' - '5' wrappy-1.0.2/LICENSE000066400000000000000000000013751271672314000140520ustar00rootroot00000000000000The ISC License Copyright (c) Isaac Z. Schlueter and Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. wrappy-1.0.2/README.md000066400000000000000000000012551271672314000143210ustar00rootroot00000000000000# 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) ``` wrappy-1.0.2/package.json000066400000000000000000000011361271672314000153260ustar00rootroot00000000000000{ "name": "wrappy", "version": "1.0.2", "description": "Callback wrapping utility", "main": "wrappy.js", "files": [ "wrappy.js" ], "directories": { "test": "test" }, "dependencies": {}, "devDependencies": { "tap": "^2.3.1" }, "scripts": { "test": "tap --coverage test/*.js" }, "repository": { "type": "git", "url": "https://github.com/npm/wrappy" }, "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC", "bugs": { "url": "https://github.com/npm/wrappy/issues" }, "homepage": "https://github.com/npm/wrappy" } wrappy-1.0.2/test/000077500000000000000000000000001271672314000140165ustar00rootroot00000000000000wrappy-1.0.2/test/basic.js000066400000000000000000000021511271672314000154340ustar00rootroot00000000000000var test = require('tap').test var wrappy = require('../wrappy.js') test('basic', function (t) { function onceifier (cb) { var called = false return function () { if (called) return called = true return cb.apply(this, arguments) } } onceifier.iAmOnce = {} var once = wrappy(onceifier) t.equal(once.iAmOnce, onceifier.iAmOnce) var called = 0 function boo () { t.equal(called, 0) called++ } // has some rando property boo.iAmBoo = true var onlyPrintOnce = once(boo) onlyPrintOnce() // prints 'boo' onlyPrintOnce() // does nothing t.equal(called, 1) // random property is retained! t.equal(onlyPrintOnce.iAmBoo, true) var logs = [] var logwrap = wrappy(function (msg, cb) { logs.push(msg + ' wrapping cb') return function () { logs.push(msg + ' before cb') var ret = cb.apply(this, arguments) logs.push(msg + ' after cb') } }) var c = logwrap('foo', function () { t.same(logs, [ 'foo wrapping cb', 'foo before cb' ]) }) c() t.same(logs, [ 'foo wrapping cb', 'foo before cb', 'foo after cb' ]) t.end() }) wrappy-1.0.2/wrappy.js000066400000000000000000000016111271672314000147160ustar00rootroot00000000000000// Returns a wrapper function that returns a wrapped callback // The wrapper function should do some stuff, and return a // presumably different callback function. // This makes sure that own properties are retained, so that // decorations and such are not lost along the way. module.exports = wrappy function wrappy (fn, cb) { if (fn && cb) return wrappy(fn)(cb) if (typeof fn !== 'function') throw new TypeError('need wrapper function') Object.keys(fn).forEach(function (k) { wrapper[k] = fn[k] }) return wrapper function wrapper() { var args = new Array(arguments.length) for (var i = 0; i < args.length; i++) { args[i] = arguments[i] } var ret = fn.apply(this, args) var cb = args[args.length-1] if (typeof ret === 'function' && ret !== cb) { Object.keys(cb).forEach(function (k) { ret[k] = cb[k] }) } return ret } }