Node.js v20.11.1 文档


Punycode#

稳定性: 0 - 已弃用

¥Stability: 0 - Deprecated

源代码: lib/punycode.js

Node.js 中打包的 punycode 模块版本已被弃用。在 Node.js 的未来主版本中,此模块将被删除。当前依赖 punycode 模块的用户应该改用用户空间提供的 Punycode.js 模块。对于基于 punycode 的 URL 编码,请参阅 url.domainToASCII 或更一般地说,WHATWG URL API

¥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. For punycode-based URL encoding, see url.domainToASCII or, more generally, the WHATWG URL API.

punycode 模块是 Punycode.js 模块的打包版本。可以使用以下方式访问它:

¥The punycode module is a bundled version of the Punycode.js module. It can be accessed using:

const punycode = require('punycode'); 

Punycode 是由 RFC 3492 定义的字符编码方案,主要用于国际化域名。由于 URL 中的主机名仅限于 ASCII 字符,因此必须使用 Punycode 方案将包含非 ASCII 字符的域名转换为 ASCII。例如,翻译成英文单词的日字面符 'example''例'。国际化域名 '例.com'(相当于 'example.com')由 Punycode 表示为 ASCII 字符串 'xn--fsq.com'

¥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'.

punycode 模块提供了 Punycode 标准的简单实现。

¥The punycode module provides a simple implementation of the Punycode standard.

punycode 模块是 Node.js 使用的第三方依赖,为方便开发者提供。必须将对模块的修复或其他修改定向到 Punycode.js 项目。

¥The punycode module is a third-party dependency used by Node.js and made available to developers as a convenience. Fixes or other modifications to the module must be directed to the Punycode.js project.

punycode.decode(string)#

punycode.decode() 方法将纯 ASCII 字符的 Punycode 字符串转换为等效的 Unicode 代码点字符串。

¥The punycode.decode() method converts a Punycode string of ASCII-only characters to the equivalent string of Unicode codepoints.

punycode.decode('maana-pta'); // 'mañana'
punycode.decode('--dqo34k'); // '☃-⌘' 

punycode.encode(string)#

punycode.encode() 方法将 Unicode 代码点字符串转换为仅 ASCII 字符的 Punycode 字符串。

¥The punycode.encode() method converts a string of Unicode codepoints to a Punycode string of ASCII-only characters.

punycode.encode('mañana'); // 'maana-pta'
punycode.encode('☃-⌘'); // '--dqo34k' 

punycode.toASCII(domain)#

punycode.toASCII() 方法将表示国际化域名的 Unicode 字符串转换为 Punycode。只转换域名的非 ASCII 部分。在已经只包含 ASCII 字符的字符串上调用 punycode.toASCII() 将无效。

¥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.

// encode domain names
punycode.toASCII('mañana.com');  // 'xn--maana-pta.com'
punycode.toASCII('☃-⌘.com');   // 'xn----dqo34k.com'
punycode.toASCII('example.com'); // 'example.com' 

punycode.toUnicode(domain)#

punycode.toUnicode() 方法将表示包含 Punycode 编码字符的域名的字符串转换为 Unicode。仅转换域名的 Punycode 编码部分。

¥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.

// decode domain names
punycode.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)#

punycode.ucs2.decode() 方法返回一个数组,其中包含字符串中每个 Unicode 符号的数字代码点值。

¥The punycode.ucs2.decode() method returns an array containing the numeric codepoint 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)#

punycode.ucs2.encode() 方法返回基于数字代码点值数组的字符串。

¥The punycode.ucs2.encode() method returns a string based on an array of numeric code point values.

punycode.ucs2.encode([0x61, 0x62, 0x63]); // 'abc'
punycode.ucs2.encode([0x1D306]); // '\uD834\uDF06' 

punycode.version#

返回标识当前 Punycode.js 版本号的字符串。

¥Returns a string identifying the current Punycode.js version number.