UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

159 lines (152 loc) 3.95 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _typeof = require('@babel/runtime/helpers/typeof'); var tslib_es6 = require('../tslib.es6-01322ba9.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof); /** * url参数变对象 * @param {string} url 输入URL * @returns {Object} search对象 * * @example * * const res = getQueryObj('https://igame.qq.com?name=mike&age=18&feel=cold&from=China'); * * console.log(res); * { * name: 'mike', * age: '18', * feel: "cold", * from: 'China', * } * */ function getQueryObj(url) { var query = {}; var qs = url.slice(url.indexOf('?') > -1 ? url.indexOf('?') + 1 : 0); var kvs; var qsList = qs.split('&'); qsList.forEach(function (item) { kvs = item.split('='); if (kvs.length === 2) { var value = kvs[1]; query[kvs[0]] = value; } }); return query; } /** * 组装`url`参数,将search参数添加在后面 * @param {string} url 输入URL * @param {Object} queryObj search对象 * @returns {string} 组装后的url * * @example * composeUrlQuery('https://baidu.com', { * name: 'mike', * feel: 'cold', * age: '18', * from: 'test', * }); * // https://baidu.com?name=mike&feel=cold&age=18&from=test * * 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 * */ function composeUrlQuery(url, queryObj) { var oriQuery = getQueryObj(url); var pathname = url.slice(0, url.indexOf('?') > -1 ? url.indexOf('?') : url.length); pathname += '?'; var allQuery = tslib_es6.__assign(tslib_es6.__assign({}, oriQuery), queryObj); Object.keys(allQuery).forEach(function (i) { pathname += "".concat(i, "=").concat(allQuery[i], "&"); }); return pathname.slice(0, pathname.length - 1); } /** * 将对象字符串化 * @param {object} obj 输入对象 * @returns {string} 字符串 * @example * * encodeUrlParam({a: 1}) * * // '%7B%22a%22%3A1%7D' * */ function encodeUrlParam(obj) { if (_typeof__default["default"](obj) === 'object') { return encodeURIComponent(JSON.stringify(obj)); } return encodeURIComponent(obj); } /** * 将字符串解码,与`encodeUrlParam`相对 * @param {string} obj 输入字符串 * @returns {object} 对象 * @example * * decodeUrlParam('%7B%22a%22%3A1%7D') * * // { a: 1 } * */ function decodeUrlParam(str) { var res = {}; try { res = JSON.parse(decodeURIComponent(str)); } catch (err) { console.log('[decodeUrlParam] error: ', err); } return res; } /** * 获取 Url 参数 * @param {string} paraName 参数 key * @param {string} search url search 部分 * @returns paraValue * * @example * ```ts * getUrlPara('gender', '?gender=male&name=mike&feel=cold&age=18&from=test') * // male * * getUrlPara('age', '?gender=male&name=mike&feel=cold&age=18&from=test') * // 18 * ``` */ function getUrlPara(paraName, search) { if (search === void 0) { search = ''; } if (!search && typeof window !== 'undefined') { search = window.location.search; } if (!search && typeof location !== 'undefined') { var hash = location.hash; search = hash ? hash.substring(hash.indexOf('?')) : ''; } if (!search) search = ''; var query = search.substring(1); var vars = query.split('&'); // eslint-disable-next-line @typescript-eslint/prefer-for-of for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('='); if (pair[0] == paraName) { return pair[1]; } } return ''; } exports.composeUrlQuery = composeUrlQuery; exports.decodeUrlParam = decodeUrlParam; exports.encodeUrlParam = encodeUrlParam; exports.getQueryObj = getQueryObj; exports.getUrlPara = getUrlPara;