deprecated.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* eslint-disable no-console */
  2. "use strict";
  3. /**
  4. * Returns a function that will invoke the supplied function and print a
  5. * deprecation warning to the console each time it is called.
  6. *
  7. * @param {Function} func
  8. * @param {string} msg
  9. * @returns {Function}
  10. */
  11. exports.wrap = function(func, msg) {
  12. var wrapped = function() {
  13. exports.printWarning(msg);
  14. return func.apply(this, arguments);
  15. };
  16. if (func.prototype) {
  17. wrapped.prototype = func.prototype;
  18. }
  19. return wrapped;
  20. };
  21. /**
  22. * Returns a string which can be supplied to `wrap()` to notify the user that a
  23. * particular part of the sinon API has been deprecated.
  24. *
  25. * @param {string} packageName
  26. * @param {string} funcName
  27. * @returns {string}
  28. */
  29. exports.defaultMsg = function(packageName, funcName) {
  30. return (
  31. packageName +
  32. "." +
  33. funcName +
  34. " is deprecated and will be removed from the public API in a future version of " +
  35. packageName +
  36. "."
  37. );
  38. };
  39. /**
  40. * Prints a warning on the console, when it exists
  41. *
  42. * @param {string} msg
  43. * @returns {undefined}
  44. */
  45. exports.printWarning = function(msg) {
  46. /* istanbul ignore next */
  47. if (typeof process === "object" && process.emitWarning) {
  48. // Emit Warnings in Node
  49. process.emitWarning(msg);
  50. } else if (console.info) {
  51. console.info(msg);
  52. } else {
  53. console.log(msg);
  54. }
  55. };