Skip to content

URL工具 (URL)

@pt/utils/modules/url 提供了URL处理相关的工具函数。

parseURL

解析URL字符串为对象。

typescript
import { parseURL } from '@pt/utils/modules/url';

const urlInfo = parseURL('https://example.com:8080/path?query=value#hash');
console.log(urlInfo);
// {
//   protocol: 'https:',
//   host: 'example.com:8080',
//   hostname: 'example.com',
//   port: '8080',
//   pathname: '/path',
//   search: '?query=value',
//   hash: '#hash',
//   params: { query: 'value' }
// }

buildURL

构建URL字符串。

typescript
import { buildURL } from '@pt/utils/modules/url';

const url = buildURL('https://example.com', {
  page: 1,
  size: 10,
  search: 'test'
});
// 'https://example.com?page=1&size=10&search=test'

getURLParam

获取URL中的指定参数。

typescript
import { getURLParam } from '@pt/utils/modules/url';

const url = 'https://example.com?name=john&age=25';
getURLParam(url, 'name'); // 'john'
getURLParam(url, 'age'); // '25'

Released under the MIT License.