mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
26 lines
728 B
TypeScript
26 lines
728 B
TypeScript
|
import { getPathFromURL } from '../utils/url.ts'
|
||
|
|
||
|
export const mergePath = (base: string, path: string) => {
|
||
|
base = base.replace(/\/+$/, '')
|
||
|
base = base + '/'
|
||
|
path = path.replace(/^\/+/, '')
|
||
|
return base + path
|
||
|
}
|
||
|
|
||
|
export const replaceUrlParam = (urlString: string, params: Record<string, string>) => {
|
||
|
for (const [k, v] of Object.entries(params)) {
|
||
|
const reg = new RegExp('/:' + k)
|
||
|
urlString = urlString.replace(reg, `/${v}`)
|
||
|
}
|
||
|
return urlString
|
||
|
}
|
||
|
|
||
|
export const removeIndexString = (urlSting: string) => {
|
||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||
|
const [path, _] = getPathFromURL(urlSting)
|
||
|
if (path === '/index') {
|
||
|
return urlSting.replace(/index$/, '')
|
||
|
}
|
||
|
return urlSting
|
||
|
}
|