Skip to content

Human-friendly categorisation of file types based on extension or MIME type

License

Notifications You must be signed in to change notification settings

anttiviljami/human-filetypes

Repository files navigation

human-filetypes

CI npm version bundle size License Buy me a coffee

Human-friendly taxonomy for file types. Zero dependencies.

List of mime types sourced from MDN.

Usage

npm i human-filetypes

The module exports two main operations fromMime() and fromExtension()

import { fromMime, fromExtension  } from 'human-filetypes'

fromMime

fromMime() converts a MIME type identifier to a human-redable file kind.

const assert = require('assert')

assert.equal(fromMime('image/png'), 'image')
assert.equal(fromMime('image/webp'), 'image')
assert.equal(fromMime('application/pdf'), 'document')
assert.equal(fromMime('application/msword'), 'document')
assert.equal(fromMime('image/jpeg'), 'image')
assert.equal(fromMime('video/mpeg'), 'video')
assert.equal(fromMime('video/webm'), 'video')
assert.equal(fromMime('text/javascript'), 'text')
assert.equal(fromMime('application/json'), 'text')
assert.equal(fromMime('text/csv'), 'spreadsheet')
assert.equal(fromMime('application/zip'), 'archive')

fromExtension

fromExtension() converts a file extension or a filename to a human-redable file kind.

assert.equal(fromExtension('.png'), 'image')
assert.equal(fromExtension('.webp'), 'image')
assert.equal(fromExtension('.gif'), 'image')
assert.equal(fromExtension('.pdf'), 'document')
assert.equal(fromExtension('.docx'), 'document')
assert.equal(fromExtension('.mp4'), 'video')
assert.equal(fromExtension('.webm'), 'video')
assert.equal(fromExtension('.json'), 'text')
assert.equal(fromExtension('.exe'), 'application')
assert.equal(fromExtension('.zip'), 'archive')

Human-readable file kinds

The following taxonomy is used:

File Kind Description Examples
image Image file .png, .gif, .webp
video Video file .mp4, .webm
audio Audio file .mp3, .wav
archive Archive file .zip, .tar, .tar.gz
document Text Document .pdf, .docx, .odt
spreadsheet Spreadsheet .xlsx, .csv, .tsv
presentation Presentation .ppt, .pptx, .odp
font Font package .ttf, .otf, .woff2
text Plain text file .txt, .html, .json
application Executable/application package .exe, .jar, .swf
unknown Unknown -

File kinds are also available to import as an enum:

import { FileKind } from 'human-filetypes'

assert.equal(fromExtension('.zip'), FileKind.Archive)

Reverse operations

In addition, we provide reverse operations getMimeTypes() and getExtensions().

getMimeTypes() returns the list of known mime types for a given file kind.

getMimeTypes('image') // ['image/gif', 'image/jpeg', 'image/png', ...]
getMimeTypes('audio') // ['audio/midi', 'audio/x-midi', 'audio/mpeg', ...]
getMimeTypes('video') // ['video/mp4', 'video/mpeg', 'video/ogg', ...]
getMimeTypes('document') // ['application/pdf', 'application/rtf', ...]
getMimeTypes('font') // ['font/ttf', 'font/woff', ...]
getMimeTypes('text') // ['text/css', 'application/html', ...]
getMimeTypes('application') // ['application/octet-stream', 'application/java-archive', ...]
getMimeTypes('archive') // ['application/gzip', 'application/x-tar', ...]

getExtensions() returns the list of known extensions for a given file kind.

getExtensions('image') // ['png', 'jpeg', ...]
getExtensions('audio') // ['mp3', 'ogg', 'wav', ...]
getExtensions('video') // ['mp4', 'mpeg', 'ogv', ...]
getExtensions('document') // ['docx', 'pdf', ...]
getExtensions('font') // ['ttf', 'woff', ...])
getExtensions('text') // ['css', 'txt', 'csh', ...]
getExtensions('application') // ['bin', 'exe', 'dll', ...]
getExtensions('archive') // ['zip', 'rar', ...]

Contributing

human-filetypes is Free and Open Source Software. Issues and pull requests are more than welcome!

About

Human-friendly categorisation of file types based on extension or MIME type

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •