Skip to content

Instantly share code, notes, and snippets.

@reu reu/hackish-url-parser.js
Last active Jul 21, 2017

Embed
What would you like to do?
// @flow
import {
always,
memoize,
pick,
} from "ramda";
type ParsedURL = {
protocol: string,
host: string,
port: number,
search: string,
hash: string,
pathname: string,
toString: void => string,
}
export const parseUrl: string => ParsedURL = memoize(url => {
const parser = document.createElement("a");
parser.href = url;
return {
...pick(["protocol", "host", "port", "search", "hash"], parser),
// IE doesn't add a slash at the beginning of the path name
pathname: parser.pathname.startsWith("/") ? parser.pathname : "/" + parser.pathname,
// We must call the `toString` instead of using it inside a closure,
// so the DOM element can be garbage collected
toString: always(parser.toString()),
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.