Last active
August 21, 2018 11:19
-
-
Save fliptheweb/47139596e712c48971e61e30ecc7b351 to your computer and use it in GitHub Desktop.
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
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('') | |
} |
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
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