https://github.com/bubkoo/break-string
Break string into lines according to visual width
https://github.com/bubkoo/break-string
Last synced: over 1 year ago
JSON representation
Break string into lines according to visual width
- Host: GitHub
- URL: https://github.com/bubkoo/break-string
- Owner: bubkoo
- License: mit
- Created: 2016-04-05T06:26:04.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-21T02:30:28.000Z (about 9 years ago)
- Last Synced: 2025-03-17T15:18:46.963Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# break-string
> Break string into lines according to visual width.
[](https://github.com/bubkoo/break-string/blob/master/LICENSE)
[](https://travis-ci.org/bubkoo/break-string)
[](https://coveralls.io/github/bubkoo/break-string)
Some Unicode characters are [fullwidth](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms) and use double the normal width. [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code) are stripped and doesn't affect the width.
Useful to be able to break the string into lines according to visual width(the number of columns) in the terminal.
Inspired by:
- [string-width](https://github.com/sindresorhus/string-width) - Get visual width of a string
- [string-length](https://github.com/sindresorhus/string-length) - Get the real length of a string
## Install
```
$ npm install --save break-string
```
## Usage
```js
var breakString = require('break-string');
// normal string
breakString('abcdefg', 4);
// => ['abcd', 'efg']
// with line break
breakString('abcd\nefg', 3);
// => ['abc', 'd', 'efg']
breakString('abcd\nefg', 4);
// => ['abcd', 'efg']
breakString('abcd\nefg', 5);
// => ['abcd', 'efg']
// with astral symbols
breakString('ađŽbcde', 4);
// => ['ađŽbc', 'de']
// with double with char
breakString('ać€bcde', 4);
// => ['ać€b', 'cde']
// with ansi code
breakString('uni\u001b[22mcorn', 4);
// => ['uni\u001b[22mc', 'orn']
```