Last active: 6 months ago
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}`;
}