Skip to content

Commit

Permalink
fix: route utilities when using routeBase
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox committed Dec 31, 2021
1 parent 2dd7391 commit eabb2df
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,27 @@ export function withSuffix(string: string, suffix: string) {
return string.endsWith(suffix) ? string : string + suffix
}
export function withoutSuffix(string: string, suffix: string) {
return string.endsWith(suffix)
? string.slice(0, -1 * suffix.length)
: string + suffix
return string.endsWith(suffix) ? string.slice(0, -1 * suffix.length) : string
}

export function createUrl(urlLike: string | URL) {
if (urlLike instanceof URL) {
return urlLike
}

if (!(urlLike || '').includes('://')) {
export function createUrl(urlLike: string | URL | Location) {
if (typeof urlLike === 'string' && !(urlLike || '').includes('://')) {
urlLike = 'http://e.g' + withPrefix(urlLike, S)
}

return new URL(urlLike)
}

export function joinPaths(...paths: string[]) {
return paths.reduce((acc, path) => acc + path, '').replace(/\/\//g, S)
return new URL(urlLike.toString())
}

export function getFullPath(url: string | URL | Location, routeBase?: string) {
url = typeof url === 'string' ? createUrl(url) : url
url = createUrl(url)
url.pathname = withSuffix(url.pathname, S)
let fullPath = withoutPrefix(url.href, url.origin)

if (routeBase) {
const parts = fullPath.split(S)
if (parts[1] === routeBase.replace(/\//g, '')) {
parts.splice(1, 1)
routeBase = withSuffix(withPrefix(routeBase, S), S)
if (fullPath.indexOf(routeBase) === 0) {
fullPath = withPrefix(fullPath.replace(routeBase, ''), S)
}

fullPath = parts.join(S)
}

return fullPath
Expand Down
69 changes: 69 additions & 0 deletions test/specs/utils/route.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import {
withPrefix,
withoutPrefix,
withSuffix,
withoutSuffix,
createUrl,
getFullPath,
} from '../../../src/utils/route'

test('withPrefix', () => {
assert.is(withPrefix('/my/path', '/'), '/my/path')
assert.is(withPrefix('my/path', '/'), '/my/path')
assert.is(withPrefix('/my/path', '/my'), '/my/path')
assert.is(withPrefix('/path', '/my'), '/my/path')
})

test('withoutPrefix', () => {
assert.is(withoutPrefix('/my/path', '/'), 'my/path')
assert.is(withoutPrefix('my/path', '/'), 'my/path')
assert.is(withoutPrefix('/my/path', '/my'), '/path')
assert.is(withoutPrefix('/path', '/my'), '/path')
})

test('withSuffix', () => {
assert.is(withSuffix('/my/path', '/'), '/my/path/')
assert.is(withSuffix('/my/path/', '/'), '/my/path/')
assert.is(withSuffix('/my', '/path'), '/my/path')
assert.is(withSuffix('/my/path', '/path'), '/my/path')
})

test('withoutSuffix', () => {
assert.is(withoutSuffix('/my/path', '/'), '/my/path')
assert.is(withoutSuffix('/my/path/', '/'), '/my/path')
assert.is(withoutSuffix('/my/path', '/path'), '/my')
assert.is(withoutSuffix('/my', '/path'), '/my')
})

test('createUrl', () => {
assert.is(
createUrl(new URL('http://e.g/my/path')).toString(),
'http://e.g/my/path'
)

assert.is(createUrl('http://e.g/my/path').toString(), 'http://e.g/my/path')
assert.is(createUrl('/my/path').toString(), 'http://e.g/my/path')
assert.is(createUrl('/my/path?query').toString(), 'http://e.g/my/path?query')
})

test('getFullPath', () => {
assert.is(getFullPath(new URL('http://e.g/my/path')), '/my/path/')
assert.is(getFullPath('/my/path'), '/my/path/')
assert.is(getFullPath('/my/path?query'), '/my/path/?query')

assert.is(getFullPath('/my/path', '/my'), '/path/')
assert.is(getFullPath('/my/path/', '/my'), '/path/')
assert.is(getFullPath('/my/path?query', '/my'), '/path/?query')

assert.is(getFullPath('/my/', '/my'), '/')
assert.is(getFullPath('/my', '/my'), '/')
assert.is(getFullPath('/my/?query', '/my'), '/?query')
assert.is(getFullPath('/my?query', '/my'), '/?query')

assert.is(getFullPath('/my/deep/path', '/my/deep'), '/path/')
assert.is(getFullPath('/my/deepest/path', '/my/deep'), '/my/deepest/path/')
})

test.run()

0 comments on commit eabb2df

Please sign in to comment.