import * as Config from 'config' export type Route = { name: 'form', config: Config.Config } | { name: 'timer', config: Config.Config } export function from(location: Location): Route { const hash = location.hash.slice(1) const parts = hash.split('?') const path = parts[0] const search = parts.length > 1 ? parts[1] : '' const name = path.startsWith('/timer') ? 'timer' : 'form' let config = Config.init() if (search.length > 0) { search.split('&').forEach(entry => { const xs = entry.split('=') if (xs.length === 2) { const key = xs[0] const value = parseInt(xs[1]) if (key == 'warmup') config.warmup = value else if (key == 'tabatas') config.tabatas = value else if (key == 'prepare') config.prepare = value else if (key == 'cycles') config.cycles = value else if (key == 'work') config.work = value else if (key == 'rest') config.rest = value } }) const params = search.split('&') } return { name, config } } export function toString(route: Route): string { const path = route.name === 'form' ? '/' : '/timer' let query = '' if (route.config !== undefined) { const { warmup, tabatas, prepare, cycles, work, rest } = route.config const params = [ `warmup=${warmup}`, `tabatas=${tabatas}`, `prepare=${prepare}`, `cycles=${cycles}`, `work=${work}`, `rest=${rest}`, ].join('&') query = `?${params}` } return `#${path}${query}` }