opusdev/vector-similarity-api
1
1## Pure JS character encoding conversion [](https://travis-ci.org/ashtuchkin/iconv-lite)2 3 * Doesn't need native code compilation. Works on Windows and in sandboxed environments like [Cloud9](http://c9.io).4 * Used in popular projects like [Express.js (body_parser)](https://github.com/expressjs/body-parser), 5 [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.6 * Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).7 * Intuitive encode/decode API8 * Streaming support for Node v0.10+9 * [Deprecated] Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.10 * In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).11 * Typescript [type definition file](https://github.com/ashtuchkin/iconv-lite/blob/master/lib/index.d.ts) included.12 * React Native is supported (need to explicitly `npm install` two more modules: `buffer` and `stream`).13 * License: MIT.14 15[](https://npmjs.org/packages/iconv-lite/)16 17## Usage18### Basic API19```javascript20var iconv = require('iconv-lite');21 22// Convert from an encoded buffer to js string.23str = iconv.decode(Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');24 25// Convert from js string to an encoded buffer.26buf = iconv.encode("Sample input string", 'win1251');27 28// Check if encoding is supported29iconv.encodingExists("us-ascii")30```31 32### Streaming API (Node v0.10+)33```javascript34 35// Decode stream (from binary stream to js strings)36http.createServer(function(req, res) {37 var converterStream = iconv.decodeStream('win1251');38 req.pipe(converterStream);39 40 converterStream.on('data', function(str) {41 console.log(str); // Do something with decoded strings, chunk-by-chunk.42 });43});44 45// Convert encoding streaming example46fs.createReadStream('file-in-win1251.txt')47 .pipe(iconv.decodeStream('win1251'))48 .pipe(iconv.encodeStream('ucs2'))49 .pipe(fs.createWriteStream('file-in-ucs2.txt'));50 51// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.52http.createServer(function(req, res) {53 req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {54 assert(typeof body == 'string');55 console.log(body); // full request body string56 });57});58```59 60### [Deprecated] Extend Node.js own encodings61> NOTE: This doesn't work on latest Node versions. See [details](https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility).62 63```javascript64// After this call all Node basic primitives will understand iconv-lite encodings.65iconv.extendNodeEncodings();66 67// Examples:68buf = new Buffer(str, 'win1251');69buf.write(str, 'gbk');70str = buf.toString('latin1');71assert(Buffer.isEncoding('iso-8859-15'));72Buffer.byteLength(str, 'us-ascii');73 74http.createServer(function(req, res) {75 req.setEncoding('big5');76 req.collect(function(err, body) {77 console.log(body);78 });79});80 81fs.createReadStream("file.txt", "shift_jis");82 83// External modules are also supported (if they use Node primitives, which they probably do).84request = require('request');85request({86 url: "http://github.com/", 87 encoding: "cp932"88});89 90// To remove extensions91iconv.undoExtendNodeEncodings();92```93 94## Supported encodings95 96 * All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.97 * Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap.98 * All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, 99 IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. 100 Aliases like 'latin1', 'us-ascii' also supported.101 * All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2312, GBK, GB18030, Big5, Shift_JIS, EUC-JP.102 103See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).104 105Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!106 107Multibyte 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!108 109 110## Encoding/decoding speed111 112Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.12.0). 113Note: your results may vary, so please always check on your hardware.114 115 operation iconv@2.1.4 iconv-lite@0.4.7116 ----------------------------------------------------------117 encode('win1251') ~96 Mb/s ~320 Mb/s118 decode('win1251') ~95 Mb/s ~246 Mb/s119 120## BOM handling121 122 * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options123 (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).124 A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found.125 * 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.126 * Encoding: No BOM added, unless overridden by `addBOM: true` option.127 128## UTF-16 Encodings129 130This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be131smart about endianness in the following ways:132 * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be 133 overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`.134 * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override.135 136## Other notes137 138When 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). 139Untranslatable characters are set to � or ?. No transliteration is currently supported. 140Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77). 141 142## Testing143 144```bash145$ git clone git@github.com:ashtuchkin/iconv-lite.git146$ cd iconv-lite147$ npm install148$ npm test149 150$ # To view performance:151$ node test/performance.js152 153$ # To view test coverage:154$ npm run coverage155$ open coverage/lcov-report/index.html156```157 