Skip to content

Instantly share code, notes, and snippets.

@markshust
Created May 7, 2012 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markshust/2625581 to your computer and use it in GitHub Desktop.
Save markshust/2625581 to your computer and use it in GitHub Desktop.
jquery str_pad
$.strPad = function(input, pad_length, pad_string, pad_type){
var output = input.toString();
if (pad_string === undefined) { pad_string = ' '; }
if (pad_type === undefined) { pad_type = 'STR_PAD_RIGHT'; }
if (pad_type == 'STR_PAD_RIGHT') {
while (output.length < pad_length) {
output = output + pad_string;
}
} else if (pad_type == 'STR_PAD_LEFT') {
while (output.length < pad_length) {
output = pad_string + output;
}
} else if (pad_type == 'STR_PAD_BOTH') {
var j = 0;
while (output.length < pad_length) {
if (j % 2) {
output = output + pad_string;
} else {
output = pad_string + output;
}
j++;
}
}
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment