uti
Version:
javascript implementation of a "Uniform Type Identifier" (UTI)
260 lines (165 loc) • 11.3 kB
Markdown
[](https://www.npmjs.com/package/uti)
[](https://spdx.org/licenses/0BSD.html)
[](https://typescriptlang.org)
[](https://bundlejs.com/?q=uti)
[](https://npmjs.org/package/uti)
[](https://github.com/arlac77/uti/issues)
[](https://actions-badge.atrox.dev/arlac77/uti/goto)
[](https://github.com/prettier/prettier)
[](http://commitizen.github.io/cz-cli/)
[](https://snyk.io/test/github/arlac77/uti)
[](https://coveralls.io/github/arlac77/uti)
# uti
## Uniform Type Identifier
Please see [ars technica](http://arstechnica.com/apple/2005/04/macosx-10-4/11/) article for a description about the principles of UTIs.
For a list of known UTIs please see [here](http://www.escape.gr/manuals/qdrop/UTI.html)
# example
## myuti.js
<!-- skip-example -->
```javascript
import { UTIController } from "uti";
const uc = new UTIController();
const doesConformTo = uc.conformsTo("public.image", "public.data");
console.log("doesConformTo: " + doesConformTo);
console.log(uc.getUTIsForFileName("a.txt")[0]);
```
Output
```txt
doesConformTo: true
public.plain-text
```
# API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
* [UTIDeclaration](#utideclaration)
* [Properties](#properties)
* [UTI](#uti)
* [Parameters](#parameters)
* [Properties](#properties-1)
* [name](#name)
* [conforms](#conforms)
* [conformsTo](#conformsto)
* [Parameters](#parameters-1)
* [toString](#tostring)
* [toJSON](#tojson)
* [UTIController](#uticontroller)
* [Properties](#properties-2)
* [registry](#registry)
* [utiByMimeType](#utibymimetype)
* [utiByFileNameExtension](#utibyfilenameextension)
* [register](#register)
* [Parameters](#parameters-2)
* [getUTI](#getuti)
* [Parameters](#parameters-3)
* [getUTIsForMimeType](#getutisformimetype)
* [Parameters](#parameters-4)
* [getUTIsForFileName](#getutisforfilename)
* [Parameters](#parameters-5)
* [conformsTo](#conformsto-1)
* [Parameters](#parameters-6)
* [fileNameConformsTo](#filenameconformsto)
* [Parameters](#parameters-7)
* [assignMimeTypes](#assignmimetypes)
* [Parameters](#parameters-8)
* [assignExtensions](#assignextensions)
* [Parameters](#parameters-9)
## UTIDeclaration
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
### Properties
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
* `conformsTo` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** 
* `mimeType` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** 
* `fileNameExtension` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** 
## UTI
Object representing a UTI.
### Parameters
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
* `conforms` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)<[UTI](#uti)>** 
### Properties
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
* `conforms` **[Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)<[UTI](#uti)>** 
### name
Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
### conforms
Type: [Set](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)<[UTI](#uti)>
### conformsTo
Check for conformity.
#### Parameters
* `other` **([UTI](#uti) | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))** 
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if other conforms to the receiver
### toString
name of the UTI.
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
### toJSON
Deliver JSON representation of the UTI.
Sample result
```json
{
"name": "myUTI",
"conformsTo": [ "uti1", "uti2"]
}
```
Returns **{name: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), conforms: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>}** json representation of the UTI
## UTIController
Registry of UTIs.
### Properties
* `registry` **[Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [UTI](#uti)>** 
* `utiByMimeType` **[Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [UTI](#uti)>** 
* `utiByFileNameExtension` **[Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [UTI](#uti)>** 
### registry
Type: [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [UTI](#uti)>
### utiByMimeType
Type: [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>>
### utiByFileNameExtension
Type: [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>>
### register
Registers additional types.
#### Parameters
* `types` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[UTIDeclaration](#utideclaration)>** 
### getUTI
Lookup a given UTI.
#### Parameters
* `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** UTI
Returns **([UTI](#uti) | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))** UTI for the given name or undefined if UTI is not present.
### getUTIsForMimeType
Lookup a UTIs for a mime type.
#### Parameters
* `mimeType` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** mime type to get UTIs for
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** UTIs for the given mime type
### getUTIsForFileName
Lookup a UTI for a file name.
First the file name extension is extracted.
Then a lookup in the registered UTIs for file name extension is executed.
#### Parameters
* `fileName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** file to detect UTI for
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** UTIs for the given fileName
### conformsTo
Check whenever two UTI are conformant.
If a conforms to b and b conforms to c then a also conforms to c.
#### Parameters
* `a` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** first UTI
* `b` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** second UTI
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if UTI a conforms to UTI b.
### fileNameConformsTo
Lookup a UTI for a file name and check conformance.
#### Parameters
* `fileName` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** file to detect UTI for
* `uti` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** to check conformance against
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** true if utils for file name are conformant
### assignMimeTypes
Assign mime types to a UTI
#### Parameters
* `uti` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
* `mimeTypes` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** 
### assignExtensions
Assign mime types to a UTI
#### Parameters
* `uti` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
* `extensions` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** 
# install
With [npm](http://npmjs.org) do:
```shell
npm install uti
```
# license
BSD-2-Clause