t-comm
Version:
专业、稳定、纯粹的工具库
32 lines (31 loc) • 1.44 kB
TypeScript
/**
* 提取链接参数,兼容hash模式和history模式,以及拼接异常情况
* @param {string} [url=''] 地址
* @param {string} [key=''] 可选,若不为空,则提取返回该key对应的参数值
* @returns 地址参数对象,或者是指定参数值
* @example
* // 地址使用 search 参数
* resolveUrlParams('https://igame.qq.com?name=mike&age=18');
* // => { name: 'mike', age: '18' }
* @example
* // 地址使用 hash 参数
* resolveUrlParams('https://igame.qq.com#/?from=china&home=china');
* // => { from: 'china', home: 'china' }
* @example
* // 地址同时使用 search 和 hash 参数
* resolveUrlParams('https://igame.qq.com?name=mike&age=18#/index?from=china&home=china');
* // => { from: 'china', home: 'china', name: 'mike', age: '18' }
* @example
* // 通过 key 直接取值
* resolveUrlParams('https://igame.qq.com?name=mike&age=18', 'age');
* // => '18'
* @example
* // 空 url 时:未传 key 返回 {},传 key 返回 undefined
* resolveUrlParams(''); // => {}
* resolveUrlParams('', 'age'); // => undefined
* @example
* // 兼容真实业务地址中带 %3F、& 等异常拼接情况
* resolveUrlParams('https://igame.qq.com/x.html#/index?brandid=b1662364289&%3BmultiCfgId=b16623642891663309541');
* // => { brandid: 'b1662364289' }
*/
export declare function resolveUrlParams(url?: string, key?: string): string | Record<string, string> | undefined;