| Conditions | 14 |
| Paths | 8 |
| Total Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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:
Complex classes like render.js ➔ onParse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright © 2017 TangDongxin |
||
| 24 | function onParse(result) { |
||
|
|
|||
| 25 | var chrome = this.chrome || this.browser; |
||
| 26 | var jsonRe = /^\s*(?:\[\s*(?=-?\d|true|false|null|["[{])[^]*\]|\{\s*"[^]+\})\s*$/; |
||
| 27 | var body = document.body; |
||
| 28 | var str, jsonpMatch, tag; |
||
| 29 | |||
| 30 | if (strictOnly) { |
||
| 31 | // only render when the contentType is json |
||
| 32 | if (/[+\/]json$/i.test(document.contentType)) { |
||
| 33 | init(); |
||
| 34 | str = body.textContent; |
||
| 35 | draw(str, body); |
||
| 36 | } |
||
| 37 | } else { |
||
| 38 | // check whether the content is json or like json |
||
| 39 | first = body && body.firstChild; |
||
| 40 | if (first && |
||
| 41 | (first.tagName == "PRE" && |
||
| 42 | first == body.lastElementChild || |
||
| 43 | first == body.lastChild && |
||
| 44 | first.nodeType == 3) && |
||
| 45 | (str = first.textContent) && |
||
| 46 | (/[+\/]json$/i.test(document.contentType) || |
||
| 47 | (jsonpMatch = /^\s*((?:\/\*\*\/\s*)?([$a-z_][$\w]*)\s*(?:&&\s*\2\s*)?\()([^]+)(\)[\s;]*)$/i.exec(str)) && |
||
| 48 | jsonRe.test(jsonpMatch[3]) || jsonRe.test(str))) { |
||
| 49 | if (jsonpMatch) { |
||
| 50 | str = jsonpMatch[3]; |
||
| 51 | body.replaceChild(fragment(jsonpMatch[1], jsonpMatch[4]), first); |
||
| 52 | first = body.lastChild.previousSibling; |
||
| 53 | } |
||
| 54 | init(); |
||
| 55 | draw(str, body); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | function init() { |
||
| 61 | if (tag) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | tag = document.createElement("style"); |
||
| 66 | tag.textContent = [ |
||
| 67 | '.R', ',.D', '{font:' + fontSize + ' ' + fontStyle + '}' + |
||
| 68 | '.D', '{margin-left:6px; padding-left:1em; margin-top: 1px; border-left:1px dashed; border-color: #93A1A1;}' + |
||
| 69 | '.X', '{border:1px solid #ccc; padding:1em}' + |
||
| 70 | 'a.L', '{text-decoration:none}' + |
||
| 71 | 'a.L', ':hover,a.L', ':focus{text-decoration:underline}' + |
||
| 72 | 'i.I', '{cursor:pointer;color:#ccc}' + |
||
| 73 | 'i.H', ',i.I', ':hover{text-shadow: 1px 1px 3px #999; color:#333}' + |
||
| 74 | 'i.I', ':before{content:" ▼ "}' + |
||
| 75 | 'i.C', ':before{content:" ▶ "}' + |
||
| 76 | 'i.I', ':after{content:attr(data-content)}' + |
||
| 77 | 'i.C', '+.D', '{width:1px; height:1px; margin:0; padding:0; border:0; display:inline-block; overflow:hidden}' + |
||
| 78 | '.S', '{color:' + strColor + '}' + // string |
||
| 79 | '.K', '{color:' + keyColor + '}' + // key |
||
| 80 | '.E', '{color:#BCADAD}' + // error |
||
| 81 | '.B', '{color:' + intColor + '}' + // number and bool |
||
| 82 | '.E', ',.B', '{font-style: italic}' + // number bold |
||
| 83 | 'h3.E', '{margin:0 0 1em}' |
||
| 84 | ].join(RAND); |
||
| 85 | |||
| 86 | tag.textContent = tag.textContent + 'body {background: ' + bgColor + '; color:' + defaultColor + ';}'; |
||
| 87 | tag.textContent = tag.textContent + '.HIDE {width:200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }'; |
||
| 88 | tag.textContent = tag.textContent + '* {font:' + fontSize + ' ' + fontStyle + ' !important;}'; |
||
| 89 | tag.textContent = tag.textContent + '.black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.6; opacity:.60; filter: alpha(opacity=60); }'; |
||
| 90 | tag.textContent = tag.textContent + '.white_content { display: none; position: absolute; top: 25%; left: 25%; max-width: 60%; max-height: 80%; padding: 16px; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; }'; |
||
| 91 | |||
| 92 | document.head.appendChild(tag); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.