parseUrl

解析 URL 字符串为结构化对象

用法

parseUrl 函数用于解析 URL 字符串为结构化对象。

import { parseUrl } from '@movk/core'

parseUrl('https://example.com:8080/path?query=1#hash')
// {
//   href: 'https://example.com:8080/path?query=1#hash',
//   protocol: 'https:',
//   host: 'example.com:8080',
//   hostname: 'example.com',
//   port: '8080',
//   pathname: '/path',
//   search: '?query=1',
//   hash: '#hash',
//   auth: '',
//   origin: 'https://example.com:8080'
// }

parseUrl('/path', 'https://example.com')
// 解析相对 URL

API

parseUrl(url, base?)

解析 URL 字符串为结构化对象。

参数

url
string required
要解析的 URL 字符串。
base
string
可选的基础 URL,用于解析相对路径。

返回值

返回值
ParsedUrl | null
解析后的 URL 对象,解析失败返回 null

Changelog

b6146 — refactor: 统一源码文件命名为 camelCase

7c832 — refactor!: 重构模块架构,拆分 utils 为专业化模块

Copyright © 2024 - 2026 YiXuan - MIT License