Skip to content

Instantly share code, notes, and snippets.

@jdfm
Last active September 5, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdfm/d77ea54c45087c70c057a0a71e167138 to your computer and use it in GitHub Desktop.
Save jdfm/d77ea54c45087c70c057a0a71e167138 to your computer and use it in GitHub Desktop.
ES6 Exploration: leftPad for natural numbers
const leftPad = (
( input=0, minOutputLength=0 ) =>
String( input ).replace( /^(\d+)$/, ( match, $1='' ) =>
'0'.repeat( Math.max( minOutputLength - $1.length, 0 ) ) + $1
)
)
console.log(leftPad(10000, 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment