Created
June 20, 2023 08:21
-
-
Save wmakeev/7eba93566dea18a9ae768074ef427dfc to your computer and use it in GitHub Desktop.
[window.location.hash parser] #window #location #hash #parse #js #browser
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
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