Skip to content

Instantly share code, notes, and snippets.

@wescleymatos
Created July 9, 2013 12:42
Show Gist options
  • Save wescleymatos/5957048 to your computer and use it in GitHub Desktop.
Save wescleymatos/5957048 to your computer and use it in GitHub Desktop.
function str_pad (input, pad_length, pad_string, pad_type) {
// Returns input string padded on the left or right to specified length with pad_string
//
// version: 1009.2513
// discuss at: http://phpjs.org/functions/str_pad
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + namespaced by: Michael White (http://getsprink.com)
// + input by: Marco van Oort
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: str_pad('Kevin van Zonneveld', 30, '-=', 'STR_PAD_LEFT');
// * returns 1: '-=-=-=-=-=-Kevin van Zonneveld'
// * example 2: str_pad('Kevin van Zonneveld', 30, '-', 'STR_PAD_BOTH');
// * returns 2: '------Kevin van Zonneveld-----'
var half = '', pad_to_go;
var str_pad_repeater = function (s, len) {
var collect = '', i;
while (collect.length < len) {collect += s;} collect = collect.substr(0,len); return collect; }; input += ''; pad_string = pad_string !== undefined ? pad_string : ' '; if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; } if ((pad_to_go = pad_length - input.length) > 0) {
if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
else if (pad_type == 'STR_PAD_BOTH') {
half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
input = half + input + half;
input = input.substr(0, pad_length);
}
}
return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment