Created
November 25, 2017 04:16
-
-
Save alextanhongpin/c105a6f037dffb4b5e942730bd3a021e to your computer and use it in GitHub Desktop.
Remove trailing zeros, and increment value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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