Skip to content

Instantly share code, notes, and snippets.

@ChrisDobby
Last active December 29, 2022 11:16
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 ChrisDobby/885ee7465efd7c08e672687878452766 to your computer and use it in GitHub Desktop.
Save ChrisDobby/885ee7465efd7c08e672687878452766 to your computer and use it in GitHub Desktop.
const addZerosCount = ({ str, zeroCount }: { str: string; zeroCount: number }) => `${str}${zeroCount || ''}`
const replaceZeros = (strIncludingZeros: string) =>
addZerosCount(
strIncludingZeros.split('').reduce((acc, ch) => (ch === '0' ? { ...acc, zeroCount: (acc.zeroCount += 1) } : { str: `${addZerosCount(acc)}${ch}`, zeroCount: 0 }), {
str: '',
zeroCount: 0,
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment