@unsudo/line-wrapper

1.0.0 • Public • Published

line-wrapper

Create an array of wrapped text lines of a certain width from a string. Convert text to an array of lines that can fit on a pre-determined width.

Usage

const { cleanText, wrapText } = require('@unsudo/line-wrapper');


// Remove EOL, NL, CR, starting and trailing whitespaces
const text = cleanText(' \
    This is a line with words. \r\n\
    Another line. \r\n\
    This is another line with words. \r\n'
);

// Custom function to measure width 
const measure = text => text.length;

// Maximum line width is 18 units
const wrapped = wrapText(text, 18, measure);

console.log(wrapped);
/**
[
    'This is a line ',
    'with words. ',
    'Another line. ', 
    'This is another ', 
    'line with words.  '
];
*/

Usage with Canvas API

const ctx = getCanvasCtx();
...

const wrapped = wrapText(
    text, 
    maxWidthInPx, 
    txt => ctx.measureText(txt).width
);

Installation

npm install @unsudo/line-wrapper

Package Sidebar

Install

npm i @unsudo/line-wrapper

Weekly Downloads

8

Version

1.0.0

License

ISC

Unpacked Size

5.87 kB

Total Files

4

Last publish

Collaborators

  • unsudo