-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve support for relative URLs (#78)
- Create a shared parseUrl utility that will make sure to match the provided URL string to a url-parse instance - Support for protocol-relative URLs (Resolves #76)
- Loading branch information
1 parent
4f845e5
commit 2c0083e
Showing
6 changed files
with
62 additions
and
24 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,32 @@ | ||
import URL from 'url-parse'; | ||
import isAbsoluteUrl from 'is-absolute-url'; | ||
import removeHostFromUrl from './remove-host-from-url'; | ||
|
||
/** | ||
* Creates an exact representation of the passed url string with url-parse. | ||
* | ||
* @param {String} url | ||
* @param {...args} args Arguments to pass through to the URL constructor | ||
* @returns {URL} A url-parse URL instance | ||
*/ | ||
export default function parseUrl(url, ...args) { | ||
const parsedUrl = new URL(url, ...args); | ||
|
||
if (!isAbsoluteUrl(url)) { | ||
if (url.startsWith('//')) { | ||
/* | ||
If the url is protocol-relative, strip out the protocol | ||
*/ | ||
parsedUrl.set('protocol', ''); | ||
} else { | ||
/* | ||
If the url is relative, setup the parsed url to reflect just that | ||
by removing the host. By default URL sets the host via window.location if | ||
it does not exist. | ||
*/ | ||
removeHostFromUrl(parsedUrl); | ||
} | ||
} | ||
|
||
return parsedUrl; | ||
} |
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,24 @@ | ||
import parseUrl from '../../../src/utils/parse-url'; | ||
|
||
describe('Unit | Utils | parseUrl', function() { | ||
it('should exist', function() { | ||
expect(parseUrl).to.be.a('function'); | ||
}); | ||
|
||
it('should exactly match passed urls', function() { | ||
[ | ||
'/movies/1', | ||
'//netflix.com/movies/1', | ||
'http://netflix.com/movies/1', | ||
'http://netflix.com/movies/1?sort=title&dir=asc' | ||
].forEach(url => expect(parseUrl(url).href).to.equal(url)); | ||
}); | ||
|
||
it('should passthrough arguments to url-parse', function() { | ||
// Passing true tells url-parse to transform the querystring into an object | ||
expect(parseUrl('/movies/1?sort=title&dir=asc', true).query).to.deep.equal({ | ||
sort: 'title', | ||
dir: 'asc' | ||
}); | ||
}); | ||
}); |
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