Created
July 10, 2020 08:55
-
-
Save namnv609/e7df884fcb086d631600ddb79c87c433 to your computer and use it in GitHub Desktop.
Simple string padding for JS (like Ruby ljust and rjust)
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
String.prototype.rJust = function(padLen, padStr) { | |
return (new Array(padLen).join(padStr) + this.valueOf()).slice(-padLen); | |
}; | |
String.prototype.lJust = function(padLen, padStr) { | |
return (this.valueOf() + new Array(padLen).join(padStr)).substring(0, padLen); | |
}; | |
/** | |
* > "6969".lJust(10, "0") | |
* < "6969000000" | |
* > "6969".rJust(10, "0") | |
* < "0000006969" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment