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.

122 lines
4.8 KiB

4 years ago
  1. # Punycode.js [![Build status](https://travis-ci.org/bestiejs/punycode.js.svg?branch=master)](https://travis-ci.org/bestiejs/punycode.js) [![Code coverage status](http://img.shields.io/codecov/c/github/bestiejs/punycode.js.svg)](https://codecov.io/gh/bestiejs/punycode.js) [![Dependency status](https://gemnasium.com/bestiejs/punycode.js.svg)](https://gemnasium.com/bestiejs/punycode.js)
  2. Punycode.js is a robust Punycode converter that fully complies to [RFC 3492](https://tools.ietf.org/html/rfc3492) and [RFC 5891](https://tools.ietf.org/html/rfc5891).
  3. This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm:
  4. * [The C example code from RFC 3492](https://tools.ietf.org/html/rfc3492#appendix-C)
  5. * [`punycode.c` by _Markus W. Scherer_ (IBM)](http://opensource.apple.com/source/ICU/ICU-400.42/icuSources/common/punycode.c)
  6. * [`punycode.c` by _Ben Noordhuis_](https://github.com/bnoordhuis/punycode/blob/master/punycode.c)
  7. * [JavaScript implementation by _some_](http://stackoverflow.com/questions/183485/can-anyone-recommend-a-good-free-javascript-for-punycode-to-unicode-conversion/301287#301287)
  8. * [`punycode.js` by _Ben Noordhuis_](https://github.com/joyent/node/blob/426298c8c1c0d5b5224ac3658c41e7c2a3fe9377/lib/punycode.js) (note: [not fully compliant](https://github.com/joyent/node/issues/2072))
  9. This project was [bundled](https://github.com/joyent/node/blob/master/lib/punycode.js) with Node.js from [v0.6.2+](https://github.com/joyent/node/compare/975f1930b1...61e796decc) until [v7](https://github.com/nodejs/node/pull/7941) (soft-deprecated).
  10. The current version supports recent versions of Node.js only. It provides a CommonJS module and an ES6 module. For the old version that offers the same functionality with broader support, including Rhino, Ringo, Narwhal, and web browsers, see [v1.4.1](https://github.com/bestiejs/punycode.js/releases/tag/v1.4.1).
  11. ## Installation
  12. Via [npm](https://www.npmjs.com/):
  13. ```bash
  14. npm install punycode --save
  15. ```
  16. In [Node.js](https://nodejs.org/):
  17. ```js
  18. const punycode = require('punycode');
  19. ```
  20. ## API
  21. ### `punycode.decode(string)`
  22. Converts a Punycode string of ASCII symbols to a string of Unicode symbols.
  23. ```js
  24. // decode domain name parts
  25. punycode.decode('maana-pta'); // 'mañana'
  26. punycode.decode('--dqo34k'); // '☃-⌘'
  27. ```
  28. ### `punycode.encode(string)`
  29. Converts a string of Unicode symbols to a Punycode string of ASCII symbols.
  30. ```js
  31. // encode domain name parts
  32. punycode.encode('mañana'); // 'maana-pta'
  33. punycode.encode('☃-⌘'); // '--dqo34k'
  34. ```
  35. ### `punycode.toUnicode(input)`
  36. Converts a Punycode string representing a domain name or an email address to Unicode. Only the Punycoded parts of the input will be converted, i.e. it doesn’t matter if you call it on a string that has already been converted to Unicode.
  37. ```js
  38. // decode domain names
  39. punycode.toUnicode('xn--maana-pta.com');
  40. // → 'mañana.com'
  41. punycode.toUnicode('xn----dqo34k.com');
  42. // → '☃-⌘.com'
  43. // decode email addresses
  44. punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
  45. // → 'джумла@джpумлатест.bрфa'
  46. ```
  47. ### `punycode.toASCII(input)`
  48. Converts a lowercased Unicode string representing a domain name or an email address to Punycode. Only the non-ASCII parts of the input will be converted, i.e. it doesn’t matter if you call it with a domain that’s already in ASCII.
  49. ```js
  50. // encode domain names
  51. punycode.toASCII('mañana.com');
  52. // → 'xn--maana-pta.com'
  53. punycode.toASCII('☃-⌘.com');
  54. // → 'xn----dqo34k.com'
  55. // encode email addresses
  56. punycode.toASCII('джумла@джpумлатест.bрфa');
  57. // → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
  58. ```
  59. ### `punycode.ucs2`
  60. #### `punycode.ucs2.decode(string)`
  61. Creates an array containing the numeric code point values of each Unicode symbol in the string. While [JavaScript uses UCS-2 internally](https://mathiasbynens.be/notes/javascript-encoding), this function will convert a pair of surrogate halves (each of which UCS-2 exposes as separate characters) into a single code point, matching UTF-16.
  62. ```js
  63. punycode.ucs2.decode('abc');
  64. // → [0x61, 0x62, 0x63]
  65. // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
  66. punycode.ucs2.decode('\uD834\uDF06');
  67. // → [0x1D306]
  68. ```
  69. #### `punycode.ucs2.encode(codePoints)`
  70. Creates a string based on an array of numeric code point values.
  71. ```js
  72. punycode.ucs2.encode([0x61, 0x62, 0x63]);
  73. // → 'abc'
  74. punycode.ucs2.encode([0x1D306]);
  75. // → '\uD834\uDF06'
  76. ```
  77. ### `punycode.version`
  78. A string representing the current Punycode.js version number.
  79. ## Author
  80. | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
  81. |---|
  82. | [Mathias Bynens](https://mathiasbynens.be/) |
  83. ## License
  84. Punycode.js is available under the [MIT](https://mths.be/mit) license.