@d1g1tal/media-type 4.1.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt CHANGED
@@ -1,7 +1,12 @@
1
- Copyright © Domenic Denicola <d@domenic.me>
1
+ Copyright (c) 2023, Jason DiMeo
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided
4
+ that the above copyright notice and this permission notice appear in all copies.
4
5
 
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
7
+ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
8
+ FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
9
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
10
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6
11
 
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+ Source: http://opensource.org/licenses/ISC
package/README.md CHANGED
@@ -5,23 +5,23 @@ This package will parse [MIME types](https://mimesniff.spec.whatwg.org/#understa
5
5
  This version is using ES Modules instead of commonJS.
6
6
 
7
7
  ```js
8
- import { MediaType } from '@d1g1tal/media-type';
8
+ import MediaType from '@d1g1tal/media-type';
9
9
 
10
- const mediaType = new MediaType(`Text/HTML;Charset="utf-8"`);
10
+ const mediaType = new MediaType('Text/HTML;Charset="utf-8"');
11
11
 
12
- console.assert(mediaType.toString() === "text/html;charset=utf-8");
12
+ console.assert(mediaType.toString() == 'text/html;charset=utf-8');
13
13
 
14
- console.assert(mediaType.type === "text");
15
- console.assert(mediaType.subtype === "html");
16
- console.assert(mediaType.essence === "text/html");
17
- console.assert(mediaType.parameters.get("charset") === "utf-8");
14
+ console.assert(mediaType.type == 'text');
15
+ console.assert(mediaType.subtype == 'html');
16
+ console.assert(mediaType.essence == 'text/html');
17
+ console.assert(mediaType.parameters.get('charset') == 'utf-8');
18
18
 
19
- mediaType.parameters.set("charset", "windows-1252");
20
- console.assert(mediaType.parameters.get("charset") === "windows-1252");
21
- console.assert(mediaType.toString() === "text/html;charset=windows-1252");
22
-
23
- console.assert(mediaType.isHTML() === true);
24
- console.assert(mediaType.isXML() === false);
19
+ mediaType.parameters.set('charset', 'windows-1252');
20
+ console.assert(mediaType.parameters.get('charset') == 'windows-1252');
21
+ console.assert(mediaType.toString() == 'text/html;charset=windows-1252');
22
+ // Still considering changing this to maybe 'includes' or 'contains'
23
+ console.assert(mediaType.matches('html') == true);
24
+ console.assert(mediaType.matches('xml') == false);
25
25
  ```
26
26
 
27
27
  Parsing is a fairly complex process; see [the specification](https://mimesniff.spec.whatwg.org/#parsing-a-mime-type) for details (and similarly [for serialization](https://mimesniff.spec.whatwg.org/#serializing-a-mime-type)).
@@ -38,9 +38,9 @@ As an alternative to the constructor, you can use `MediaType.parse(string)`. The
38
38
 
39
39
  ### Properties
40
40
 
41
- - `type`: the media type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `"text"`
42
- - `subtype`: the media type's [subtype](https://mimesniff.spec.whatwg.org/#mime-type-subtype), e.g. `"html"`
43
- - `essence`: the media type's [essence](https://mimesniff.spec.whatwg.org/#mime-type-essence), e.g. `"text/html"`
41
+ - `type`: the media type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `'text'`
42
+ - `subtype`: the media type's [subtype](https://mimesniff.spec.whatwg.org/#mime-type-subtype), e.g. `'html'`
43
+ - `essence`: the media type's [essence](https://mimesniff.spec.whatwg.org/#mime-type-essence), e.g. `'text/html'`
44
44
  - `parameters`: an instance of `MediaTypeParameters`, containing this media type's [parameters](https://mimesniff.spec.whatwg.org/#mime-type-parameters)
45
45
 
46
46
  `type` and `subtype` can be changed. They will be validated to be non-empty and only contain [HTTP token code points](https://mimesniff.spec.whatwg.org/#http-token-code-point).
@@ -52,52 +52,35 @@ As an alternative to the constructor, you can use `MediaType.parse(string)`. The
52
52
  ### Methods
53
53
 
54
54
  - `toString()` serializes the media type to a string
55
- - `isHTML()`: returns true if this instance represents [a HTML media type](https://mimesniff.spec.whatwg.org/#html-mime-type)
56
- - `isXML()`: returns true if this instance represents [an XML media type](https://mimesniff.spec.whatwg.org/#xml-mime-type)
57
- - `isJavaScript({ prohibitParameters })`: returns true if this instance represents [a JavaScript media type](https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type). `prohibitParameters` can be set to true to disallow any parameters, i.e. to test if the media type's serialization is a [JavaScript media type essence match](https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match).
58
-
59
- _Note: the `isHTML()`, `isXML()`, and `isJavaScript()` methods are speculative, and may be removed or changed in future major versions. See [whatwg/mimesniff#48](https://github.com/whatwg/mimesniff/issues/48) for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom._
55
+ - `matches(MediaType|string)`: Checks if the media type matches the specified type.
60
56
 
61
57
  ## `MediaTypeParameters` API
62
58
 
63
- The `MediaTypeParameters` class, instances of which are returned by `MediaType.parameters`, has equivalent surface API to a [JavaScript `Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).
59
+ The `MediaTypeParameters` class, instances of which are returned by `MediaType.parameters`, extends a [JavaScript `Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) with both the keys and values being strings. The keys are parameter names, and the values are parameter values.
64
60
 
65
61
  However, `MediaTypeParameters` methods will always interpret their arguments as appropriate for media types, so e.g. parameter names will be lowercased, and attempting to set invalid characters will throw.
66
62
 
67
63
  Some examples:
68
64
 
69
65
  ```js
70
- const mediaType = new MediaType(`x/x;a=b;c=D;E="F"`);
66
+ const mediaType = new MediaType(`x/x;a=b;c=D;E='F'`);
71
67
 
72
68
  // Logs:
73
69
  // a b
74
70
  // c D
75
71
  // e F
76
- for (const [name, value] of mediaType.parameters) {
72
+ for (const [ name, value ] of mediaType.parameters) {
77
73
  console.log(name, value);
78
74
  }
79
75
 
80
- console.assert(mediaType.parameters.has("a"));
81
- console.assert(mediaType.parameters.has("A"));
82
- console.assert(mediaType.parameters.get("A") === "b");
76
+ console.assert(mediaType.parameters.has('a'));
77
+ console.assert(mediaType.parameters.has('A'));
78
+ console.assert(mediaType.parameters.get('A') === 'b');
83
79
 
84
- mediaType.parameters.set("Q", "X");
85
- console.assert(mediaType.parameters.get("q") === "X");
86
- console.assert(mediaType.toString() === "x/x;a=b;c=d;e=F;q=X");
80
+ mediaType.parameters.set('Q', 'X');
81
+ console.assert(mediaType.parameters.get('q') === 'X');
82
+ console.assert(mediaType.toString() === 'x/x;a=b;c=d;e=F;q=X');
87
83
 
88
84
  // Throws:
89
- mediaType.parameters.set("@", "x");
90
- ```
91
-
92
- ## Raw parsing/serialization APIs
93
-
94
- If you want primitives on which to build your own API, you can get direct access to the parsing and serialization algorithms as follows:
95
-
96
- ```js
97
- import { parse } from '@d1g1tal/media-type';
98
- import { serialize } from '@d1g1tal/media-type';
99
- ```
100
-
101
- `parse(string)` returns an object containing the `type` and `subtype` strings, plus `parameters`, which is a `Map`. This is roughly our equivalent of the spec's [MIME type record](https://mimesniff.spec.whatwg.org/#mime-type). If parsing fails, it instead returns `null`.
102
-
103
- `serialize(record)` operates on the such an object, giving back a string according to the serialization algorithm.
85
+ mediaType.parameters.set('@', 'x');
86
+ ```