- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
Node.js v24.13.0 文档
- Node.js v24.13.0
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- 其他版本
Punycode#>
源代码: 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)#>
string<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)#>
string<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)#>
domain<string>
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)#>
domain<string>
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)#>
string<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#>
- 类型:<string>
返回一个字符串,标识当前的 Punycode.js 版本号。
【Returns a string identifying the current Punycode.js version number.】