| Conditions | 6 |
| Paths | 5 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | import {Promise} from 'es6-promise' |
||
| 65 | var p = new Promise((resolve) => { |
||
| 66 | if(obj.autocomplete !== true && obj.autocomplete !== 'true') { |
||
| 67 | var host = obj.sourceString |
||
| 68 | host = host.split('/') |
||
| 69 | var httpUse = http |
||
| 70 | var defaultPort = 80 |
||
| 71 | if(host[0] === 'https:') { |
||
| 72 | httpUse = https |
||
| 73 | defaultPort = 443 |
||
| 74 | } |
||
| 75 | host = host[2].split(':') |
||
| 76 | |||
| 77 | var pathSource = obj.sourceString.split('//') |
||
| 78 | if(pathSource[1] != null) { |
||
| 79 | pathSource = pathSource[1].split('/') |
||
| 80 | pathSource.shift() |
||
| 81 | pathSource = '/' + path.join('/') |
||
| 82 | }else { |
||
| 83 | pathSource = '/' |
||
| 84 | } |
||
| 85 | var options = { |
||
| 86 | hostname: host[0], |
||
| 87 | port: (host[1] != null) ? host[1] : defaultPort, |
||
| 88 | path: pathSource, |
||
| 89 | method: 'GET', |
||
| 90 | headers: { |
||
| 91 | 'Content-Type': 'application/x-www-form-urlencoded', |
||
| 92 | 'Content-Length': 0 |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | var body = '' |
||
| 97 | |||
| 98 | var localReq = httpUse.request(options, (localRes) => { |
||
| 99 | localRes.setEncoding('utf8') |
||
| 100 | localRes.on('data', (chunk) => { |
||
| 101 | body += chunk |
||
| 102 | }) |
||
| 103 | localRes.on('end', () => { |
||
| 104 | try { |
||
| 105 | if(typeof body === 'string') { |
||
| 106 | var parsedBody = JSON.parse(body) |
||
| 107 | if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Array]') { |
||
| 108 | jsonPage['abe_source'][obj.key] = parsedBody |
||
| 109 | }else if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Object]') { |
||
| 110 | jsonPage['abe_source'][obj.key] = [parsedBody] |
||
| 111 | } |
||
| 112 | }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Array]') { |
||
| 113 | jsonPage['abe_source'][obj.key] = body |
||
| 114 | }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Object]') { |
||
| 115 | jsonPage['abe_source'][obj.key] = body |
||
| 116 | } |
||
| 117 | } catch(e) { |
||
| 118 | console.log(`Error ${obj.sourceString} is not a valid JSON`, `\n${e}`) |
||
| 119 | } |
||
| 120 | resolve() |
||
| 121 | }) |
||
| 122 | }) |
||
| 123 | |||
| 124 | localReq.on('error', (e) => { |
||
| 125 | console.log(e) |
||
| 126 | }) |
||
| 127 | |||
| 128 | // write data to request body |
||
| 129 | localReq.write('') |
||
| 130 | localReq.end() |
||
| 131 | |||
| 132 | }else { |
||
| 133 | jsonPage['abe_source'][obj.key] = obj.sourceString |
||
| 134 | resolve() |
||
| 135 | } |
||
| 136 | }) |
||
| 137 | |||
| 229 | } |