Skip to content

Commit

Permalink
Make code examples executable.
Browse files Browse the repository at this point in the history
Explain role of `blob` argument in function comment.
  • Loading branch information
Borewit committed Nov 6, 2023
1 parent de706c5 commit 331502d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
28 changes: 14 additions & 14 deletions core.d.ts
Expand Up @@ -419,9 +419,9 @@ if (stream2.fileType?.mime === 'image/jpeg') {
export function fileTypeStream(readableStream: ReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>;

/**
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob).
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob) or .
@param blob
@param blob [`Blob`](https://nodejs.org/api/buffer.html#class-blob) used for file detection
@returns The detected file type and MIME type, or `undefined` when there is no match.
@example
Expand Down Expand Up @@ -458,6 +458,8 @@ If the detector returns `undefined`, there are 2 possible scenarios:
Example detector array which can be extended and provided via the fileTypeOptions argument:
```js
import {FileTypeParser} from 'file-type';
const customDetectors = [
async tokenizer => {
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" as decimal string
Expand All @@ -466,22 +468,20 @@ const customDetectors = [
if (unicornHeader.every((value, index) => value === buffer[index])) {
return {ext: 'unicorn', mime: 'application/unicorn'};
}
return undefined;
}
]
Example usage:
return undefined;
},
];
```js
const buffer = ...
const buffer = Buffer.from("UNICORN");
const parser = new FileTypeParser({customDetectors});
const fileType = await parser.fromBuffer(buffer);
console.log(fileType);
```
fromStream(...), fromTokenizer(...), fromBlob(...) and toDetectingStream(...) are available in the same manner.
@param tokenizer - An [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) usable as source of the examined file.
@param fileType - FileTypeResult detected by the standard detections or a previous custom detection. Undefined if no matching fileTypeResult could be found.
@returns supposedly detected file extension and MIME type as a FileTypeResult-like object, or `undefined` when there is no match.
@param tokenizer - An [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) usable as source of the examined file.
@param fileType - FileTypeResult detected by the standard detections or a previous custom detection. Undefined if no matching fileTypeResult could be found.
@returns supposedly detected file extension and MIME type as a FileTypeResult-like object, or `undefined` when there is no match.
*/
export type Detector = (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise<FileTypeResult | undefined>;

Expand Down Expand Up @@ -526,5 +526,5 @@ export declare class FileTypeParser {
*
* Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
toDetectingStream(readableStream: ReadableStream, options?: StreamOptions): Promise<FileTypeResult | undefined>;
toDetectionStream(readableStream: ReadableStream, options?: StreamOptions): Promise<FileTypeResult | undefined>;
}
4 changes: 2 additions & 2 deletions core.js
Expand Up @@ -101,7 +101,7 @@ export class FileTypeParser {
}
}

async toDetectingStream(readableStream, options = {}) {
async toDetectionStream(readableStream, options = {}) {
const {default: stream} = await import('node:stream');
const {sampleSize = minimumBytes} = options;

Expand Down Expand Up @@ -1669,7 +1669,7 @@ export class FileTypeParser {
}

export async function fileTypeStream(readableStream, options = {}) {
return new FileTypeParser().toDetectingStream(readableStream, options);
return new FileTypeParser().toDetectionStream(readableStream, options);
}

export const supportedExtensions = new Set(extensions);
Expand Down
12 changes: 5 additions & 7 deletions readme.md
Expand Up @@ -328,7 +328,9 @@ If the detector returns `undefined`, there are 2 possible scenarios:
Example detector array which can be extended and provided to each public method via the fileTypeOptions argument:
```
```js
import {FileTypeParser} from 'file-type';

const customDetectors = [
async tokenizer => {
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" as decimal string
Expand All @@ -341,15 +343,11 @@ const customDetectors = [
return undefined;
},
];
```

Example usage:
```
const buffer = ...
const buffer = Buffer.from("UNICORN");
const parser = new FileTypeParser({customDetectors});
const fileType = await parser.fromBuffer(buffer);
// fromStream(...), fromTokenizer(...), fromBlob(...) and toDetectingStream(...) are available in the same manner.

console.log(fileType);
```
## Supported file types
Expand Down

0 comments on commit 331502d

Please sign in to comment.