node/tools/eslint/node_modules/onetime/index.js
Michaël Zasso 2d441493a4 tools: update eslint to v1.10.3
PR-URL: https://github.com/nodejs/io.js/pull/2286
Reviewed-By: Roman Reiss <me@silverwind.io>
2016-01-13 23:15:39 +01:00

32 lines
608 B
JavaScript

'use strict';
module.exports = function (fn, errMsg) {
if (typeof fn !== 'function') {
throw new TypeError('Expected a function');
}
var ret;
var called = false;
var fnName = fn.displayName || fn.name || (/function ([^\(]+)/.exec(fn.toString()) || [])[1];
var onetime = function () {
if (called) {
if (errMsg === true) {
fnName = fnName ? fnName + '()' : 'Function';
throw new Error(fnName + ' can only be called once.');
}
return ret;
}
called = true;
ret = fn.apply(this, arguments);
fn = null;
return ret;
};
onetime.displayName = fnName;
return onetime;
};