Files
tuono/packages/router/src/utils.ts
T
Valerio Ageno 75c790dd6e refactor: create standalone router package (#22)
* refactor: create standalone router package

* fix: add jsdom to tuono-router package

* fix: remove test in tuono vite.config

* update turbo to latest

* fix: wait build before lint

* fix: router types
2024-08-11 17:14:54 +02:00

78 lines
1.6 KiB
TypeScript

import type { Segment } from './types'
export function joinPaths(paths: (string | undefined)[]): string {
return cleanPath(paths.filter(Boolean).join('/'))
}
function cleanPath(path: string): string {
// remove double slashes
return path.replace(/\/{2,}/g, '/')
}
export function parsePathname(pathname?: string): Segment[] {
if (!pathname) {
return []
}
pathname = cleanPath(pathname)
const segments: Segment[] = []
if (pathname.slice(0, 1) === '/') {
pathname = pathname.substring(1)
segments.push({
type: 'pathname',
value: '/',
})
}
if (!pathname) {
return segments
}
// Remove empty segments and '.' segments
const split = pathname.split('/').filter(Boolean)
segments.push(
...split.map((part): Segment => {
if (part === '$' || part === '*') {
return {
type: 'wildcard',
value: part,
}
}
if (part.charAt(0) === '$') {
return {
type: 'param',
value: part,
}
}
return {
type: 'pathname',
value: part,
}
}),
)
if (pathname.slice(-1) === '/') {
pathname = pathname.substring(1)
segments.push({
type: 'pathname',
value: '/',
})
}
return segments
}
export function trimPathLeft(path: string): string {
return path === '/' ? path : path.replace(/^\/{1,}/, '')
}
export function trimPathRight(path: string): string {
return path === '/' ? path : path.replace(/\/{1,}$/, '')
}
export function trimPath(path: string): string {
return trimPathRight(trimPathLeft(path))
}