Skip to content

Commit

Permalink
chore: docs and api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Sep 13, 2023
1 parent 3286d6d commit 2e8578a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
21 changes: 12 additions & 9 deletions README.md
Expand Up @@ -54,21 +54,18 @@ here's a brief rundown ...
truth" for MIME type information. It is a dataset (JSON file), not an API, with mime type definitions pulled from a variety of authoritative sources.

[`mime-types`](https://github.com/jshttp/mime-types) is a thin
wrapper around mime-db that provides an API drop-in compatible(ish) with `mime @ < v1.3.6` API.
wrapper around mime-db that provides an API that is mostly-compatible with `mime @ < v1.3.6`.

`mime` (this project) is similar to `mime-types`, but with the following enhancements:
`mime` (this project) is like `mime-types`, but with the following enhancements:

- Resolves type conflicts (See [mime-score](https://github.com/broofa/mime-score) for details)
- Compact footprint. (2KB vs 18KB)
- Native TS support
- Smaller (`mime` is 2-8KB, `mime-types` is 18KB)
- Zero dependencies
- Built-in TS support

## Mime API

Both `require('mime')` and `require('mime/lite')` return instances of the MIME
class, documented below.

Note: Inputs to this API are case-insensitive. Outputs (returned values) will
be lowercase.
Both `require('mime')` and `require('mime/lite')` return instances of the `Mime` class, documented below.

### new Mime(typeMap, ... more maps)

Expand Down Expand Up @@ -125,6 +122,12 @@ Content-Type headers) are ignored.
mime.getExtension('text/plain'); // ⇨ 'txt'
mime.getExtension('application/json'); // ⇨ 'json'
mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
### mime.getAllExtensions(type)

Get all extensions for the given mime type.

```javascript --run default
mime.getAllExtensions('image/jpeg'); // ⇨ [ 'jpeg', 'jpg', 'jpe' ]
```

### mime.define(typeMap[, force = false])
Expand Down
17 changes: 13 additions & 4 deletions docs/README_js.md
Expand Up @@ -54,13 +54,14 @@ here's a brief rundown ...
truth" for MIME type information. It is a dataset (JSON file), not an API, with mime type definitions pulled from a variety of authoritative sources.

[`mime-types`](https://github.com/jshttp/mime-types) is a thin
wrapper around mime-db that provides an API drop-in compatible(ish) with `mime @ < v1.3.6` API.
wrapper around mime-db that provides an API that is mostly-compatible with `mime @ < v1.3.6`.

`mime` (this project) is similar to `mime-types`, but with the following enhancements:
`mime` (this project) is like `mime-types`, but with the following enhancements:

- Resolves type conflicts (See [mime-score](https://github.com/broofa/mime-score) for details)
- Compact footprint (2-8KB .vs. 18KB)
- Native TS support
- Smaller (`mime` is 2-8KB, `mime-types` is 18KB)
- Zero dependencies
- Built-in TS support

## Mime API

Expand Down Expand Up @@ -123,6 +124,14 @@ mime.getExtension('application/json'); // RESULT
mime.getExtension('text/html; charset=utf8'); // RESULT
```

### mime.getAllExtensions(type)

Get all extensions for the given mime type.

```javascript --run default
mime.getAllExtensions('text/plain'); // RESULT
```

### mime.define(typeMap[, force = false])

Define [more] type mappings.
Expand Down
17 changes: 9 additions & 8 deletions src/Mime.ts
Expand Up @@ -67,7 +67,7 @@ export default class Mime {
}

/**
* Lookup a mime type based on extension
* Get mime type associated with an extension
*/
getType(path: string) {
if (typeof path !== 'string') return null;
Expand All @@ -88,7 +88,7 @@ export default class Mime {
}

/**
* Return file extension associated with a mime type
* Get default file extension associated with a mime type
*/
getExtension(type: string) {
if (typeof type !== 'string') return null;
Expand All @@ -101,16 +101,17 @@ export default class Mime {
);
}

/**
* Get all file extensions associated with a mime type
*/
getAllExtensions(type: string) {
if (typeof type !== 'string') return [];

const extensions = this.#typeToExtensions.get(type.toLowerCase());
if (typeof type !== 'string') return null;

return extensions ? [...extensions] : [];
return this.#typeToExtensions.get(type.toLowerCase()) ?? null;
}

//
// INTERNAL USE ONLY
// Private API, for internal use only. These APIs may change at any time
//

_freeze() {
Expand All @@ -121,7 +122,7 @@ export default class Mime {
Object.freeze(this);

for (const extensions of this.#typeToExtensions.values()) {
Object.freeze([...extensions]);
Object.freeze(extensions);
}

return this;
Expand Down
6 changes: 5 additions & 1 deletion test/mime.test.js
Expand Up @@ -151,7 +151,11 @@ describe('class Mime', (t) => {

it('getAllExtensions()', () => {
const mime = new Mime({ 'text/a': ['a', 'b'] }, { 'text/a': ['b', 'c'] });
assert.deepEqual(mime.getAllExtensions('text/a').sort(), ['a', 'b', 'c']);
assert.deepEqual([...mime.getAllExtensions('text/a')].sort(), [
'a',
'b',
'c',
]);
});

describe('DB', () => {
Expand Down

0 comments on commit 2e8578a

Please sign in to comment.