word-wrap

中文文档
1.2.5发布于 2023/07/22MITTypeScriptCommonJS零依赖近 30 天下载 5.4亿

Wrap words to a specified length.

原始使用文档
文档目录14 节
  1. Install
  2. Usage
  3. Options
  4. options.width
  5. options.indent
  6. options.newline
  7. options.escape
  8. options.trim
  9. options.cut
  10. About
  11. Related projects
  12. Contributors
  13. Author
  14. License

word-wrap NPM version NPM monthly downloads NPM total downloads Linux Build Status

Wrap words to a specified length.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save word-wrap

Usage

var wrap = require('word-wrap');

wrap('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');

Results in:

  Lorem ipsum dolor sit amet, consectetur adipiscing
  elit, sed do eiusmod tempor incididunt ut labore
  et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut
  aliquip ex ea commodo consequat.

Options

image

options.width

Type: Number

Default: 50

The width of the text before wrapping to a new line.

Example:

wrap(str, {width: 60});

options.indent

Type: String

Default: `` (two spaces)

The string to use at the beginning of each line.

Example:

wrap(str, {indent: '      '});

options.newline

Type: String

Default: \n

The string to use at the end of each line.

Example:

wrap(str, {newline: '\n\n'});

options.escape

Type: function

Default: function(str){return str;}

An escape function to run on each line after splitting them.

Example:

var xmlescape = require('xml-escape');
wrap(str, {
  escape: function(string){
    return xmlescape(string);
  }
});

options.trim

Type: Boolean

Default: false

Trim trailing whitespace from the returned string. This option is included since .trim() would also strip the leading indentation from the first line.

Example:

wrap(str, {trim: true});

options.cut

Type: Boolean

Default: false

Break a word between any two letters when the word is longer than the specified width.

Example:

wrap(str, {cut: true});

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Contributors

| Commits | Contributor |
| --- | --- |
| 47 | jonschlinkert |
| 7 | OlafConijn |
| 3 | doowb |
| 2 | aashutoshrathi |
| 2 | lordvlad |
| 2 | hildjj |
| 1 | danilosampaio |
| 1 | 2fd |
| 1 | leonard-thieu |
| 1 | mohd-akram |
| 1 | toddself |
| 1 | wolfgang42 |
| 1 | zachhale |

Author

Jon Schlinkert

License

Copyright © 2023, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on July 22, 2023.

版本与发布记录

word-wrap 共发布 17 个版本,首次发布于 2014/03/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.5latest2023/07/223 年前
1.2.42023/07/193 年前
1.2.32017/06/039 年前
1.2.22017/05/029 年前
1.2.12017/01/319 年前
1.2.02016/12/299 年前

关于 word-wrap 的常见问题

word-wrap 是什么?

Wrap words to a specified length.

如何安装 word-wrap?

在项目目录运行 npm install word-wrap,或使用 pnpm add / yarn add / bun add。

word-wrap 支持 TypeScript 吗?

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

word-wrap 支持 ESM 还是 CommonJS?

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

word-wrap 需要什么版本的 Node.js?

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

word-wrap 可以商用吗?

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

word-wrap 的下载量有多少?

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

word-wrap 还在维护吗?

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