Punycode#
Source Code:lib/punycode.js
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. Userscurrently depending on thepunycode module should switch to using theuserland-providedPunycode.js module instead. For punycode-based URLencoding, seeurl.domainToASCII or, more generally, theWHATWG URL API.
Thepunycode module is a bundled version of thePunycode.js module. Itcan be accessed using:
const punycode =require('node:punycode');Punycode is a character encoding scheme defined by RFC 3492 that isprimarily intended for use in Internationalized Domain Names. Because hostnames in URLs are limited to ASCII characters only, Domain Names that containnon-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' (equivalentto'example.com') is represented by Punycode as the ASCII string'xn--fsq.com'.
Thepunycode module provides a simple implementation of the Punycode standard.
Thepunycode module is a third-party dependency used by Node.js andmade available to developers as a convenience. Fixes or other modifications tothe module must be directed to thePunycode.js project.
punycode.decode(string)#
string<string>
Thepunycode.decode() method converts aPunycode string of ASCII-onlycharacters to the equivalent string of Unicode codepoints.
punycode.decode('maana-pta');// 'mañana'punycode.decode('--dqo34k');// '☃-⌘'punycode.encode(string)#
string<string>
Thepunycode.encode() method converts a string of Unicode codepoints to aPunycode string of ASCII-only characters.
punycode.encode('mañana');// 'maana-pta'punycode.encode('☃-⌘');// '--dqo34k'punycode.toASCII(domain)#
domain<string>
Thepunycode.toASCII() method converts a Unicode string representing anInternationalized Domain Name toPunycode. Only the non-ASCII parts of thedomain name will be converted. Callingpunycode.toASCII() on a string thatalready only contains ASCII characters will have no effect.
// encode domain namespunycode.toASCII('mañana.com');// 'xn--maana-pta.com'punycode.toASCII('☃-⌘.com');// 'xn----dqo34k.com'punycode.toASCII('example.com');// 'example.com'punycode.toUnicode(domain)#
domain<string>
Thepunycode.toUnicode() method converts a string representing a domain namecontainingPunycode encoded characters into Unicode. Only thePunycodeencoded parts of the domain name are be converted.
// decode domain namespunycode.toUnicode('xn--maana-pta.com');// 'mañana.com'punycode.toUnicode('xn----dqo34k.com');// '☃-⌘.com'punycode.toUnicode('example.com');// 'example.com'punycode.ucs2#
punycode.ucs2.decode(string)#
string<string>
Thepunycode.ucs2.decode() method returns an array containing the numericcodepoint values of each Unicode symbol in the string.
punycode.ucs2.decode('abc');// [0x61, 0x62, 0x63]// surrogate pair for U+1D306 tetragram for centre:punycode.ucs2.decode('\uD834\uDF06');// [0x1D306]punycode.ucs2.encode(codePoints)#
codePoints<integer[]>
Thepunycode.ucs2.encode() method returns a string based on an array ofnumeric code point values.
punycode.ucs2.encode([0x61,0x62,0x63]);// 'abc'punycode.ucs2.encode([0x1D306]);// '\uD834\uDF06'punycode.version#
- Type:<string>
Returns a string identifying the currentPunycode.js version number.