Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Created February 23, 2017 15:31
Show Gist options
  • Save vdeturckheim/b9ef86afae5d4784a3c11308e01dc3c4 to your computer and use it in GitHub Desktop.
Save vdeturckheim/b9ef86afae5d4784a3c11308e01dc3c4 to your computer and use it in GitHub Desktop.
exports.deprecate = function deprecate(fn, msg, code) {
// pre wrapper checks go here
function deprecated() {
if (!warned) {
warned = true;
if (code !== undefined) {
process.emitWarning(msg, 'DeprecationWarning', code, deprecated);
} else {
process.emitWarning(msg, 'DeprecationWarning', deprecated);
}
}
if (new.target) {
return Reflect.construct(fn, arguments, new.target);
}
return fn.apply(this, arguments);
}
// The wrapper will keep the same prototype as fn to maintain prototype chain
Object.setPrototypeOf(deprecated, fn);
if (fn.prototype) {
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
}
return deprecated;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment