A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.
This JavaScript library is the result of comparing, optimizing and documenting different open-source implementations of the Punycode algorithm:
- The C example code from RFC 3492
punycode.c
by Markus W. Scherer (IBM)punycode.c
by Ben Noordhuis- JavaScript implementation by some
punycode.js
by Ben Noordhuis (note: not fully compliant)
This project is bundled with Node.js v0.6.2+.
In a browser:
<script src="punycode.js"></script>
Via npm (only required for Node.js releases older than v0.6.2):
npm install punycode
In Narwhal, Node.js, and RingoJS:
var punycode = require('punycode');
In Rhino:
load('punycode.js');
Using an AMD loader like RequireJS:
require(
{
'paths': {
'punycode': 'path/to/punycode'
}
},
['punycode'],
function(punycode) {
console.log(punycode);
}
);
Usage example:
// encode/decode domain names
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
// encode/decode domain name parts
punycode.encode('mañana'); // 'maana-pta'
punycode.decode('maana-pta'); // 'mañana'
punycode.encode('☃-⌘'); // '--dqo34k'
punycode.decode('--dqo34k'); // '☃-⌘'
Full API documentation is available.
To clone this repository including all submodules, using Git 1.6.5 or later:
git clone --recursive https://github.com/bestiejs/punycode.js.git
cd punycode.js
For older Git versions, just use:
git clone https://github.com/bestiejs/punycode.js.git
cd punycode.js
git submodule update --init
Feel free to fork if you see possible improvements!
Punycode.js is dual licensed under the MIT and GPL licenses.