UNPKG

1.14 kBJavaScriptView Raw
1/*
2* Copyright 2014 the original author or authors
3* @license MIT, see LICENSE.txt for details
4*
5* @author Scott Andrews
6*/
7
8(function (define) {
9 'use strict';
10
11 var undef;
12
13 define(function (/* require */) {
14
15 /**
16 * Parse a MIME type into it's constituent parts
17 *
18 * @param {string} mime MIME type to parse
19 * @return {{
20 * {string} raw the original MIME type
21 * {string} type the type and subtype
22 * {string} [suffix] mime suffix, including the plus, if any
23 * {Object} params key/value pair of attributes
24 * }}
25 */
26 function parse(mime) {
27 var params, type;
28
29 params = mime.split(';');
30 type = params[0].trim().split('+');
31
32 return {
33 raw: mime,
34 type: type[0],
35 suffix: type[1] ? '+' + type[1] : '',
36 params: params.slice(1).reduce(function (params, pair) {
37 pair = pair.split('=');
38 params[pair[0].trim()] = pair[1] ? pair[1].trim() : undef;
39 return params;
40 }, {})
41 };
42 }
43
44 return {
45 parse: parse
46 };
47
48 });
49
50}(
51 typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
52 // Boilerplate for AMD and Node
53));