Skip to content

Commit 023de15

Browse files
committed
Initial commit
Extracted from https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/headers.js, and added tests plus the lowercasing semantics.
0 parents  commit 023de15

File tree

11 files changed

+648
-0
lines changed

11 files changed

+648
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 2

.eslintrc.json

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"node": true,
5+
"es6": true
6+
},
7+
"parserOptions": {
8+
"ecmaVersion": 6
9+
},
10+
"rules": {
11+
// Possible errors
12+
"comma-dangle": ["error", "never"],
13+
"no-cond-assign": ["error", "except-parens"],
14+
"no-console": "error",
15+
"no-constant-condition": "error",
16+
"no-control-regex": "off",
17+
"no-debugger": "error",
18+
"no-dupe-args": "error",
19+
"no-dupe-keys": "error",
20+
"no-duplicate-case": "error",
21+
"no-empty": "error",
22+
"no-empty-character-class": "error",
23+
"no-ex-assign": "error",
24+
"no-extra-boolean-cast": "error",
25+
"no-extra-parens": ["error", "all", { "conditionalAssign": false, "nestedBinaryExpressions": false, "returnAssign": false }],
26+
"no-extra-semi": "error",
27+
"no-func-assign": "error",
28+
"no-inner-declarations": "off",
29+
"no-invalid-regexp": "error",
30+
"no-irregular-whitespace": "error",
31+
"no-negated-in-lhs": "error",
32+
"no-obj-calls": "error",
33+
"no-regex-spaces": "error",
34+
"no-sparse-arrays": "error",
35+
"no-unexpected-multiline": "error",
36+
"no-unreachable": "error",
37+
"no-unsafe-finally": "off",
38+
"use-isnan": "error",
39+
"valid-jsdoc": "off",
40+
"valid-typeof": "error",
41+
42+
// Best practices
43+
"accessor-pairs": "error",
44+
"array-callback-return": "error",
45+
"block-scoped-var": "off",
46+
"complexity": "off",
47+
"consistent-return": "error",
48+
"curly": ["error", "all"],
49+
"default-case": "off",
50+
"dot-location": ["error", "property"],
51+
"dot-notation": "error",
52+
"eqeqeq": "error",
53+
"guard-for-in": "off",
54+
"no-alert": "error",
55+
"no-caller": "error",
56+
"no-case-declarations": "error",
57+
"no-div-regex": "off",
58+
"no-else-return": "error",
59+
"no-empty-function": "error",
60+
"no-empty-pattern": "error",
61+
"no-eq-null": "error",
62+
"no-eval": "error",
63+
"no-extend-native": "error",
64+
"no-extra-bind": "error",
65+
"no-extra-label": "error",
66+
"no-fallthrough": "error",
67+
"no-floating-decimal": "error",
68+
"no-implicit-coercion": "error",
69+
"no-implicit-globals": "error",
70+
"no-implied-eval": "error",
71+
"no-invalid-this": "error",
72+
"no-iterator": "error",
73+
"no-labels": ["error", { "allowLoop": true }],
74+
"no-lone-blocks": "error",
75+
"no-loop-func": "off",
76+
"no-magic-numbers": "off",
77+
"no-multi-spaces": "error",
78+
"no-multi-str": "error",
79+
"no-native-reassign": "error",
80+
"no-new": "error",
81+
"no-new-func": "error",
82+
"no-new-wrappers": "error",
83+
"no-octal": "error",
84+
"no-octal-escape": "error",
85+
"no-param-reassign": "off",
86+
"no-process-env": "error",
87+
"no-proto": "error",
88+
"no-redeclare": "error",
89+
"no-return-assign": ["error", "except-parens"],
90+
"no-script-url": "off",
91+
"no-self-assign": "error",
92+
"no-self-compare": "error",
93+
"no-sequences": "error",
94+
"no-throw-literal": "error",
95+
"no-unmodified-loop-condition": "error",
96+
"no-unused-expressions": "error",
97+
"no-unused-labels": "error",
98+
"no-useless-call": "error",
99+
"no-useless-concat": "error",
100+
"no-useless-escape": "error",
101+
"no-void": "error",
102+
"no-warning-comments": "off",
103+
"no-with": "error",
104+
"radix": ["error", "as-needed"],
105+
"vars-on-top": "off",
106+
"wrap-iife": ["error", "outside"],
107+
"yoda": ["error", "never"],
108+
109+
// Strict Mode
110+
"strict": ["error", "global"],
111+
112+
// Variables
113+
"init-declarations": "off",
114+
"no-catch-shadow": "error",
115+
"no-delete-var": "error",
116+
"no-label-var": "error",
117+
"no-restricted-globals": "off",
118+
"no-shadow": "error",
119+
"no-shadow-restricted-names": "error",
120+
"no-undef": "error",
121+
"no-undef-init": "error",
122+
"no-undefined": "off",
123+
"no-unused-vars": "error",
124+
"no-use-before-define": ["error", "nofunc"],
125+
126+
// Node.js and CommonJS
127+
"callback-return": "off",
128+
"global-require": "error",
129+
"handle-callback-err": "error",
130+
"no-mixed-requires": ["error", true],
131+
"no-new-require": "error",
132+
"no-path-concat": "error",
133+
"no-process-exit": "error",
134+
"no-restricted-imports": "off",
135+
"no-restricted-modules": "off",
136+
"no-sync": "off",
137+
138+
// Stylistic Issues
139+
"array-bracket-spacing": ["error", "never"],
140+
"block-spacing": ["error", "always"],
141+
"brace-style": ["error", "1tbs", { "allowSingleLine": false }],
142+
"camelcase": ["error", { "properties": "always" }],
143+
"comma-spacing": ["error", { "before": false, "after": true }],
144+
"comma-style": ["error", "last"],
145+
"computed-property-spacing": ["error", "never"],
146+
"consistent-this": "off",
147+
"eol-last": "error",
148+
"func-names": "off",
149+
"func-style": ["error", "declaration"],
150+
"id-blacklist": "off",
151+
"id-length": "off",
152+
"id-match": "off",
153+
"indent": ["error", 2, { "SwitchCase": 1 }],
154+
"jsx-quotes": "off",
155+
"key-spacing": ["error", { "beforeColon": false, "afterColon": true, "mode": "strict" }],
156+
"keyword-spacing": ["error", { "before": true, "after": true }],
157+
"linebreak-style": ["error", "unix"],
158+
"lines-around-comment": "off",
159+
"max-depth": "off",
160+
"max-len": ["error", 120, { "ignoreUrls": true }],
161+
"max-nested-callbacks": "off",
162+
"max-params": "off",
163+
"max-statements": "off",
164+
"max-statements-per-line": ["error", { "max": 1 }],
165+
"new-cap": ["error", { "capIsNewExceptions": ["USVString"] }],
166+
"new-parens": "error",
167+
"newline-after-var": "off",
168+
"newline-before-return": "off",
169+
"newline-per-chained-call": "off",
170+
"no-array-constructor": "error",
171+
"no-bitwise": "off",
172+
"no-continue": "off",
173+
"no-inline-comments": "off",
174+
"no-lonely-if": "error",
175+
"no-mixed-spaces-and-tabs": "error",
176+
"no-multiple-empty-lines": "error",
177+
"no-negated-condition": "off",
178+
"no-nested-ternary": "error",
179+
"no-new-object": "error",
180+
"no-plusplus": "off",
181+
"no-restricted-syntax": "off",
182+
"no-spaced-func": "error",
183+
"no-ternary": "off",
184+
"no-trailing-spaces": "error",
185+
"no-underscore-dangle": "off",
186+
"no-unneeded-ternary": "error",
187+
"no-whitespace-before-property": "error",
188+
"object-curly-spacing": ["error", "always"],
189+
"object-property-newline": "off",
190+
"one-var": ["error", "never"],
191+
"one-var-declaration-per-line": ["error", "initializations"],
192+
"operator-assignment": ["error", "always"],
193+
"operator-linebreak": ["error", "after"],
194+
"padded-blocks": ["error", "never"],
195+
"quote-props": ["error", "as-needed"],
196+
"quotes": ["error", "double", { "avoidEscape": true, "allowTemplateLiterals": true }],
197+
"require-jsdoc": "off",
198+
"semi": ["error", "always"],
199+
"semi-spacing": "error",
200+
"sort-imports": "off",
201+
"sort-vars": "off",
202+
"space-before-blocks": ["error", "always"],
203+
"space-before-function-paren": ["error", { "anonymous": "always", "named": "never" }],
204+
"space-in-parens": ["error", "never"],
205+
"space-infix-ops": "error",
206+
"space-unary-ops": ["error", { "words": true, "nonwords": false }],
207+
"spaced-comment": ["error", "always", { "markers": ["///"] }],
208+
"wrap-regex": "off",
209+
210+
// ECMAScript 6
211+
"arrow-body-style": "off", // meh
212+
"arrow-parens": ["error", "as-needed"],
213+
"arrow-spacing": "error",
214+
"constructor-super": "error",
215+
"generator-star-spacing": ["error", "after"],
216+
"no-class-assign": "error",
217+
"no-confusing-arrow": "off",
218+
"no-const-assign": "error",
219+
"no-dupe-class-members": "error",
220+
"no-duplicate-imports": "error",
221+
"no-new-symbol": "error",
222+
"no-this-before-super": "error",
223+
"no-useless-computed-key": "error",
224+
"no-useless-constructor": "error",
225+
"no-var": "error",
226+
"object-shorthand": "error",
227+
"prefer-arrow-callback": "error",
228+
"prefer-const": ["error", { "ignoreReadBeforeAssign": true }],
229+
"prefer-reflect": "off",
230+
"prefer-rest-params": "off",
231+
"prefer-spread": "off", // TODO with new Node versions
232+
"prefer-template": "off",
233+
"require-yield": "error",
234+
"template-curly-spacing": ["error", "never"],
235+
"yield-star-spacing": ["error", "after"]
236+
}
237+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/npm-debug.log

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- stable
4+
script:
5+
npm run lint && npm test

LICENSE.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright © 2016 Domenic Denicola <[email protected]>
2+
3+
This work is free. You can redistribute it and/or modify it under the
4+
terms of the Do What The Fuck You Want To Public License, Version 2,
5+
as published by Sam Hocevar. See below for more details.
6+
7+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8+
Version 2, December 2004
9+
10+
Copyright (C) 2004 Sam Hocevar <[email protected]>
11+
12+
Everyone is permitted to copy and distribute verbatim or modified
13+
copies of this license document, and changing it is allowed as long
14+
as the name is changed.
15+
16+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
17+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
18+
19+
0. You just DO WHAT THE FUCK YOU WANT TO.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Parse `Content-Type` Header Strings
2+
3+
This package will parse the [`Content-Type`](https://tools.ietf.org/html/rfc7231#section-3.1.1.1) header field into an introspectable data structure, whose parameters can be manipulated:
4+
5+
```js
6+
const contentTypeParser = require("content-type-parser");
7+
8+
const contentType = contentTypeParser(`Text/HTML;Charset="utf-8"`);
9+
10+
console.assert(contentType.toString() === "text/html;charset=utf-8");
11+
12+
console.assert(contentType.type === "text");
13+
console.assert(contentType.subtype === "html");
14+
console.assert(contentType.get("charset") === "utf-8");
15+
16+
contentType.set("charset", "windows-1252");
17+
console.assert(contentType.get("charset") === "windows-1252");
18+
console.assert(contentType.toString() === "text/html;charset=windows-1252");
19+
20+
console.assert(contentType.isHTML() === true);
21+
console.assert(contentType.isXML() === false);
22+
console.assert(contentType.isText() === true);
23+
```
24+
25+
Note how parsing will lowercase the type, subtype, and parameter name tokens (but not parameter values).
26+
27+
If the passed string cannot be parsed as a content-type, `contentTypeParser` will return `null`.
28+
29+
## `ContentType` instance API
30+
31+
This package's main module's default export will return an instance of the `ContentType` class, which has the following public APIs:
32+
33+
### Properties
34+
35+
- `type`: the top-level media type, e.g. `"text"`
36+
- `subtype`: the subtype, e.g. `"html"`
37+
- `parameterList`: an array of `{ separator, key, value }` pairs representing the parameters. The `separator` field contains any whitespace, not just the `;` character.
38+
39+
### Parameter manipulation
40+
41+
In general you should not directly manipulate `parameterList`. Instead, use the following APIs:
42+
43+
- `get("key")`: returns the value of the parameter with the given key, or `undefined` if no such parameter is present
44+
- `set("key", "value")`: adds the given key/value pair to the parameter list, or overwrites the existing value if an entry already existed
45+
46+
Both of these will lowercase the keys.
47+
48+
### MIME type tests
49+
50+
- `isHTML()`: returns true if this instance's MIME type is [the HTML MIME type](https://html.spec.whatwg.org/multipage/infrastructure.html#html-mime-type), `"text/html"`
51+
- `isXML()`: returns true if this instance's MIME type is [an XML MIME type](https://html.spec.whatwg.org/multipage/infrastructure.html#xml-mime-type)
52+
- `isText()`: returns true if this instance's top-level media type is `"text"`
53+
54+
### Serialization
55+
56+
- `toString()` will return a canonicalized representation of the content-type, re-built from the parsed components
57+
58+
## Credits
59+
60+
This package was originally based on the excellent work of [@nicolashenry](https://github.com/nicolashenry), [in jsdom](https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/headers.js). It has since been pulled out into this separate package.

0 commit comments

Comments
 (0)