Sallar Kaboli / Exploring JavaScript & Things.

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: 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

red more