1 |
|
2 |
|
3 | <h1>Is Text or Binary?</h1>
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | <span class="badge-githubworkflow"><a href="https://github.com/bevry/istextorbinary/actions?query=workflow%3Abevry" title="View the status of this project's GitHub Workflow: bevry"><img src="https://github.com/bevry/istextorbinary/workflows/bevry/badge.svg" alt="Status of the GitHub Workflow: bevry" /></a></span>
|
11 | <span class="badge-npmversion"><a href="https://npmjs.org/package/istextorbinary" title="View this project on NPM"><img src="https://img.shields.io/npm/v/istextorbinary.svg" alt="NPM version" /></a></span>
|
12 | <span class="badge-npmdownloads"><a href="https://npmjs.org/package/istextorbinary" title="View this project on NPM"><img src="https://img.shields.io/npm/dm/istextorbinary.svg" alt="NPM downloads" /></a></span>
|
13 | <span class="badge-daviddm"><a href="https://david-dm.org/bevry/istextorbinary" title="View the status of this project's dependencies on DavidDM"><img src="https://img.shields.io/david/bevry/istextorbinary.svg" alt="Dependency Status" /></a></span>
|
14 | <span class="badge-daviddmdev"><a href="https://david-dm.org/bevry/istextorbinary#info=devDependencies" title="View the status of this project's development dependencies on DavidDM"><img src="https://img.shields.io/david/dev/bevry/istextorbinary.svg" alt="Dev Dependency Status" /></a></span>
|
15 | <br class="badge-separator" />
|
16 | <span class="badge-githubsponsors"><a href="https://github.com/sponsors/balupton" title="Donate to this project using GitHub Sponsors"><img src="https://img.shields.io/badge/github-donate-yellow.svg" alt="GitHub Sponsors donate button" /></a></span>
|
17 | <span class="badge-patreon"><a href="https://patreon.com/bevry" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span>
|
18 | <span class="badge-flattr"><a href="https://flattr.com/profile/balupton" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span>
|
19 | <span class="badge-liberapay"><a href="https://liberapay.com/bevry" title="Donate to this project using Liberapay"><img src="https://img.shields.io/badge/liberapay-donate-yellow.svg" alt="Liberapay donate button" /></a></span>
|
20 | <span class="badge-buymeacoffee"><a href="https://buymeacoffee.com/balupton" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
|
21 | <span class="badge-opencollective"><a href="https://opencollective.com/bevry" title="Donate to this project using Open Collective"><img src="https://img.shields.io/badge/open%20collective-donate-yellow.svg" alt="Open Collective donate button" /></a></span>
|
22 | <span class="badge-crypto"><a href="https://bevry.me/crypto" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span>
|
23 | <span class="badge-paypal"><a href="https://bevry.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span>
|
24 | <span class="badge-wishlist"><a href="https://bevry.me/wishlist" title="Buy an item on our wishlist for us"><img src="https://img.shields.io/badge/wishlist-donate-yellow.svg" alt="Wishlist browse button" /></a></span>
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | Determination works like so:
|
37 |
|
38 | 1. Extension Check: If filename is available, check if any of its extensions (from right to left) are an [text extension](https://github.com/bevry/textextensions) or a [binary extension](https://github.com/bevry/binaryextensions), this is near instant.
|
39 | 2. Contents Check: If no filename was provided, or the extension check was indeterminate, then check the contents of the buffer.
|
40 |
|
41 | 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.
|
42 |
|
43 | 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.
|
44 |
|
45 | 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.
|
46 |
|
47 | 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.
|
48 |
|
49 | Ever since 2012, this module's superior accuracy and performance has been essential to the operation of [DocPad](https://docpad.org) and its other dependents.
|
50 |
|
51 | ## Usage
|
52 |
|
53 | [Complete API Documentation.](http://master.istextorbinary.bevry.surge.sh/docs/)
|
54 |
|
55 | ```typescript
|
56 | import { isText, isBinary, getEncoding } from 'istextorbinary'
|
57 | ```
|
58 |
|
59 | or
|
60 |
|
61 | ```javascript
|
62 | const { isText, isBinary, getEncoding } = require('istextorbinary')
|
63 | ```
|
64 |
|
65 | then
|
66 |
|
67 | ```javascript
|
68 | isText(aFilename) // returns true if a text file otherwise false, checks only filename
|
69 | isText(null, aBuffer) // returns true if a text file otherwise false, checks only buffer
|
70 | isText(aFilename, aBuffer) // returns true if a text file otherwise false, checks filename then buffer
|
71 | isText(null, null) // returns null
|
72 |
|
73 | isBinary(aFilename) // returns true if a binary file otherwise false, checks only filename
|
74 | isBinary(null, aBuffer) // returns true if a binary file otherwise false, checks only buffer
|
75 | isBinary(aFilename, aBuffer) // returns true if a binary file otherwise false, checks filename then buffer
|
76 | isBinary(null, null) // returns null
|
77 |
|
78 | getEncoding(aBuffer) // returns 'binary' if it contained non-utf8 characters, otherwise returns 'utf8'
|
79 | ```
|
80 |
|
81 |
|
82 |
|
83 | <h2>Install</h2>
|
84 |
|
85 | <a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a>
|
86 | <ul>
|
87 | <li>Install: <code>npm install --save istextorbinary</code></li>
|
88 | <li>Import: <code>import * as pkg from ('istextorbinary')</code></li>
|
89 | <li>Require: <code>const pkg = require('istextorbinary')</code></li>
|
90 | </ul>
|
91 |
|
92 | <a href="https://www.skypack.dev" title="Skypack is a JavaScript Delivery Network for modern web apps"><h3>Skypack</h3></a>
|
93 |
|
94 | ``` html
|
95 | <script type="module">
|
96 | import * as pkg from '//cdn.skypack.dev/istextorbinary@^6.0.0'
|
97 | </script>
|
98 | ```
|
99 |
|
100 | <a href="https://unpkg.com" title="unpkg is a fast, global content delivery network for everything on npm"><h3>unpkg</h3></a>
|
101 |
|
102 | ``` html
|
103 | <script type="module">
|
104 | import * as pkg from '//unpkg.com/istextorbinary@^6.0.0'
|
105 | </script>
|
106 | ```
|
107 |
|
108 | <a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a>
|
109 |
|
110 | ``` html
|
111 | <script type="module">
|
112 | import * as pkg from '//dev.jspm.io/istextorbinary@6.0.0'
|
113 | </script>
|
114 | ```
|
115 |
|
116 | <h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3>
|
117 |
|
118 | <p>This package is published with the following editions:</p>
|
119 |
|
120 | <ul><li><code>istextorbinary/source/index.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> source code with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li>
|
121 | <li><code>istextorbinary/edition-browsers/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#11th_Edition_–_ECMAScript_2020" title="ECMAScript ES2020">ES2020</a> for web browsers with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li>
|
122 | <li><code>istextorbinary</code> aliases <code>istextorbinary/edition-es2019/index.js</code></li>
|
123 | <li><code>istextorbinary/edition-es2019/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_-_ECMAScript_2019" title="ECMAScript ES2019">ES2019</a> for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 10 || 12 || 14 || 16 with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li>
|
124 | <li><code>istextorbinary/edition-es2019-esm/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_-_ECMAScript_2019" title="ECMAScript ES2019">ES2019</a> for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> 12 || 14 || 16 with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li></ul>
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | <h2>History</h2>
|
132 |
|
133 | <a href="https://github.com/bevry/istextorbinary/blob/master/HISTORY.md#files">Discover the release history by heading on over to the <code>HISTORY.md</code> file.</a>
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | <h2>Contribute</h2>
|
141 |
|
142 | <a href="https://github.com/bevry/istextorbinary/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | <h2>Backers</h2>
|
150 |
|
151 | <h3>Maintainers</h3>
|
152 |
|
153 | These amazing people are maintaining this project:
|
154 |
|
155 | <ul><li><a href="https://balupton.com">Benjamin Lupton</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/istextorbinary">view contributions</a></li>
|
156 | <li><a href="https://github.com/mikeumus">Michael Mooring</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=mikeumus" title="View the GitHub contributions of Michael Mooring on repository bevry/istextorbinary">view contributions</a></li>
|
157 | <li><a href="https://github.com/robloach">Rob Loach</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=robloach" title="View the GitHub contributions of Rob Loach on repository bevry/istextorbinary">view contributions</a></li></ul>
|
158 |
|
159 | <h3>Sponsors</h3>
|
160 |
|
161 | No sponsors yet! Will you be the first?
|
162 |
|
163 | <span class="badge-githubsponsors"><a href="https://github.com/sponsors/balupton" title="Donate to this project using GitHub Sponsors"><img src="https://img.shields.io/badge/github-donate-yellow.svg" alt="GitHub Sponsors donate button" /></a></span>
|
164 | <span class="badge-patreon"><a href="https://patreon.com/bevry" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span>
|
165 | <span class="badge-flattr"><a href="https://flattr.com/profile/balupton" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span>
|
166 | <span class="badge-liberapay"><a href="https://liberapay.com/bevry" title="Donate to this project using Liberapay"><img src="https://img.shields.io/badge/liberapay-donate-yellow.svg" alt="Liberapay donate button" /></a></span>
|
167 | <span class="badge-buymeacoffee"><a href="https://buymeacoffee.com/balupton" title="Donate to this project using Buy Me A Coffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg" alt="Buy Me A Coffee donate button" /></a></span>
|
168 | <span class="badge-opencollective"><a href="https://opencollective.com/bevry" title="Donate to this project using Open Collective"><img src="https://img.shields.io/badge/open%20collective-donate-yellow.svg" alt="Open Collective donate button" /></a></span>
|
169 | <span class="badge-crypto"><a href="https://bevry.me/crypto" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span>
|
170 | <span class="badge-paypal"><a href="https://bevry.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span>
|
171 | <span class="badge-wishlist"><a href="https://bevry.me/wishlist" title="Buy an item on our wishlist for us"><img src="https://img.shields.io/badge/wishlist-donate-yellow.svg" alt="Wishlist browse button" /></a></span>
|
172 |
|
173 | <h3>Contributors</h3>
|
174 |
|
175 | These amazing people have contributed code to this project:
|
176 |
|
177 | <ul><li><a href="https://balupton.com">Benjamin Lupton</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/istextorbinary">view contributions</a></li>
|
178 | <li><a href="https://github.com/sibnerian">Ian Sibner</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=sibnerian" title="View the GitHub contributions of Ian Sibner on repository bevry/istextorbinary">view contributions</a></li>
|
179 | <li><a href="https://github.com/sainthkh">Kukhyeon Heo</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=sainthkh" title="View the GitHub contributions of Kukhyeon Heo on repository bevry/istextorbinary">view contributions</a></li>
|
180 | <li><a href="https://github.com/mikeumus">Michael Mooring</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=mikeumus" title="View the GitHub contributions of Michael Mooring on repository bevry/istextorbinary">view contributions</a></li>
|
181 | <li><a href="https://github.com/robloach">Rob Loach</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=robloach" title="View the GitHub contributions of Rob Loach on repository bevry/istextorbinary">view contributions</a></li>
|
182 | <li><a href="https://github.com/AlbinoDrought">Sean</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=AlbinoDrought" title="View the GitHub contributions of Sean on repository bevry/istextorbinary">view contributions</a></li>
|
183 | <li><a href="https://github.com/shinnn">shinnn</a> — <a href="https://github.com/bevry/istextorbinary/commits?author=shinnn" title="View the GitHub contributions of shinnn on repository bevry/istextorbinary">view contributions</a></li></ul>
|
184 |
|
185 | <a href="https://github.com/bevry/istextorbinary/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | <h2>License</h2>
|
193 |
|
194 | Unless stated otherwise all works are:
|
195 |
|
196 | <ul><li>Copyright © 2012+ <a href="http://bevry.me">Bevry Pty Ltd</a></li>
|
197 | <li>Copyright © 2011 <a href="https://balupton.com">Benjamin Lupton</a></li></ul>
|
198 |
|
199 | and licensed under:
|
200 |
|
201 | <ul><li><a href="http://spdx.org/licenses/MIT.html">MIT License</a></li></ul>
|
202 |
|
203 |
|