Skip to content

Instantly share code, notes, and snippets.

@Arlen22
Last active September 14, 2023 18:49
Show Gist options
  • Save Arlen22/db6d65812dc4e7237bb3cf14983d8848 to your computer and use it in GitHub Desktop.
Save Arlen22/db6d65812dc4e7237bb3cf14983d8848 to your computer and use it in GitHub Desktop.
function leftpad(content, length, pad){
// make sure this is a proper string
content = String(content);
// get the pad character if set (or if zero)
pad = String(pad || pad === 0 ? pad : '')[0];
// get the extra length we need to add, but not less than zero if it's already longer
var left = Math.max(length-content.length, 0);
// voila!
return pad.repeat(left) + content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment