包详细信息

istextorbinary

bevry5.7mArtistic-2.09.5.0

Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.

bin, binary, browser, check

自述文件

Is Text or Binary?

Status of the GitHub Workflow: bevry NPM version NPM downloads
GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button
Discord server badge Twitch community badge

Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.

Determination works like so:

  1. Extension Check: If filename is available, check if any of its extensions (from right to left) are an text extension or a binary extension, this is near instant.
  2. Contents Check: If no filename was provided, or the extension check was indeterminate, then check the contents of the buffer.

The extension check will check each of the filename's extensions, from right to left. This is done as certain applications utilise multiple extensions for transformations, such as app.x.y may tell a compiler to transform from x format to y format, in this case perhaps x is not a recognized extension but y is, in which case we can make use of that to provide superior accuracy and convenience compared to just checking the rightmost extension.

The contents check (with the default options) will check 24 bytes at the start, middle, and end of the buffer. History has shown that checking all three locations is mandatory for accuracy, and that anything less is not accurate. This technique offers superior performance while still offering superior accuracy. Alternatives generally just do 1000 bytes at the start, which is slower, and inaccurate.

One cannot just do the contents check alone because UTF16 characters are indistinguishable from binary which would return an inaccurate result, hence why the combination is necessary for accuracy, with performance for known extensions a side-effect.

As such, this library's combination of extension check (if filename is provided), then contents check (if buffer is provided), offers superior performance and accuracy to alternatives.

Ever since 2012, this module's superior accuracy and performance has been essential to the operation of DocPad and its other dependents.

Usage

Complete API Documentation.

import { isText, isBinary, getEncoding } from 'istextorbinary'

or

const { isText, isBinary, getEncoding } = require('istextorbinary')

then

isText(aFilename) // returns true if a text file otherwise false, checks only filename
isText(null, aBuffer) // returns true if a text file otherwise false, checks only buffer
isText(aFilename, aBuffer) // returns true if a text file otherwise false, checks filename then buffer
isText(null, null) // returns null

isBinary(aFilename) // returns true if a binary file otherwise false, checks only filename
isBinary(null, aBuffer) // returns true if a binary file otherwise false, checks only buffer
isBinary(aFilename, aBuffer) // returns true if a binary file otherwise false, checks filename then buffer
isBinary(null, null) // returns null

getEncoding(aBuffer) // returns 'binary' if it contained non-utf8 characters, otherwise returns 'utf8'

Install

npm

  • Install: npm install --save istextorbinary
  • Import: import * as pkg from ('istextorbinary')
  • Require: const pkg = require('istextorbinary')

Deno

import * as pkg from 'https://unpkg.com/istextorbinary@^9.5.0/edition-deno/index.ts'

Skypack

<script type="module">
    import * as pkg from '//cdn.skypack.dev/istextorbinary@^9.5.0'
</script>

unpkg

<script type="module">
    import * as pkg from '//unpkg.com/istextorbinary@^9.5.0'
</script>

jspm

<script type="module">
    import * as pkg from '//dev.jspm.io/istextorbinary@9.5.0'
</script>

Editions

This package is published with the following editions:

  • istextorbinary aliases istextorbinary/index.cjs which uses the Editions Autoloader to automatically select the correct edition for the consumer's environment
  • istextorbinary/source/index.ts is TypeScript source code with Import for modules
  • istextorbinary/edition-browsers/index.js is TypeScript compiled against ES2022 for web browsers with Import for modules
  • istextorbinary/edition-es2022/index.js is TypeScript compiled against ES2022 for Node.js 14 || 16 || 18 || 20 || 21 with Require for modules
  • istextorbinary/edition-es2017/index.js is TypeScript compiled against ES2017 for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules
  • istextorbinary/edition-es5/index.js is TypeScript compiled against ES5 for Node.js 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules
  • istextorbinary/edition-es2017-esm/index.js is TypeScript compiled against ES2017 for Node.js 12 || 14 || 16 || 18 || 20 || 21 with Import for modules
  • istextorbinary/edition-types/index.d.ts is TypeScript compiled Types with Import for modules
  • istextorbinary/edition-deno/index.ts is TypeScript source code made to be compatible with Deno

History

Discover the release history by heading on over to the HISTORY.md file.

Backers

Code

Discover how to contribute via the CONTRIBUTING.md file.

Authors

Maintainers

Contributors

Finances

GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button

Sponsors

  • Andrew Nesbitt — Software engineer and researcher
  • Balsa — We're Balsa, and we're building tools for builders.
  • Codecov — Empower developers with tools to improve code quality and testing.
  • Poonacha Medappa
  • Rob Morris
  • Sentry — Real-time crash reporting for your web apps, mobile apps, and games.
  • Syntax — Syntax Podcast

Donors

License

Unless stated otherwise all works are:

and licensed under:

更新日志

History

v9.5.0 2023 December 29

v9.4.0 2023 December 27

v9.3.0 2023 December 6

v9.2.0 2023 November 25

v9.1.0 2023 November 24

v9.0.0 2023 November 23

  • Updated dependencies, base files, and editions using boundation
  • Minimum required Node.js version changed from node: >=10 to node: >=4 adapting to ecosystem changes

v8.2.0 2023 November 21

v8.1.0 2023 November 15

v8.0.0 2023 November 14

  • Updated dependencies, base files, and editions using boundation
  • Minimum required Node.js version changed from node: >=14 to node: >=10 adapting to ecosystem changes

v7.0.0 2023 November 1

  • Updated dependencies, base files, and editions using boundation
  • Updated license from MIT to Artistic-2.0
  • Minimum required node version changed from node: >=10 to node: >=14 to keep up with mandatory ecosystem changes

v6.0.0 2021 August 1

v5.15.0 2021 July 30

v5.14.0 2021 July 29

v5.13.0 2021 July 28

v5.12.0 2020 October 29

v5.11.0 2020 September 5

v5.10.0 2020 August 18

v5.9.0 2020 August 4

v5.8.0 2020 July 22

v5.7.0 2020 June 25

v5.6.0 2020 June 21

v5.5.0 2020 June 21

v5.4.0 2020 June 21

v5.3.0 2020 June 20

v5.2.0 2020 June 10

v5.1.0 2020 June 10

v5.0.0 2020 May 30

  • Potential Breaking Change: Removed the long-standing deprecated sync, callback, and promise wrappers, now the only exports are isText, isBinary, and getEncoding
  • Potential Breaking Change: getEncoding now checks start, middle, and end if checkBegin was not provided. Prior functionality only checked start, middle, and end, if opts were not provided. This new functionality allows custom checkLength for start, middle, and end.
  • Converted to TypeScript, and provided proper documentation for isBinary instead of just referencing isText, so your intellisense is now more helpful
  • Updated dependencies, base files, and editions using boundation

v4.3.0 2020 May 22

v4.2.0 2020 May 21

v4.1.0 2020 May 21

v4.0.0 2020 May 11

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=8 to node: >=10 to keep up with mandatory ecosystem changes

v3.3.0 2019 December 9

v3.2.0 2019 December 1

v3.1.0 2019 December 1

v3.0.0 2019 November 18

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=0.12 to node: >=8 to keep up with mandatory ecosystem changes

v2.6.0 2019 November 13

v2.5.1 2019 January 21

  • Fixed a readme documentation inconsistency

  • Fixed node v0.12 and v4 support (regression since v2.5.0)

v2.5.0 2019 January 21

As the detection algorithms are result returns, with the asynchronous signatures just wrappers, we have changed isText, isBinary, and getEncoding to return the result if no callback was provided to them, maintaining backwards compatibility, but encouraging intuitive usage of the methods with the least overhead.

  • The following methods have had return signatures added to them, which should be the preferable usage:

    • isText, which you should use instead of isTextSync (a method which only lingers for backwards compatibility)
    • isBinary, which you should use instead of isBinarySync (a method which only lingers for backwards compatibility)
    • getEncoding, which you should use instead of getEncoding (a method which only lingers for backwards compatibility)
  • If you require callback usage, the following callback wrapper methods have been added:

    • isTextCallback, which you should use instead of isText's callback signature (a signature which only lingers for backwards compatibility)
    • isBinaryCallback, which you should use instead of isBinary's callback signature (a signature which only lingers for backwards compatibility)
    • getEncodingCallback, which you should use instead of getEncoding's callback signature (a signature which only lingers for backwards compatibility)
  • If you require promise usage, the following promise wrapper methods have been added:

    • isTextPromise which wraps isText with a promise signature
    • isBinaryPromise which wraps isBinary with a promise signature
    • getEncodingPromise which wraps getEncoding with a promise signature
  • isBinary method now correctly returns null instead of true when no inputs are provided

  • Added tests for all methods

v2.4.2 2019 January 21

  • Added more keywords to package.json

v2.4.1 2019 January 21

  • README now elaborates on the operation of this package

v2.4.0 2019 January 20

  • Asynchronous methods now try...catch the synchronous methods to ensure an error from invalid inputs would be given to the callback.
    • Before they would not do any try...catch so if invalid inputs were given, the error would throw.
  • The JSDoc documentation has been updated for accuracy.
    • It previously indicated that the return types of the sync methods could have been an error instance, this was incorrect, they would throw if received invalid inputs.
    • It previously indicated that the result for of the async getEncoding callback was a boolean, this was incorrect, it would be the string result of getEncodingSync.
  • Updated base files and editions using boundation

v2.3.0 2018 November 7

v2.2.1 2018 January 24

  • Added missing development dependency

v2.2.0 2018 January 24

v2.1.0 2016 May 10

v2.0.0 2016 May 2

  • Converted from CoffeeScript to JavaScript
  • Fixed getEncoding and isText not handling errors correctly
  • Right-most extension takes preference, instead of left-most
    • Thanks to Ian Sibner for pull request #5
    • This has bumped the major as it changes the output result, which could potentially break some apps, despite the API remaining exactly the same

v1.0.2 2015 January 16

  • Fixed build
  • Added test for text files

v1.0.1 2015 January 16

v1.0.0 2013 October 25