UNPKG

@ray-js/library

Version:

Ray library for browser

55 lines 1.72 kB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["pathname"], _excluded2 = ["____h_a_s_h____"]; import "core-js/modules/esnext.iterator.constructor.js"; import "core-js/modules/esnext.iterator.for-each.js"; import { NamedRegexp } from '../regexp'; import searchParse from './searchParse'; import format from './format'; const URL_REGEX = new NamedRegexp({ regex: /(((?:http|https):)?(?:\/\/)?([^./]([^:/?#]*)(?::(\d+))?))?([^?#]+)?(\?[^#]*)?(#.*)?/, groups: ['origin', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'] }); export default function parse(href) { /* istanbul ignore next */ const { groups = {} } = URL_REGEX.exec(href) || { groups: {} }; Object.keys(groups).forEach(key => { if (!groups[key]) { groups[key] = ''; } }); const { pathname } = groups, restGroups = _objectWithoutProperties(groups, _excluded); const res = _objectSpread({ query: {}, href, pathname: pathname ? pathname : '/' }, restGroups); if (groups.search) { // 小程序不能传递hash,用query.____h_a_s_h____传递 // 回显恢复____h_a_s_h____ ====> hash // 从query中接出hash值 const _searchParse = searchParse(groups.search), { ____h_a_s_h____ } = _searchParse, query = _objectWithoutProperties(_searchParse, _excluded2); res.query = query; if (____h_a_s_h____) { // 剔除 ____h_a_s_h____ res.hash = res.hash || `#${____h_a_s_h____}`; res.search = format({ query }); res.href = format(res); } } return res; }