|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
794 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * @param {string} msg The message to wrap
|
|---|
| 5 | * @param {object} opts
|
|---|
| 6 | * @param {number|string} [opts.margin] Left margin
|
|---|
| 7 | * @param {number} opts.width Maximum characters per line including the margin
|
|---|
| 8 | */
|
|---|
| 9 | module.exports = (msg, opts = {}) => {
|
|---|
| 10 | const tab = Number.isSafeInteger(parseInt(opts.margin))
|
|---|
| 11 | ? new Array(parseInt(opts.margin)).fill(' ').join('')
|
|---|
| 12 | : (opts.margin || '');
|
|---|
| 13 |
|
|---|
| 14 | const width = opts.width;
|
|---|
| 15 |
|
|---|
| 16 | return (msg || '').split(/\r?\n/g)
|
|---|
| 17 | .map(line => line
|
|---|
| 18 | .split(/\s+/g)
|
|---|
| 19 | .reduce((arr, w) => {
|
|---|
| 20 | if (w.length + tab.length >= width || arr[arr.length - 1].length + w.length + 1 < width)
|
|---|
| 21 | arr[arr.length - 1] += ` ${w}`;
|
|---|
| 22 | else arr.push(`${tab}${w}`);
|
|---|
| 23 | return arr;
|
|---|
| 24 | }, [ tab ])
|
|---|
| 25 | .join('\n'))
|
|---|
| 26 | .join('\n');
|
|---|
| 27 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.