# whatwg-encoding 中文文档

规范地址：https://npmcn.com/package/whatwg-encoding/

版本：3\.1\.1
npm 元数据更新时间：2025-12-28T03:47:24.435Z
中文译文更新时间：2026-11-05T15:44:19.860Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/whatwg-encoding
原文对照：https://npmcn.com/package/whatwg-encoding/?lang=original
项目主页：https://github.com/jsdom/whatwg-encoding#readme
源码仓库：https://github.com/jsdom/whatwg-encoding

弃用说明：Use @exodus/bytes instead for a more spec-conformant and faster implementation

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：13.3 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=18
- 运行依赖：1 个
- 首次发布：2016-10-16T02:07:07.293Z
- 最新版本发布：2023-11-12T07:29:21.747Z
- 近 30 天下载量：300512153（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：10（近 12 个月 0 个）

## 中文简介

根据 WHWG 编码标准解码字符串

## 安装

```sh
npm install whatwg-encoding
```

也可以使用：`pnpm add whatwg-encoding`、`yarn add whatwg-encoding`、`bun add whatwg-encoding`

## README

# 根据WHWG编码标准解码

这个软件包提供了一层薄薄的图层 [图标-升](https://github.com/ashtuchkin/iconv-lite) 这使得它暴露了一些 相同的原始人与人类 [编码标准](https://encoding.spec.whatwg.org/)。 。 。 .

```js
const whatwgEncoding = require("whatwg-encoding");

console.assert(whatwgEncoding.labelToName("latin1") === "windows-1252");
console.assert(whatwgEncoding.labelToName("  CYRILLic ") === "ISO-8859-5");

console.assert(whatwgEncoding.isSupported("IBM866") === true);

// Not supported by the Encoding Standard
console.assert(whatwgEncoding.isSupported("UTF-32") === false);

// In the Encoding Standard, but this package can't decode it
console.assert(whatwgEncoding.isSupported("x-mac-cyrillic") === false);

console.assert(whatwgEncoding.getBOMEncoding(new Uint8Array([0xFE, 0xFF])) === "UTF-16BE");
console.assert(whatwgEncoding.getBOMEncoding(new Uint8Array([0x48, 0x69])) === null);

console.assert(whatwgEncoding.decode(new Uint8Array([0x48, 0x69]), "UTF-8") === "Hi");
```

## API 密码

-   `decode(uint8Array, fallbackEncodingName)`: 执行 [解码](https://encoding.spec.whatwg.org/#decode) 算法(在算法中, BOM 将覆盖经过的回落编码),并返回所产生的字符串
-   `labelToName(label)`: 执行 [获得编码](https://encoding.spec.whatwg.org/#concept-encoding-get) 算法并返回生成的编码名称,或 `null` 失败
-   `isSupported(name)`: 返回编码是否为 [编码](https://encoding.spec.whatwg.org/#names-and-labels) 编码标准, _财务报告和审定财务报表_ 是一个编码, 此软件包可以解码( 通过图标v- lite)
-   `getBOMEncoding(uint8Array)`: 嗅出所供应的第一个 2–3 字节 `Uint8Array`,返回一个编码名称 `"UTF-8"`, (中文(简体) ), `"UTF-16LE"`,或 `"UTF-16BE"` 如果有适当的管理局,或 `null` 如果没有BOM的话

## 不支持的编码

由于我们依赖图标-lite,我们仅限于支持它们支持的编码. 目前,我们没有支持:

-   ISO-2022-JP 标准
-   ISO-8859-8-I号标准
-   替换
-   x- mac- 细胞
-   x 用户定义

传递这些编码名称将返回 `false` 调用时 `isSupported`,并将这些编码的任何可能标签传递给 `labelToName` 将返回 `null`。 。 。 .

## 贷项

这一一揽子方案最初是基于以下各方面的出色工作: [@nicolashenry (英语)](https://github.com/nicolashenry), (中文(简体) ), [缩写](https://github.com/tmpvar/jsdom/blob/7ce11776ce161e8d5921a7a183585327400f786b/lib/jsdom/living/helpers/encoding.js)从那时起,它就被拉进了这个单独的包.

## 替代品

如果您正在寻找编码标准的 JavaScript 执行 `TextEncoder` 财务报告和审定财务报表 `TextDecoder` APIs,你会想要的 [@ 无法忍受的tash](https://github.com/inexorabletash)是因为 [文本编码](https://github.com/inexorabletash/text-encoding) 软件包。 节点J也有 [内置](https://nodejs.org/dist/latest/docs/api/globals.html#globals_textdecoder)。 。 。 .

## 常见问题

### whatwg-encoding 是什么？

根据 WHWG 编码标准解码字符串注意：该包已被作者标记为弃用——Use @exodus/bytes instead for a more spec-conformant and faster implementation

### 如何安装 whatwg-encoding？

在项目目录运行 npm install whatwg-encoding，或使用 pnpm add / yarn add / bun add。

### whatwg-encoding 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/whatwg-encoding 是否存在。

### whatwg-encoding 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### whatwg-encoding 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=18。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### whatwg-encoding 可以商用吗？

whatwg-encoding 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### whatwg-encoding 的下载量有多少？

根据 npm 官方下载统计，whatwg-encoding 在 2026/10/06 – 2026/11/04 这 30 天内被下载 300,512,153 次，日均约 10,017,072 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### whatwg-encoding 还在维护吗？

最新版本 3\.1\.1 发布于 2023/11/12（2 年前）；至今共发布 10 个版本，其中过去 12 个月发布了 0 个；首次发布于 2016/10/16。作者已将其标记为弃用，新项目建议寻找替代方案。

