May 10, 2021 Node.js
Punycode is a character encoding scheme defined according to the RFC 3492 standard and is mainly used to convert domain names from Unicode encodings used in local languages to encodings that can be used in DNS systems.
稳定性: 2 - 不稳定
Punycode .js built
in .js from Node v0.6.2 plus. U
se
require('punycode')
to access it.
(For access in .js versions of Node, install
punycode
Convert a pure ASCII Punycode string to a Unicode string.
// decode domain name parts
punycode.decode('maana-pta'); // 'mañana'
punycode.decode('--dqo34k'); // '☃-⌘'
Convert a pure Unicode Punycode string to a pure ASCII string.
// encode domain name parts
punycode.encode('mañana'); // 'maana-pta'
punycode.encode('☃-⌘'); // '--dqo34k'
Convert a Punycode string that represents a domain name to Unicode. Only the Punycode section of the domain name is converted, which means you can also call it on a string that has been converted to Unicode.
// decode domain names
punycode.toUnicode('xn--maana-pta.com'); // 'mañana.com'
punycode.toUnicode('xn----dqo34k.com'); // '☃-⌘.com'
Convert a Unicode string that represents a domain name to Punycode. Only the non-ASCII portion of the domain name is converted, which means you can also call it on a string that has been converted to ASCII.
// encode domain names
punycode.toASCII('mañana.com'); // 'xn--maana-pta.com'
punycode.toASCII('☃-⌘.com'); // 'xn----dqo34k.com'
Create an array of numeric encoding points for each Unicode symbol in the string. Because JavaScript uses UCS-2 internally, the function converts a pair of generational halfs (separate characters exposed by UCS-2) to a single encoding point based on UTC-16.
punycode.ucs2.decode('abc'); // [0x61, 0x62, 0x63]
// surrogate pair for U+1D306 tetragram for centre:
punycode.ucs2.decode('\uD834\uDF06'); // [0x1D306]
Create a string based on a set of numeric encoding points.
punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06'
A string that represents the .js version number of the current Punycode.