browser.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Module exports.
  3. */
  4. module.exports = deprecate;
  5. /**
  6. * Mark that a method should not be used.
  7. * Returns a modified function which warns once by default.
  8. *
  9. * If `localStorage.noDeprecation = true` is set, then it is a no-op.
  10. *
  11. * If `localStorage.throwDeprecation = true` is set, then deprecated functions
  12. * will throw an Error when invoked.
  13. *
  14. * If `localStorage.traceDeprecation = true` is set, then deprecated functions
  15. * will invoke `console.trace()` instead of `console.error()`.
  16. *
  17. * @param {Function} fn - the function to deprecate
  18. * @param {String} msg - the string to print to the console when `fn` is invoked
  19. * @returns {Function} a new "deprecated" version of `fn`
  20. * @api public
  21. */
  22. function deprecate (fn, msg) {
  23. if (config('noDeprecation')) {
  24. return fn;
  25. }
  26. var warned = false;
  27. function deprecated() {
  28. if (!warned) {
  29. if (config('throwDeprecation')) {
  30. throw new Error(msg);
  31. } else if (config('traceDeprecation')) {
  32. console.trace(msg);
  33. } else {
  34. console.warn(msg);
  35. }
  36. warned = true;
  37. }
  38. return fn.apply(this, arguments);
  39. }
  40. return deprecated;
  41. }
  42. /**
  43. * Checks `localStorage` for boolean values for the given `name`.
  44. *
  45. * @param {String} name
  46. * @returns {Boolean}
  47. * @api private
  48. */
  49. function config (name) {
  50. // accessing global.localStorage can trigger a DOMException in sandboxed iframes
  51. try {
  52. if (!global.localStorage) return false;
  53. } catch (_) {
  54. return false;
  55. }
  56. var val = global.localStorage[name];
  57. if (null == val) return false;
  58. return String(val).toLowerCase() === 'true';
  59. }