Fix license issues
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / node_modules / type-is / node_modules / mime-types / index.js
1
2 var db = require('mime-db')
3
4 // types[extension] = type
5 exports.types = Object.create(null)
6 // extensions[type] = [extensions]
7 exports.extensions = Object.create(null)
8
9 Object.keys(db).forEach(function (name) {
10   var mime = db[name]
11   var exts = mime.extensions
12   if (!exts || !exts.length) return
13   exports.extensions[name] = exts
14   exts.forEach(function (ext) {
15     exports.types[ext] = name
16   })
17 })
18
19 exports.lookup = function (string) {
20   if (!string || typeof string !== "string") return false
21   // remove any leading paths, though we should just use path.basename
22   string = string.replace(/.*[\.\/\\]/, '').toLowerCase()
23   if (!string) return false
24   return exports.types[string] || false
25 }
26
27 exports.extension = function (type) {
28   if (!type || typeof type !== "string") return false
29   // to do: use media-typer
30   type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/)
31   if (!type) return false
32   var exts = exports.extensions[type[1].toLowerCase()]
33   if (!exts || !exts.length) return false
34   return exts[0]
35 }
36
37 // type has to be an exact mime type
38 exports.charset = function (type) {
39   var mime = db[type]
40   if (mime && mime.charset) return mime.charset
41
42   // default text/* to utf-8
43   if (/^text\//.test(type)) return 'UTF-8'
44
45   return false
46 }
47
48 // backwards compatibility
49 exports.charsets = {
50   lookup: exports.charset
51 }
52
53 // to do: maybe use set-type module or something
54 exports.contentType = function (type) {
55   if (!type || typeof type !== "string") return false
56   if (!~type.indexOf('/')) type = exports.lookup(type)
57   if (!type) return false
58   if (!~type.indexOf('charset')) {
59     var charset = exports.charset(type)
60     if (charset) type += '; charset=' + charset.toLowerCase()
61   }
62   return type
63 }