Skip to content

Instantly share code, notes, and snippets.

@DefectingCat
Created May 28, 2024 03:09
Show Gist options
  • Save DefectingCat/d27af6f77675f797fd2d848140d9cc22 to your computer and use it in GitHub Desktop.
Save DefectingCat/d27af6f77675f797fd2d848140d9cc22 to your computer and use it in GitHub Desktop.
remove query in url hash
/**
* 根据 key 删除 url 中 hash 中的指定 query
*
* #/?booth_id=Ep3wmDXbjk2&code=041Cax100CXyaS1mjI1005Tw1E3Cax1N
*
* @param hash url 中的 hash
* @param deleteKey 需要删除的 query 的 key
*/
export function removeHash(hash: string, deleteKey: string) {
if (hash.length < 3) return;
const body = hash
.slice(3)
// ['booth_id=Ep3wmDXbjk2', 'code=041Cax100CXyaS1mjI1005Tw1E3Cax1N']
.split('&')
// [{'booth_id': 'Ep3wmDXbjk2'}, {code: '041Cax100CXyaS1mjI1005Tw1E3Cax1N'}]
.map((query) => {
const string = query.split('=');
return {
[string[0]]: string[1],
};
})
// [{'booth_id': 'Ep3wmDXbjk2'}]
.filter((b) => !b[deleteKey])
// ['booth_id=Ep3wmDXbjk2']
.map((b) => `${Object.keys(b)}=${Object.values(b)}`)
// 'booth_id=Ep3wmDXbjk2'
.join('&');
return `#/?${body}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment