Socket
Socket
Sign inDemoInstall

iconv-lite

Package Overview
Dependencies
0
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    iconv-lite

Convert character encodings in pure javascript.


Version published
Weekly downloads
60M
decreased by-9.6%
Maintainers
1
Install size
326 kB
Created
Weekly downloads
 

Package description

What is iconv-lite?

The iconv-lite npm package provides utilities for converting character encodings in pure JavaScript. It supports many different encodings and can convert to and from Buffer objects without the need for a native C++ binding. This makes it a lightweight and portable solution for encoding conversion.

What are iconv-lite's main functionalities?

Encoding Conversion

Converts text from one character encoding to another. The example shows how to decode a buffer to a string and encode a string to a buffer using Windows-1251 encoding.

const iconv = require('iconv-lite');

// Convert from an encoded buffer to js string.
const str = iconv.decode(Buffer.from([0x63, 0x61, 0x66, 0xe9]), 'win1251');

// Convert from js string to an encoded buffer.
const buf = iconv.encode('Sample input text', 'win1251');

Streaming Conversion

Provides a streaming interface for encoding conversion. This example demonstrates how to create a read stream from a file and pipe it through iconv-lite's decode stream.

const iconv = require('iconv-lite');
const fs = require('fs');

// Decode stream (from a file, for example)
const readStream = fs.createReadStream('file.txt');
const decodeStream = iconv.decodeStream('win1251');

readStream.pipe(decodeStream);

decodeStream.on('data', function(str) {
  console.log(str); // converted text
});

Encoding Detection

Checks if a particular encoding is supported by iconv-lite. The example checks if UTF-8 encoding is supported.

const iconv = require('iconv-lite');

// Check if encoding is supported
const encodingSupported = iconv.encodingExists('utf-8');

console.log(encodingSupported); // true or false

Other packages similar to iconv-lite

Readme

Source

iconv-lite - native javascript conversion between character encodings.

Usage

var iconv = require('iconv-lite');

// Convert from an encoded buffer to string.
str = iconv.fromEncoding(buf, 'win-1251');
// Or
str = iconv.decode(buf, 'win-1251');

// Convert from string to an encoded buffer.
buf = iconv.toEncoding("Sample input string", 'win-1251');
// Or
buf = iconv.encode("Sample input string", 'win-1251');

Supported encodings

Currently the following encodings supported:

  • All node.js native encodings: 'utf8', 'ucs2', 'ascii', 'binary', 'base64'
  • Base encodings: 'latin1'
  • Western encoding: 'windows-1252'
  • Cyrillic encodings: 'windows-1251', 'koi8-r', 'iso-8859-5'
  • Simplified chinese: 'gbk', 'gb2313'
  • Greek encodings: 'windows-1253', 'iso-8859-7'/'greek', 'cp737', 'cp28597'
  • Turkish encodings: 'windows1254', 'iso-8859-9'/'turkish'

Other encodings are easy to add, see the source. Please, participate.

Encoding/decoding speed

Comparison with iconv module (1000 times 256kb, on Core i5/2.5 GHz).

Operation\module            iconv       iconv-lite (this)
toEncoding('win1251')       19.57 mb/s  49.04 mb/s
fromEncoding('win1251')     16.39 mb/s  24.11 mb/s

Notes

This module is JavaScript-only, thus can be used in a sandboxed environment like Cloud9.

Untranslatable characters are set to '?'. No transliteration is currently supported, pull requests are welcome.

Testing

npm install --dev iconv-lite
vows

TODO

  • Support streaming character conversion, something like util.pipe(req, iconv.fromEncodingStream('latin1')).
  • Add more encodings.
  • Add transliteration (best fit char).
  • Add tests and correct support of variable-byte encodings (currently work is delegated to node).

Keywords

FAQs

Last updated on 06 May 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc