1.1.3 • Published 8 months ago

human-filetypes v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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 KindDescriptionExamples
imageImage file.png, .gif, .webp
videoVideo file.mp4, .webm
audioAudio file.mp3, .wav
archiveArchive file.zip, .tar, .tar.gz
documentText Document.pdf, .docx, .odt
spreadsheetSpreadsheet.xlsx, .csv, .tsv
presentationPresentation.ppt, .pptx, .odp
fontFont package.ttf, .otf, .woff2
textPlain text file.txt, .html, .json
applicationExecutable/application package.exe, .jar, .swf
unknownUnknown-

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', ...]
etMimeTypes('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!

1.1.1

8 months ago

1.1.0

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.2

2 years ago

0.1.0

2 years ago