Last active
March 23, 2021 14:28
-
-
Save technige/f1cf9fbf42d54720079c178b24817bbd to your computer and use it in GitHub Desktop.
Basic URI parser in JS
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
// JS version of Python partition function | |
function partition(s, delimiter) { | |
const i = s.indexOf(delimiter); | |
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]; | |
else return [s, "", ""]; | |
} | |
// JS version of Python rpartition function | |
function rpartition(s, delimiter) { | |
const i = s.lastIndexOf(delimiter); | |
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]; | |
else return ["", "", s]; | |
} | |
// Parse an authority string into an object | |
// with the following keys: | |
// - userInfo (optional, might contain both user name and password) | |
// - host | |
// - port (optional, included only as a string) | |
function parseAuthority(value) { | |
let parsed = {}, parts; | |
// Parse user info | |
parts = rpartition(value, "@"); | |
if (parts[1] === "@") { | |
parsed.userInfo = decodeURIComponent(parts[0]); | |
value = parts[2]; | |
} | |
// Parse host and port | |
parts = partition(value, ":"); | |
parsed.host = parts[0]; | |
if (parts[1] === ":") { | |
parsed.port = parts[2]; | |
} | |
return parsed; | |
} | |
// Parse a query string into an object. If repeated | |
// keys exist, these will get overwritten. | |
function parseQueryString(value) { | |
let parsed = {}; | |
value.split("&").forEach(function(bit) { | |
let parts = partition(bit, "="); | |
parsed[decodeURIComponent(parts[0])] = decodeURIComponent(parts[2]); | |
}); | |
return parsed; | |
} | |
// Parse a URI into an object with the following | |
// top-level keys: | |
// - scheme (optional) | |
// - authority (optional, further parsed if present) | |
// - path | |
// - query (optional, further parsed if present) | |
// - fragment (optional) | |
function parseURI(value) { | |
let parsed = {}, parts; | |
// Parse scheme | |
parts = partition(value, ":"); | |
if (parts[1] === ":") { | |
parsed.scheme = decodeURIComponent(parts[0]); | |
value = parts[2]; | |
} | |
// Parse fragment | |
parts = partition(value, "#"); | |
if (parts[1] === "#") { | |
parsed.fragment = decodeURIComponent(parts[2]); | |
value = parts[0]; | |
} | |
// Parse query | |
parts = partition(value, "?"); | |
if (parts[1] === "?") { | |
parsed.query = parseQuery(parts[2]); | |
value = parts[0]; | |
} | |
// Parse authority and path | |
if (value.startsWith("//")) { | |
parts = partition(value.substr(2), "/"); | |
parsed.authority = parseAuthority(parts[0]); | |
parsed.path = parts[1] + parts[2]; | |
} | |
else { | |
parsed.path = value; | |
} | |
return parsed | |
} | |
// And here's how to use it | |
console.log(parseURI('neo4j+s://[email protected]:7687?region=brazil')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment