-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
380044c
commit 8bc0c39
Showing
5 changed files
with
89 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { FileExtension } from "@/types/files"; | ||
|
||
export const getContentType = (extension: FileExtension) => { | ||
switch (extension) { | ||
case "txt": | ||
return "text/plain"; | ||
case "md": | ||
return "text/markdown"; | ||
case "json": | ||
return "application/json"; | ||
case "csv": | ||
return "text/csv"; | ||
default: | ||
return "text/plain"; | ||
} | ||
}; | ||
|
||
export const saveFile = ( | ||
content: string, | ||
name: string, | ||
extension: FileExtension | ||
) => { | ||
try { | ||
const blob = new Blob([content], { type: getContentType(extension) }); | ||
const url = window.URL.createObjectURL(blob); | ||
const link = document.createElement("a"); | ||
link.href = url; | ||
link.download = `${name}.${extension}`; | ||
link.click(); | ||
window.URL.revokeObjectURL(url); | ||
document.body.removeChild(link); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
export const readFile = (file: File) => { | ||
return new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
resolve(reader.result as string); | ||
}; | ||
reader.onerror = () => { | ||
reject(reader.error); | ||
}; | ||
reader.readAsText(file); | ||
}); | ||
}; | ||
|
||
export const exportAsJSON = (data: any, name: string) => { | ||
const json = JSON.stringify(data, null, 2); | ||
saveFile(json, name, "json"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type FileExtension = "txt" | "md" | "json" | "csv"; |