Skip to content

Instantly share code, notes, and snippets.

@reu
Last active July 21, 2017 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reu/aa865c7c518c766973612413e41ffba7 to your computer and use it in GitHub Desktop.
Save reu/aa865c7c518c766973612413e41ffba7 to your computer and use it in GitHub Desktop.
// @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