Last active
April 28, 2022 23:13
-
-
Save westc/b49622af9500b2a605cb928df9b793ae to your computer and use it in GitHub Desktop.
Parses the given URL and returns it as a JSON string.
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
/** | |
* Parses the given URL and returns it as a JSON string. | |
* @param {string} url | |
* The URL to parse and return as JSON. | |
* @param {number|string} space | |
* Optional. Used to insert white space (including indentation, line break | |
* characters, etc.) into the output JSON string for readability purposes. If | |
* this is a number, it indicates the number of space characters to use aa | |
* white space for indenting purposes; this number is capped at 10 (if it is | |
* greater, the value is just 10). Values less than 1 indicate that no space | |
* should be used. If this is a String, the string (or the first 10 | |
* characters of the string, if it's longer than that) is used as white space. | |
* @returns {string} | |
* A JSON string representing the given URL as an object with the following | |
* properties: | |
* - `hash`: A string containing a "#" followed by the fragment identifier of | |
* the URL. | |
* - `host`: A string containing the domain (that is the `hostname`) followed | |
* by (if a port was specified) a ":" and the `port` of the URL. | |
* - `hostname`: A string containing the domain of the URL. | |
* - `href`: A string containing the whole URL. | |
* - `origin`: A string containing the origin of the URL, that is its scheme, | |
* its domain and its port. | |
* - `password`: A string containing the password specified before the domain | |
* name. | |
* - `pathname`: A string containing an initial "/" followed by the path of | |
* the URL, not including the query string or fragment. | |
* - `port`: A string containing the port number of the URL. | |
* - `protocol`: A string containing the protocol scheme of the URL, | |
* including the final ":". | |
* - `search`: A string indicating the URL's parameter string; if any | |
* parameters are provided, this string includes all of them, beginning with | |
* the leading ? character. | |
* - `username`: A string containing the username specified before the domain | |
* name. | |
* - `singleValueParams`: An object where the keys correspond to the URL | |
* parameter names and the corresponding values are the last value found in | |
* the URL with that name. | |
* - `multiValueParams`: A object where the keys correspond to the URL | |
* parameter names and the corresponding values are arrays of all of the | |
* values with that corresponding name. | |
*/ | |
function jsonifyURL(url, space=2) { | |
const u = new URL(url); | |
const singleValueParams = {}; | |
const multiValueParams = {}; | |
Object.assign(u, { singleValueParams, multiValueParams }); | |
const hasOwn = atob.call.bind(multiValueParams.hasOwnProperty); | |
for (const [k, v] of u.searchParams) { | |
singleValueParams[k] = v; | |
if (hasOwn(multiValueParams, k)) { | |
multiValueParams[k].push(v); | |
} | |
else { | |
multiValueParams[k] = [v]; | |
} | |
} | |
return JSON.stringify( | |
'hash host hostname href origin password pathname port protocol search username singleValueParams multiValueParams'.split(' ').reduce( | |
(result, key) => { | |
result[key] = u[key]; | |
return result; | |
}, | |
{} | |
), | |
null, | |
space | |
); | |
} |
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
Show hidden characters
{ | |
// Title of the IO App | |
"title": "JSONify URL", | |
// Name of the function that takes the input string and produces the output string. | |
"transform": "jsonifyURL", | |
// Where to find the code that will be eval'd. | |
"files": ["jsonifyURL.js"], | |
// Input setup | |
"input": { "label": "Input URL", "language": "plain" }, | |
// Output setup | |
"output": { "label": "Output JSON", "language": "json" }, | |
// Type of IO App | |
"type": "split" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment