You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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