| Conditions | 10 |
| Total Lines | 133 |
| Code Lines | 106 |
| 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-validation.service.ts ➔ fileValidationService 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 | type MimeTypes = { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @private |
||
| 20 | * @method fileHelperService |
||
| 21 | * @returns FileHelperServiceType |
||
| 22 | */ |
||
| 23 | export default function fileValidationService(): FileValidationService { |
||
| 24 | const extensionByType: MimeTypes = { |
||
| 25 | 'image/jpeg': ['jpg', 'jpeg'], |
||
| 26 | 'image/png': ['png'], |
||
| 27 | 'image/webp': ['webp'], |
||
| 28 | 'image/gif': ['gif'], |
||
| 29 | 'image/svg+xml': ['svg'], |
||
| 30 | 'image/bmp': ['bmp'], |
||
| 31 | 'image/tiff': ['tif', 'tiff'], |
||
| 32 | 'application/postscript': ['eps'], |
||
| 33 | 'video/webm': ['webm'], |
||
| 34 | 'video/x-matroska': ['mkv'], |
||
| 35 | 'video/x-flv': ['flv'], |
||
| 36 | 'video/ogg': ['ogv'], |
||
| 37 | 'audio/ogg': ['ogg', 'ogv', 'oga'], |
||
| 38 | 'video/quicktime': ['mov'], |
||
| 39 | 'video/mp4': ['mp4'], |
||
| 40 | 'audio/mp4': ['mp4'], |
||
| 41 | 'video/x-msvideo': ['avi'], |
||
| 42 | 'video/x-ms-wmv': ['wmv'], |
||
| 43 | 'application/pdf': ['pdf'], |
||
| 44 | 'audio/aac': ['aac'], |
||
| 45 | 'video/mp3': ['mp3'], |
||
| 46 | 'audio/mp3': ['mp3'], |
||
| 47 | 'audio/wav': ['wav'], |
||
| 48 | 'audio/x-flac': ['flac'], |
||
| 49 | 'text/plain': ['txt'], |
||
| 50 | 'application/msword': ['doc'], |
||
| 51 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['docx'], |
||
| 52 | 'image/vnd.microsoft.icon': ['ico'], |
||
| 53 | 'application/zip': ['zip'], |
||
| 54 | 'application/vnd.rar': ['rar'], |
||
| 55 | 'application/json': ['json'], |
||
| 56 | 'application/xml': ['xml'], |
||
| 57 | 'application/x-shockwave-flash': ['swf'], |
||
| 58 | 'application/octet-stream': ['bin'], |
||
| 59 | 'application/x-rar-compressed': ['rar'], |
||
| 60 | 'application/x-tar': ['tar'], |
||
| 61 | 'application/x-gzip': ['gzip'], |
||
| 62 | 'application/x-bzip2': ['bz2'], |
||
| 63 | 'application/x-7z-compressed': ['7z'], |
||
| 64 | 'application/x-zip-compressed': ['zip'], |
||
| 65 | 'application/vnd.android.package-archive': ['apk'], |
||
| 66 | 'application/vnd.apple.keynote': ['key'], |
||
| 67 | 'application/vnd.apple.pages': ['pages'], |
||
| 68 | 'application/vnd.apple.numbers': ['numbers'], |
||
| 69 | 'application/vnd.ms-excel': ['xls'], |
||
| 70 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['xlsx'], |
||
| 71 | 'application/vnd.ms-powerpoint': ['ppt'], |
||
| 72 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['pptx'], |
||
| 73 | 'application/vnd.oasis.opendocument.text': ['odt'], |
||
| 74 | 'application/vnd.oasis.opendocument.spreadsheet': ['ods'], |
||
| 75 | 'application/vnd.oasis.opendocument.presentation': ['odp'], |
||
| 76 | }; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @example |
||
| 80 | * checkByExtension(file, 'png, pdf, svg', {...}); |
||
| 81 | */ |
||
| 82 | function checkByExtension( |
||
| 83 | file: File, |
||
| 84 | extensionAccept: string, |
||
| 85 | mimeOverride: MimeTypes, |
||
| 86 | ): boolean { |
||
| 87 | if (extensionAccept === '*') { |
||
| 88 | return true; |
||
| 89 | } |
||
| 90 | |||
| 91 | const fileExtensions: string[] = extensionAccept |
||
| 92 | .replace(/\s/g, '') |
||
| 93 | .split(','); |
||
| 94 | |||
| 95 | const types = Object.assign(extensionByType, mimeOverride); |
||
| 96 | |||
| 97 | return fileExtensions.some((extension) => { |
||
| 98 | const currentFileExtension = file.name.split('.')[1]; |
||
| 99 | |||
| 100 | if (!currentFileExtension) { |
||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | if (extension !== currentFileExtension) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!types.hasOwnProperty(file.type)) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | return types[file.type].includes(currentFileExtension); |
||
| 113 | }); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @example |
||
| 118 | * checkByExtension(file, 'image/png, application/*', {...}); |
||
| 119 | */ |
||
| 120 | function checkByType(file: File, mimeAccept: string): boolean { |
||
| 121 | if (mimeAccept === '*/*') { |
||
| 122 | return true; |
||
| 123 | } |
||
| 124 | |||
| 125 | const fileTypes = mimeAccept.replace(/\s/g, '').split(','); |
||
| 126 | |||
| 127 | return fileTypes.some((fileType) => { |
||
| 128 | const fileAcceptType = fileType.split('/'); |
||
| 129 | const currentFileType = file.type.split('/'); |
||
| 130 | |||
| 131 | if ( |
||
| 132 | fileAcceptType[0] !== currentFileType[0] && |
||
| 133 | fileAcceptType[0] !== '*' |
||
| 134 | ) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (fileAcceptType[1] === '*') { |
||
| 139 | return true; |
||
| 140 | } |
||
| 141 | |||
| 142 | return fileAcceptType[1] === currentFileType[1]; |
||
| 143 | }); |
||
| 144 | } |
||
| 145 | |||
| 146 | return { |
||
| 147 | extensionByType, |
||
| 148 | checkByExtension, |
||
| 149 | checkByType, |
||
| 150 | }; |
||
| 152 |