@amoutonbrady/lz-string

中文文档
0.2.0发布于 2026/09/07MITTypeScriptESM + CJS零依赖近 30 天下载 1.3万

Modern TypeScript port of lz-string: LZ-based compression for JavaScript strings

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. API
  4. Development
  5. License

@amoutonbrady/lz-string

Modern TypeScript port of lz-string, the LZ-based compression algorithm for JavaScript strings.

  • Wire-compatible with lz-string 1.x: anything compressed by one decompresses with the other (verified against the reference implementation in the test suite).
  • Same API, fully typed, ESM and CommonJS builds, zero dependencies, tree-shakeable.
  • About half the source of the original: the bit reader/writer is shared instead of copy-pasted.

Install

pnpm add @amoutonbrady/lz-string

Usage

import { compressToURL, decompressFromURL } from '@amoutonbrady/lz-string';

const compressed = compressToURL('hello world'); // safe to put in a query string as-is
const original = decompressFromURL(compressed); // 'hello world'

API

Every compress* function returns '' for null/undefined. Every decompress* function returns '' for null/undefined, null for an empty or corrupt input.

CompressDecompressOutput
compress(input: string): stringdecompress(input: string): string | nullRaw UTF-16 code units, smallest but not safe to store as text
compressToUTF16(input: string): stringdecompressFromUTF16(input: string): string | nullValid UTF-16, safe for localStorage
compressToBase64(input: string): stringdecompressFromBase64(input: string): string | nullStandard Base64
compressToURL(input: string): stringdecompressFromURL(input: string): string | nullURL-safe alphabet (+-$), no padding, no encoding needed
compressToUint8Array(input: string): Uint8ArraydecompressFromUint8Array(input: Uint8Array): string | nullBinary

compressToURL was named compressToEncodedURIComponent in the original library.

Development

pnpm install
pnpm test       # node:test, runs the TypeScript sources directly
pnpm typecheck
pnpm build      # tsdown: ESM + CJS + .d.ts, then publint + arethetypeswrong

Releases: bump the version, push a v* tag. The release.yml workflow builds, tests and publishes to npm with provenance through npm trusted publishing (no token to manage).

License

MIT

版本与发布记录

@amoutonbrady/lz-string 共发布 3 个版本,首次发布于 2021/01/26;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.2.0latest2026/09/071 个月前
0.1.02023/03/113 年前
0.0.12021/01/265 年前

关于 @amoutonbrady/lz-string 的常见问题

@amoutonbrady/lz-string 是什么?

Modern TypeScript port of lz-string: LZ-based compression for JavaScript strings

如何安装 @amoutonbrady/lz-string?

在项目目录运行 npm install @amoutonbrady/lz-string,或使用 pnpm add / yarn add / bun add。

@amoutonbrady/lz-string 支持 TypeScript 吗?

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

@amoutonbrady/lz-string 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@amoutonbrady/lz-string 需要什么版本的 Node.js?

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

@amoutonbrady/lz-string 可以商用吗?

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

@amoutonbrady/lz-string 的下载量有多少?

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

@amoutonbrady/lz-string 还在维护吗?

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