Skip to content

Instantly share code, notes, and snippets.

@callerobertsson
Created March 13, 2015 13:03
Show Gist options
  • Save callerobertsson/65b0eaefa1c728a1641f to your computer and use it in GitHub Desktop.
Save callerobertsson/65b0eaefa1c728a1641f to your computer and use it in GitHub Desktop.
Javascript: Pad string but don't truncate if too long
// Javascript Pad String
// Inspired by http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/
function pad(filling, value) {
if (('' + value).length > filling.length) {
return value;
}
return (filling + value).slice(-1 * filling.length);
}
console.log(pad('0000000000', 123456789));
console.log(pad('xx', 'monkey'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment