Last active
April 1, 2016 11:50
-
-
Save karolk/24ee3c1223d62efb078f0b1ed10236b6 to your computer and use it in GitHub Desktop.
leftpad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function leftpad(str, length, char) { | |
if (typeof str === "undefined") throw new Error("Cound't find string to leftpad") | |
str = String(str) | |
length = length || 0 | |
if (typeof char === "undefined") char = " " | |
char = String(char) | |
var padding = leftpad._repeat(char, Math.max(length - str.length, 0)) | |
return padding + str | |
} | |
leftpad._repeat = function(str, count) { | |
return typeof str.repeat === "function" ? str.repeat(count) : new Array(count + 1).join(str) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Up for grabs!