whatwg-encoding

中文文档
3.1.1发布于 2023/11/12MITCommonJS近 30 天下载 3亿

Decode strings according to the WHATWG Encoding Standard

此软件包已被标记为弃用

Use @exodus/bytes instead for a more spec-conformant and faster implementation

原始使用文档
文档目录4 节
  1. API
  2. Unsupported encodings
  3. Credits
  4. Alternatives

Decode According to the WHATWG Encoding Standard

This package provides a thin layer on top of iconv-lite which makes it expose some of the same primitives as the Encoding Standard.

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): performs the decode algorithm (in which any BOM will override the passed fallback encoding), and returns the resulting string
  • labelToName(label): performs the get an encoding algorithm and returns the resulting encoding's name, or null for failure
  • isSupported(name): returns whether the encoding is one of the encodings of the Encoding Standard, and is an encoding that this package can decode (via iconv-lite)
  • getBOMEncoding(uint8Array): sniffs the first 2–3 bytes of the supplied Uint8Array, returning one of the encoding names "UTF-8", "UTF-16LE", or "UTF-16BE" if the appropriate BOM is present, or null if no BOM is present

Unsupported encodings

Since we rely on iconv-lite, we are limited to support only the encodings that they support. Currently we are missing support for:

  • ISO-2022-JP
  • ISO-8859-8-I
  • replacement
  • x-mac-cyrillic
  • x-user-defined

Passing these encoding names will return false when calling isSupported, and passing any of the possible labels for these encodings to labelToName will return null.

Credits

This package was originally based on the excellent work of @nicolashenry, in jsdom. It has since been pulled out into this separate package.

Alternatives

If you are looking for a JavaScript implementation of the Encoding Standard's TextEncoder and TextDecoder APIs, you'll want @inexorabletash's text-encoding package. Node.js also has them built-in.

版本与发布记录

whatwg-encoding 共发布 10 个版本,首次发布于 2016/10/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.1latest2023/11/122 年前
3.1.02023/11/122 年前
3.0.02023/11/072 年前
2.0.02021/09/135 年前
1.0.52018/09/308 年前
1.0.42018/08/138 年前

关于 whatwg-encoding 的常见问题

whatwg-encoding 是什么?

Decode strings according to the WHATWG Encoding Standard注意:该包已被作者标记为弃用——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。作者已将其标记为弃用,新项目建议寻找替代方案。