Skip to content

Instantly share code, notes, and snippets.

@fliptheweb
Last active August 21, 2018 11:19
Show Gist options
  • Save fliptheweb/47139596e712c48971e61e30ecc7b351 to your computer and use it in GitHub Desktop.
Save fliptheweb/47139596e712c48971e61e30ecc7b351 to your computer and use it in GitHub Desktop.
const capitalizeExeptions = ['dc']
export const capitalizeCityName = string => {
if (!string) return ''
string = string.toLowerCase()
if (string.includes(' ')) {
return string
.split(' ')
.reduce((result, word) => `${result && `${result} `}${capitalizeCityName(word)}`, '')
}
if (capitalizeExeptions.includes(string)) {
return string.toUpperCase()
}
const [first, ...rest] = string
return first.toUpperCase() + rest.join('')
}
export const decodeCityPath = cityPath => cityPath.replace(/_/g, ' ').toLowerCase()
export const parseRouteUrl = path => {
if (!/[a-z_!,]+-[a-z_!,]+-\d{6}/.test(path)) return null
const parts = path.split('-')
let date = parts[parts.length - 1]
date = new Date(`${date.slice(0, 2)}/${date.slice(2, 4)}/${date.slice(4)}`)
if (!isValid(date)) return null
return {
from: capitalizeCityName(decodeCityPath(parts[0])),
to: capitalizeCityName(decodeCityPath(parts[1])),
date,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment