t-comm
Version:
专业、稳定、纯粹的工具库
82 lines (81 loc) • 2.66 kB
TypeScript
/**
* 多重解码。避免内嵌在外部时地址参数被编码,先进行URL解码再进行HTML字符实体解码
* @docgen
* @function decode
* @param {string} str 文本
* @returns 解码后的文本
* @example
* decode('') // => ''
* @example
* decode() // => ''
* @example
* decode('hello%20world') // => 'hello world'
* @example
* // 同时进行 URL 解码和 HTML 实体(&)解码
* decode('a%3D1%26amp%3Bb%3D2') // => 'a=1&b=2'
* @example
* decode('hello') // => 'hello'
*/
export declare function decode(str?: string): string;
/**
* 将参数对象转成字符串
* @docgen
* @function stringifyParams
* @param {Object} params 参数对象
* @returns {String}
* @example
* stringifyParams({}) // => ''
* @example
* stringifyParams({ a: '1' }) // => 'a=1'
* @example
* stringifyParams({ a: '1', b: '2' }) // => 'a=1&b=2'
* @example
* stringifyParams({ page: 1, size: 10 }) // => 'page=1&size=10'
*/
export declare function stringifyParams(params: Record<string, string | number>): string;
/**
* 小程序不支持URL对象,用字符串拼接方式添加
* 注意:已有相同key不支持覆盖,会重复添加
* @docgen
* @function addUrlParam
* @param url 输入url
* @param key 键
* @param value 值
* @example
* // url 无参数时用 ? 连接
* addUrlParam('https://example.com', 'a', '1') // => 'https://example.com?a=1'
* @example
* // url 已有参数时用 & 连接
* addUrlParam('https://example.com?x=1', 'a', '1') // => 'https://example.com?x=1&a=1'
*/
export declare function addUrlParam(url: string, key: string, value: string): string;
/**
* 为url添加参数
*
* @export
* @param {string} url
* @param {object} params
* @param {boolean} [shouldOverride=false]
* @returns {string}
* @example
* // 向 url 添加多个参数
* addUrlParams('https://example.com', { a: '1', b: '2' })
* // => 'https://example.com?a=1&b=2'
* @example
* // shouldOverride=false(默认)时,不会覆盖已有参数
* addUrlParams('https://example.com?a=old', { a: 'new' }, false)
* // => 'https://example.com?a=old'
* @example
* // shouldOverride=true 时,会覆盖已有参数
* addUrlParams('https://example.com?a=old', { a: 'new' }, true)
* // => 'https://example.com?a=new'
* @example
* // 保留 hash 部分
* addUrlParams('https://example.com#section', { a: '1' })
* // => 'https://example.com?a=1#section'
* @example
* // url 无参数时正确添加参数
* addUrlParams('https://example.com', { key: 'val' })
* // => 'https://example.com?key=val'
*/
export declare function addUrlParams(url: string, params: object, shouldOverride?: boolean): string;