-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add url component for URL specific functions
- Loading branch information
1 parent
c08464b
commit 14d9aa9
Showing
7 changed files
with
307 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { parseUri } from './url'; | ||
export type { ParsedUri } from './url'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
labels: ['Url', 'module'] | ||
description: 'A Url module.' | ||
--- | ||
|
||
A url module. | ||
|
||
```ts | ||
url(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { parseUri } from './url'; | ||
|
||
describe('parseUri', () => { | ||
it('should parse a simple URL', () => { | ||
const uri = 'https://example.com'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a port', () => { | ||
const uri = 'https://example.com:8080'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com:8080/', | ||
hostOnlyDomain: '//example.com:8080/', | ||
host: 'example.com', | ||
port: '8080', | ||
pathname: '/', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a search', () => { | ||
const uri = 'https://example.com?foo=bar'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '?foo=bar', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a hash', () => { | ||
const uri = 'https://example.com#foo'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '', | ||
hash: '#foo', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a path', () => { | ||
const uri = 'https://example.com/path/to/file'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/path/to/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/path/to/file', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a query string', () => { | ||
const uri = 'https://example.com?foo=bar&baz=qux'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '?foo=bar&baz=qux', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a fragment identifier', () => { | ||
const uri = 'https://example.com#foo'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '', | ||
hash: '#foo', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with a username and password', () => { | ||
const uri = 'https://username:[email protected]'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com/', | ||
port: '', | ||
pathname: '/', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL that its an IP with port', () => { | ||
const uri = 'https://192.168.1.1:8080'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//192.168.1.1:8080/', | ||
hostOnlyDomain: '//192.168.1.1:8080/', | ||
host: '192.168.1.1', | ||
port: '8080', | ||
pathname: '/', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should parse a URL with port and subpaths ', () => { | ||
const uri = 'https://example.com:8080/path/to/file'; | ||
const expected = { | ||
raw: uri, | ||
protocol: 'https:', | ||
nerf: '//example.com:8080/path/to/', | ||
host: 'example.com', | ||
hostOnlyDomain: '//example.com:8080/', | ||
port: '8080', | ||
pathname: '/path/to/file', | ||
search: '', | ||
hash: '', | ||
}; | ||
const actual = parseUri(uri); | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import nerfDart from 'nerf-dart'; | ||
|
||
export interface ParsedUri { | ||
/** | ||
* The url as string | ||
*/ | ||
raw: string; | ||
/** | ||
* The protocol of the url | ||
*/ | ||
protocol: string; | ||
/** | ||
* The nerf dart of the url | ||
* @example https://example.com -> //example.com/ | ||
* @example https://example.com:8080/path/to/file -> //example.com:8080/path/to/ | ||
*/ | ||
nerf: string; | ||
/** | ||
* The host of the url | ||
* @example https://example.com -> example.com | ||
*/ | ||
host: string; | ||
/** | ||
* The host of the url with port | ||
* @example https://example.com:8080 -> //example.com:8080/ | ||
*/ | ||
hostOnlyDomain: string; | ||
/** | ||
* The port of the url | ||
* @example https://example.com:8080 -> 8080 | ||
*/ | ||
port: string; | ||
/** | ||
* The pathname of the url | ||
* @example https://example.com/path/to/file -> /path/to/file | ||
*/ | ||
pathname: string; | ||
/** | ||
* The search of the url | ||
* @example https://example.com/path/to/file?search=query -> ?search=query | ||
*/ | ||
search: string; | ||
/** | ||
* The hash of the url | ||
* @example https://example.com/path/to/file#hash -> #hash | ||
*/ | ||
hash: string; | ||
} | ||
|
||
export function parseUri(uri: string): ParsedUri { | ||
const parsed = new URL(uri); | ||
return { | ||
raw: uri, | ||
protocol: parsed.protocol, | ||
nerf: nerfDart(uri), | ||
host: parsed.hostname, | ||
hostOnlyDomain: convertToDomain(parsed), | ||
port: parsed.port, | ||
pathname: parsed.pathname, | ||
search: parsed.search, | ||
hash: parsed.hash, | ||
}; | ||
} | ||
|
||
function convertToDomain(url: URL): string { | ||
let result = `//${url.hostname}`; | ||
if (url.port) { | ||
result += `:${url.port}`; | ||
} | ||
return `${result}/`; | ||
} |