├── .npmignore ├── .eslintignore ├── .gitignore ├── .babelrc ├── test ├── .eslintrc └── test.js ├── .travis.yml ├── src ├── icons.js ├── extensions.js ├── mimetypes.js └── index.js ├── .eslintrc ├── CHANGELOG.md ├── LICENSE.md ├── package.json └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | /test 2 | 3 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | lib/** 2 | node_modules/** 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /node_modules 3 | yarn.lock 4 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": [ 4 | "transform-runtime" 5 | ] 6 | } -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "es6": true, 4 | "mocha": true 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4.0' 4 | sudo: false 5 | 6 | before_script: 7 | - npm install 8 | 9 | script: npm run test 10 | -------------------------------------------------------------------------------- /src/icons.js: -------------------------------------------------------------------------------- 1 | const icons = { 2 | image: 'fa-file-image', 3 | pdf: 'fa-file-pdf', 4 | word: 'fa-file-word', 5 | powerpoint: 'fa-file-powerpoint', 6 | excel: 'fa-file-excel', 7 | csv: 'fa-file-csv', 8 | audio: 'fa-file-audio', 9 | video: 'fa-file-video', 10 | archive: 'fa-file-archive', 11 | code: 'fa-file-code', 12 | text: 'fa-file-alt', 13 | file: 'fa-file' 14 | } 15 | 16 | export default icons 17 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "indent": [ 4 | 2, 5 | 4 6 | ], 7 | "quotes": [ 8 | 2, 9 | "single" 10 | ], 11 | "linebreak-style": [ 12 | 2, 13 | "unix" 14 | ], 15 | "semi": [ 16 | 2, 17 | "never" 18 | ] 19 | }, 20 | "ecmaFeatures": { 21 | "modules": true 22 | }, 23 | "env": { 24 | "es6": true, 25 | "browser": true 26 | }, 27 | "extends": "eslint:recommended" 28 | } 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `font-awesome-filetypes` will be documented in this file. 4 | 5 | ## 2.1.0 6 | - Added support for mime types with `getClassNameForMimeType` and `getIconForMimeType` 7 | - Added support for more extensions 8 | 9 | ## 2.0.1 10 | - Fix archive file icons 11 | 12 | ## 2.0.0 13 | - Font Awesome 5 icons 14 | 15 | ## 1.2.0 16 | - Added `getExtensionForFilename`, `getClassNameForFilename` & `getIconForFilename` functions 17 | 18 | ## 1.1.0 19 | - Added icons for `txt` files 20 | 21 | ## 1.0.1 22 | - Fixed npmignore issue 23 | 24 | ## 1.0.0 25 | - Initial release 26 | -------------------------------------------------------------------------------- /src/extensions.js: -------------------------------------------------------------------------------- 1 | import icons from './icons' 2 | 3 | const extensions = { 4 | gif: icons.image, 5 | jpeg: icons.image, 6 | jpg: icons.image, 7 | png: icons.image, 8 | 9 | pdf: icons.pdf, 10 | 11 | doc: icons.word, 12 | docx: icons.word, 13 | 14 | ppt: icons.powerpoint, 15 | pptx: icons.powerpoint, 16 | 17 | xls: icons.excel, 18 | xlsx: icons.excel, 19 | 20 | csv: icons.csv, 21 | 22 | aac: icons.audio, 23 | mp3: icons.audio, 24 | ogg: icons.audio, 25 | 26 | avi: icons.video, 27 | flv: icons.video, 28 | mkv: icons.video, 29 | mp4: icons.video, 30 | 31 | gz: icons.archive, 32 | zip: icons.archive, 33 | 34 | css: icons.code, 35 | html: icons.code, 36 | js: icons.code, 37 | 38 | txt: icons.text 39 | } 40 | 41 | export default extensions 42 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Sebastian De Deyne 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "font-awesome-filetypes", 3 | "version": "2.1.0", 4 | "description": "Helper to retrieve the Font Awesome icon for a specific file extension", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "node_modules/.bin/babel src -d lib", 8 | "test:lint": "node_modules/.bin/eslint .", 9 | "test:unit": "mocha --require babel-polyfill --compilers js:babel-register", 10 | "test": "npm run test:lint && npm run test:unit", 11 | "prepublish": "npm run build" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/spatie/font-awesome-filetypes.git" 16 | }, 17 | "keywords": [ 18 | "fontawesome", 19 | "files", 20 | "extensions" 21 | ], 22 | "author": "Sebastian De Deyne", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/spatie/font-awesome-filetypes/issues" 26 | }, 27 | "homepage": "https://github.com/spatie/font-awesome-filetypes#readme", 28 | "devDependencies": { 29 | "babel-cli": "^6.26.0", 30 | "babel-plugin-transform-runtime": "^6.23.0", 31 | "babel-polyfill": "^6.26.0", 32 | "babel-preset-es2015": "^6.24.1", 33 | "babel-register": "^6.26.0", 34 | "babel-runtime": "^6.26.0", 35 | "chai": "^3.3.0", 36 | "eslint": "^1.7.2", 37 | "mocha": "^2.3.3" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/mimetypes.js: -------------------------------------------------------------------------------- 1 | import icons from './icons' 2 | 3 | const mimeTypes = { 4 | 'image/gif': icons.image, 5 | 'image/jpeg': icons.image, 6 | 'image/png': icons.image, 7 | 8 | 'application/pdf': icons.pdf, 9 | 10 | 'application/msword': icons.word, 11 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': icons.word, 12 | 13 | 'application/mspowerpoint': icons.powerpoint, 14 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation': icons.powerpoint, 15 | 16 | 'application/msexcel': icons.excel, 17 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': icons.excel, 18 | 19 | 'text/csv': icons.csv, 20 | 21 | 'audio/aac': icons.audio, 22 | 'audio/wav': icons.audio, 23 | 'audio/mpeg': icons.audio, 24 | 'audio/mp4': icons.audio, 25 | 'audio/ogg': icons.audio, 26 | 27 | 'video/x-msvideo': icons.video, 28 | 'video/mpeg': icons.video, 29 | 'video/mp4': icons.video, 30 | 'video/ogg': icons.video, 31 | 'video/quicktime': icons.video, 32 | 'video/webm': icons.video, 33 | 34 | 'application/gzip': icons.archive, 35 | 'application/zip': icons.archive, 36 | 37 | 'text/css': icons.code, 38 | 'text/html': icons.code, 39 | 'text/javascript': icons.code, 40 | 'application/javascript': icons.code, 41 | 42 | 'text/plain': icons.text, 43 | 'text/richtext': icons.text, 44 | 'text/rtf': icons.text 45 | } 46 | 47 | export default mimeTypes 48 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import extensions from './extensions' 2 | import mimeTypes from './mimetypes' 3 | import icons from './icons' 4 | 5 | /** 6 | * @param {string} extension 7 | * @returns {string} 8 | */ 9 | export function getClassNameForExtension(extension) { 10 | return extensions[extension.toLowerCase()] || icons.file 11 | } 12 | 13 | /** 14 | * @param {string} extension 15 | * @returns {string} 16 | */ 17 | export function getIconForExtension(extension) { 18 | return `` 19 | } 20 | 21 | /** 22 | * @param {string} extension 23 | * @returns {string} 24 | */ 25 | export function getExtensionForFilename(filename) { 26 | return filename.slice((filename.lastIndexOf('.') - 1 >>> 0) + 2) 27 | } 28 | 29 | /** 30 | * @param {string} extension 31 | * @returns {string} 32 | */ 33 | export function getClassNameForFilename(filename) { 34 | return getClassNameForExtension(getExtensionForFilename(filename)) 35 | } 36 | 37 | /** 38 | * @param {string} extension 39 | * @returns {string} 40 | */ 41 | export function getIconForFilename(filename) { 42 | return getIconForExtension(getExtensionForFilename(filename)) 43 | } 44 | 45 | /** 46 | * @param {string} mimeType 47 | * @returns {string} 48 | */ 49 | export function getClassNameForMimeType(mimeType) { 50 | return mimeTypes[mimeType.toLowerCase()] || icons.file 51 | } 52 | 53 | /** 54 | * @param {string} mimeType 55 | * @returns {string} 56 | */ 57 | export function getIconForMimeType(mimeType) { 58 | return `` 59 | } 60 | 61 | export default getClassNameForExtension 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://supportukrainenow.org) 3 | 4 | # Font Awesome Filetypes 5 | 6 | [![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 7 | [![Build Status](https://img.shields.io/travis/spatie/font-awesome-filetypes.svg?style=flat-square)](https://travis-ci.org/spatie/font-awesome-filetypes) 8 | 9 | Helper to retrieve the Font Awesome 5 icon for a specific file extension. 10 | 11 | ## Support us 12 | 13 | [](https://spatie.be/github-ad-click/font-awesome-filetypes) 14 | 15 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 16 | 17 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 18 | 19 | ## Installation 20 | 21 | ```bash 22 | npm install font-awesome-filetypes 23 | ``` 24 | 25 | > For Font Awesome 4, install v1 of this package 26 | 27 | ## Usage 28 | 29 | ### Basic Examples 30 | 31 | By default, the module exports the function to retrieve an icon's class name: 32 | 33 | ```es6 34 | import getClassNameForExtension from 'font-awesome-filetypes' 35 | 36 | const filename = 'foobar.jpg' 37 | const extension = filename.split('.').pop() 38 | 39 | const className = getClassNameForExtension(extension) 40 | 41 | // className = 'fa-file-image-o' 42 | ``` 43 | 44 | You could also explicitely import the function: 45 | 46 | ```es6 47 | import { getClassNameForExtension } from 'font-awesome-filetypes' 48 | ``` 49 | 50 | Ths module also exposes a function to retrieve an html string of the icon (as per the Font Awesome docs): 51 | 52 | ```es6 53 | import { getIconForExtension } from 'font-awesome-filetypes' 54 | 55 | const filename = 'foobar.jpg' 56 | const extension = filename.split('.').pop() 57 | 58 | const icon = getIconForExtension(extension) 59 | 60 | // icon = '' 61 | ``` 62 | 63 | ### Other Exposed Objects 64 | 65 | Retrieve the object containing all icon classnames: 66 | 67 | ```es6 68 | import icons from 'font-awesome-filetypes/lib/icons' 69 | 70 | // icons = { image: 'fa-file-image-o', pdf: 'fa-file-pdf-o', ... } 71 | ``` 72 | 73 | Retrieve the object containing all supported extensions: 74 | 75 | ```es6 76 | import extensions from 'font-awesome-filetypes/lib/extensions' 77 | 78 | // extensions = { gif: 'fa-file-image-o', jpeg: 'fa-file-image-o', ... } 79 | ``` 80 | 81 | ## Testing 82 | 83 | You can run the tests (ESLint & Mocha) with: 84 | 85 | ```bash 86 | npm run test 87 | ``` 88 | 89 | ### Changelog 90 | 91 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 92 | 93 | ## Contributing 94 | 95 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 96 | 97 | ## Security 98 | 99 | If you discover any security related issues, please email [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 100 | 101 | ## Postcardware 102 | 103 | You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. 104 | 105 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 106 | 107 | We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). 108 | 109 | ## Credits 110 | 111 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne) 112 | - [All Contributors](../../contributors) 113 | 114 | ## License 115 | 116 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 117 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import { assert } from 'chai' 2 | import { 3 | getClassNameForExtension, 4 | getIconForExtension, 5 | getExtensionForFilename, 6 | getClassNameForFilename, 7 | getIconForFilename, 8 | getClassNameForMimeType, 9 | getIconForMimeType 10 | } from '../src/index' 11 | import icons from '../src/icons' 12 | 13 | describe('icons', () => { 14 | 15 | it('returns_an_object_with_all_icons', () => { 16 | assert.equal(Object.keys(icons).length, 12) 17 | assert.equal(Object.values(icons).length, 12) 18 | }) 19 | }) 20 | 21 | describe('get_class_name_for_extension', () => { 22 | 23 | it('returns_a_default_file_icon', () => { 24 | assert.equal('fa-file', getClassNameForExtension('randomExtension')) 25 | }) 26 | 27 | it('can_handle_other_cases', () => { 28 | assert.equal('fa-file-image', getClassNameForExtension('JPG')) 29 | }) 30 | 31 | it('returns_the_correct_class_name_for_an_extension', () => { 32 | let testCases = { 33 | gif: 'fa-file-image', 34 | jpeg: 'fa-file-image', 35 | jpg: 'fa-file-image', 36 | png: 'fa-file-image', 37 | pdf: 'fa-file-pdf', 38 | doc: 'fa-file-word', 39 | docx: 'fa-file-word', 40 | ppt: 'fa-file-powerpoint', 41 | pptx: 'fa-file-powerpoint', 42 | xls: 'fa-file-excel', 43 | xlsx: 'fa-file-excel', 44 | csv: 'fa-file-csv', 45 | aac: 'fa-file-audio', 46 | mp3: 'fa-file-audio', 47 | ogg: 'fa-file-audio', 48 | avi: 'fa-file-video', 49 | flv: 'fa-file-video', 50 | mkv: 'fa-file-video', 51 | mp4: 'fa-file-video', 52 | gz: 'fa-file-archive', 53 | zip: 'fa-file-archive', 54 | css: 'fa-file-code', 55 | html: 'fa-file-code', 56 | js: 'fa-file-code', 57 | txt: 'fa-file-alt' 58 | } 59 | 60 | Object.keys(testCases).map(extension => { 61 | assert.equal(testCases[extension], getClassNameForExtension(extension)) 62 | }) 63 | }) 64 | }) 65 | 66 | describe('get_icon_for_extension', () => { 67 | 68 | it('returns_the_correct_icon_for_an_extension', () => { 69 | assert.equal('', getIconForExtension('jpg')) 70 | }) 71 | }) 72 | 73 | describe('get_extension_for_filename', () => { 74 | 75 | it('returns_the_correct_extension_for_a_filename', () => { 76 | let testCases = { 77 | 'picture.jpg': 'jpg', 78 | 'audio.v1.mp3': 'mp3', 79 | '.htaccess': '' 80 | } 81 | Object.keys(testCases).map(filename => { 82 | assert.equal(testCases[filename], getExtensionForFilename(filename)) 83 | }) 84 | }) 85 | }) 86 | 87 | describe('get_class_name_for_filename', () => { 88 | 89 | it('returns_a_default_file_icon', () => { 90 | assert.equal('fa-file', getClassNameForFilename('file.randomExtension')) 91 | assert.equal('fa-file', getClassNameForFilename('.htaccess')) 92 | }) 93 | 94 | it('can_handle_other_cases', () => { 95 | assert.equal('fa-file-image', getClassNameForFilename('image.JPG')) 96 | }) 97 | 98 | it('returns_the_correct_class_name_for_a_filename', () => { 99 | /* 100 | * Just some smoke testing, going through every single filename would be madness. 101 | */ 102 | let testCases = { 103 | 'image.jpg': 'fa-file-image', 104 | 'document.pdf': 'fa-file-pdf', 105 | 'document.doc': 'fa-file-word', 106 | 'presentation.ppt': 'fa-file-powerpoint', 107 | 'spreadsheet.xls': 'fa-file-excel', 108 | 'song.mp3': 'fa-file-audio', 109 | 'song.mp4': 'fa-file-video', 110 | 'archive.zip': 'fa-file-archive', 111 | 'script.js': 'fa-file-code' 112 | } 113 | 114 | Object.keys(testCases).map(filename => { 115 | assert.equal(testCases[filename], getClassNameForFilename(filename)) 116 | }) 117 | }) 118 | }) 119 | 120 | describe('get_icon_for_filename', () => { 121 | 122 | it('returns_the_correct_icon_for_a_filename', () => { 123 | assert.equal('', getIconForFilename('image.jpg')) 124 | }) 125 | }) 126 | 127 | describe('get_class_name_for_mimeType', () => { 128 | 129 | it('returns_a_default_file_icon', () => { 130 | assert.equal('fa-file', getClassNameForMimeType('example/mimeType')) 131 | }) 132 | 133 | it('can_handle_other_cases', () => { 134 | assert.equal('fa-file-image', getClassNameForMimeType('IMAGE/jpeg')) 135 | }) 136 | 137 | it('returns_the_correct_class_name_for_a_mimeType', () => { 138 | let testCases = { 139 | 'image/gif': 'fa-file-image', 140 | 'image/jpeg': 'fa-file-image', 141 | 'image/png': 'fa-file-image', 142 | 'application/pdf': 'fa-file-pdf', 143 | 'application/msword': 'fa-file-word', 144 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'fa-file-word', 145 | 'application/mspowerpoint': 'fa-file-powerpoint', 146 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'fa-file-powerpoint', 147 | 'application/msexcel': 'fa-file-excel', 148 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'fa-file-excel', 149 | 'text/csv': 'fa-file-csv', 150 | 'audio/aac': 'fa-file-audio', 151 | 'audio/wav': 'fa-file-audio', 152 | 'audio/mpeg': 'fa-file-audio', 153 | 'audio/mp4': 'fa-file-audio', 154 | 'audio/ogg': 'fa-file-audio', 155 | 'video/x-msvideo': 'fa-file-video', 156 | 'video/mpeg': 'fa-file-video', 157 | 'video/ogg': 'fa-file-video', 158 | 'video/quicktime': 'fa-file-video', 159 | 'video/webm': 'fa-file-video', 160 | 'application/gzip': 'fa-file-archive', 161 | 'application/zip': 'fa-file-archive', 162 | 'text/css': 'fa-file-code', 163 | 'text/html': 'fa-file-code', 164 | 'text/javascript': 'fa-file-code', 165 | 'application/javascript': 'fa-file-code', 166 | 'text/plain': 'fa-file-alt', 167 | 'text/richtext': 'fa-file-alt', 168 | 'text/rtf': 'fa-file-alt' 169 | } 170 | 171 | Object.keys(testCases).map(mimeType => { 172 | assert.equal(testCases[mimeType], getClassNameForMimeType(mimeType)) 173 | }) 174 | }) 175 | }) 176 | 177 | describe('get_icon_for_mimeType', () => { 178 | 179 | it('returns_the_correct_icon_for_a_mimeType', () => { 180 | assert.equal('', getIconForMimeType('image/jpeg')) 181 | }) 182 | }) 183 | --------------------------------------------------------------------------------