Created
January 12, 2010 15:00
-
-
Save livingston/275259 to your computer and use it in GitHub Desktop.
pads string with specified character, with defined length
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
/* padString.js - pads string with specified character, with defined length | |
* @author - Livingston Samuel | |
*/ | |
var padString = function (str, char, len) { | |
if (arguments.length !== 3 || String(char).length !== '' || str.length >= len) { | |
return str; | |
} | |
var l = str.length; | |
while (l++ < len) { | |
str = char + str; | |
} | |
return str; | |
}; |
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
/* String.pad.js - String prototype method to pad string with specified character, with defined length | |
* @author - Livingston Samuel | |
*/ | |
String.prototype.pad = function (char, len) { | |
var str = this, l = str.length; | |
if (arguments.length !== 2 || String(char).length !== 1 || str.length >= len) { | |
return str; | |
} | |
while (l++ < len) { | |
str = char + str; | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment