t-comm
Version:
专业、稳定、纯粹的工具库
158 lines (151 loc) • 5.4 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
/**
* 多重解码。避免内嵌在外部时地址参数被编码,先进行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'
*/
function decode() {
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
if (!str) return str;
var text = decodeURIComponent(str);
// 含有实体符号 &,需要实体解码处理
var regex = /&/g;
if (regex.test(text)) {
text = text.replace(regex, '&');
}
return text;
}
/**
* 将参数对象转成字符串
* @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'
*/
function stringifyParams(params) {
return Object.keys(params).reduce(function (acc, key) {
var value = params[key];
return "".concat(acc ? "".concat(acc, "&") : '').concat(key, "=").concat(value);
}, '');
}
/**
* 小程序不支持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'
*/
function addUrlParam(url, key, value) {
var ret = url;
if (url.indexOf('?') !== -1) {
ret = "".concat(ret, "&").concat(key, "=").concat(value);
} else {
ret = "".concat(ret, "?").concat(key, "=").concat(value);
}
return ret;
}
/**
* 为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'
*/
function addUrlParams(url, params) {
var shouldOverride = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
// 使用正则表达式找到当前 URL 中的哈希部分
var hashPart = url.match(/#.*/);
var hash = hashPart ? hashPart[0] : '';
// 从原始 URL 中删除哈希部分
var mainUrl = url.replace(/#.*/, '');
var _mainUrl$split = mainUrl.split('?'),
_mainUrl$split2 = _slicedToArray__default["default"](_mainUrl$split, 2),
baseUrl = _mainUrl$split2[0],
originalQueryString = _mainUrl$split2[1];
var originalParams = originalQueryString ? originalQueryString.split('&').reduce(function (acc, entry) {
var _entry$split = entry.split('='),
_entry$split2 = _slicedToArray__default["default"](_entry$split, 2),
key = _entry$split2[0],
value = _entry$split2[1];
acc[decodeURIComponent(key)] = decodeURIComponent(value);
return acc;
}, {}) : {};
// 向原始参数对象添加新参数,同时判断是否覆盖现有参数值
Object.entries(params).forEach(function (_ref) {
var _ref2 = _slicedToArray__default["default"](_ref, 2),
key = _ref2[0],
value = _ref2[1];
if (shouldOverride || !Object.prototype.hasOwnProperty.call(originalParams, key)) {
originalParams[key] = value;
}
});
// 将原始参数和新参数合并为查询字符串
var updatedQueryString = Object.entries(originalParams).map(function (_ref3) {
var _ref4 = _slicedToArray__default["default"](_ref3, 2),
key = _ref4[0],
value = _ref4[1];
return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
}).join('&');
// 拼接新的 URL
var updatedUrl = "".concat(baseUrl, "?").concat(updatedQueryString).concat(hash);
return updatedUrl;
}
exports.addUrlParam = addUrlParam;
exports.addUrlParams = addUrlParams;
exports.decode = decode;
exports.stringifyParams = stringifyParams;