I have these functions and they’re being consumed by 3 components.
Where is the appropriate place to put them?
I was thinking of like in Ruby on Rails. They have lib, but I'm not sure these methods are ok in the lib folder.
Currently in src/helpers/upload-file-helpers.ts
export function fileSizeConverter(size: number, fromUnit: string, toUnit: string ): number | string {
const units: string[] = ['B', 'KB', 'MB', 'GB', 'TB'];
const from = units.indexOf(fromUnit.toUpperCase());
const to = units.indexOf(toUnit.toUpperCase());
const BASE_SIZE = 1024;
let result: number | string = 0;
if (from < 0 || to < 0 ) { return result = 'Error: Incorrect units'; }
result = from < to ? size / (BASE_SIZE ** to) : size * (BASE_SIZE ** from);
return result.toFixed(2);
}
export function isFileMoreThanLimit(fileSize: number, fromUnit: string, toUnit: string , limit: number) {
return fileSizeConverter(fileSize, fromUnit, toUnit) > limit;
}
export function fileExtensionChecker(file: string): boolean {
const fileExtensions = {
'png' : true,
'jpg' : true,
'jpeg': true,
'stl' : true,
'obj' : true,
'zip' : true,
'dcm' : true,
'3oxz': true
};
// this is weird, instead of showing undefined if file argument is not present in the hash it will throw error.
return fileExtensions[file] ? true : false;
}
export function fileTypeParser(fileType: string): string {
return fileType.split('/')[1];
}
Also, I deliberately do not want to put these in a class together. This is just being called individually.