Phil Andelhofs Phil Andelhofs - 6 months ago 14
Javascript Question

Break long string at specif char and max length (JS)

Probably there is an answser much cleaner and more working than mine, but i cannot find. So i did try for myself with not much result so far.



var str = "Persons: Amy, bertha, Charlie, Donkey, Evy, Felicia, Ghunter, Hercules, Indica, Jody, Katy, Linsay, Moony, Nigel, Opethry, Phil, Quinton, Ricial, Stefany, Trudy, Ursla, Vlinder, Wendy, Xion, Yvy, Zulu"; // person name come from an Array()

var maxCount = 100;
var jki = Math.floor(str.length / maxCount);
var i = 0;
arr = [];
var startPoint = 0;
while (i <= jki) {
i++;
arr.push(str.slice(startPoint, str.slice((i * maxCount) - maxCount, i * maxCount).lastIndexOf(" ") - 1))
startPoint = str.slice((i * maxCount) - maxCount, i * maxCount).lastIndexOf(" ");
}
console.log(arr);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





This way i get things like the first is good, second is empty ??, next is wrecked :-s :

[
"Persons: Amy, bertha, Charlie, Donkey, Evy, Felicia, Ghunter, Hercules, Indica, Jody, Katy, Linsay",
"",
"say, Moony, Nigel, Opethry, Phil, Quinton, Ricial, Stefany, Trudy, Ursla, Vlinder, Wendy, Xion, Yvy, Zu"
]


Edit

What i looking for is that str is maximum 100 char in length and names are full written.

[
"Persons: Amy, bertha, Charlie, Donkey, Evy, Felicia, Ghunter, Hercules, Indica, Jody, Katy, Linsay,",
"Moony, Nigel, Opethry, Phil, Quinton, Ricial, Stefany, Trudy, Ursla, Vlinder, Wendy, Xion, Yvy, Zulu"
]


In this example
arr[0].length // 99
,
arr[1].length // 100


If we would add a new name to list example
ZZ-top


[
"Persons: Amy, bertha, Charlie, Donkey, Evy, Felicia, Ghunter, Hercules, Indica, Jody, Katy, Linsay,",
"Moony, Nigel, Opethry, Phil, Quinton, Ricial, Stefany, Trudy, Ursla, Vlinder, Wendy, Xion, Yvy,",
"Zulu, ZZ-top"
]


In this example
arr[0].length // 99
,
arr[1].length // 95
,
arr[2].length // 12


"Zulu," would made arr[2].length // 101 so moving over to next (as is variable maxCount = 100)


Can someone see what i am missing ?

Thanks in advance!

Answer

So I took the time to actually make it work according to your criteria. All the steps are explained in the comments.

var str = "Persons: Amy, bertha, Charlie, Donkey, Evy, Felicia, Ghunter, Hercules, Indica, Jody, Katy, Linsay, Moony, Nigel, Opethry, Phil, Quinton, Ricial, Stefany, Trudy, Ursla, Vlinder, Wendy, Xion, Yvy, Zulu";
var maxCount = 100;
var splitNames = function(str, max){
    // ["person:","Amy,","bertha,",...]
    var names = str.split(' '),
        lines = [],
        line = 0,
        lineLength = 0;

    // loop through our array of names
    names.forEach(function(name){
        // if length is more than our max
        if(lineLength + name.length + 1 > max){
            line += 1; // move to next line
            lineLength = 0; // reset length counter
        } else {
            lineLength += 1; // add 1 for the space we'll add later
        }
        // make sure we add an empty array if there isn't one
        lines[line] = lines[line] || [];
        
        // add the current name
        lines[line].push(name);
        
        // add name length to the line length
        lineLength += name.length;
    });
    
    return lines.map(function(line){
        // join our names with a space (which we counted toward the length)
        return line.join(' ');
    });
}
console.log(splitNames(str, maxCount));