Skip to content

Commit

Permalink
feat: withBase and withoutBase utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 10, 2021
1 parent 3a9c973 commit 1667eca
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,50 @@ export function hasProtocol (inputStr: string): boolean {
return /^\w+:\/\//.test(inputStr)
}

export function hasTrailingSlash (input: string = ''): boolean {
return input.endsWith('/')
}

export function withoutTrailingSlash (input: string = ''): string {
return (input.endsWith('/') ? input.slice(0, -1) : input) || '/'
return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || '/'
}

export function withTrailingSlash (input: string = ''): string {
return input.endsWith('/') ? input : (input + '/')
}

export function hasLeadingSlash (input: string = ''): boolean {
return input.startsWith('/')
}

export function withoutLeadingSlash (input: string = ''): string {
return (input.startsWith('/') ? input.substr(1) : input) || '/'
return (hasLeadingSlash(input) ? input.substr(1) : input) || '/'
}

export function withLeadingSlash (input: string = ''): string {
return input.startsWith('/') ? input : ('/' + input)
return hasLeadingSlash(input) ? input : ('/' + input)
}

export function cleanDoubleSlashes (input: string = ''): string {
return input.split('://').map(str => str.replace(/\/{2,}/g, '/')).join('://')
}

export function withBase (input: string, base: string) {
const _base = withoutTrailingSlash(base)
if (input.startsWith(_base)) {
return input
}
return joinURL(_base, input)
}

export function withoutBase (input: string, base: string) {
const _base = withoutTrailingSlash(base)
if (input.startsWith(_base)) {
return input.substr(_base.length) || '/'
}
return input
}

export function withQuery (input: string, query: QueryObject): string {
const parsed = parseURL(input)
const mergedQuery = { ...parseQuery(parsed.search), ...query }
Expand Down
41 changes: 41 additions & 0 deletions test/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-nocheck
import { withBase, withoutBase } from '../src'

describe('withBase', () => {
const tests = [
{ base: '/', input: '/', out: '/' },
{ base: '/foo', input: '/', out: '/foo' },
{ base: '/foo/', input: '/', out: '/foo' },
{ base: '/foo', input: '/bar', out: '/foo/bar' },
{ base: '/base/', input: '/base', out: '/base' },
{ base: '/base', input: '/base/', out: '/base/' },
{ base: '/base', input: '/base/a', out: '/base/a' },
{ base: '/base/', input: '/base/a', out: '/base/a' }
]

for (const t of tests) {
test(JSON.stringify(t.base) + ' + ' + JSON.stringify(t.input), () => {
expect(withBase(t.input, t.base)).toBe(t.out)
})
}
})

describe('withoutBase', () => {
const tests = [
{ base: '/', input: '/', out: '/' },
{ base: '/foo', input: '/', out: '/' },
{ base: '/foo/', input: '/', out: '/' },
{ base: '/foo', input: '/bar', out: '/bar' },
{ base: '/base/', input: '/base', out: '/' },
{ base: '/base', input: '/base/', out: '/' },
{ base: '/base', input: '/base/a', out: '/a' },
{ base: '/base/', input: '/base/a', out: '/a' },
{ base: '/base/a/', input: '/base/a', out: '/' }
]

for (const t of tests) {
test(JSON.stringify(t.input) + '-' + JSON.stringify(t.base), () => {
expect(withoutBase(t.input, t.base)).toBe(t.out)
})
}
})

0 comments on commit 1667eca

Please sign in to comment.