Skip to content

url

URL 解析与构建工具。

导入

typescript
import { urlParse, urlStringify, type UrlMeta } from '@cloudcome/utils-core/url'

类型定义

UrlMeta

URL 元信息。

typescript
type UrlMeta = {
  protocol: string
  host: string
  hostname: string
  port: string
  pathname: string
  search: string
  hash: string
  username: string
  password: string
}

属性说明

属性类型描述
protocolstring协议部分,包含冒号,如 'https:'
hoststring主机部分,包括主机名和端口,如 'example.com:8080'
hostnamestring主机名,如 'example.com'
portstring端口,如 '8080'
pathnamestring路径部分,如 '/path/to/page'
searchstring查询字符串,如 '?key=value'
hashstring哈希部分,如 '#section'
usernamestring用户名部分
passwordstring密码部分

函数

urlParse

解析 URL 字符串为组件对象。

typescript
function urlParse(url: string): UrlMeta

参数

参数类型描述
urlstringURL 字符串

返回值

UrlMeta - 解析后的 URL 对象

示例

typescript
const meta = urlParse('https://user:pass@example.com:8080/path?key=value#section')
console.log(meta.protocol) // 'https:'
console.log(meta.hostname) // 'example.com'
console.log(meta.port) // '8080'
console.log(meta.pathname) // '/path'
console.log(meta.search) // '?key=value'
console.log(meta.hash) // '#section'
console.log(meta.username) // 'user'
console.log(meta.password) // 'pass'

urlStringify

将 UrlMeta 对象转换回 URL 字符串。

typescript
function urlStringify(url: UrlMeta): string

参数

参数类型描述
urlUrlMetaURL 对象

返回值

string - URL 字符串

示例

typescript
const url = urlStringify({
  protocol: 'https:',
  host: 'example.com:8080',
  hostname: 'example.com',
  port: '8080',
  pathname: '/path',
  search: '?key=value',
  hash: '#section',
  username: 'user',
  password: 'pass',
})
console.log(url) // 'https://user:pass@example.com:8080/path?key=value#section'

基于 MIT 许可发布