code refactor

This commit is contained in:
MaysWind
2025-06-22 22:27:45 +08:00
parent a69db9d299
commit dfa573b49b
2 changed files with 64 additions and 29 deletions
+36
View File
@@ -1,3 +1,39 @@
export class KnownFileType {
private static readonly allInstancesByExtension: Record<string, KnownFileType> = {};
public static readonly CSV = new KnownFileType('csv', 'text/csv');
public static readonly TSV = new KnownFileType('tsv', 'text/tab-separated-values');
public static readonly MARKDOWN = new KnownFileType('md', 'text/markdown');
public readonly extension: string;
public readonly contentType: string;
private constructor(extension: string, contentType: string) {
this.extension = extension;
this.contentType = contentType;
KnownFileType.allInstancesByExtension[extension] = this;
}
public formatFileName(fileName: string): string {
if (fileName.endsWith(`.${this.extension}`)) {
return fileName;
}
return `${fileName}.${this.extension}`;
}
public createBlob(content: string): Blob {
return new Blob([content], {
type: this.contentType,
});
}
public static parse(extension: string): KnownFileType | undefined {
return KnownFileType.allInstancesByExtension[extension];
}
}
export interface ImportFileTypeAndExtensions {
readonly type: string;
readonly extensions?: string;