Skip to content

Allow file-type options to be directly passed to exported functions #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 21, 2025

Conversation

Borewit
Copy link
Collaborator

@Borewit Borewit commented May 15, 2025

Changes:

Related to #735, as this addresses some of the inconsistencies as well
Resolves #715

@Borewit Borewit self-assigned this May 15, 2025
@Borewit Borewit added the debt Technical debt label May 15, 2025
@Borewit Borewit requested a review from Copilot May 15, 2025 17:56
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables the exported file-type functions to accept a new options parameter, making it easier to pass custom settings and enhancing test coverage.

  • File-type functions now accept an options parameter to customize behavior.
  • Documentation was updated to centralize file-type options and improve clarity.
  • Test coverage was enhanced to cover the new options parameter in various functions.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

File Description
readme.md Updated documentation to include options parameters and anchors.
index.test-d.ts Added tests to verify behavior when options are passed.
index.js Modified exported functions to accept and forward options.
core.js Updated internal functions to propagate new options parameter.

@Borewit Borewit requested a review from sindresorhus May 15, 2025 18:03
@Borewit Borewit force-pushed the impove-options-handling-and-typing branch 2 times, most recently from 218fe7e to 66dca8c Compare May 19, 2025 09:03
Borewit added 3 commits May 20, 2025 19:15
Improve coverage of type tests
Centralized file-type options documentation
Improved documentation consistency for `customDetectors` in `options`.
Reversed using FileTypeParser constructor, in "Example adding a detector", as I believe it adds value showing that. As, that way you can reuse the tweaked parser.
@Borewit Borewit force-pushed the impove-options-handling-and-typing branch from 66dca8c to 9f87f29 Compare May 20, 2025 17:33
@Borewit Borewit requested a review from sindresorhus May 20, 2025 17:34
@Borewit Borewit force-pushed the impove-options-handling-and-typing branch from 6b121df to 6264f68 Compare May 20, 2025 18:39
index.test-d.ts Outdated
Comment on lines 82 to 94
expectType<FileTypeResult | undefined>(await fileTypeParser.fromFile('myFile'));

// From a Node Readable (stream)
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new Readable()));

// From a DOM type Blob
expectType<FileTypeResult | undefined>(await fileTypeParser.fromBlob(new Blob()));

// From a DOM type Web byte ReadableStream
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new ReadableStream({type: 'bytes'})));

// From a Node type byte ReadableStream
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new NodeReadableStream({type: 'bytes'})));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these tests are moot as they don't do anything other than just duplicating what the type signature already says. Type tests are only useful when they test something more advanced than just confirming what the actual types say.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly not moot.
Some of the types are not that straightforward, for example the ReadableStream which is both defined on the global DOM space, as well in Node.js, as a different type. The implementation is designed to accept either of those. I don't see any issue with testing / confirming that and to ensure that and that this behavior is kept in tact. In addition to that, is also helps to clarify the design. This also helps to get overview the high level API definition are typed in a consistent way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type tests can be useful for advanced types, but just testing what it accepts is simply duplicating the type definition. index.test-d.ts was a mistake in general. Most of the time, just getting TS to validate the index.d.ts is enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to make it more effective, and got across an issue of using mixed options (FileTypeOptions and StreamOptions), lacking right declaration. Something I could have found as well by having a critical look to the TypeScript declaration, indeed.

Corrected that, as well I (believe) improved declaration of allowing mixed 2f6aad8, apparently an intersection works better. From a mathematical definition this does not make sense to me, but in the TypeScript world this seems to better reflect the type intended.

If there are things in ndex.test-d.ts, which you believe are moot, please just toss them out. For me it to arbitrary what should be in or out. I believe it is now in a more sensible and consistent state, as when found it prior to this PR.

@Borewit Borewit requested a review from sindresorhus May 21, 2025 09:40
index.test-d.ts Outdated
Comment on lines 82 to 94
expectType<FileTypeResult | undefined>(await fileTypeParser.fromFile('myFile'));

// From a Node Readable (stream)
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new Readable()));

// From a DOM type Blob
expectType<FileTypeResult | undefined>(await fileTypeParser.fromBlob(new Blob()));

// From a DOM type Web byte ReadableStream
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new ReadableStream({type: 'bytes'})));

// From a Node type byte ReadableStream
expectType<FileTypeResult | undefined>(await fileTypeParser.fromStream(new NodeReadableStream({type: 'bytes'})));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type tests can be useful for advanced types, but just testing what it accepts is simply duplicating the type definition. index.test-d.ts was a mistake in general. Most of the time, just getting TS to validate the index.d.ts is enough.

Borewit added 2 commits May 21, 2025 13:37
…an intersection instead of a union.

Note that the words "intersection" and "union" in TypeScript are misleading coming from a mathematical background.
@Borewit Borewit requested a review from sindresorhus May 21, 2025 11:59
@sindresorhus sindresorhus merged commit d264029 into main May 21, 2025
6 checks passed
@sindresorhus sindresorhus deleted the impove-options-handling-and-typing branch May 21, 2025 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debt Technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing fileTypeOptions for fileTypeFromStream function in index declaration file
2 participants