| Conditions | 1 |
| Paths | 1 |
| Total Lines | 76 |
| 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 extend from 'extend' |
||
| 76 | export function urlList(obj, tplPath, match, jsonPage) { |
||
| 77 | var p = new Promise((resolve, reject) => { |
||
| 78 | if(obj.autocomplete !== true && obj.autocomplete !== 'true') { |
||
| 79 | var host = obj.sourceString |
||
| 80 | host = host.split('/') |
||
| 81 | var httpUse = http |
||
| 82 | var defaultPort = 80 |
||
| 83 | if(host[0] === 'https:') { |
||
| 84 | httpUse = https |
||
| 85 | defaultPort = 443 |
||
| 86 | } |
||
| 87 | host = host[2].split(':') |
||
| 88 | |||
| 89 | var pathSource = obj.sourceString.split('//') |
||
| 90 | if(typeof pathSource[1] !== 'undefined' && pathSource[1] !== null) { |
||
| 91 | pathSource = pathSource[1].split('/') |
||
| 92 | pathSource.shift() |
||
| 93 | pathSource = '/' + path.join('/') |
||
| 94 | }else { |
||
| 95 | pathSource = '/' |
||
| 96 | } |
||
| 97 | var options = { |
||
| 98 | hostname: host[0], |
||
| 99 | port: (typeof host[1] !== 'undefined' && host[1] !== null) ? host[1] : defaultPort, |
||
| 100 | path: pathSource, |
||
| 101 | method: 'GET', |
||
| 102 | headers: { |
||
| 103 | 'Content-Type': 'application/x-www-form-urlencoded', |
||
| 104 | 'Content-Length': 0 |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | var body = '' |
||
| 109 | |||
| 110 | var localReq = httpUse.request(options, (localRes) => { |
||
| 111 | localRes.setEncoding('utf8') |
||
| 112 | localRes.on('data', (chunk) => { |
||
| 113 | body += chunk |
||
| 114 | }) |
||
| 115 | localRes.on('end', () => { |
||
| 116 | try { |
||
| 117 | if(typeof body === 'string') { |
||
| 118 | var parsedBody = JSON.parse(body) |
||
| 119 | if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Array]') { |
||
| 120 | jsonPage['abe_source'][obj.key] = parsedBody |
||
| 121 | }else if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Object]') { |
||
| 122 | jsonPage['abe_source'][obj.key] = [parsedBody] |
||
| 123 | } |
||
| 124 | }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Array]') { |
||
| 125 | jsonPage['abe_source'][obj.key] = body |
||
| 126 | }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Object]') { |
||
| 127 | jsonPage['abe_source'][obj.key] = body |
||
| 128 | } |
||
| 129 | } catch(e) { |
||
| 130 | console.log(clc.red(`Error ${obj.sourceString} is not a valid JSON`), `\n${e}`) |
||
| 131 | } |
||
| 132 | resolve() |
||
| 133 | }) |
||
| 134 | }) |
||
| 135 | |||
| 136 | localReq.on('error', (e) => { |
||
| 137 | console.log(e) |
||
| 138 | }) |
||
| 139 | |||
| 140 | // write data to request body |
||
| 141 | localReq.write('') |
||
| 142 | localReq.end() |
||
| 143 | |||
| 144 | }else { |
||
| 145 | jsonPage['abe_source'][obj.key] = obj.sourceString |
||
| 146 | resolve() |
||
| 147 | } |
||
| 148 | }) |
||
| 149 | |||
| 150 | return p |
||
| 151 | } |
||
| 152 | |||
| 241 | } |