mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-17 00:59:29 -07:00
fix word wrap
This commit is contained in:
@@ -136,33 +136,98 @@ const utils = (() => {
|
||||
const wrap = (x, m) => ((x % m) + m) % m;
|
||||
|
||||
// ********************************* strings *********************************************
|
||||
const wordWrap = (str = '', ...rest) => {
|
||||
const m = ((rest.length >= 1) ? rest[0] : 75);
|
||||
const b = ((rest.length >= 2) ? rest[1] : '\n');
|
||||
const c = ((rest.length >= 3) ? rest[2] : false);
|
||||
const wordWrap = (_str, ...rest) => {
|
||||
// discuss at: https://locutus.io/php/wordwrap/
|
||||
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
|
||||
// improved by: Nick Callen
|
||||
// improved by: Kevin van Zonneveld (https://kvz.io)
|
||||
// improved by: Sakimori
|
||||
// revised by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
|
||||
// bugfixed by: Michael Grier
|
||||
// bugfixed by: Feras ALHAEK
|
||||
// improved by: Rafał Kukawski (https://kukawski.net)
|
||||
// example 1: wordwrap('Kevin van Zonneveld', 6, '|', true)
|
||||
// returns 1: 'Kevin|van|Zonnev|eld'
|
||||
// example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n')
|
||||
// returns 2: 'The quick brown fox<br />\njumped over the lazy<br />\ndog.'
|
||||
// example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.')
|
||||
// returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\ncommodo consequat.'
|
||||
const intWidth = rest[0] ?? 75;
|
||||
const strBreak = rest[1] ?? '\n';
|
||||
const cut = rest[2] ?? false;
|
||||
|
||||
let i;
|
||||
let j;
|
||||
let l;
|
||||
let s;
|
||||
let r;
|
||||
let line;
|
||||
|
||||
if (m < 1) {
|
||||
let str = _str;
|
||||
str += '';
|
||||
|
||||
if (intWidth < 1) {
|
||||
return str;
|
||||
}
|
||||
|
||||
for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
|
||||
for (s = r[i], r[i] = '';
|
||||
s.length > m;
|
||||
r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : '')) {
|
||||
j = c === 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1]
|
||||
? m
|
||||
: ((j.input.length - j[0].length || c === true) && m)
|
||||
|| j.input.length + (j = s.slice(m).match(/^\S*/))[0].length;
|
||||
const reLineBreaks = /\r\n|\n|\r/;
|
||||
const reBeginningUntilFirstWhitespace = /^\S*/;
|
||||
const reLastCharsWithOptionalTrailingWhitespace = /\S*(\s)?$/;
|
||||
|
||||
const lines = str.split(reLineBreaks);
|
||||
const l = lines.length;
|
||||
let match;
|
||||
|
||||
// for each line of text
|
||||
// eslint-disable-next-line no-plusplus
|
||||
for (i = 0; i < l; lines[i++] += line) {
|
||||
line = lines[i];
|
||||
lines[i] = '';
|
||||
|
||||
while (line.length > intWidth) {
|
||||
// get slice of length one char above limit
|
||||
const slice = line.slice(0, intWidth + 1);
|
||||
|
||||
// remove leading whitespace from rest of line to parse
|
||||
let ltrim = 0;
|
||||
// remove trailing whitespace from new line content
|
||||
let rtrim = 0;
|
||||
|
||||
match = slice.match(reLastCharsWithOptionalTrailingWhitespace);
|
||||
|
||||
// if the slice ends with whitespace
|
||||
if (match[1]) {
|
||||
// then perfect moment to cut the line
|
||||
j = intWidth;
|
||||
ltrim = 1;
|
||||
} else {
|
||||
// otherwise cut at previous whitespace
|
||||
j = slice.length - match[0].length;
|
||||
|
||||
if (j) {
|
||||
rtrim = 1;
|
||||
}
|
||||
|
||||
// but if there is no previous whitespace
|
||||
// and cut is forced
|
||||
// cut just at the defined limit
|
||||
if (!j && cut && intWidth) {
|
||||
j = intWidth;
|
||||
}
|
||||
|
||||
// if cut wasn't forced
|
||||
// cut at next possible whitespace after the limit
|
||||
if (!j) {
|
||||
const charsUntilNextWhitespace = (line.slice(intWidth).match(reBeginningUntilFirstWhitespace) || [''])[0];
|
||||
|
||||
j = slice.length + charsUntilNextWhitespace.length;
|
||||
}
|
||||
}
|
||||
|
||||
lines[i] += line.slice(0, j - rtrim);
|
||||
line = line.slice(j + ltrim);
|
||||
lines[i] += line.length ? strBreak : '';
|
||||
}
|
||||
}
|
||||
|
||||
return r.join('\n').replace(/\n /g, '\n');
|
||||
return lines.join('\n');
|
||||
};
|
||||
// ********************************* cors ********************************************
|
||||
// rewrite some urls for local server
|
||||
|
||||
Reference in New Issue
Block a user