From eabb2dfcac5574c8db35bfa809f903ea4fd275b4 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Fri, 31 Dec 2021 17:53:11 +0100 Subject: [PATCH] fix: route utilities when using routeBase --- src/utils/route.ts | 29 +++++--------- test/specs/utils/route.spec.ts | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 20 deletions(-) create mode 100644 test/specs/utils/route.spec.ts diff --git a/src/utils/route.ts b/src/utils/route.ts index f75cf65..6e7ce47 100644 --- a/src/utils/route.ts +++ b/src/utils/route.ts @@ -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 diff --git a/test/specs/utils/route.spec.ts b/test/specs/utils/route.spec.ts new file mode 100644 index 0000000..51ed647 --- /dev/null +++ b/test/specs/utils/route.spec.ts @@ -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()