Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Created June 20, 2023 08:21
Show Gist options
  • Save wmakeev/7eba93566dea18a9ae768074ef427dfc to your computer and use it in GitHub Desktop.
Save wmakeev/7eba93566dea18a9ae768074ef427dfc to your computer and use it in GitHub Desktop.
[window.location.hash parser] #window #location #hash #parse #js #browser
const getLocationHashMap = () => {
const hash = window.location.hash.substring(
window.location.hash.substring(0, 2) === "#!" ? 2 : 1
);
const hashMap = hash
.split("&")
.map((entry) => {
const eqIndex = entry.indexOf("=");
if (eqIndex === -1) {
return [entry, undefined];
} else {
const kv = [entry.substring(0, eqIndex), entry.substring(eqIndex + 1)];
return kv;
}
})
.filter((entry) => entry[0])
.reduce((res, entry) => {
return Object.assign(res, { [entry[0]]: entry[1] });
}, {});
return hashMap;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment