broofa/node-mime — pull request #82 by dvijayak (2014-01-15 23:24:32+00:00)
Title: Support wildcard "*" mimes, Compare two mime types

Synopsis
----
I am currently working on the implementation of an algorithm for one of my applications that requires comparisons of mime types with wildcards for identifying intersecting sets of types. I felt that it made sense to extend this fine module that compares with a compiled list of real-world types as opposed to rolling my own solution that naively only checks for a few regexps. For example, regex alone cannot account for non-existent types like `asd/asd`.

I hope someone finds this useful.

Changes
----

1. I modified the `extension()` function to support the wildcard `*` character in the toplevel or sublevel of a mime type when looking for an extension. Additionally, I added some extra validation to comply with [RFC2046](http://www.ietf.org/rfc/rfc2046.txt). For example,

mime.extension('text/html'); // returns 'html'
mime.extension('text/*'); // returns ['appcache', 'ics', 'css', ... , 'event-stream', 'lua', 'markdown']

2. I added a new function `compare()` which checks if two mime types are exactly the same or overlap with each other (a la wildcard). It benefits from the modified `extension()` with regards to validation and wildcard support. For example,

mime.extension('text/html', 'text/html'); // returns true
mime.extension('text/html', 'text/java'); // returns false
mime.extension('text/html', 'text/*'); // returns true

3. Test cases for 1 and 2 have been added to `test.js`.