@47ng/codec

中文文档
1.1.0发布于 2022/01/23MITTypeScriptCommonJS近 30 天下载 41.2万

Universal conversion of Uint8Array from/into UTF-8, base64url and hex in the browser and Node.js

原始使用文档
文档目录10 节
  1. Features
  2. Installation
  3. Support
  4. Examples
  5. Documentation
  6. UTF-8
  7. Base 64
  8. Hex
  9. Utilities
  10. License

@47ng/codec

NPM MIT License GitHub Workflow Status (branch) Coverage Status NPM Downloads

Universal conversion of Uint8Array from/into UTF-8, base64url and hex in the browser and Node.js

Features

Available codecs:

  • utf8 - UTF-8
  • b64 - Base 64 URL (RFC-4648, Section 5)
  • hex - Hexadecimal, lowercase
  • Simple conversion between string representation
  • Encoding detection

Watch out ! utf8 uses a different convention than the other codecs (encode and decode are swapped), to reflect how TextEncoder and TextDecoder behave.

Installation

$ yarn add @47ng/codec
# or
$ npm i @47ng/codec

Support

  • Node.js: >=11.x
  • Browser: See caniuse for TextEncoder / TextDecoder

Examples

import { b64, hex, utf8 } from '@47ng/codec'

b64.encode(utf8.encode('Hello, World !')) // SGVsbG8sIFdvcmxkICE=
hex.encode(b64.decode('SGVsbG8sIFdvcmxkICE=')) // 48656c6c6f2c20576f726c642021
utf8.decode(hex.decode('48656c6c6f2c20576f726c642021')) // Hello, World !

b64.decode('SGVsbG8sIFdvcmxkICE=') // <Buffer 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 20 21>

Documentation

UTF-8

  • utf8.encode: Convert an UTF-8 string into an array of bytes (Uint8Array)
  • utf8.decode: Convert an array of bytes into an UTF-8 string

Examples:

import { utf8 } from '@47ng/codec'

const uint8Array = utf8.encode('Hello, World!')
// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

const backToText = utf8.decode(uint8Array)
// 'Hello, World!'

Base 64

  • b64.encode: Convert an array of bytes into a base64url string
  • b64.decode: Convert a base64 string into an array of bytes
  • b64.urlSafe: Convert a standard base64 string to base64url
  • b64.urlUnsafe: Convert a base64url string to standard base64 dictionary

Note: For decoding, any dictionary is supported, and trailing padding (=) is optional.

Examples:

import { b64, utf8 } from '@47ng/codec'

const uint8Array = b64.decode('8J-Ri_CfjI0')
// Uint8Array [240, 159, 145, 139, 240, 159, 140, 141]

// Encoding always emits padding
const backToBase64 = b64.encode(uint8Array)
// '8J-Ri_CfjI0='

const asText = utf8.decode(uint8Array)
// '👋🌍'

Hex

  • hex.encode: Convert an array of bytes into a hex string
  • hex.decode: Convert a hex string into an array of bytes

Note: Decoding accepts any case (lowercase, uppercase, mixed).

Examples:

import { hex } from '@47ng/codec'

const uint8Array = hex.decode('48656C6C6F2C20576f726c642021')
// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

// Encoding is always lowercase
const backToBase64 = hex.encode(uint8Array)
// '48656c6c6f2c20576f726c642021'

Utilities

The library exports convenience methods for converting from one string representation to another:

import {
  hexToBase64url,
  base64ToHex,
  utf8ToBase64,
  base64toUTF8,
  utf8ToHex,
  hexToUTF8
} from '@47ng/codec'

base64ToHex('SGVsbG8sIFdvcmxkICE=')
// 48656c6c6f2c20576f726c642021

hexToBase64url('48656c6c6f2c20576f726c642021')
// SGVsbG8sIFdvcmxkICE=

utf8ToBase64('Hello, World !')
// SGVsbG8sIFdvcmxkICE=

base64toUTF8('SGVsbG8sIFdvcmxkICE=')
// Hello, World !

utf8ToHex('Hello, World !')
// 48656c6c6f2c20576f726c642021

hexToUTF8('48656c6c6f2c20576f726c642021')
// Hello, World !

It also exports encoder / decoder objects with strong TypeScript types, to help you build your own encoders & decoders:

import { encoders, decoders, Encoding } from '@47ng/codec'

// type Encoding = 'base64' | 'utf8' | 'hex'

function convert(
  input: string,
  inputEncoding: Encoding,
  outputEncoding: Encoding
): string {
  const decoder = decoders[inputEncoding]
  const encoder = encoders[outputEncoding]
  return encoder(decoder(input))
}

convert('Hello, World!', 'utf8', 'base64')
// SGVsbG8sIFdvcmxkICE

convert('Hello, World!', 'utf8', 'hex')
// 48656c6c6f2c20576f726c642021

You can detect the encoding of a string:

import { detectEncoding } from '@47ng/codec'

detectEncoding('baadf00dcafebabe') // hex

// Both variants of base64 are detected
detectEncoding('SGVs+G8s/FdvcmxkICE=') // base64
detectEncoding('SGVs-G8s_FdvcmxkICE=') // base64

detectEncoding('not hex not base64') // utf8

License

MIT - Made with ❤️ by François Best.

Using this package at work ? Sponsor me to help with support and maintenance.

版本与发布记录

@47ng/codec 共发布 10 个版本,首次发布于 2020/01/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/01/234 年前
1.0.12020/12/235 年前
1.0.02020/04/246 年前
0.5.02020/03/146 年前
0.4.02020/01/226 年前
0.3.12020/01/186 年前

关于 @47ng/codec 的常见问题

@47ng/codec 是什么?

Universal conversion of Uint8Array from/into UTF-8, base64url and hex in the browser and Node.js

如何安装 @47ng/codec?

在项目目录运行 npm install @47ng/codec,或使用 pnpm add / yarn add / bun add。

@47ng/codec 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@47ng/codec 支持 ESM 还是 CommonJS?

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

@47ng/codec 需要什么版本的 Node.js?

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

@47ng/codec 可以商用吗?

@47ng/codec 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@47ng/codec 的下载量有多少?

根据 npm 官方下载统计,@47ng/codec 在 2026/10/05 – 2026/11/03 这 30 天内被下载 412,381 次,日均约 13,746 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@47ng/codec 还在维护吗?

最新版本 1.1.0 发布于 2022/01/23(4 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。