-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
143 lines (116 loc) · 3.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* media-typer
* Copyright(c) 2014-2017 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* RegExp to match type in RFC 6838
*
* type-name = restricted-name
* subtype-name = restricted-name
* restricted-name = restricted-name-first *126restricted-name-chars
* restricted-name-first = ALPHA / DIGIT
* restricted-name-chars = ALPHA / DIGIT / "!" / "#" /
* "$" / "&" / "-" / "^" / "_"
* restricted-name-chars =/ "." ; Characters before first dot always
* ; specify a facet name
* restricted-name-chars =/ "+" ; Characters after last plus always
* ; specify a structured syntax suffix
* ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
* DIGIT = %x30-39 ; 0-9
*/
var SUBTYPE_NAME_REGEXP = /^[A-Za-z0-9][A-Za-z0-9!#$&^_.-]{0,126}$/
var TYPE_NAME_REGEXP = /^[A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126}$/
var TYPE_REGEXP = /^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})\/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$/
/**
* Module exports.
*/
exports.format = format
exports.parse = parse
exports.test = test
/**
* Format object to media type.
*
* @param {object} obj
* @return {string}
* @public
*/
function format (obj) {
if (!obj || typeof obj !== 'object') {
throw new TypeError('argument obj is required')
}
var subtype = obj.subtype
var suffix = obj.suffix
var type = obj.type
if (!type || !TYPE_NAME_REGEXP.test(type)) {
throw new TypeError('invalid type')
}
if (!subtype || !SUBTYPE_NAME_REGEXP.test(subtype)) {
throw new TypeError('invalid subtype')
}
// format as type/subtype
var string = type + '/' + subtype
// append +suffix
if (suffix) {
if (!TYPE_NAME_REGEXP.test(suffix)) {
throw new TypeError('invalid suffix')
}
string += '+' + suffix
}
return string
}
/**
* Test media type.
*
* @param {string} string
* @return {object}
* @public
*/
function test (string) {
if (!string) {
throw new TypeError('argument string is required')
}
if (typeof string !== 'string') {
throw new TypeError('argument string is required to be a string')
}
return TYPE_REGEXP.test(string.toLowerCase())
}
/**
* Parse media type to object.
*
* @param {string} string
* @return {object}
* @public
*/
function parse (string) {
if (!string) {
throw new TypeError('argument string is required')
}
if (typeof string !== 'string') {
throw new TypeError('argument string is required to be a string')
}
var match = TYPE_REGEXP.exec(string.toLowerCase())
if (!match) {
throw new TypeError('invalid media type')
}
var type = match[1]
var subtype = match[2]
var suffix
// suffix after last +
var index = subtype.lastIndexOf('+')
if (index !== -1) {
suffix = subtype.substr(index + 1)
subtype = subtype.substr(0, index)
}
return new MediaType(type, subtype, suffix)
}
/**
* Class for MediaType object.
* @public
*/
function MediaType (type, subtype, suffix) {
this.type = type
this.subtype = subtype
this.suffix = suffix
}