From 45fb337c87b16324115d8a32c4a6bfe209c16b81 Mon Sep 17 00:00:00 2001 From: Samuel Martineau Date: Sat, 21 Aug 2021 19:08:40 +0000 Subject: [PATCH] Add support for all protocols for urls (#74) --- client/src/utility/urlParser.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/client/src/utility/urlParser.ts b/client/src/utility/urlParser.ts index 87edb63..666935f 100644 --- a/client/src/utility/urlParser.ts +++ b/client/src/utility/urlParser.ts @@ -1,24 +1,23 @@ -export const urlParser = (url: string): string[] => { - let parsedUrl: string; - let displayUrl: string; +const hasProtocol = (url: string): boolean => /^\w+:\/\//.test(url); +const isSteamUrl = (url: string): boolean => /^steam:\/\//.test(url); +const isWebUrl = (url: string): boolean => /^https?:\/\//.test(url); - if (/(https?|steam):\/\//.test(url)) { - // Url starts with http[s]:// or steam:// -> leave it as it is - parsedUrl = url; - } else { +export const urlParser = (url: string): string[] => { + if (!hasProtocol(url)) { // No protocol -> apply http:// prefix - parsedUrl = `http://${url}`; + url = `http://${url}`; } // Create simplified url to display as text - if (/steam:\/\//.test(url)) { + let displayUrl: string; + if (isSteamUrl(url)) { displayUrl = 'Run Steam App'; - } else { + } else if (isWebUrl(url)) { displayUrl = url - .replace(/https?:\/\//, '') - .replace('www.', '') - .replace(/\/$/, ''); - } - - return [displayUrl, parsedUrl] -} \ No newline at end of file + .replace(/https?:\/\//, '') + .replace('www.', '') + .replace(/\/$/, ''); + } else displayUrl = url; + + return [displayUrl, url]; +};