README.md (4941B)
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 3 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). 4 5 This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm: 6 7 * [The C example code from RFC 3492](https://tools.ietf.org/html/rfc3492#appendix-C) 8 * [`punycode.c` by _Markus W. Scherer_ (IBM)](http://opensource.apple.com/source/ICU/ICU-400.42/icuSources/common/punycode.c) 9 * [`punycode.c` by _Ben Noordhuis_](https://github.com/bnoordhuis/punycode/blob/master/punycode.c) 10 * [JavaScript implementation by _some_](http://stackoverflow.com/questions/183485/can-anyone-recommend-a-good-free-javascript-for-punycode-to-unicode-conversion/301287#301287) 11 * [`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)) 12 13 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). 14 15 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). 16 17 ## Installation 18 19 Via [npm](https://www.npmjs.com/): 20 21 ```bash 22 npm install punycode --save 23 ``` 24 25 In [Node.js](https://nodejs.org/): 26 27 ```js 28 const punycode = require('punycode'); 29 ``` 30 31 ## API 32 33 ### `punycode.decode(string)` 34 35 Converts a Punycode string of ASCII symbols to a string of Unicode symbols. 36 37 ```js 38 // decode domain name parts 39 punycode.decode('maana-pta'); // 'mañana' 40 punycode.decode('--dqo34k'); // '☃-⌘' 41 ``` 42 43 ### `punycode.encode(string)` 44 45 Converts a string of Unicode symbols to a Punycode string of ASCII symbols. 46 47 ```js 48 // encode domain name parts 49 punycode.encode('mañana'); // 'maana-pta' 50 punycode.encode('☃-⌘'); // '--dqo34k' 51 ``` 52 53 ### `punycode.toUnicode(input)` 54 55 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. 56 57 ```js 58 // decode domain names 59 punycode.toUnicode('xn--maana-pta.com'); 60 // → 'mañana.com' 61 punycode.toUnicode('xn----dqo34k.com'); 62 // → '☃-⌘.com' 63 64 // decode email addresses 65 punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'); 66 // → 'джумла@джpумлатест.bрфa' 67 ``` 68 69 ### `punycode.toASCII(input)` 70 71 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. 72 73 ```js 74 // encode domain names 75 punycode.toASCII('mañana.com'); 76 // → 'xn--maana-pta.com' 77 punycode.toASCII('☃-⌘.com'); 78 // → 'xn----dqo34k.com' 79 80 // encode email addresses 81 punycode.toASCII('джумла@джpумлатест.bрфa'); 82 // → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq' 83 ``` 84 85 ### `punycode.ucs2` 86 87 #### `punycode.ucs2.decode(string)` 88 89 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. 90 91 ```js 92 punycode.ucs2.decode('abc'); 93 // → [0x61, 0x62, 0x63] 94 // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE: 95 punycode.ucs2.decode('\uD834\uDF06'); 96 // → [0x1D306] 97 ``` 98 99 #### `punycode.ucs2.encode(codePoints)` 100 101 Creates a string based on an array of numeric code point values. 102 103 ```js 104 punycode.ucs2.encode([0x61, 0x62, 0x63]); 105 // → 'abc' 106 punycode.ucs2.encode([0x1D306]); 107 // → '\uD834\uDF06' 108 ``` 109 110 ### `punycode.version` 111 112 A string representing the current Punycode.js version number. 113 114 ## Author 115 116 | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") | 117 |---| 118 | [Mathias Bynens](https://mathiasbynens.be/) | 119 120 ## License 121 122 Punycode.js is available under the [MIT](https://mths.be/mit) license.