Skip to content

Commit

Permalink
feat: add parser url function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto authored and andrey-canon committed Jan 16, 2024
1 parent 312389b commit 6559890
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
convertKeyNames,
getQueryParameters,
ensureDefinedConfig,
parseURL,
} from './utils';
export {
APP_TOPIC,
Expand Down
21 changes: 21 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ export function convertKeyNames(object, nameMap) {
return modifyObjectKeys(object, transformer);
}

/**
* Given a string URL return an element that has been parsed via href.
* This element has the possibility to return different part of the URL.
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
* https://gist.github.com/jlong/2428561
*
* @param {string}
* @returns {Object}
*/
export function parseURL(url) {
const parser = document.createElement('a');
parser.href = url;
return parser;
}

/**
* *Deprecated*: A method which converts the supplied query string into an object of
* key-value pairs and returns it. Defaults to the current query string - should perform like
Expand Down
39 changes: 39 additions & 0 deletions src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
camelCaseObject,
snakeCaseObject,
convertKeyNames,
parseURL,
getQueryParameters,
} from '.';

Expand Down Expand Up @@ -113,3 +114,41 @@ describe('getQueryParameters', () => {
});
});
});

describe('ParseURL', () => {
const testURL = 'http://example.com:3000/pathname/?search=test#hash';
const parsedURL = parseURL(testURL);
it('String URL is correctly parsed', () => {
expect(parsedURL.toString()).toEqual(testURL);
expect(parsedURL.href).toEqual(testURL);
expect(typeof (parsedURL)).toEqual('object');
});

it('should return protocol from URL', () => {
expect(parsedURL.protocol).toEqual('http:');
});

it('should return hostname from URL', () => {
expect(parsedURL.hostname).toEqual('example.com');
});

it('should return port from URL', () => {
expect(parsedURL.port).toEqual('3000');
});

it('should return pathname from URL', () => {
expect(parsedURL.pathname).toEqual('/pathname/');
});

it('should return search rom URL', () => {
expect(parsedURL.search).toEqual('?search=test');
});

it('should return hash from URL', () => {
expect(parsedURL.hash).toEqual('#hash');
});

it('should return host from URL', () => {
expect(parsedURL.host).toEqual('example.com:3000');
});
});

0 comments on commit 6559890

Please sign in to comment.