chardet
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Chardet is a character encoding detection module that is completely written in TypeScript. Its primary function is to determine the most probable encoding of a given input by leveraging occurrence analysis. The npm package is lightweight, with a packed size of only 22 KB, and does not contain any dependencies or native bindings. It has the ability to work in all environments including Node, browsers and native as well as on all platforms such as Linux, Mac and Windows.
Chardet can be easily implemented in your JavaScript or TypeScript project. To start, you need to install the package via npm by running npm i chardet
. Once installed, you can import it into your code using the import
statement. The detect
method can be used to return the encoding with the highest confidence.
Here is an example:
import chardet from 'chardet';
const encoding = chardet.detect(Buffer.from('hello there!'));
// or
const encoding = await chardet.detectFile('/path/to/file');
// or
const encoding = chardet.detectFileSync('/path/to/file');
To return the full list of possible encodings, you can use the analyse
method:
import chardet from 'chardet';
chardet.analyse(Buffer.from('hello there!'));
For working with large data sets, you can specify parameters sampleSize
and offset
in the detectFile
method.
chardet
.detectFile('/path/to/file', { sampleSize: 32 })
.then((encoding) => console.log(encoding));
chardet
.detectFile('/path/to/file', { sampleSize: 32, offset: 128 })
.then((encoding) => console.log(encoding));
The complete documentation for chardet can be found within the npm package's readme file on its GitHub repository. It is comprehensive and provides a detailed overview of the module's functionality, its installation process, usage, methods, and supported encodings.