2017-02-08 00:56:07 +01:00
|
|
|
# Punycode
|
2017-02-21 23:38:47 +01:00
|
|
|
<!-- YAML
|
|
|
|
changes:
|
|
|
|
- version: v7.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7941
|
|
|
|
description: Accessing this module will now emit a deprecation warning.
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 0 - Deprecated
|
2016-08-01 20:53:11 +02:00
|
|
|
|
|
|
|
**The version of the punycode module bundled in Node.js is being deprecated**.
|
|
|
|
In a future major version of Node.js this module will be removed. Users
|
|
|
|
currently depending on the `punycode` module should switch to using the
|
|
|
|
userland-provided [Punycode.js][] module instead.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-05-27 21:08:17 +02:00
|
|
|
The `punycode` module is a bundled version of the [Punycode.js][] module. It
|
|
|
|
can be accessed using:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const punycode = require('punycode');
|
|
|
|
```
|
|
|
|
|
|
|
|
[Punycode][] is a character encoding scheme defined by RFC 3492 that is
|
|
|
|
primarily intended for use in Internationalized Domain Names. Because host
|
|
|
|
names in URLs are limited to ASCII characters only, Domain Names that contain
|
|
|
|
non-ASCII characters must be converted into ASCII using the Punycode scheme.
|
|
|
|
For instance, the Japanese character that translates into the English word,
|
|
|
|
`'example'` is `'例'`. The Internationalized Domain Name, `'例.com'` (equivalent
|
|
|
|
to `'example.com'`) is represented by Punycode as the ASCII string
|
|
|
|
`'xn--fsq.com'`.
|
|
|
|
|
|
|
|
The `punycode` module provides a simple implementation of the Punycode standard.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
The `punycode` module is a third-party dependency used by Node.js and
|
2016-05-27 21:08:17 +02:00
|
|
|
made available to developers as a convenience. Fixes or other modifications to
|
|
|
|
the module must be directed to the [Punycode.js][] project.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.decode(string)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.1
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `string` {string}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.decode()` method converts a [Punycode][] string of ASCII-only
|
|
|
|
characters to the equivalent string of Unicode codepoints.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
punycode.decode('maana-pta'); // 'mañana'
|
|
|
|
punycode.decode('--dqo34k'); // '☃-⌘'
|
|
|
|
```
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.encode(string)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.5.1
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `string` {string}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.encode()` method converts a string of Unicode codepoints to a
|
|
|
|
[Punycode][] string of ASCII-only characters.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
punycode.encode('mañana'); // 'maana-pta'
|
|
|
|
punycode.encode('☃-⌘'); // '--dqo34k'
|
|
|
|
```
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.toASCII(domain)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.6.1
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `domain` {string}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.toASCII()` method converts a Unicode string representing an
|
|
|
|
Internationalized Domain Name to [Punycode][]. Only the non-ASCII parts of the
|
|
|
|
domain name will be converted. Calling `punycode.toASCII()` on a string that
|
|
|
|
already only contains ASCII characters will have no effect.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// encode domain names
|
2016-05-27 21:08:17 +02:00
|
|
|
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
|
|
|
|
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
|
|
|
|
punycode.toASCII('example.com'); // 'example.com'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.toUnicode(domain)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.6.1
|
|
|
|
-->
|
2015-11-04 18:25:20 +01:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `domain` {string}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.toUnicode()` method converts a string representing a domain name
|
|
|
|
containing [Punycode][] encoded characters into Unicode. Only the [Punycode][]
|
|
|
|
encoded parts of the domain name are be converted.
|
2015-11-04 18:25:20 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// decode domain names
|
|
|
|
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
|
2016-05-27 21:08:17 +02:00
|
|
|
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
|
|
|
|
punycode.toUnicode('example.com'); // 'example.com'
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-11-04 18:25:20 +01:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.ucs2`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
### `punycode.ucs2.decode(string)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
* `string` {string}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.ucs2.decode()` method returns an array containing the numeric
|
|
|
|
codepoint values of each Unicode symbol in the string.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
|
|
|
|
// surrogate pair for U+1D306 tetragram for centre:
|
|
|
|
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
|
|
|
|
```
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
### `punycode.ucs2.encode(codePoints)`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.7.0
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2018-04-09 14:25:04 +02:00
|
|
|
* `codePoints` {integer[]}
|
2016-05-27 21:08:17 +02:00
|
|
|
|
|
|
|
The `punycode.ucs2.encode()` method returns a string based on an array of
|
|
|
|
numeric code point values.
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
|
|
|
|
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
|
|
|
|
```
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2019-12-24 22:42:28 +01:00
|
|
|
## `punycode.version`
|
2016-05-17 12:58:03 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.6.1
|
|
|
|
-->
|
2012-05-21 16:51:09 +02:00
|
|
|
|
2018-04-11 20:07:14 +02:00
|
|
|
* {string}
|
|
|
|
|
2016-05-27 21:08:17 +02:00
|
|
|
Returns a string identifying the current [Punycode.js][] version number.
|
2015-11-14 04:21:49 +01:00
|
|
|
|
2018-07-14 14:10:10 +02:00
|
|
|
[Punycode.js]: https://github.com/bestiejs/punycode.js
|
2017-05-08 18:30:13 +02:00
|
|
|
[Punycode]: https://tools.ietf.org/html/rfc3492
|