Last active
July 21, 2017 14:56
-
-
Save reu/aa865c7c518c766973612413e41ffba7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// @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