Giter Club home page Giter Club logo

mimetype-js's Introduction

build status mimetype-js

Overview

I find keep making these file extension lookup tables for mime types. It's about time I put it in a module to save me the trouble.

Examples

general case

	var mimetype = require('mimetype');
	
	console.log(mimetype.lookup("myfile.txt")); // Should display text/plain
	mimetype.set('.exotic', 'x-application/experimental'); // Add/update a mime type in the catalog
	console.log(mimetype.lookup("myfile.exotic")); // Should display x-application/experimental
	mimetype.del('.exotic'); // Removes the mime type from the catalog
	console.log(mimetype.lookup("myfile.exitoc")); // Should display false
	mimetype.forEach(function (ext, mime_type_string) {
		console.log(ext, mime_type_string); // Display the extension and matching mimetype in catalog
	});

Special cases

Sometimes detecting by filename extensions isn't work and you want to default to a general purposes mime-type (e.g. text/plain, application/octet-stream).

	var mimetype = require('mimetype');
	
	// This should display 0 (false)
	console.log(mimetype.lookup("filename.unknownMimeType");
	// This should display the string text/plain
	console.log(mimetype.lookup("filename.unknownMimeType", false, "text/plain");
	// This should display the string text/plain; charset=UTF-8
	console.log(mimetype.lookup("filename.unknownMimeType", "UTF-8", "text/plain");

Using mimetype.js with MongoDB Shell

While this was implemented as a NodeJS module it also works under MongoDB's shell. Instead of including with a "require" you would load the JavaScript file load-mimetype.js.

	load("./extras/load-mimetype.js");
	print("Check the mime type of test.txt:" + MimeType.lookup("test.txt"));

This would display something like-

	MongoDB shell version: 2.2.0
	connecting to: test
	> load("./extras/load-mimetype.js");
	> print("Check the mime type of test.txt: " + MimeType.lookup("test.txt"));
	Check the mime type of test.txt: text/plain
	> 

mimetype-js's People

Contributors

rsdoiel avatar bioball avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.