| Conditions | 20 |
| Paths | 241 |
| Total Lines | 87 |
| 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 file.js ➔ read 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | import path from 'path' |
||
| 181 | export function read(base, dirName, type, flatten, extensions = /(.*?)/, max = 99, current = 0, inversePattern = false) { |
||
| 182 | var arr = [] |
||
| 183 | var level = fse.readdirSync(dirName) |
||
| 184 | var fileCurrentLevel = [] |
||
| 185 | let assets = config.files.templates.assets |
||
| 186 | |||
| 187 | for (var i = 0; i < level.length; i++) { |
||
| 188 | var pathLevel = dirName + '/' + level[i] |
||
| 189 | var isFolder = true |
||
| 190 | try { |
||
| 191 | var directory = fse.lstatSync(pathLevel) |
||
| 192 | if (!directory.isDirectory()) { |
||
| 193 | isFolder = false |
||
| 194 | } |
||
| 195 | } catch (e) { |
||
| 196 | isFolder = false |
||
| 197 | } |
||
| 198 | var match = (isFolder) ? true : (inversePattern) ? !extensions.test(level[i]) : extensions.test(level[i]) |
||
| 199 | if((type === 'files' || type === null) && match) { |
||
| 200 | |||
| 201 | if(level[i].indexOf('.') > -1) { |
||
| 202 | var extension = /(\.[\s\S]*)/.exec(level[i])[0] |
||
| 203 | var cleanName = cmsData.fileAttr.delete(level[i]) |
||
| 204 | var cleanNameNoExt = cleanName.replace(/\..+$/, '') |
||
| 205 | var fileData = cmsData.fileAttr.get(level[i]) |
||
| 206 | |||
| 207 | var date |
||
| 208 | if (fileData.d) { |
||
| 209 | date = fileData.d |
||
| 210 | }else { |
||
| 211 | var stat = fse.statSync(pathLevel) |
||
| 212 | date = stat.mtime |
||
| 213 | } |
||
| 214 | var cleanFilePath = cmsData.fileAttr.delete(pathLevel).replace(config.root, '').replace(/^\/?.+?\//, '') |
||
| 215 | |||
| 216 | var fileDate = moment(date) |
||
| 217 | var duration = moment.duration(moment(fileDate).diff(new Date())).humanize(true) |
||
| 218 | |||
| 219 | var filePath = pathLevel.replace(config.root, '') |
||
| 220 | filePath = filePath.split('/') |
||
| 221 | filePath.shift() |
||
| 222 | filePath = filePath.join('/') |
||
| 223 | var item = { |
||
| 224 | 'name': level[i], |
||
| 225 | 'path': pathLevel, |
||
| 226 | 'cleanPathName': cmsData.fileAttr.delete(pathLevel), |
||
| 227 | 'cleanPath': pathLevel.replace(base + '/', ''), |
||
| 228 | date: date, |
||
| 229 | cleanDate: fileDate.format('YYYY/MM/DD HH:MM:ss'), |
||
| 230 | duration: duration, |
||
| 231 | cleanName: cleanName, |
||
| 232 | cleanNameNoExt: cleanNameNoExt, |
||
| 233 | cleanFilePath: cleanFilePath, |
||
| 234 | filePath: filePath, |
||
| 235 | 'type': 'file', |
||
| 236 | 'fileType': extension |
||
| 237 | } |
||
| 238 | |||
| 239 | if(!flatten) item['folders'] = [] |
||
| 240 | arr.push(item) |
||
| 241 | // push current file name into array to check if siblings folder are assets folder |
||
| 242 | fileCurrentLevel.push(level[i].replace(/\..+$/, '') + assets) |
||
| 243 | } |
||
| 244 | } |
||
| 245 | if(!fileCurrentLevel.includes(level[i]) && match) { |
||
| 246 | if(isFolder) { |
||
| 247 | if(!flatten) { |
||
| 248 | var index = arr.push({'name': level[i], 'path': pathLevel, 'cleanPath': pathLevel.replace(base + '/', ''), 'folders': [], 'type': 'folder'}) - 1 |
||
| 249 | if(current < max){ |
||
| 250 | arr[index].folders = cmsData.file.read(base, pathLevel, type, flatten, extensions, max, current + 1, inversePattern) |
||
| 251 | } |
||
| 252 | }else { |
||
| 253 | if(type === 'folders' || type === null) { |
||
| 254 | arr.push({'name': level[i], 'path': pathLevel, 'cleanPath': pathLevel.replace(base + '/', ''), 'type': 'folder'}) |
||
| 255 | } |
||
| 256 | if(current < max){ |
||
| 257 | Array.prototype.forEach.call(cmsData.file.read(base, pathLevel, type, flatten, extensions, max, current + 1, inversePattern), (files) => { |
||
| 258 | arr.push(files) |
||
| 259 | }) |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return arr |
||
| 267 | } |