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; |
||
0 ignored issues
–
show
|
|||
61 | } catch (_) { |
||
62 | return false; |
||
63 | } |
||
64 | var val = global.localStorage[name]; |
||
65 | if (null == val) return false; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
66 | return String(val).toLowerCase() === 'true'; |
||
67 | } |
||
68 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.