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