what I am trying to do is have a text "box" that has a certain line length in width without cutting any words in half. what I am trying to do at the moment is cut the text into lines of n length, and then I am trying to repair words by knowing when the next space is. this is what I have at the moment but it does not fully work and only repairs some of the words
function TextBox(length,text){
array = [];
test = /[A-Za-z]+/g;
//cutting the text up
for (i= 0;i < Math.ceil(text.length / length); i ++){
array.push(text.slice(i * length,Math.min((i + 1) * length,text.length)));
}
//in case it ruins the lines the first time round
for (z = 0; z < 3; z++){
//reformatting the code
for (i = 0; i < array.length - 1; i ++){
if (test.test(array[i][array[i].length - 1])){
array[i] += array[i+1].substr(0,array[i+1].indexOf(' ') + 1);
array[i + 1] = array[i + 1].substr(array[i + 1].indexOf(' ') + 1);
}
}
}
//for debugging
console.log(array);
}
TextBox(5,"i like to eat cheese when I am hungry");
EDIT an example of an input is: "i like to eat cheese when I am hungry" and I want something like: [ "i like", "to eat", "cheese", "when I", "am hungry" ] what I am getting out right now is this: [ 'i like ', 'to ', 'eat c', notice the "c" from cheese 'heese', ' when ', 'I am ', 'hung', 'ry' ] and the "ry" from hungry
with the line length around n (5 in this example) characters long.
I have tried adding and removing the extra for loop but from what I can tell it helps to format it.
if you know what I am doing wrong, or an easier way to do this that would be great.
-
1Can you post a couple inputs and the expected output from each?CertainPerformance– CertainPerformance2018年12月07日 04:02:24 +00:00Commented Dec 7, 2018 at 4:02
-
did you mean is wrapping the text? As @CertainPerformance said, please add input and expected output.Fadhly Permata– Fadhly Permata2018年12月07日 04:04:47 +00:00Commented Dec 7, 2018 at 4:04
2 Answers 2
One option would be to construct a regular expression that alternates between 5 characters, or 4 characters, or 7 characters, etc:
..... // 5 characters
.... // 4 characters
...... // 6 characters
... // 3 characters
....... // 7 characters
And precede those alternations with
(?! )
to ensure that the matched line doesn't start with a space, and end the alternations with
(?= |$)
to ensure that the match ends right before a space (or the end of the string).
Then, all you have to do is execute the pattern .match on the input:
function TextBox(length,text){
const alternations = Array.from(
{ length: length * 2 - 1 },
(_, i) => {
const charCount = i % 2 === 0
? length - i / 2
: length + (i - 1) / 2 + 1;
return '.'.repeat(charCount);
}
);
// in case none of the above alternations matched, lazy-match characters
// until coming to a character followed by a space or the end of the string:
alternations.push('.+?');
const pattern = new RegExp('(?! )(?:' + alternations.join('|') + ')(?= |$)', 'g');
console.log(pattern);
return text.match(pattern);
}
console.log(TextBox(5,"i like to eat cheese when I am hungry"));
console.log(TextBox(8,"Lorem ipsum dolor sit amet, consectetur adipiscing 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"));
1 Comment
Here is another take using only Array split() and .reduce()
function TextBox(length,text){
var array = [];
text.split(' ').reduce(function(prev, cur) {
var next = [prev, cur].join(' ').trim();
if (next.length <= length) {
return next;
} else {
array.push(next);
return '';
}
});
console.log(array);
}
TextBox(5, 'i like to eat cheese when I am hungry');
TextBox(8, 'Lorem ipsum dolor sit amet, consectetur adipiscing 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum')