t-comm
Version:
专业、稳定、纯粹的工具库
116 lines (115 loc) • 3.64 kB
TypeScript
/**
* url参数变对象
* @param {string} url 输入URL
* @returns {Object} search对象
* @example
* // 完整 url
* getQueryObj('https://igame.qq.com?name=mike&age=18&feel=cold&from=china');
* // => { name: 'mike', age: '18', feel: 'cold', from: 'china' }
* @example
* // 仅传入 query 字符串(不含 ?)也能解析
* getQueryObj('name=mike&age=18&feel=cold&from=china');
* // => { name: 'mike', age: '18', feel: 'cold', from: 'china' }
*/
export declare function getQueryObj(url: string): Record<string, string>;
/**
* 组装`url`参数,将search参数添加在后面
* @param {string} url 输入URL
* @param {Object} queryObj search对象
* @returns {string} 组装后的url
* @example
* // url 本身无 query 参数
* composeUrlQuery('https://baidu.com', {
* name: 'mike',
* feel: 'cold',
* age: '18',
* from: 'test',
* });
* // => 'https://baidu.com?name=mike&feel=cold&age=18&from=test'
* @example
* // url 已有 query 参数,会与新参数合并
* composeUrlQuery('https://baidu.com?gender=male', {
* name: 'mike',
* feel: 'cold',
* age: '18',
* from: 'test',
* });
* // => 'https://baidu.com?gender=male&name=mike&feel=cold&age=18&from=test'
*/
export declare function composeUrlQuery(url: string, queryObj: object): string;
/**
* 将对象字符串化
* @param {object} obj 输入对象
* @returns {string} 字符串
* @example
* // 编码普通对象
* encodeUrlParam({ a: 1 });
* // => '%7B%22a%22%3A1%7D'
* @example
* // 编码空对象
* encodeUrlParam({});
* // => '%7B%7D'
* @example
* // 编码嵌套对象(等价于 encodeURIComponent(JSON.stringify(obj)))
* encodeUrlParam({ name: 'mike', info: { age: 18 } });
* @example
* // 编码数组
* encodeUrlParam([1, 2, 3]);
* // => encodeURIComponent('[1,2,3]')
* @example
* // 编码含中文的对象
* encodeUrlParam({ name: '张三' });
* // => encodeURIComponent(JSON.stringify({ name: '张三' }))
*/
export declare function encodeUrlParam(obj: object): string;
/**
* 将字符串解码,与`encodeUrlParam`相对
* @param {string} obj 输入字符串
* @returns {object} 对象
* @example
* // 解码普通对象
* decodeUrlParam('%7B%22a%22%3A1%7D');
* // => { a: 1 }
* @example
* // 解码空对象
* decodeUrlParam('%7B%7D');
* // => {}
* @example
* // 解码嵌套对象
* const encoded = encodeURIComponent(JSON.stringify({ name: 'mike', info: { age: 18 } }));
* decodeUrlParam(encoded);
* // => { name: 'mike', info: { age: 18 } }
* @example
* // 无效字符串返回空对象
* decodeUrlParam('invalid-string');
* // => {}
* @example
* // encode 和 decode 互逆
* decodeUrlParam(encodeUrlParam({ name: 'mike', age: 18, list: [1, 2] }));
* // => { name: 'mike', age: 18, list: [1, 2] }
*/
export declare function decodeUrlParam(str: string): object;
/**
* 获取 Url 参数
* @param {string} paraName 参数 key
* @param {string} search url search 部分
* @returns paraValue
* @example
* getUrlPara('gender', '?gender=male&name=mike&feel=cold&age=18&from=test');
* // => 'male'
* @example
* getUrlPara('from', '?gender=male&name=mike&feel=cold&age=18&from=test');
* // => 'test'
* @example
* getUrlPara('age', '?gender=male&name=mike&feel=cold&age=18&from=test');
* // => '18'
* @example
* // 不存在的 key 返回空字符串
* getUrlPara('other', '?gender=male&name=mike&feel=cold&age=18&from=test');
* // => ''
* @example
* // 未传 search 且环境不存在 location 时,返回空字符串
* getUrlPara('other');
* // => ''
*/
export declare function getUrlPara(paraName: string, search?: string): string;