@fry/base62

中文文档
1.1.0发布于 2020/12/11MITTypeScriptCommonJS近 30 天下载 5990

Base62 encoder/decoder

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Binary input
  4. String input
  5. Custom symbol table
  6. Base62 encoding process

base62

Base62 encoder/decoder

Installation

npm install --save @fry/base62

Usage

encode() expects data that can be turned into a Buffer (e.g. a string, an Array, ArrayBuffer, or a Buffer) and it returns the Base62 encoded string.

decode() returns the decoded data in a Buffer which has good readers for binary data as well as a fast toString() that can return the data encoded in utf8 (default), base64, hex, etc.

Binary input

    const base62 = require('@fry/base62');

    const encoded = base62.encode([0x2a, 0x2a]);
    // encoded => AYA

    const decoded = base62.decode(encoded);
    // decoded => <Buffer 2a 2a>

    decoded[0]; // => 0x2a
    decoded.readUInt16BE(); // => 0x2a2a

String input

    const encoded = base62.encode('Memento vivere');
    // encoded => JMLjPMvqRo1sQNPbSc5

    const decoded = base62.decode(encoded);
    // decoded => <Buffer 4d 65 6d 65 6e 74 6f 20 76 69 76 65 72 65>

    decoded.toString(); // => Memento vivere

Custom symbol table

There is no standard for Base62 encoding so various encoders may use different symbol tables. This module will by default use alphanumerical characters in their natural order: 0-9A-Za-z.

To use another set of characters first initiate them with prepareSymbolTable() and then supply the resulting object as a second parameter to the encode/decode functions, e.g.:

    const symbols = base62.prepareSymbolTable('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
    const encoded = base62.encode([0x53, 0xfe, 0x92], symbols);
    // encoded => U98kC

    const decoded = base62.decode(encoded, symbols);
    // decoded => <Buffer 53 fe 92>

Base62 encoding process

The Base62 symbol table is limited to 62 characters, which is just shy of 6 bits of information. This means that special care needs to be taken in order to encode any given stream of bytes. This module takes the approach of reading a sextet (6-bit chunk) from the binary stream and encode it into the symbol table as long as the chunk is not in risk of overflowing. If the sextet is in the ”overflow zone“ then only 5 bits are used while encoding that particular chunk, indicated in the output by the presence of one of the symbol table’s last two characters. The overflow bit is left in the stream until the next sextet will be read. The last chunk may also be left-padded with zeroes during the encoding process which the decoder will detect and reverse to ensure byte-alignment of the underlying binary stream.

Happy encoding!

/ Fry

版本与发布记录

@fry/base62 共发布 2 个版本,首次发布于 2020/09/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2020/12/115 年前
1.0.02020/09/286 年前

关于 @fry/base62 的常见问题

@fry/base62 是什么?

Base62 encoder/decoder

如何安装 @fry/base62?

在项目目录运行 npm install @fry/base62,或使用 pnpm add / yarn add / bun add。

@fry/base62 支持 TypeScript 吗?

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

@fry/base62 支持 ESM 还是 CommonJS?

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

@fry/base62 可以商用吗?

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

@fry/base62 的下载量有多少?

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

@fry/base62 还在维护吗?

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