Skip to content

Commit

Permalink
feat!: lightweight joinURL + resolveURL
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 16, 2020
1 parent a80e98d commit 9a34622
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ normalizeURL('http://localhost:3000')
```ts
// Result: a/b/c
joinURL('a', '/b', '/c')
```

### `resolveURL`

```ts
// Result: http://foo.com/foo/bar/baz?test=123#token
joinURL('http://foo.com/foo?test=123#token', 'bar', 'baz')
```
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export function getParams (input: string): ParamsObject {
}

export function joinURL (base: string, ...input: string[]): string {
let url = base || ''

for (const i of input) {
url = withTrailingSlash(url) + withoutLeadingSlash(i)
}

return url
}

export function resolveURL (base: string, ...input: string[]): string {
const url = createURL(base)

for (const i of input) {
Expand Down
13 changes: 1 addition & 12 deletions test/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ describe('joinURL', () => {
{ input: ['/a'], out: '/a' },
{ input: ['a', 'b'], out: 'a/b' },
{ input: ['a', 'b/', 'c'], out: 'a/b/c' },
{ input: ['a', 'b/', '/c'], out: 'a/b/c' },
{ input: ['/a?foo=bar#123', 'b/', 'c/'], out: '/a/b/c/?foo=bar#123' },
{ input: ['http://foo.com', 'a'], out: 'http://foo.com/a' },
{ input: ['a?x=1', 'b?y=2&y=3&z=4'], out: 'a/b?x=1&y=2&y=3&z=4' }
{ input: ['a', 'b/', '/c'], out: 'a/b/c' }
]

for (const t of tests) {
Expand All @@ -20,14 +17,6 @@ describe('joinURL', () => {
})
}

test('invalid URL (null)', () => {
expect(() => joinURL(null)).toThrow('URL input should be string received object (null)')
})

test('invalid URL (array)', () => {
expect(() => joinURL([])).toThrow('URL input should be string received object ()')
})

test('no arguments', () => {
expect(joinURL()).toBe('')
})
Expand Down
34 changes: 34 additions & 0 deletions test/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @ts-nocheck
import { resolveURL } from '../src'

describe('resolveURL', () => {
const tests = [
{ input: [], out: '' },
{ input: ['/'], out: '/' },
{ input: ['/a'], out: '/a' },
{ input: ['a', 'b'], out: 'a/b' },
{ input: ['a', 'b/', 'c'], out: 'a/b/c' },
{ input: ['a', 'b/', '/c'], out: 'a/b/c' },
{ input: ['/a?foo=bar#123', 'b/', 'c/'], out: '/a/b/c/?foo=bar#123' },
{ input: ['http://foo.com', 'a'], out: 'http://foo.com/a' },
{ input: ['a?x=1', 'b?y=2&y=3&z=4'], out: 'a/b?x=1&y=2&y=3&z=4' }
]

for (const t of tests) {
test(t.input.toString(), () => {
expect(resolveURL(...t.input)).toBe(t.out)
})
}

test('invalid URL (null)', () => {
expect(() => resolveURL(null)).toThrow('URL input should be string received object (null)')
})

test('invalid URL (array)', () => {
expect(() => resolveURL([])).toThrow('URL input should be string received object ()')
})

test('no arguments', () => {
expect(resolveURL()).toBe('')
})
})

0 comments on commit 9a34622

Please sign in to comment.