所以我有这些功能,它们正由3个组件使用。
放在哪里合适的位置?
我当时想像在红宝石上。他们有lib,但是我不确定这些方法在lib文件夹中是否可以使用。
当前位于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,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];
}
此外,我故意不想将它们放在一个类中。这只是被单独调用。