Skip to content

Commit

Permalink
Adjust number code and comment style suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 10, 2023
1 parent ede94d9 commit ca6e449
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
34 changes: 18 additions & 16 deletions core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,13 @@ 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) or .
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob) or [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File).
@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
```
import {fileTypeFromBlob} from 'file-type';
Expand All @@ -436,13 +437,14 @@ const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
console.log(await fileTypeFromBlob(blob));
//=> {ext: 'txt', mime: 'plain/text'}
```
*/
export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;

/**
Function that allows specifying custom detection mechanisms.
An iterable of detectors can be provided via the fileTypeOptions argument for the {@link FileTypeParser.constructor}.
An iterable of detectors can be provided via the `fileTypeOptions` argument for the {@link FileTypeParser.constructor}.
The detectors are called before the default detections in the provided order.
Expand All @@ -452,12 +454,12 @@ If the detector returns `undefined`, there are 2 possible scenarios:
1. The detector has not read from the tokenizer, it will be proceeded with the next available detector.
2. The detector has read from the tokenizer (`tokenizer.position` has been increased).
In that case no further detectors will be executed and the final conclusion is that file-type returns undefined.
Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type.
In that case no further detectors will be executed and the final conclusion is that file-type returns undefined.
Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type.
Example detector array which can be extended and provided via the fileTypeOptions argument:
```js
```
import {FileTypeParser} from 'file-type';
const customDetectors = [
Expand All @@ -479,7 +481,7 @@ const fileType = await parser.fromBuffer(buffer);
console.log(fileType);
```
@param tokenizer - An [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) usable as source of the examined file.
@param tokenizer - [Tokenizer](https://github.com/Borewit/strtok3#tokenizer), used to read the file content from.
@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.
*/
Expand All @@ -499,27 +501,27 @@ export declare class FileTypeParser {
constructor(options?: {customDetectors?: Iterable<Detector>});

/**
* Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
fromBuffer(buffer: Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;

/**
* Works the same way as {@link fileTypeFromStream}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
Works the same way as {@link fileTypeFromStream}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
fromStream(stream: ReadableStream): Promise<FileTypeResult | undefined>;

/**
* Works the same way as {@link fileTypeFromTokenizer}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
Works the same way as {@link fileTypeFromTokenizer}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
fromTokenizer(tokenizer: ITokenizer): Promise<FileTypeResult | undefined>;

/**
* Works the same way as {@link fileTypeFromBlob}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
Works the same way as {@link fileTypeFromBlob}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
fromBlob(blob: Blob): Promise<FileTypeResult | undefined>;

/**
* Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
*/
toDetectionStream(readableStream: ReadableStream, options?: StreamOptions): Promise<FileTypeResult | undefined>;
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ If the detector returns `undefined`, there are 2 possible scenarios:
Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type.
Example detector array which can be extended and provided to each public method via the fileTypeOptions argument:
Example detector array which can be extended and provided to each public method via the `fileTypeOptions` argument:
```js
import {FileTypeParser} from 'file-type';

Expand All @@ -336,6 +336,7 @@ const customDetectors = [
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" as decimal string
const buffer = Buffer.alloc(7);
await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true});

if (unicornHeader.every((value, index) => value === buffer[index])) {
return {ext: 'unicorn', mime: 'application/unicorn'};
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,5 +825,5 @@ test('fileTypeFromTokenizer should return undefined when a custom detector chang
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromTokenizer(strtok3.fromBuffer(uint8ArrayContent));
t.true(result === undefined);
t.is(result, undefined);
});

0 comments on commit ca6e449

Please sign in to comment.