The string to break into string array
parameters to affect the output. Can be number or IStringBreakOpt If a number is passed in it becomes the width for the output. If no parameter is passed then output will be broken into string array with 80 characters per element.
let x: string[];
x = stringBreaker('some long text');
// is the same as
x = stringBreaker('some long text', 80),
// is the same as
x = stringBreaker('some long text' {width: 80});
Example:
import { stringBreaker } from 'string-breaker';
let x = stringBreaker('The quick brown fox jumped over the lazy dog', 5);
// x => ['The q','uick ','brown',' fox ','jumpe','d ove','r the',' lazy',' dog']
x = stringBreaker('Hello World\nNice ๐\nhmm... ', 5);
// x => ['Hello', ' Worl', 'dNice', ' ๐hmm', '... ']
x = stringBreaker('\uD83D\uDE07Hello World\nNice ๐\nhmm...', 6);
// x => ['๐Hello', ' World', 'Nice ๐', 'hmm...']
x = stringBreaker('\uD83D\uDE07Hello World\nNice ๐\r\nhmm...', {
width: 6,
lnEnd: lnEndOpt.encode
});
// x => ['๐Hello', ' World', '\\nNice', ' ๐\\nhm', 'm...']
Split by End of Line stringBreaker can split by eol by setting option lnEnd: lnEndOpt.splitByEol
Example Splitting by End of Line:
import { stringBreaker } from 'string-breaker';
// mixing \n and \r will result in the same output
let strSrc = 'Happy cat.';
strSrc += '\nThe quick brown fox jumped over the lazy dog.';
strSrc += '\r\nThe moon is full tonight.\rI like full moons!';
const x = stringBreaker(strSrc, { splitOpt: splitByOpt.line });
// x => [
// 'Happy cat.',
// 'The quick brown fox jumped over the lazy dog.',
// 'The moon is full tonight.',
// 'I like full moons!' ]
Example splitting by Word:
import { stringBreaker } from 'string-breaker';
// mixing \n and \r will result in the same output
let strSrc = 'Happy cat.';
strSrc += '\nThe quick brown\t\t fox jumped over the lazy dog.';
strSrc += '\r\nThe moon is full tonight.\rI like full moons!';
const x = stringBreaker(strSrc, { splitOpt: splitByOpt.word });
// x => [ 'Happy','cat.','The','quick','brown','fox','jumped',
// 'over','the','lazy','dog.','The','moon','is','full',
// 'tonight.','I','like','full','moons!' ]
Example split by width and preserve words
import { stringBreaker } from 'string-breaker';
var str = 'On this\u1680day.\u1680For this morning, when Gregor\u3000Samsa woke from troubled dreams; he found himself transformed.';
const result: string[] = stringBreaker(str, { width: 10, lenOpt: widthFlags.nearestWord });
const strResult = result.join('\n');
console.log(strResult);
Generated using TypeDoc
Breaks a string into a string array