Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,12 @@
function wrap (text, limit) {
if (text.length > limit) {
// find the last space within limit
var edge = text.slice(0, limit).lastIndexOf(' ');
if (edge > 0) {
var line = text.slice(0, edge);
var remainder = text.slice(edge + 1);
return line + '\n' + wrap(remainder, limit);
}
}
return text;
}

View file

@ -0,0 +1 @@
console.log(wrap(text, 80));

View file

@ -0,0 +1 @@
console.log(wrap(text, 42));

View file

@ -0,0 +1,23 @@
(function (width) {
'use strict';
function wrapByRegex(n, s) {
return s.match(
RegExp('.{1,' + n + '}(\\s|$)', 'g')
)
.join('\n');
}
return wrapByRegex(width,
'Even today, with proportional fonts and compl\
ex layouts, there are still cases where you ne\
ed to wrap text at a specified column. The bas\
ic task is to wrap a paragraph of text in a si\
mple way in your language. If there is a way t\
o do this that is built-in, trivial, or provid\
ed in a standard library, show that. Otherwise\
implement the minimum length greedy algorithm\
from Wikipedia.'
)
})(60);

View file

@ -0,0 +1,56 @@
/**
* [wordwrap description]
* @param {[type]} text [description]
* @param {Number} width [description]
* @param {String} br [description]
* @param {Boolean} cut [description]
* @return {[type]} [description]
*/
function wordwrap(text, width = 80, br = '\n', cut = false) {
// Приводим к uint
// 0..2^32-1 либо 0..2^64-1
width >>>= 0;
// Длина текста меньше или равна максимальной
if (0 === width || text.length <= width) {
return text;
}
// Разбиваем текст на строки
return text.split('\n').map(line => {
if (line.length <= width) {
return line;
}
// Разбиваем строку на слова
let words = line.split(' ');
// Если требуется, то обрезаем длинные слова
if (cut) {
let temp = [];
for (const word of words) {
if (word.length > width) {
let i = 0;
const length = word.length;
while (i < length) {
temp.push(word.slice(i, Math.min(i + width, length)));
i += width;
}
} else {
temp.push(word);
}
}
words = temp;
}
// console.log(words);
// Собираем новую строку
let wrapped = words.shift();
let spaceLeft = width - wrapped.length;
for (const word of words) {
if (word.length + 1 > spaceLeft) {
wrapped += br + word;
spaceLeft = width - word.length;
} else {
wrapped += ' ' + word;
spaceLeft -= 1 + word.length;
}
}
return wrapped;
}).join('\n'); // Объединяем элементы массива по LF
}

View file

@ -0,0 +1 @@
console.log(wordwrap("The quick brown fox jumped over the lazy dog.", 20, "<br />\n"));