mirror of https://github.com/lukechilds/node.git
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.
83 lines
2.4 KiB
83 lines
2.4 KiB
13 years ago
|
# punycode
|
||
|
|
||
10 years ago
|
Stability: 2 - Stable
|
||
13 years ago
|
|
||
9 years ago
|
[Punycode.js][] is bundled with Node.js v0.6.2+. Use `require('punycode')` to
|
||
|
access it. (To use it with other Node.js versions, use npm to install the
|
||
|
`punycode` module first.)
|
||
13 years ago
|
|
||
|
## punycode.decode(string)
|
||
|
|
||
12 years ago
|
Converts a Punycode string of ASCII-only symbols to a string of Unicode symbols.
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
// decode domain name parts
|
||
|
punycode.decode('maana-pta'); // 'mañana'
|
||
|
punycode.decode('--dqo34k'); // '☃-⌘'
|
||
|
```
|
||
13 years ago
|
|
||
|
## punycode.encode(string)
|
||
|
|
||
12 years ago
|
Converts a string of Unicode symbols to a Punycode string of ASCII-only symbols.
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
// encode domain name parts
|
||
|
punycode.encode('mañana'); // 'maana-pta'
|
||
|
punycode.encode('☃-⌘'); // '--dqo34k'
|
||
|
```
|
||
13 years ago
|
|
||
|
## punycode.toASCII(domain)
|
||
|
|
||
|
Converts a Unicode string representing a domain name to Punycode. Only the
|
||
|
non-ASCII parts of the domain name will be converted, i.e. it doesn't matter if
|
||
|
you call it with a domain that's already in ASCII.
|
||
|
|
||
9 years ago
|
```js
|
||
|
// encode domain names
|
||
|
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
|
||
|
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
|
||
|
```
|
||
13 years ago
|
|
||
9 years ago
|
## punycode.toUnicode(domain)
|
||
|
|
||
|
Converts a Punycode string representing a domain name to Unicode. Only the
|
||
|
Punycoded parts of the domain name will be converted, i.e. it doesn't matter if
|
||
|
you call it on a string that has already been converted to Unicode.
|
||
|
|
||
9 years ago
|
```js
|
||
|
// decode domain names
|
||
|
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
|
||
|
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
|
||
|
```
|
||
9 years ago
|
|
||
13 years ago
|
## punycode.ucs2
|
||
|
|
||
|
### punycode.ucs2.decode(string)
|
||
|
|
||
12 years ago
|
Creates an array containing the numeric code point values of each Unicode
|
||
9 years ago
|
symbol in the string. While [JavaScript uses UCS-2 internally][], this function
|
||
13 years ago
|
will convert a pair of surrogate halves (each of which UCS-2 exposes as
|
||
|
separate characters) into a single code point, matching UTF-16.
|
||
|
|
||
9 years ago
|
```js
|
||
|
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
|
||
|
// surrogate pair for U+1D306 tetragram for centre:
|
||
|
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
|
||
|
```
|
||
13 years ago
|
|
||
|
### punycode.ucs2.encode(codePoints)
|
||
|
|
||
12 years ago
|
Creates a string based on an array of numeric code point values.
|
||
13 years ago
|
|
||
9 years ago
|
```js
|
||
|
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
|
||
|
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
|
||
|
```
|
||
13 years ago
|
|
||
|
## punycode.version
|
||
|
|
||
|
A string representing the current Punycode.js version number.
|
||
9 years ago
|
|
||
|
[Punycode.js]: https://mths.be/punycode
|
||
9 years ago
|
[JavaScript uses UCS-2 internally]: https://mathiasbynens.be/notes/javascript-encoding
|