From 221b6451fb4f8559a10e7fefebd13ce125ef29d0 Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 13 May 2021 14:50:51 +0200 Subject: Rewrite in TypeScript BuckleScript is no longer maintained. Choose a widely used techno that will still be maintained in the following years. --- src/router.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/router.ts (limited to 'src/router.ts') diff --git a/src/router.ts b/src/router.ts new file mode 100644 index 0000000..abbdc65 --- /dev/null +++ b/src/router.ts @@ -0,0 +1,55 @@ +import * as Config from 'config' + +export enum Kind { + Form, + Timer, +} + +export interface Route { + kind: Kind, + 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 kind = path.startsWith('/timer') ? Kind.Timer : Kind.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 == 'prepare') config.prepare = value + else if (key == 'tabatas') config.tabatas = 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 { kind, config } +} + +export function toString(route: Route): string { + const path = route.kind === Kind.Form ? '/' : '/timer' + let query = '' + if (route.config !== undefined) { + const { prepare, tabatas, cycles, work, rest } = route.config + const params = [ + `prepare=${prepare}`, + `tabatas=${tabatas}`, + `cycles=${cycles}`, + `work=${work}`, + `rest=${rest}`, + ].join('&') + query = `?${params}` + } + return `#${path}${query}` +} -- cgit v1.2.3