Skip to content

Instantly share code, notes, and snippets.

@Jabher
Created June 14, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jabher/08d30df93d6683bb7961115c3462a9f0 to your computer and use it in GitHub Desktop.
Save Jabher/08d30df93d6683bb7961115c3462a9f0 to your computer and use it in GitHub Desktop.
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