Skip to content

Commit

Permalink
fix(withoutBase): preserve leading slash
Browse files Browse the repository at this point in the history
resolves #64
  • Loading branch information
pi0 committed Jul 7, 2022
1 parent cbdda7a commit fc72dd0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/utils.ts
Expand Up @@ -77,10 +77,11 @@ export function withoutBase (input: string, base: string) {
return input
}
const _base = withoutTrailingSlash(base)
if (input.startsWith(_base)) {
return input.substr(_base.length) || '/'
if (!input.startsWith(_base)) {
return input
}
return input
const trimmed = input.substring(_base.length)
return trimmed[0] === '/' ? trimmed : ('/' + trimmed)
}

export function withQuery (input: string, query: QueryObject): string {
Expand Down
4 changes: 3 additions & 1 deletion test/base.test.ts
Expand Up @@ -31,7 +31,9 @@ describe('withoutBase', () => {
{ base: '/base', input: '/base/a', out: '/a' },
{ base: '/base/', input: '/base/a', out: '/a' },
{ base: '/base/a/', input: '/base/a', out: '/' },
{ base: '/', input: '/test/', out: '/test/' }
{ base: '/', input: '/test/', out: '/test/' },
{ base: '/', input: '/?test', out: '/?test' },
{ base: '/api', input: '/api?test', out: '/?test' }
]

for (const t of tests) {
Expand Down

0 comments on commit fc72dd0

Please sign in to comment.