Disclaimer: A repost of my article for CafeDev

A while ago I needed a sim­ple string pad func­tion, and since it’s a freak­ishly sim­ple task, I just wrote a sim­ple
func­tion:

1
2
3
4
5
6
7
8
9
10
11
function limit(str, limit = 16, padString = "#", padPosition = "right") {
const strLength = str.length;
if (strLength > limit) {
return str.substring(0, limit);
} else if (strLength < limit) {
const padRepeats = padString.repeat(limit - strLength);
return (padPosition === "left") ? padRepeats + str : str + padRepeats;
}
return str;
}

Pretty sim­ple, right? And it works too… Until you add in an emoji to your text. Then every­thing falls apart and worlds
col­lide! How? Simple:

1
"💩".length // 2!

(If you’re read­ing this on Linux you might ac­tu­ally see 2 bro­ken uni­code char­ac­ters, and the re­sult makes sense un­less
you have in­stalled an emoji pack­age on your browser or OS)…

Read the full story on CafeDev →