Created
June 3, 2022 03:05
-
-
Save dbwodlf3/255a6f2c701bb09efb8e2759226b6e74 to your computer and use it in GitHub Desktop.
parse url
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
| export function parse_url(inputString: string){ | |
| const allowed_protocols = ["https", "http"]; | |
| let input_string = inputString; | |
| let protocol = ""; | |
| let hostname:any = ""; | |
| let subdomain = ""; | |
| let domain = ""; | |
| let tld = ""; | |
| let paths = ""; | |
| let queries: any = {}; | |
| let temp; | |
| let temp2; | |
| for(const _protocol of allowed_protocols) { | |
| let isProtocol = _protocol == input_string.substring(0, _protocol.length); if(!isProtocol) continue; | |
| let isSplitter = "://" == input_string.substring(_protocol.length, _protocol.length+3); if(!isSplitter) break; | |
| protocol = _protocol; | |
| } | |
| if(protocol) input_string = input_string.substring(protocol.length+3); | |
| temp = input_string.split("/"); | |
| hostname = temp.shift(); | |
| paths = temp.join("/").split("#")[0].split("?")[0]; | |
| temp2 = temp[temp.length-1]; | |
| temp2 = temp2?.split("#")[0]; | |
| temp2 = temp2?.split("?")[1]; | |
| temp2 = temp2?.split("&"); | |
| if(temp2) { | |
| for(const item of temp2){ | |
| let _temp = item.split("="); | |
| if(_temp.length==2)queries[_temp[0]] = _temp[1]; | |
| } | |
| } | |
| temp = hostname.split("."); | |
| return {protocol: protocol, hostname: hostname, paths: paths, queries: queries}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment