Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created November 25, 2017 04: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 alextanhongpin/c105a6f037dffb4b5e942730bd3a021e to your computer and use it in GitHub Desktop.
Save alextanhongpin/c105a6f037dffb4b5e942730bd3a021e to your computer and use it in GitHub Desktop.
Remove trailing zeros, and increment value
// Map location to a range, based on the number of trailing zeroes.
// e.g. 50000 will become { from: 50000, to: 60000 }
// 50100 will become { from: 50100, to: 50200 }
// 10600 will become { from: 10600, to: 10700 }
export function mapLocation (code) {
const reverseString = s => s.split('').reverse().join('')
const str = code.toString()
const startLen = str.length
const trimmedZeros = reverseString(parseInt(reverseString(str), 10).toString())
const endLen = trimmedZeros.length
const multiplier = Math.pow(10, startLen - endLen)
const from = parseInt(str, 10)
const to = from + multiplier
return { from, to }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment