Conditions | 3 |
Paths | 2 |
Total Lines | 57 |
Code Lines | 40 |
Lines | 1 |
Ratio | 1.75 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /*! |
||
8 | (function () { |
||
9 | 'use strict'; |
||
10 | |||
11 | function stripJsonComments(str) { |
||
12 | var currentChar; |
||
13 | var nextChar; |
||
14 | var insideString = false; |
||
15 | var insideComment = false; |
||
16 | var ret = ''; |
||
17 | |||
18 | for (var i = 0; i < str.length; i++) { |
||
19 | currentChar = str[i]; |
||
20 | nextChar = str[i + 1]; |
||
21 | |||
22 | if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') { |
||
23 | insideString = !insideString; |
||
24 | } |
||
25 | |||
26 | if (insideString) { |
||
27 | ret += currentChar; |
||
28 | continue; |
||
29 | } |
||
30 | |||
31 | if (!insideComment && currentChar + nextChar === '//') { |
||
32 | insideComment = 'single'; |
||
33 | i++; |
||
|
|||
34 | } else if (insideComment === 'single' && currentChar + nextChar === '\r\n') { |
||
35 | insideComment = false; |
||
36 | i++; |
||
37 | View Code Duplication | } else if (insideComment === 'single' && currentChar === '\n') { |
|
38 | insideComment = false; |
||
39 | } else if (!insideComment && currentChar + nextChar === '/*') { |
||
40 | insideComment = 'multi'; |
||
41 | i++; |
||
42 | continue; |
||
43 | } else if (insideComment === 'multi' && currentChar + nextChar === '*/') { |
||
44 | insideComment = false; |
||
45 | i++; |
||
46 | continue; |
||
47 | } |
||
48 | |||
49 | if (insideComment) { |
||
50 | continue; |
||
51 | } |
||
52 | |||
53 | ret += currentChar; |
||
54 | } |
||
55 | |||
56 | return ret; |
||
57 | } |
||
58 | |||
59 | if (typeof module !== 'undefined' && module.exports) { |
||
60 | module.exports = stripJsonComments; |
||
61 | } else { |
||
62 | window.stripJsonComments = stripJsonComments; |
||
63 | } |
||
64 | })(); |
||
65 |