| Conditions | 2 |
| Paths | 8 |
| Total Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| 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 | ////////////////////////////////////////////////////////////////////////////////////// |
||
| 165 | function draw(str, to, first, box) { |
||
| 166 | tag || init(); |
||
| 167 | |||
| 168 | var re = /("(?:((?:https?|file):\/\/(?:\\?\S)+?)|(?:\\?.)*?)")\s*(:?)|-?\d+\.?\d*(?:e[+-]?\d+)?|true|false|null|[[\]{},]|(\S[^-[\]{},"\d]*)/gi |
||
| 169 | , node = div.cloneNode() |
||
| 170 | , link = document.createElement("a") |
||
| 171 | , span = document.createElement("span") |
||
| 172 | , info = document.createElement("i") |
||
| 173 | , colon = document.createTextNode(": ") |
||
| 174 | , comma = fragment(",") |
||
| 175 | , path = [] |
||
| 176 | , cache = { |
||
| 177 | "{": fragment("{", "}"), |
||
| 178 | "[": fragment("[", "]") |
||
| 179 | }; |
||
| 180 | |||
| 181 | node.className = "R" + rand + (box ? " " + box : ""); |
||
| 182 | |||
| 183 | link.classList.add("L" + rand); |
||
| 184 | info.classList.add("I" + rand); |
||
| 185 | |||
| 186 | to.addEventListener("click", function(e) { |
||
| 187 | var target = e.target, open = target.classList.contains(COLL); |
||
| 188 | if (target.tagName == "I") { |
||
| 189 | if (e.altKey) { |
||
| 190 | changeSiblings(target, COLL, !open); |
||
| 191 | } else if (e[mod]) { |
||
| 192 | open = target.nextSibling.querySelector("i"); |
||
| 193 | if (open) change(target.nextSibling, "i", COLL, !open.classList.contains(COLL)); |
||
| 194 | } else { |
||
| 195 | target.classList[open ? "remove" : "add"](COLL); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | }, true); |
||
| 199 | |||
| 200 | to.replaceChild(box = node, first); |
||
| 201 | loop(str, re); |
||
| 202 | |||
| 203 | function loop(str, re) { |
||
| 204 | str = reconvert(str); |
||
| 205 | var match, val, tmp, i = 0, len = str.length; |
||
| 206 | try { |
||
| 207 | for (; match = re.exec(str); ) { |
||
| 208 | val = match[0]; |
||
| 209 | if (val == "{" || val == "[") { |
||
| 210 | path.push(node); |
||
| 211 | node.appendChild(cache[val].cloneNode(true)); |
||
| 212 | node = node.lastChild.previousSibling; |
||
| 213 | node.len = 1; |
||
| 214 | node.start = re.lastIndex; |
||
| 215 | } else if ((val == "}" || val == "]") && node.len) { |
||
| 216 | if (node.childNodes.length) { |
||
| 217 | tmp = info.cloneNode(); |
||
| 218 | tmp.dataset.content = node.len + ( |
||
| 219 | node.len == 1 ? |
||
| 220 | (val == "]" ? " item, " : " property, ") : |
||
| 221 | (val == "]" ? " items, " : " properties, ") |
||
| 222 | ) + units(re.lastIndex - node.start + 1); |
||
| 223 | |||
| 224 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 225 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, "\\'")); |
||
| 226 | } |
||
| 227 | node.parentNode.insertBefore(tmp, node); |
||
| 228 | } else { |
||
| 229 | node.parentNode.removeChild(node); |
||
| 230 | } |
||
| 231 | node = path.pop(); |
||
| 232 | } else if (val == ",") { |
||
| 233 | node.len += 1; |
||
| 234 | node.appendChild(comma.cloneNode(true)); |
||
| 235 | } else { |
||
| 236 | if (match[2]) { |
||
| 237 | tmp = link.cloneNode(); |
||
| 238 | tmp.href = match[2].replace(/\\"/g, '"'); |
||
| 239 | } else { |
||
| 240 | tmp = span.cloneNode(); |
||
| 241 | } |
||
| 242 | tmp.textContent = match[1] || val; |
||
| 243 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 244 | node.appendChild(tmp); |
||
| 245 | if (match[3]) { |
||
| 246 | node.appendChild(colon.cloneNode()); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | if (++i > 1000) { |
||
| 250 | document.title = (0|(100*re.lastIndex/len)) + "% of " + units(len); |
||
| 251 | return setTimeout(function() { loop(str, re) }); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | document.title = "" |
||
| 255 | JSON.parse(str) |
||
| 256 | } catch(e) { |
||
| 257 | tmp = document.createElement("h3"); |
||
| 258 | tmp.className = ERR; |
||
| 259 | tmp.textContent = e; |
||
| 260 | box.insertBefore(tmp, box.firstChild); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 305 |