Created
August 29, 2013 12:50
-
-
Save guigmaster/6377657 to your computer and use it in GitHub Desktop.
function str_pad javascript
This file contains 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 str_pad (input, pad_length, pad_string, pad_type) { | |
// * example 1: str_pad('', 30, '-=', 'STR_PAD_LEFT'); | |
// * returns 1: '-=-=-=-=-=-foo bar milk' | |
// * example 2: str_pad('foo bar milk', 30, '-', 'STR_PAD_BOTH'); | |
// * returns 2: '------foo bar milk-----' | |
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