1.0.0 • Published 9 years ago
break-string v1.0.0
break-string
Break string into lines according to visual width.
Some Unicode characters are fullwidth and use double the normal width. ANSI escape codes 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 - Get visual width of a string
- string-length - Get the real length of a string
Install
$ npm install --save break-string
Usage
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']