@foliojs-fork/linebreak

中文文档
1.1.2发布于 2024/03/07MITCommonJS近 30 天下载 753.3万

An implementation of the Unicode Line Breaking Algorithm (UAX #14)

原始使用文档
文档目录5 节
  1. Why fork?
  2. Installation
  3. Example
  4. Development Notes
  5. License

Fork of linebreak npm

An implementation of the Unicode Line Breaking Algorithm (UAX #14)

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.

This is a JavaScript implementation of the Unicode Line Breaking Algorithm for Node.js (and browsers I guess). Currently supports Unicode version 13. It is used by PDFKit for line wrapping text in PDF documents, but since the algorithm knows nothing about the actual visual appearance or layout of text, it could be used for other things as well.

Why fork?

Because on original repo is not possible release new version on npm see foliojs/linebreak#24. This fork is same as original repo and is maintained up-to-date. This forks contains these changes:

  • Move brfs package as devDependencies

Installation

You can install via npm

npm install @foliojs-fork/linebreak

Example

var LineBreaker = require('linebreak');

var lorem = 'lorem ipsum...';
var breaker = new LineBreaker(lorem);
var last = 0;
var bk;

while (bk = breaker.nextBreak()) {
  // get the string between the last break and this one
  var word = lorem.slice(last, bk.position);
  console.log(word);

  // you can also check bk.required to see if this was a required break...
  if (bk.required) {
    console.log('\n\n');
  }

  last = bk.position;
}

Development Notes

In order to use the library, you shouldn't need to know this, but if you're interested in contributing or fixing bugs, these things might be of interest.

  • The src/classes.js file is automatically generated from LineBreak.txt in the Unicode database by src/generate_data.js. It should be rare that you need to run this, but you may if, for instance, you want to change the Unicode version.

  • You can run the tests using npm test. They are written using mocha, and generated from LineBreakTest.txt from the Unicode database, which is included in the repository for performance reasons while running them. About 50 of the over 7600 tests are currently skipped due to implementation differences. It appears that some of the tests may be wrong or use different tailoring from the spec.

License

MIT

版本与发布记录

@foliojs-fork/linebreak 共发布 3 个版本,首次发布于 2021/07/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2024/03/072 年前
1.1.12021/08/025 年前
1.1.02021/07/015 年前

关于 @foliojs-fork/linebreak 的常见问题

@foliojs-fork/linebreak 是什么?

An implementation of the Unicode Line Breaking Algorithm (UAX #14)

如何安装 @foliojs-fork/linebreak?

在项目目录运行 npm install @foliojs-fork/linebreak,或使用 pnpm add / yarn add / bun add。

@foliojs-fork/linebreak 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/foliojs-fork__linebreak 是否存在。

@foliojs-fork/linebreak 支持 ESM 还是 CommonJS?

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

@foliojs-fork/linebreak 可以商用吗?

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

@foliojs-fork/linebreak 的下载量有多少?

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

@foliojs-fork/linebreak 还在维护吗?

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