xref: /third_party/node/doc/api/punycode.md (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci# Punycode
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!-- YAML
41cb0ef41Sopenharmony_cideprecated: v7.0.0
51cb0ef41Sopenharmony_ci-->
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci> Stability: 0 - Deprecated
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci<!-- source_link=lib/punycode.js -->
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci**The version of the punycode module bundled in Node.js is being deprecated.**
141cb0ef41Sopenharmony_ciIn a future major version of Node.js this module will be removed. Users
151cb0ef41Sopenharmony_cicurrently depending on the `punycode` module should switch to using the
161cb0ef41Sopenharmony_ciuserland-provided [Punycode.js][] module instead. For punycode-based URL
171cb0ef41Sopenharmony_ciencoding, see [`url.domainToASCII`][] or, more generally, the
181cb0ef41Sopenharmony_ci[WHATWG URL API][].
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciThe `punycode` module is a bundled version of the [Punycode.js][] module. It
211cb0ef41Sopenharmony_cican be accessed using:
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci```js
241cb0ef41Sopenharmony_ciconst punycode = require('punycode');
251cb0ef41Sopenharmony_ci```
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci[Punycode][] is a character encoding scheme defined by RFC 3492 that is
281cb0ef41Sopenharmony_ciprimarily intended for use in Internationalized Domain Names. Because host
291cb0ef41Sopenharmony_cinames in URLs are limited to ASCII characters only, Domain Names that contain
301cb0ef41Sopenharmony_cinon-ASCII characters must be converted into ASCII using the Punycode scheme.
311cb0ef41Sopenharmony_ciFor instance, the Japanese character that translates into the English word,
321cb0ef41Sopenharmony_ci`'example'` is `'例'`. The Internationalized Domain Name, `'例.com'` (equivalent
331cb0ef41Sopenharmony_cito `'example.com'`) is represented by Punycode as the ASCII string
341cb0ef41Sopenharmony_ci`'xn--fsq.com'`.
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciThe `punycode` module provides a simple implementation of the Punycode standard.
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciThe `punycode` module is a third-party dependency used by Node.js and
391cb0ef41Sopenharmony_cimade available to developers as a convenience. Fixes or other modifications to
401cb0ef41Sopenharmony_cithe module must be directed to the [Punycode.js][] project.
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci## `punycode.decode(string)`
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci<!-- YAML
451cb0ef41Sopenharmony_ciadded: v0.5.1
461cb0ef41Sopenharmony_ci-->
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci* `string` {string}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciThe `punycode.decode()` method converts a [Punycode][] string of ASCII-only
511cb0ef41Sopenharmony_cicharacters to the equivalent string of Unicode codepoints.
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci```js
541cb0ef41Sopenharmony_cipunycode.decode('maana-pta'); // 'mañana'
551cb0ef41Sopenharmony_cipunycode.decode('--dqo34k'); // '☃-⌘'
561cb0ef41Sopenharmony_ci```
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci## `punycode.encode(string)`
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci<!-- YAML
611cb0ef41Sopenharmony_ciadded: v0.5.1
621cb0ef41Sopenharmony_ci-->
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci* `string` {string}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciThe `punycode.encode()` method converts a string of Unicode codepoints to a
671cb0ef41Sopenharmony_ci[Punycode][] string of ASCII-only characters.
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci```js
701cb0ef41Sopenharmony_cipunycode.encode('mañana'); // 'maana-pta'
711cb0ef41Sopenharmony_cipunycode.encode('☃-⌘'); // '--dqo34k'
721cb0ef41Sopenharmony_ci```
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci## `punycode.toASCII(domain)`
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci<!-- YAML
771cb0ef41Sopenharmony_ciadded: v0.6.1
781cb0ef41Sopenharmony_ci-->
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci* `domain` {string}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciThe `punycode.toASCII()` method converts a Unicode string representing an
831cb0ef41Sopenharmony_ciInternationalized Domain Name to [Punycode][]. Only the non-ASCII parts of the
841cb0ef41Sopenharmony_cidomain name will be converted. Calling `punycode.toASCII()` on a string that
851cb0ef41Sopenharmony_cialready only contains ASCII characters will have no effect.
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci```js
881cb0ef41Sopenharmony_ci// encode domain names
891cb0ef41Sopenharmony_cipunycode.toASCII('mañana.com');  // 'xn--maana-pta.com'
901cb0ef41Sopenharmony_cipunycode.toASCII('☃-⌘.com');   // 'xn----dqo34k.com'
911cb0ef41Sopenharmony_cipunycode.toASCII('example.com'); // 'example.com'
921cb0ef41Sopenharmony_ci```
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci## `punycode.toUnicode(domain)`
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci<!-- YAML
971cb0ef41Sopenharmony_ciadded: v0.6.1
981cb0ef41Sopenharmony_ci-->
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci* `domain` {string}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciThe `punycode.toUnicode()` method converts a string representing a domain name
1031cb0ef41Sopenharmony_cicontaining [Punycode][] encoded characters into Unicode. Only the [Punycode][]
1041cb0ef41Sopenharmony_ciencoded parts of the domain name are be converted.
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci```js
1071cb0ef41Sopenharmony_ci// decode domain names
1081cb0ef41Sopenharmony_cipunycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
1091cb0ef41Sopenharmony_cipunycode.toUnicode('xn----dqo34k.com');  // '☃-⌘.com'
1101cb0ef41Sopenharmony_cipunycode.toUnicode('example.com');       // 'example.com'
1111cb0ef41Sopenharmony_ci```
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci## `punycode.ucs2`
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci<!-- YAML
1161cb0ef41Sopenharmony_ciadded: v0.7.0
1171cb0ef41Sopenharmony_ci-->
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci### `punycode.ucs2.decode(string)`
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci<!-- YAML
1221cb0ef41Sopenharmony_ciadded: v0.7.0
1231cb0ef41Sopenharmony_ci-->
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci* `string` {string}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciThe `punycode.ucs2.decode()` method returns an array containing the numeric
1281cb0ef41Sopenharmony_cicodepoint values of each Unicode symbol in the string.
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci```js
1311cb0ef41Sopenharmony_cipunycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
1321cb0ef41Sopenharmony_ci// surrogate pair for U+1D306 tetragram for centre:
1331cb0ef41Sopenharmony_cipunycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
1341cb0ef41Sopenharmony_ci```
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci### `punycode.ucs2.encode(codePoints)`
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci<!-- YAML
1391cb0ef41Sopenharmony_ciadded: v0.7.0
1401cb0ef41Sopenharmony_ci-->
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci* `codePoints` {integer\[]}
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ciThe `punycode.ucs2.encode()` method returns a string based on an array of
1451cb0ef41Sopenharmony_cinumeric code point values.
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci```js
1481cb0ef41Sopenharmony_cipunycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
1491cb0ef41Sopenharmony_cipunycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
1501cb0ef41Sopenharmony_ci```
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci## `punycode.version`
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci<!-- YAML
1551cb0ef41Sopenharmony_ciadded: v0.6.1
1561cb0ef41Sopenharmony_ci-->
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci* {string}
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ciReturns a string identifying the current [Punycode.js][] version number.
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci[Punycode]: https://tools.ietf.org/html/rfc3492
1631cb0ef41Sopenharmony_ci[Punycode.js]: https://github.com/bestiejs/punycode.js
1641cb0ef41Sopenharmony_ci[WHATWG URL API]: url.md#the-whatwg-url-api
1651cb0ef41Sopenharmony_ci[`url.domainToASCII`]: url.md#urldomaintoasciidomain
166