Skip to content

Instantly share code, notes, and snippets.

@toastal
Last active October 18, 2021 08:16
Show Gist options
  • Save toastal/0c6780b70446038cd5a0 to your computer and use it in GitHub Desktop.
Save toastal/0c6780b70446038cd5a0 to your computer and use it in GitHub Desktop.
Ramda URL Parser
// 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