npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fttlib

v1.1.6

Published

A library of helper methods for your Javascript project to get mime types and general file category

Downloads

9

Readme

FTT Logo File Type Translator (FTT)

A library of helper methods for your Javascript project to get mime types and general file category

Releases

Available as an npm package

1.1.6 - Initial Javascript (Node.js) release

Examples

Install

npm i fttlib

Import the library

import { FTT } from 'fttlib'

When you need to know the mime type of a file based on its extension.

let mimeType = FTT.getMimeType('Path/To/My/File.doc')
console.log(mimeType)

mimeType = FTT.getMimeType('Path/To/My/File.docx')
console.log(mimeType)

>  application/msword
>  application/vnd.openxmlformats-officedocument.wordprocessingml.document

When you need to know the file type category of a file based on its extension.

let category = FTT.getFileCategory('Path/To/My/File.docx')
console.log(category)

category = FTT.getFileCategory('Path/To/My/File.jpg')
console.log(category)

>  Document
>  Image

When you need to know the preferred file extension for a file based on its mime type.

let extensions = FTT.getMimeTypeFileExtensions("text/csv")
console.log(extensions[0])

>  csv

Design

This library is designed along the following tenets:

  • No external dependencies
  • No file system access
  • Small memory footprint
  • Simple static methods (no extension methods and no instantiatable classes)
  • Case-insensitive
  • Portable - works in applications targeting any runtime

File Categories

File media types are broken down into relatively few categories. Sub-categories may be considered in a future release according to information on Wikipedia Here are the guidelines that determine a file's category

  • Archive: any file that can be extracted into several files
  • Audio: any file that can only contain an audio stream
  • Binary: any file that is unclassified or does not have a text representation
  • Code: any file that contains instructions that are compilable or machine-readable
  • Document: any file that is designed for conveying structured information between people
  • Image: any file that can only contain a single image or series of images
  • PDF: any file that is considered a document archive format
  • Presentation: any file that is designed for electronic presentations consisting of a series of separate pages or slides
  • Spreadsheet: any file in which data is arranged in rows and columns and can be manipulated and used in calculations
  • Text: any file that is not classified under another category and is not binary
  • Video: any file that is designed to be a container for a video stream

Known Limitations

  • Even though a mime type to file extension mapping is not necessarily one-to-one, the most common should be returned by the library
  • This library does not provide file sniffing capabilities - in other words, if you have a file and you want to verify the contents are of a specific type, look elsewhere. FTT only uses the file name with an extension. That capability may be added in a future release.
  • The only file categories presently considered are Archive, Audio, Binary, Code, Document, Image, PDF, Presentation, Spreadsheet, Text, and Video. Binary is the default if there is no match in the database.

Sources

While it is unrealistic to expect this library to provide a comprehensive list with all the file types the world has to offer, it would be nice if we could get close. Generally, if the file type is even remotely common, it is listed in one of the sources. You can submit a bug report for a file type that is missing or that you think should be added. You will have to include a reputable source as well.

Information sources for this library are provided through the following and in order of preference:

  1. http://www.iana.org/assignments/media-types/media-types.xhtml
  2. https://gitlab.freedesktop.org/xdg/shared-mime-info
  3. https://cdn.rawgit.com/jshttp/mime-db/master/db.json
  4. http://www.file-extensions.org/