Skip to content

Instantly share code, notes, and snippets.

@namnv609
Created July 10, 2020 08:55
Show Gist options
  • Save namnv609/e7df884fcb086d631600ddb79c87c433 to your computer and use it in GitHub Desktop.
Save namnv609/e7df884fcb086d631600ddb79c87c433 to your computer and use it in GitHub Desktop.
Simple string padding for JS (like Ruby ljust and rjust)
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