From c1936ea3e4cc2b1c56b31c3177c569ce440fd291 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Mon, 25 Sep 2023 12:54:04 -0500 Subject: [PATCH] feat: add parser url function to utils --- src/index.js | 1 + src/utils.js | 21 +++++++++++++++++++++ src/utils.test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/index.js b/src/index.js index 428012f3e..7e0c785f8 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ export { getQueryParameters, ensureDefinedConfig, mix, + parseURL, } from './utils'; export { APP_TOPIC, diff --git a/src/utils.js b/src/utils.js index e173ca75f..79e8231c8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 diff --git a/src/utils.test.js b/src/utils.test.js index 1c366c772..0f887fd58 100644 --- a/src/utils.test.js +++ b/src/utils.test.js @@ -3,6 +3,7 @@ import { camelCaseObject, snakeCaseObject, convertKeyNames, + parseURL, getQueryParameters, mix, } from '.'; @@ -115,6 +116,7 @@ describe('getQueryParameters', () => { }); }); + describe('mix', () => { it('should return rigth value', () => { const expected = '#546e88'; // This value was calculated in https://sass.js.org/ by using sass mix function @@ -130,5 +132,43 @@ describe('mix', () => { const expected = '#8598aa'; // This value was calculated in https://sass.js.org/ by using sass mix function expect(mix('FFFFFF', '0A3055')).toBe(expected); + }); +}); + +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'); }); });