Last active
August 29, 2015 14:21
-
-
Save vitormil/c1728cb20130fdbdc700 to your computer and use it in GitHub Desktop.
pads the side of a string with a specific set of characters
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 pad (text, padded_length, pad_string, pad_direction) { | |
pad_string = pad_string || " "; | |
text = text + ""; | |
if (text.length >= padded_length) { | |
return text; | |
} | |
var pad_content = new Array(padded_length - text.length + 1).join(pad_string); | |
if (!pad_direction || pad_direction === "left") { | |
return pad_content + text; | |
} else { | |
return text + pad_content; | |
} | |
} | |
function lpad (text, padded_length, pad_string) { | |
return pad(text, padded_length, pad_string, "left"); | |
} | |
function rpad (text, padded_length, pad_string) { | |
return pad(text, padded_length, pad_string, "right"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment