mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
code refactor
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user