Skip to content

Instantly share code, notes, and snippets.

@DigitalLeaves
Last active May 23, 2022 16:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DigitalLeaves/414365b686f7991af9d4fb9bdcc4cc4e to your computer and use it in GitHub Desktop.
Save DigitalLeaves/414365b686f7991af9d4fb9bdcc4cc4e to your computer and use it in GitHub Desktop.
Mapping file extensions to font awesome classes for easy UI display
const fontAwesomeIconClasses = {
// Media
"png": "fa-file-image-o",
"jpg": "fa-file-image-o",
"jpeg": "fa-file-image-o",
"gif": "fa-file-image-o",
"mp3": "fa-file-audio-o",
"mpg": "fa-file-video-o",
"mpeg": "fa-file-video-o",
"mp4": "fa-file-video-o",
// Documents
"pdf": "fa-file-pdf-o",
"pages": "fa-file-word-o",
"doc": "fa-file-word-o",
"docx": "fa-file-word-o",
"odt": "fa-file-word-o",
"xls": "fa-file-excel-o",
"numbers": "fa-file-excel-o",
"xlsx": "fa-file-excel-o",
"ods": "fa-file-excel-o",
"ppt": "fa-file-powerpoint-o",
"pptx": "fa-file-powerpoint-o",
"key": "fa-file-powerpoint-o",
"odp": "fa-file-powerpoint-o",
"txt": "fa-file-text-o",
"htm": "fa-file-code-o",
"html": "fa-file-code-o",
"json": "fa-file-code-o",
// Archives
"gzip": "fa-file-archive-o",
"zip": "fa-file-archive-o"
}
function isValidString(o) {
if (o === null) {
return false;
}
if (
typeof o == "string" ||
(typeof o == "object" && o.constructor === String)
) {
return o.length > 0;
} else {
return false;
}
}
function getFontAwesomeIconFromMIME(filename) {
const extension = filename.split('.').pop()
const result = fontAwesomeIconClasses[extension]
if (isValidString(result)) { return result }
else { return "fa-file-o" }
}
const icon = getFontAwesomeIconFromMIME('png')
console.log(icon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment