@cto.af/linebreak

中文文档
5.0.0发布于 2026/10/26MITTypeScriptES Module构建来源已验证近 30 天下载 3.2万

Unicode line-breaking algorithm from UAX #14

原始使用文档
文档目录5 节
  1. Installation
  2. API
  3. API Documentation
  4. Conformance to UAX #14
  5. License

@cto.af/linebreak

An implementation of the Unicode Line Breaking Algorithm UAX #14. This implementation was originally started as a refresh of the linebreak package, and still shares a small amount of test driver code with that project. The rest has been rewritten to support a fully rules-based approach that implements UAX #14 from Unicode version 18.0.0 From that document:

Line breaking, also known as word wrapping, is the process of breaking a section of text into lines such that it will fit in the available width of a page, window or other display area. The Unicode Line Breaking Algorithm performs part of this process. Given an input text, it produces a set of positions called "break opportunities" that are appropriate points to begin a new line. The selection of actual line break positions from the set of break opportunities is not covered by the Unicode Line Breaking Algorithm, but is in the domain of higher level software with knowledge of the available width and the display size of the text.

Installation

npm install @cto.af/linebreak

API

Create and use a new Rules object:

import {Rules} from '@cto.af/linebreak'
const r = new Rules({string: true});
for (const brk of r.breaks('my input string')) {
  console.log(brk.string); // "my ", "input ", "string"
  console.log(brk.pos); // 3, 9, 15
  console.log(brk.required); // false, false, true
}

The string option in the constructor will chop the input up for you into strings, rather than your having to do the slicing yourself. You may only need the positions of the breaks, which is why this isn't done by default. The iterated Break objects also have a required field.

You can tailor the rules that will be applied:

import {Rules, PASS} from '@cto.af/linebreak'
const r = new Rules();
r.replaceRule('LB25', (state) => PASS); // Do something more interesting that this!

There are a few other convenience function available for modifying rules. A few of the rules have interactions with one another due to idiosyncrasies of the specification text. Comments have been left at these points in the source. If you are going to replace or remove an existing rule, please make sure to account for those interactions.

API Documentation

Full API documentation is available.

Conformance to UAX #14

This package intends to be fully conformant with UAX #14. It currently passes ALL of the tests published by Unicode.

Other tailoring is possible by adding and removing rules.

License

MIT


Tests codecov

版本与发布记录

@cto.af/linebreak 共发布 18 个版本,首次发布于 2023/05/28;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.0latest2026/10/2616 天前
4.0.32026/03/038 个月前
4.0.02025/09/111.2 年前
3.0.52025/08/061.3 年前
3.0.42025/08/061.3 年前
3.0.32025/08/061.3 年前

关于 @cto.af/linebreak 的常见问题

@cto.af/linebreak 是什么?

Unicode line-breaking algorithm from UAX #14

如何安装 @cto.af/linebreak?

在项目目录运行 npm install @cto.af/linebreak,或使用 pnpm add / yarn add / bun add。

@cto.af/linebreak 支持 TypeScript 吗?

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

@cto.af/linebreak 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cto.af/linebreak 需要什么版本的 Node.js?

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

@cto.af/linebreak 可以商用吗?

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

@cto.af/linebreak 的下载量有多少?

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

@cto.af/linebreak 还在维护吗?

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