Skip to content

Instantly share code, notes, and snippets.

@vitormil
Last active August 29, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitormil/c1728cb20130fdbdc700 to your computer and use it in GitHub Desktop.
Save vitormil/c1728cb20130fdbdc700 to your computer and use it in GitHub Desktop.
pads the side of a string with a specific set of 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