Node.js v24.13.0 文档


Punycode#>

稳定性: 0 - 已弃用

源代码: lib/punycode.js

Node.js 中打包的 punycode 模块版本即将被弃用。 在未来的 Node.js 主要版本中,该模块将被移除。目前依赖 punycode 模块的用户应切换使用用户社区提供的 Punycode.js 模块。对于基于 punycode 的 URL 编码,请参见 url.domainToASCII,或更一般地,参见 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('node:punycode'); 

Punycode 是由 RFC 3492 定义的一种字符编码方案,主要用于国际化域名。由于 URL 中的主机名仅限于使用 ASCII 字符,因此包含非 ASCII 字符的域名必须使用 Punycode 方案转换为 ASCII。例如,表示英文单词 'example' 的日文字符是 '例'。国际化域名 '例.com'(相当于 'example.com')在 Punycode 中表示为 ASCII 字符串 '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)#>

  • 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.】

Node.js 中文网 - 粤ICP备13048890号