Skip to content

Instantly share code, notes, and snippets.

@element6
Last active October 14, 2018 13:08
Show Gist options
  • Save element6/61f9ce16f5d3ce20bc635181c882cac9 to your computer and use it in GitHub Desktop.
Save element6/61f9ce16f5d3ce20bc635181c882cac9 to your computer and use it in GitHub Desktop.
padding function
function padZeros(str, pad = '00') {
const padding = typeof pad === 'number' ? Array(pad).fill('0').join('') : pad // '0'.repeat(pad) is faster but need polyfill for IE
return (padding + str).slice(-padding.length)
}
padZeros(2) // '02'
padZeros(2, '000') // '002'
padZeros(234) // '34'
padZeros(1, 4) // '0001' second argument can be a number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment