UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

33 lines 1.39 kB
import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose"; /** * 解析 url 查询字符串。 * * 兼容以 `?` 开头的查询字符串,因此你可以直接传入 `location.search` 的值。 * * @param query 查询字符串 * @param options 选项 * @returns 返回 url 查询参数 * @example * ```typescript * parseUrlQueryString('x=1&y=z') // => { x: '1', y: 'z' } * ``` */ export function parseUrlQueryString(query, options) { var _options$pairSeparato, _options$partSeparato; if (!query) return {}; var pairSeparator = (_options$pairSeparato = options == null ? void 0 : options.pairSeparator) != null ? _options$pairSeparato : '='; var partSeparator = (_options$partSeparato = options == null ? void 0 : options.partSeparator) != null ? _options$partSeparato : '&'; var parameters = {}; query = query.charAt(0) === '?' ? query.substring(1) : query; for (var _iterator = _createForOfIteratorHelperLoose(query.split(partSeparator)), _step; !(_step = _iterator()).done;) { var pair = _step.value; var _pair$split = pair.split(pairSeparator), key = _pair$split[0], value = _pair$split[1]; if (!key) continue; var decodedKey = decodeURIComponent(key); var decodedValue = value ? decodeURIComponent(value) : ''; parameters[decodedKey] = decodedValue; } return parameters; }