Fork me on GitHub

Mimoza

Build Status NPM version Coverage Status

Mimoza is a tiny but comprehensive MIME tools library. Features:

  • Resolving mime type by file path/name/extention (with fallback for unknown cases).
  • Finding file extention by mime type.
  • Checking if mime type (or file) can be compressed.
  • Checking if mime type has text content (if you wish to force UTF-8 encoding)
  • You can have multimple instances with different configs.
  • Works in browser too (AMD module).

See detailed API docs.

Installation

for node.js:

npm install mimoza

for browser (AMD module):

bower install mimoza

Example

var Mimoza = require('mimoza');

// Use builtin methods:

Mimoza.getExtension('audio/ogg');       // -> '.oga'

Mimoza.getMimeType('ogg');              // -> 'audio/ogg'
Mimoza.getMimeType('.oga');             // -> 'audio/ogg'
Mimoza.getMimeType('test.oga');         // -> 'audio/ogg'
Mimoza.getMimeType('foo/bar.oga');      // -> 'audio/ogg'

Mimoza.isCompressible('text/html');                // -> true
Mimoza.isCompressible('application/octet-stream'); // -> false

Mimoza.isText('text/html');                // -> true
Mimoza.isText('application/javascript');   // -> true
Mimoza.isText('application/json');         // -> true
Mimoza.isText('application/octet-stream'); // -> false


// Define your own instance

var mime = new Mimoza({
  defaultType: 'hard/core', // mime type for unknown extentions
  preloaded: true           // load default rules
});

// instances are customizeable
mime.register('foo/bar', ['baz', 'moo']);

mime.getExtension('foo/bar');           // -> '.baz'
mime.getMimeType('baz');                // -> 'foo/bar'
mime.getMimeType('moo');                // -> 'foo/bar'

// unknown file types, with default & custom fallback
mime.getMimeType('tada');               // -> 'hard/core'
mime.getMimeType('tada', 'soft/core');  // -> 'soft/core'
class

Mimoza

Constructor

constructor

Mimoza.new

    • new Mimoza([options])

Initiates new instance of Mimoza.

Options
  • defaultType (String): Default mime type used as last-resort for Mimoza#getMimeType. By default: undefined.
  • preloaded (Boolean): Init instance with default mime rules
class method

Mimoza.getExtension

    • Mimoza.getExtension(mimeType)
      • String

Proxy to Mimoza#getExtension of internal, built-in instance of Mimoza filled with some default types.

class method

Mimoza.getMimeType

    • Mimoza.getMimeType(path, fallback)
      • String

Proxy to Mimoza#getMimeType of internal, built-in instance of Mimoza filled with some default types.

class method

Mimoza.isText

    • Mimoza.isText(mimeType)
      • Boolean

Proxy to Mimoza#isText of internal, built-in instance of Mimoza.

instance method

Mimoza#clone

    • Mimoza#clone()
      • Object

Creates copy of current Mimoza instanse

instance method

Mimoza#getExtension

    • Mimoza#getExtension(mimeType)
      • String

Return file extension associated with a mime type.

instance method

Mimoza#getMimeType

    • Mimoza#getMimeType(path[, fallback])
      • String

Lookup a mime type based on extension

instance method

Mimoza#isText

    • Mimoza#isText(mimeType)
      • Boolean

Check if mime type provides text content. Can be used to add encoding.

instance method

Mimoza#register

    • Mimoza#register(mimeType, extensions[, overrideDefault = false])
      • Void
    • mimeType
      • String
    • extensions
      • String
      • Array
    • overrideDefault
      • Boolean

Register given extensions as representatives of mimeType and register first element of extensions as default extension for the mimeType.

Example
mime.register('audio/ogg', ['oga', 'ogg', 'spx']);

mime.getMimeType('.oga');       // -> 'audio/ogg'
mime.getMimeType('.ogg');       // -> 'audio/ogg'
mime.getExtension('audio/ogg'); // -> '.oga'
Overriding default extension

mimeType -> extension is set only once, if you wnt to override it, pass overrideDefault flag as true. See example below:

mime.register('audio/ogg', ['oga']);
mime.getExtension('audio/ogg');
// -> '.oga'

mime.register('audio/ogg', ['spx']);
mime.getExtension('audio/ogg');
// -> '.oga'

mime.register('audio/ogg', ['ogg'], true);
mime.getExtension('audio/ogg');
// -> '.ogg'