@acta/url 中文文档教程

发布于 5年前 浏览 12 项目主页 更新于 3年前

URL @acta/url

将 URL 解析为格式化对象并从中构建 URL对象。

Table of Contents

Parsing URLs

返回目录 ↑

您可以这样解析 URL:

import URL from '@acta/url';

const parsedURL = URL.parse('http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue')

/**
 {
    auth: { password: 'password', username: 'username' },
    domain: 'domain',
    domainChain: ['tld', 'domain', 'subdomain'],
    hash: 'hashvalue',
    host: 'subdomain.domain.tld:8080',
    hostname: 'subdomain.domain.tld',
    href: 'http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue',
    isValidURL: true,
    origin: 'http://subdomain.domain.tld:8080',
    path: '/foo/bar/baz?key1=value1&key2=value2',
    pathname: '/foo/bar/baz',
    port: '8080',
    protocol: 'http:',
    query: 'key1=value1&key2=value2',
    queryParams: { key1: 'value1', key2: 'value2' },
    search: '?key1=value1&key2=value2',
    tld: 'tld',
  }
  */

集成了查询解析器。

当缺少元素时,其值为 null(例如没有哈希:hash: null)。

Formating URLs

返回目录 ↑

您可以这样格式化 URL:

import URL from '@acta/url';

const formatedURL = URL.format({
  auth: { password: 'password', username: 'username' },
  domain: 'domain',
  domainChain: ['tld', 'domain', 'subdomain'],
  hash: 'hashvalue',
  host: 'subdomain.domain.tld:8080',
  hostname: 'subdomain.domain.tld',
  href: 'http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue',
  isValidURL: true,
  origin: 'http://subdomain.domain.tld:8080',
  path: '/foo/bar/baz?key1=value1&key2=value2',
  pathname: '/foo/bar/baz',
  port: '8080',
  protocol: 'http:',
  query: 'key1=value1&key2=value2',
  queryParams: { key1: 'value1', key2: 'value2' },
  search: '?key1=value1&key2=value2',
  tld: 'tld',
})

/**
 * http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue
 */

Dev scripts

返回 ToC ↑

  • npm run dev to dev (build, test and watch)
  • npm run build to build the production version
  • npm run release to release a new version. Relies on release-it for deployments.

要使用来自另一个应用程序或包的包在本地进行开发,请转到符号链接:npm link /the/absolute/path/url

发布:npm publish --access=public

URL @acta/url

To parse URLs to formated objects and build URLs from objects.

Table of Contents

Parsing URLs

back to ToC ↑

You can parse an URL that way:

import URL from '@acta/url';

const parsedURL = URL.parse('http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue')

/**
 {
    auth: { password: 'password', username: 'username' },
    domain: 'domain',
    domainChain: ['tld', 'domain', 'subdomain'],
    hash: 'hashvalue',
    host: 'subdomain.domain.tld:8080',
    hostname: 'subdomain.domain.tld',
    href: 'http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue',
    isValidURL: true,
    origin: 'http://subdomain.domain.tld:8080',
    path: '/foo/bar/baz?key1=value1&key2=value2',
    pathname: '/foo/bar/baz',
    port: '8080',
    protocol: 'http:',
    query: 'key1=value1&key2=value2',
    queryParams: { key1: 'value1', key2: 'value2' },
    search: '?key1=value1&key2=value2',
    tld: 'tld',
  }
  */

The query parser is integrated.

When an element is missing, its value is null (ex without hash: hash: null).

Formating URLs

back to ToC ↑

You can format an URL that way:

import URL from '@acta/url';

const formatedURL = URL.format({
  auth: { password: 'password', username: 'username' },
  domain: 'domain',
  domainChain: ['tld', 'domain', 'subdomain'],
  hash: 'hashvalue',
  host: 'subdomain.domain.tld:8080',
  hostname: 'subdomain.domain.tld',
  href: 'http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue',
  isValidURL: true,
  origin: 'http://subdomain.domain.tld:8080',
  path: '/foo/bar/baz?key1=value1&key2=value2',
  pathname: '/foo/bar/baz',
  port: '8080',
  protocol: 'http:',
  query: 'key1=value1&key2=value2',
  queryParams: { key1: 'value1', key2: 'value2' },
  search: '?key1=value1&key2=value2',
  tld: 'tld',
})

/**
 * http://username:password@subdomain.domain.tld:8080/foo/bar/baz?key1=value1&key2=value2#hashvalue
 */

Dev scripts

back to ToC ↑

  • npm run dev to dev (build, test and watch)
  • npm run build to build the production version
  • npm run release to release a new version. Relies on release-it for deployments.

To develop in local using the package from another application or package, go for a symlink: npm link /the/absolute/path/url.

To publish: npm publish --access=public.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文