1 | # Punycode.js [![punycode on npm](https://img.shields.io/npm/v/punycode)](https://www.npmjs.com/package/punycode) [![](https://data.jsdelivr.com/v1/package/npm/punycode/badge)](https://www.jsdelivr.com/package/npm/punycode)
|
---|
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 | This project provides a CommonJS module that uses ES2015+ features and JavaScript module, which work in modern Node.js versions and browsers. For the old Punycode.js version that offers the same functionality in a UMD build with support for older pre-ES2015 runtimes, including Rhino, Ringo, and Narwhal, see [v1.4.1](https://github.com/mathiasbynens/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 | > ⚠️ Note that userland modules don't hide core modules.
|
---|
28 | > For example, `require('punycode')` still imports the deprecated core module even if you executed `npm install punycode`.
|
---|
29 | > Use `require('punycode/')` to import userland modules rather than core modules.
|
---|
30 |
|
---|
31 | ```js
|
---|
32 | const punycode = require('punycode/');
|
---|
33 | ```
|
---|
34 |
|
---|
35 | ## API
|
---|
36 |
|
---|
37 | ### `punycode.decode(string)`
|
---|
38 |
|
---|
39 | Converts a Punycode string of ASCII symbols to a string of Unicode symbols.
|
---|
40 |
|
---|
41 | ```js
|
---|
42 | // decode domain name parts
|
---|
43 | punycode.decode('maana-pta'); // 'mañana'
|
---|
44 | punycode.decode('--dqo34k'); // '☃-⌘'
|
---|
45 | ```
|
---|
46 |
|
---|
47 | ### `punycode.encode(string)`
|
---|
48 |
|
---|
49 | Converts a string of Unicode symbols to a Punycode string of ASCII symbols.
|
---|
50 |
|
---|
51 | ```js
|
---|
52 | // encode domain name parts
|
---|
53 | punycode.encode('mañana'); // 'maana-pta'
|
---|
54 | punycode.encode('☃-⌘'); // '--dqo34k'
|
---|
55 | ```
|
---|
56 |
|
---|
57 | ### `punycode.toUnicode(input)`
|
---|
58 |
|
---|
59 | 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.
|
---|
60 |
|
---|
61 | ```js
|
---|
62 | // decode domain names
|
---|
63 | punycode.toUnicode('xn--maana-pta.com');
|
---|
64 | // → 'mañana.com'
|
---|
65 | punycode.toUnicode('xn----dqo34k.com');
|
---|
66 | // → '☃-⌘.com'
|
---|
67 |
|
---|
68 | // decode email addresses
|
---|
69 | punycode.toUnicode('джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq');
|
---|
70 | // → 'джумла@джpумлатест.bрфa'
|
---|
71 | ```
|
---|
72 |
|
---|
73 | ### `punycode.toASCII(input)`
|
---|
74 |
|
---|
75 | 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.
|
---|
76 |
|
---|
77 | ```js
|
---|
78 | // encode domain names
|
---|
79 | punycode.toASCII('mañana.com');
|
---|
80 | // → 'xn--maana-pta.com'
|
---|
81 | punycode.toASCII('☃-⌘.com');
|
---|
82 | // → 'xn----dqo34k.com'
|
---|
83 |
|
---|
84 | // encode email addresses
|
---|
85 | punycode.toASCII('джумла@джpумлатест.bрфa');
|
---|
86 | // → 'джумла@xn--p-8sbkgc5ag7bhce.xn--ba-lmcq'
|
---|
87 | ```
|
---|
88 |
|
---|
89 | ### `punycode.ucs2`
|
---|
90 |
|
---|
91 | #### `punycode.ucs2.decode(string)`
|
---|
92 |
|
---|
93 | 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.
|
---|
94 |
|
---|
95 | ```js
|
---|
96 | punycode.ucs2.decode('abc');
|
---|
97 | // → [0x61, 0x62, 0x63]
|
---|
98 | // surrogate pair for U+1D306 TETRAGRAM FOR CENTRE:
|
---|
99 | punycode.ucs2.decode('\uD834\uDF06');
|
---|
100 | // → [0x1D306]
|
---|
101 | ```
|
---|
102 |
|
---|
103 | #### `punycode.ucs2.encode(codePoints)`
|
---|
104 |
|
---|
105 | Creates a string based on an array of numeric code point values.
|
---|
106 |
|
---|
107 | ```js
|
---|
108 | punycode.ucs2.encode([0x61, 0x62, 0x63]);
|
---|
109 | // → 'abc'
|
---|
110 | punycode.ucs2.encode([0x1D306]);
|
---|
111 | // → '\uD834\uDF06'
|
---|
112 | ```
|
---|
113 |
|
---|
114 | ### `punycode.version`
|
---|
115 |
|
---|
116 | A string representing the current Punycode.js version number.
|
---|
117 |
|
---|
118 | ## For maintainers
|
---|
119 |
|
---|
120 | ### How to publish a new release
|
---|
121 |
|
---|
122 | 1. On the `main` branch, bump the version number in `package.json`:
|
---|
123 |
|
---|
124 | ```sh
|
---|
125 | npm version patch -m 'Release v%s'
|
---|
126 | ```
|
---|
127 |
|
---|
128 | Instead of `patch`, use `minor` or `major` [as needed](https://semver.org/).
|
---|
129 |
|
---|
130 | Note that this produces a Git commit + tag.
|
---|
131 |
|
---|
132 | 1. Push the release commit and tag:
|
---|
133 |
|
---|
134 | ```sh
|
---|
135 | git push && git push --tags
|
---|
136 | ```
|
---|
137 |
|
---|
138 | Our CI then automatically publishes the new release to npm, under both the [`punycode`](https://www.npmjs.com/package/punycode) and [`punycode.js`](https://www.npmjs.com/package/punycode.js) names.
|
---|
139 |
|
---|
140 | ## Author
|
---|
141 |
|
---|
142 | | [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|
---|
143 | |---|
|
---|
144 | | [Mathias Bynens](https://mathiasbynens.be/) |
|
---|
145 |
|
---|
146 | ## License
|
---|
147 |
|
---|
148 | Punycode.js is available under the [MIT](https://mths.be/mit) license.
|
---|