Skip to content

Commit eabb2df

Browse files
committed
fix: route utilities when using routeBase
1 parent 2dd7391 commit eabb2df

File tree

2 files changed

+78
-20
lines changed

2 files changed

+78
-20
lines changed

src/utils/route.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,27 @@ export function withSuffix(string: string, suffix: string) {
1212
return string.endsWith(suffix) ? string : string + suffix
1313
}
1414
export function withoutSuffix(string: string, suffix: string) {
15-
return string.endsWith(suffix)
16-
? string.slice(0, -1 * suffix.length)
17-
: string + suffix
15+
return string.endsWith(suffix) ? string.slice(0, -1 * suffix.length) : string
1816
}
1917

20-
export function createUrl(urlLike: string | URL) {
21-
if (urlLike instanceof URL) {
22-
return urlLike
23-
}
24-
25-
if (!(urlLike || '').includes('://')) {
18+
export function createUrl(urlLike: string | URL | Location) {
19+
if (typeof urlLike === 'string' && !(urlLike || '').includes('://')) {
2620
urlLike = 'http://e.g' + withPrefix(urlLike, S)
2721
}
2822

29-
return new URL(urlLike)
30-
}
31-
32-
export function joinPaths(...paths: string[]) {
33-
return paths.reduce((acc, path) => acc + path, '').replace(/\/\//g, S)
23+
return new URL(urlLike.toString())
3424
}
3525

3626
export function getFullPath(url: string | URL | Location, routeBase?: string) {
37-
url = typeof url === 'string' ? createUrl(url) : url
27+
url = createUrl(url)
28+
url.pathname = withSuffix(url.pathname, S)
3829
let fullPath = withoutPrefix(url.href, url.origin)
3930

4031
if (routeBase) {
41-
const parts = fullPath.split(S)
42-
if (parts[1] === routeBase.replace(/\//g, '')) {
43-
parts.splice(1, 1)
32+
routeBase = withSuffix(withPrefix(routeBase, S), S)
33+
if (fullPath.indexOf(routeBase) === 0) {
34+
fullPath = withPrefix(fullPath.replace(routeBase, ''), S)
4435
}
45-
46-
fullPath = parts.join(S)
4736
}
4837

4938
return fullPath

test/specs/utils/route.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { test } from 'uvu'
2+
import * as assert from 'uvu/assert'
3+
import {
4+
withPrefix,
5+
withoutPrefix,
6+
withSuffix,
7+
withoutSuffix,
8+
createUrl,
9+
getFullPath,
10+
} from '../../../src/utils/route'
11+
12+
test('withPrefix', () => {
13+
assert.is(withPrefix('/my/path', '/'), '/my/path')
14+
assert.is(withPrefix('my/path', '/'), '/my/path')
15+
assert.is(withPrefix('/my/path', '/my'), '/my/path')
16+
assert.is(withPrefix('/path', '/my'), '/my/path')
17+
})
18+
19+
test('withoutPrefix', () => {
20+
assert.is(withoutPrefix('/my/path', '/'), 'my/path')
21+
assert.is(withoutPrefix('my/path', '/'), 'my/path')
22+
assert.is(withoutPrefix('/my/path', '/my'), '/path')
23+
assert.is(withoutPrefix('/path', '/my'), '/path')
24+
})
25+
26+
test('withSuffix', () => {
27+
assert.is(withSuffix('/my/path', '/'), '/my/path/')
28+
assert.is(withSuffix('/my/path/', '/'), '/my/path/')
29+
assert.is(withSuffix('/my', '/path'), '/my/path')
30+
assert.is(withSuffix('/my/path', '/path'), '/my/path')
31+
})
32+
33+
test('withoutSuffix', () => {
34+
assert.is(withoutSuffix('/my/path', '/'), '/my/path')
35+
assert.is(withoutSuffix('/my/path/', '/'), '/my/path')
36+
assert.is(withoutSuffix('/my/path', '/path'), '/my')
37+
assert.is(withoutSuffix('/my', '/path'), '/my')
38+
})
39+
40+
test('createUrl', () => {
41+
assert.is(
42+
createUrl(new URL('http://e.g/my/path')).toString(),
43+
'http://e.g/my/path'
44+
)
45+
46+
assert.is(createUrl('http://e.g/my/path').toString(), 'http://e.g/my/path')
47+
assert.is(createUrl('/my/path').toString(), 'http://e.g/my/path')
48+
assert.is(createUrl('/my/path?query').toString(), 'http://e.g/my/path?query')
49+
})
50+
51+
test('getFullPath', () => {
52+
assert.is(getFullPath(new URL('http://e.g/my/path')), '/my/path/')
53+
assert.is(getFullPath('/my/path'), '/my/path/')
54+
assert.is(getFullPath('/my/path?query'), '/my/path/?query')
55+
56+
assert.is(getFullPath('/my/path', '/my'), '/path/')
57+
assert.is(getFullPath('/my/path/', '/my'), '/path/')
58+
assert.is(getFullPath('/my/path?query', '/my'), '/path/?query')
59+
60+
assert.is(getFullPath('/my/', '/my'), '/')
61+
assert.is(getFullPath('/my', '/my'), '/')
62+
assert.is(getFullPath('/my/?query', '/my'), '/?query')
63+
assert.is(getFullPath('/my?query', '/my'), '/?query')
64+
65+
assert.is(getFullPath('/my/deep/path', '/my/deep'), '/path/')
66+
assert.is(getFullPath('/my/deepest/path', '/my/deep'), '/my/deepest/path/')
67+
})
68+
69+
test.run()

0 commit comments

Comments
 (0)