import {Route, RouteParameters} from './Route.ts' export class DeepmatchRoute extends Route { protected base_regex: RegExp constructor( protected base: string ) { super(base) this.base_regex = this.build_regex(this.split(base)) } public match(incoming: string): boolean { return this.base_regex.test(incoming.toLowerCase()) } public build_parameters(incoming: string): RouteParameters { const results = this.base_regex.exec(incoming.toLowerCase()) if ( !results ) return {} const [match, ...wildcards] = results const params: RouteParameters = {} let current_wildcard: number = 1 for ( const wild of wildcards ) { params[`$${current_wildcard}`] = wild current_wildcard += 1 } return params } protected build_regex(base_parts: string[]) { const deepmatch_group = '([a-zA-Z0-9\\-\\_\\.\\/]+)' // allows for alphanum, -, _, ., and / const shallowmatch_group = '([a-zA-Z0-9\\-\\.\\_]+)' // allows for alphanum, -, ., and _ const regex = base_parts.map(part => { return part.split('**').join(deepmatch_group).split('*').join(shallowmatch_group) }).join('\\/') return new RegExp(regex) } }