media-typer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Media-Typer is an efficient tool for managing media types in your JavaScript applications. It functions as a simple RFC 6838 media type parser and formatter which can accurately parse a given media type into its component parts such as type, subtype and suffix. Moreover, it also includes a formatter that can compile these component parts back together. This dual functionality allows it to be effectively used for normalizing media types into a canonical form. Please note, if you wish to parse the string that represents a media type and its parameters as commonly found in HTTP like in 'Content-Type' header, it is suggested to use the content-type module.
Using Media-Typer in your program is straightforward. First, you will need to install it from the npm registry. You can simply do this with the npm install command like: npm install media-typer
.
Once you have installed it, require it in your project as such:
var typer = require('media-typer')
To parse a media type string, you can call the parse function on the required object, passing the media type string as an argument. For example:
var obj = typer.parse('image/svg+xml')
This will return an object with the format as described in the 'parse(string)' section of the readme.
Similarly, if you wish to format an object into a media type string, you can do so using the format function, passing the object as the argument. For example:
var obj = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' })
This would return a media type string in the required format.
Finally, to validate a media-type string, call the test function on the desired string:
var valid = typer.test('image/svg+xml')
This returns true
if the string is well-formatted, and false
otherwise.
Please note, TypeError is thrown if an invalid type string or invalid object values are provided to the parse
and format
functions respectively.
The official documentation and more detailed usage guidelines for media-typer can be found directly in the README file of the GitHub project repository - https://github.com/jshttp/media-typer.git. It contains robust instructions for installation, API usage as well as license information. For more complex or specific issues, users might find it helpful to refer directly to the source code which is also available in the repository.