Created
January 23, 2023 16:09
-
-
Save Cauen/80db9eee7ce5c04969e73ef46159c18d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Name: Deep URL decode | |
// Description: Sometimes you want to know what inside a URL, use this to show as a deep json... | |
import "@johnlindquist/kit"; | |
const decode = (str: string) => decodeURIComponent((str + '').replace(/\+/g, '%20')) | |
function getParamsFromUrl(url: string): Record<string, string> | void { | |
if (url.startsWith("https%")) return getParamsFromUrl(decode(url)) | |
try { | |
if (typeof url === 'string') { | |
let params = url.split('?'); | |
let eachParamsArr = params[1].split('&'); | |
let obj: Record<string, string> = {}; | |
if (eachParamsArr && eachParamsArr.length) { | |
eachParamsArr.map(param => { | |
let keyValuePair = param.split('=') | |
let key = keyValuePair[0]; | |
let value = keyValuePair[1]; | |
obj[key] = value; | |
}) | |
} | |
return obj; | |
} | |
} catch (err) { | |
// return getParamsFromUrl(decode(url)) | |
return {} | |
} | |
} | |
interface DecodeType { | |
['1: URL Original']: string | |
['2: URL Decoded']: string | |
['3: URL Params']: void | Record<string, string> | |
['4: Children']: Record<string, DecodeType> | |
} | |
function decodeDeep(urlString: string): DecodeType { | |
const decodedUrl = decode(urlString) | |
const paramsFromUrl = getParamsFromUrl(urlString) | |
return { | |
['1: URL Original']: urlString, | |
['2: URL Decoded']: decodedUrl, | |
['3: URL Params']: paramsFromUrl, | |
['4: Children']: (() => { | |
if (!paramsFromUrl) return {} | |
const entries = Object.entries(paramsFromUrl) | |
return entries.reduce((current, [paramKey, paramValue]) => { | |
const itemIsUrl = paramValue.startsWith("http") | |
if (!itemIsUrl) return current | |
const decoded = decodeDeep(paramValue) | |
if (!decoded) return current | |
// return current | |
return { ...current, [paramKey]: decoded } | |
}, {}) | |
})() | |
} | |
} | |
let url = await arg("enter URL") | |
inspect(decodeDeep(url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment