Last active
October 18, 2021 08:16
-
-
Save toastal/0c6780b70446038cd5a0 to your computer and use it in GitHub Desktop.
Ramda URL Parser
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
// requires Ramda (http://ramdajs.com/) | |
// urlParser : String -> Object | |
;urlParser = (function(document, R) { | |
"use strict"; | |
const {fromPairs, pipe, reduce, replace, split} = R; | |
let parser = document.createElement("a"); | |
// searchToKeyVal : String -> Object | |
const searchToKeyVal = | |
pipe(replace(/^\?/, ""), split("&"), map(split("=")), fromPairs); | |
return function urlParser(url) { | |
parser.href = url; | |
return { protocol : parser.protocol | |
, host : parser.host | |
, hostname : parser.hostname | |
, port : parser.port | |
, pathname : parser.pathname | |
, search : parser.search | |
, searchObject : searchToKeyVal(parser.search) | |
, hash : parser.hash | |
}; | |
}; | |
}(document, R)); | |
// TEST | |
console.log(urlParser("http://test.com/tester?q=test")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment