CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
README.md138 linesDownload Raw Back to iconv-lite
1## iconv-lite: Pure JS character encoding conversion2 3[![NPM Version][npm-version-image]][npm-url]4[![NPM Downloads][npm-downloads-image]][npm-downloads-url]5[![License][license-image]][license-url]6[![NPM Install Size][npm-install-size-image]][npm-install-size-url]7 8* No need for native code compilation. Quick to install, works on Windows, Web, and in sandboxed environments.9* Used in popular projects like [Express.js (body_parser)](https://github.com/expressjs/body-parser), 10  [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.11* Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).12* Intuitive encode/decode API, including Streaming support.13* In-browser usage via [browserify](https://github.com/substack/node-browserify) or [webpack](https://webpack.js.org/) (~180kb gzip compressed with Buffer shim included).14* Typescript [type definition file](https://github.com/ashtuchkin/iconv-lite/blob/master/lib/index.d.ts) included.15* React Native is supported (need to install `stream` module to enable Streaming API).16 17## Usage18 19### Basic API20 21```javascript22var iconv = require('iconv-lite');23 24// Convert from an encoded buffer to a js string.25str = iconv.decode(Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');26 27// Convert from a js string to an encoded buffer.28buf = iconv.encode("Sample input string", 'win1251');29 30// Check if encoding is supported31iconv.encodingExists("us-ascii")32```33 34### Streaming API35 36```javascript37// Decode stream (from binary data stream to js strings)38http.createServer(function(req, res) {39    var converterStream = iconv.decodeStream('win1251');40    req.pipe(converterStream);41 42    converterStream.on('data', function(str) {43        console.log(str); // Do something with decoded strings, chunk-by-chunk.44    });45});46 47// Convert encoding streaming example48fs.createReadStream('file-in-win1251.txt')49    .pipe(iconv.decodeStream('win1251'))50    .pipe(iconv.encodeStream('ucs2'))51    .pipe(fs.createWriteStream('file-in-ucs2.txt'));52 53// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.54http.createServer(function(req, res) {55    req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {56        assert(typeof body == 'string');57        console.log(body); // full request body string58    });59});60```61 62## Supported encodings63 64 *  All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.65 *  Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap, utf32, utf32-le, and utf32-be.66 *  All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, 67    IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. 68    Aliases like 'latin1', 'us-ascii' also supported.69 *  All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2312, GBK, GB18030, Big5, Shift_JIS, EUC-JP.70 71See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).72 73Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!74 75Multibyte encodings are generated from [Unicode.org mappings](http://www.unicode.org/Public/MAPPINGS/) and [WHATWG Encoding Standard mappings](http://encoding.spec.whatwg.org/). Thank you, respective authors!76 77## Encoding/decoding speed78 79Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.12.0). 80Note: your results may vary, so please always check on your hardware.81 82    operation             iconv@2.1.4   iconv-lite@0.4.783    ----------------------------------------------------------84    encode('win1251')     ~96 Mb/s      ~320 Mb/s85    decode('win1251')     ~95 Mb/s      ~246 Mb/s86 87## BOM handling88 89 * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options90   (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).91   A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found.92 * If you want to detect UTF-8 BOM when decoding other encodings, use [node-autodetect-decoder-stream](https://github.com/danielgindi/node-autodetect-decoder-stream) module.93 * Encoding: No BOM added, unless overridden by `addBOM: true` option.94 95## UTF-16 Encodings96 97This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be98smart about endianness in the following ways:99 * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be 100   overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`.101 * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override.102 103## UTF-32 Encodings104 105This library supports UTF-32LE, UTF-32BE and UTF-32 encodings. Like the UTF-16 encoding above, UTF-32 defaults to UTF-32LE, but uses BOM and 'spaces heuristics' to determine input endianness. 106 * The default of UTF-32LE can be overridden with the `defaultEncoding: 'utf-32be'` option. Strips BOM unless `stripBOM: false`.107 * Encoding: uses UTF-32LE and writes BOM by default. Use `addBOM: false` to override. (`defaultEncoding: 'utf-32be'` can also be used here to change encoding.)108 109## Other notes110 111When decoding, be sure to supply a Buffer to decode() method, otherwise [bad things usually happen](https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding).  112Untranslatable characters are set to � or ?. No transliteration is currently supported.  113Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see [#65](https://github.com/ashtuchkin/iconv-lite/issues/65), [#77](https://github.com/ashtuchkin/iconv-lite/issues/77)).  114 115## Testing116 117```sh118git clone git@github.com:ashtuchkin/iconv-lite.git119cd iconv-lite120npm install121npm test122    123# To view performance:124npm run test:performance125 126# To view test coverage: 127npm run test:cov128open coverage/index.html129```130 131[npm-downloads-image]: https://badgen.net/npm/dm/iconv-lite132[npm-downloads-url]: https://npmcharts.com/compare/iconv-lite?minimal=true133[npm-url]: https://npmjs.org/package/iconv-lite134[npm-version-image]: https://badgen.net/npm/v/iconv-lite135[npm-install-size-image]: https://badgen.net/packagephobia/install/iconv-lite136[npm-install-size-url]: https://packagephobia.com/result?p=iconv-lite137[license-image]: https://img.shields.io/npm/l/iconv-lite.svg138[license-url]: https://github.com/ashtuchkin/iconv-lite/blob/HEAD/LICENSE