Last active
December 20, 2015 10:49
-
-
Save markshust/6118500 to your computer and use it in GitHub Desktop.
jquery.strpad.js
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 that imitates PHP's str_pad. | |
| * | |
| * @param input string | |
| * @param padLength string | |
| * @param padString string | |
| * @param padType string | |
| * @returns string | |
| */ | |
| jQuery.strPad = function(input, padLength, padString, padType) { | |
| if (input == undefined) input = '' | |
| var output = input.toString() | |
| if (padString == undefined) padString = ' ' | |
| if (padType === undefined) padType = 'STR_PAD_RIGHT' | |
| if (padType == 'STR_PAD_RIGHT') { | |
| while (output.length < padLength) output = output + padString | |
| } else if (padType == 'STR_PAD_LEFT') { | |
| while (output.length < padLength) output = padString + output | |
| } else if (padType == 'STR_PAD_BOTH') { | |
| var j = 0 | |
| while (output.length < padLength) { | |
| if (j % 2) { | |
| output = output + padString | |
| } else { | |
| output = padString + output | |
| } | |
| j++ | |
| } | |
| } | |
| return output | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment