| Conditions | 14 |
| Total Lines | 61 |
| Code Lines | 43 |
| 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:
Complex classes like utf8-parser.js ➔ unpack 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 | /* |
||
| 44 | export function unpack(buffer, start=0, end=buffer.length) { |
||
| 45 | /** @type {string} */ |
||
| 46 | let str = ''; |
||
| 47 | for(let index = start; index < end;) { |
||
| 48 | /** @type {number} */ |
||
| 49 | let lowerBoundary = 0x80; |
||
| 50 | /** @type {number} */ |
||
| 51 | let upperBoundary = 0xBF; |
||
| 52 | /** @type {boolean} */ |
||
| 53 | let replace = false; |
||
| 54 | /** @type {number} */ |
||
| 55 | let charCode = buffer[index++]; |
||
| 56 | if (charCode >= 0x00 && charCode <= 0x7F) { |
||
| 57 | str += String.fromCharCode(charCode); |
||
| 58 | } else { |
||
| 59 | /** @type {number} */ |
||
| 60 | let count = 0; |
||
| 61 | if (charCode >= 0xC2 && charCode <= 0xDF) { |
||
| 62 | count = 1; |
||
| 63 | } else if (charCode >= 0xE0 && charCode <= 0xEF ) { |
||
| 64 | count = 2; |
||
| 65 | if (buffer[index] === 0xE0) { |
||
| 66 | lowerBoundary = 0xA0; |
||
| 67 | } |
||
| 68 | if (buffer[index] === 0xED) { |
||
| 69 | upperBoundary = 0x9F; |
||
| 70 | } |
||
| 71 | } else if (charCode >= 0xF0 && charCode <= 0xF4 ) { |
||
| 72 | count = 3; |
||
| 73 | if (buffer[index] === 0xF0) { |
||
| 74 | lowerBoundary = 0x90; |
||
| 75 | } |
||
| 76 | if (buffer[index] === 0xF4) { |
||
| 77 | upperBoundary = 0x8F; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | replace = true; |
||
| 81 | } |
||
| 82 | charCode = charCode & (1 << (8 - count - 1)) - 1; |
||
| 83 | for (let i = 0; i < count; i++) { |
||
| 84 | if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) { |
||
| 85 | replace = true; |
||
| 86 | } |
||
| 87 | charCode = (charCode << 6) | (buffer[index] & 0x3f); |
||
| 88 | index++; |
||
| 89 | } |
||
| 90 | if (replace) { |
||
| 91 | str += String.fromCharCode(0xFFFD); |
||
| 92 | } |
||
| 93 | else if (charCode <= 0xffff) { |
||
| 94 | str += String.fromCharCode(charCode); |
||
| 95 | } else { |
||
| 96 | charCode -= 0x10000; |
||
| 97 | str += String.fromCharCode( |
||
| 98 | ((charCode >> 10) & 0x3ff) + 0xd800, |
||
| 99 | (charCode & 0x3ff) + 0xdc00); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | return str; |
||
| 104 | } |
||
| 105 | |||
| 153 |