iconv-lite
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
safer-buffer | 2.1.2 | 11.75 kB | MIT | prod |
Iconv-lite is a pure JavaScript library specialized in converting character encodings. It comes without the need for native code compilation, making it quick to install and ideal for use on Windows and sandboxed environments like Cloud9. This package is used in many popular projects such as Express.js (body_parser), Grunt, Nodemailer, Yeoman, and others. With its intuitive encode/decode API and streaming support, iconv-lite is renowned for being faster than node-iconv and supports in-browser usage via browserify or webpack.
In order to utilize iconv-lite, you'll first need to install it by using npm. Afterward, it's as simple as requiring 'iconv-lite' in your JavaScript file. It provides an API that allows you to encode and decode text using various character sets. For example, to convert from an encoded buffer to a JavaScript string, you could use the following code:
var iconv = require('iconv-lite');
// Convert from an encoded buffer to a js string.
str = iconv.decode(Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
As for encoding from a JavaScript string to an encoded buffer, here is a code sample:
// Convert from a js string to an encoded buffer.
buf = iconv.encode("Sample input string", 'win1251');
Iconv-lite also supports the ability to check if a specific encoding is supported:
// Check if encoding is supported
iconv.encodingExists("us-ascii")
Iconv-lite even allows for streaming API usage:
// Decay stream (from binary data stream to js strings)
http.createServer(function(req, res) {
var converterStream = iconv.decodeStream('win1251');
req.pipe(converterStream);
converterStream.on('data', function(str) {
console.log(str); // Do something with decoded strings, chunk-by-chunk.
});
});
For a complete guide to using Iconv-lite, you can find the documentation on GitHub at https://github.com/ashtuchkin/iconv-lite. This guide includes a comprehensive rundown of the usage, the API, all supported encodings, and additional details on UTF-16 and UTF-32 encoding handling, BOM handling, and more. Not to mention, it also includes instructions for testing, BOM handling information, and guides for encoding/decoding speeds.