Created
June 14, 2020 21:27
-
-
Save Jabher/08d30df93d6683bb7961115c3462a9f0 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
function router (method: string, path: string) { | |
const chunks = path.split('/') | |
let endpoints = compiledTrieMap | |
let children = endpoints.children | |
const chunksLength = chunks.length | |
let i = 0 | |
while (true) { | |
const chunk = chunks[i] | |
if (children.has(chunk)) { | |
endpoints = children.get(chunk) | |
} else if (children.has(':')) { | |
endpoints = children.get(':') | |
} else if (children.has('*')) { | |
children.get('*').handler(chunks) | |
return | |
} else { | |
return | |
} | |
children = endpoints.children | |
if (++i === chunksLength) { | |
const value = endpoints.handler | |
if (value instanceof Function) { | |
value(chunks) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment