版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. [![npm version][npm-version-src]][npm-version-href]
  2. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  3. [![Github Actions][github-actions-src]][github-actions-href]
  4. [![Codecov][codecov-src]][codecov-href]
  5. [![bundle][bundle-src]][bundle-href]
  6. ![👽 ufo](.github/banner.svg)
  7. ## Install
  8. Install using npm or yarn:
  9. ```bash
  10. npm i ufo
  11. # or
  12. yarn add ufo
  13. ```
  14. Import:
  15. ```js
  16. // CommonJS
  17. const { normalizeURL, joinURL } = require('ufo')
  18. // ESM
  19. import { normalizeURL, joinURL } from 'ufo'
  20. // Deno
  21. import { parseURL } from 'https://unpkg.com/ufo/dist/index.mjs'
  22. ```
  23. **Notice:** You may need to transpile package and add URL polyfill for legacy environments
  24. ## Usage
  25. ### `normalizeURL`
  26. - Ensures URL is properly encoded
  27. - Ensures pathname starts with slash
  28. - Preserves protocol/host if provided
  29. ```ts
  30. // Result: test?query=123%20123#hash,%20test
  31. normalizeURL('test?query=123 123#hash, test')
  32. // Result: http://localhost:3000/
  33. normalizeURL('http://localhost:3000')
  34. ```
  35. ### `joinURL`
  36. ```ts
  37. // Result: a/b/c
  38. joinURL('a', '/b', '/c')
  39. ```
  40. ### `resolveURL`
  41. ```ts
  42. // Result: http://foo.com/foo/bar/baz?test=123#token
  43. resolveURL('http://foo.com/foo?test=123#token', 'bar', 'baz')
  44. ```
  45. ### `parseURL`
  46. ```ts
  47. // Result: { protocol: 'http:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
  48. parseURL('http://foo.com/foo?test=123#token')
  49. // Result: { pathname: 'foo.com/foo', search: '?test=123', hash: '#token' }
  50. parseURL('foo.com/foo?test=123#token')
  51. // Result: { protocol: 'https:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
  52. parseURL('foo.com/foo?test=123#token', 'https://')
  53. ```
  54. ### `withQuery`
  55. ```ts
  56. // Result: /foo?page=a&token=secret
  57. withQuery('/foo?page=a', { token: 'secret' })
  58. ```
  59. ### `getQuery`
  60. ```ts
  61. // Result: { test: '123', unicode: '好' }
  62. getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD')
  63. ```
  64. ### `$URL`
  65. Implementing URL interface with some improvements:
  66. - Supporting schemeless and hostless URLs
  67. - Supporting relative URLs
  68. - Preserving trailing-slash status
  69. - Decoded and mutable classs properties (`protocol`, `host`, `auth`, `pathname`, `query`, `hash`)
  70. - Consistent URL parser independent of environment
  71. - Consistent encoding independent of environment
  72. - Punycode support for host encoding
  73. ### `withTrailingSlash`
  74. Ensures url ends with a trailing slash
  75. ```ts
  76. // Result: /foo/
  77. withTrailingSlash('/foo')
  78. ```
  79. ```ts
  80. // Result: /path/?query=true
  81. withTrailingSlash('/path?query=true', true)
  82. ```
  83. ### `withoutTrailingSlash`
  84. Ensures url does not ends with a trailing slash
  85. ```ts
  86. // Result: /foo
  87. withoutTrailingSlash('/foo/')
  88. ```
  89. ```ts
  90. // Result: /path?query=true
  91. withoutTrailingSlash('/path/?query=true', true)
  92. ```
  93. ### `cleanDoubleSlashes`
  94. Ensures url does not have double slash (except for protocol)
  95. ```ts
  96. // Result: /foo/bar/
  97. cleanDoubleSlashes('//foo//bar//')
  98. // Result: http://example.com/analyze/http://localhost:3000/
  99. cleanDoubleSlashes('http://example.com/analyze//http://localhost:3000//')
  100. ```
  101. ### `isSamePath`
  102. Check two paths are equal or not. Trailing slash and encoding are normalized before comparation.
  103. ```ts
  104. // Result: true
  105. isSamePath('/foo', '/foo/')
  106. ```
  107. ### `isRelative`
  108. Check if a path starts with `./` or `../`.
  109. ```ts
  110. // Result: true
  111. isRelative('./foo')
  112. ```
  113. ### `withHttp`
  114. Ensures url protocol is `http`
  115. ```ts
  116. // Result: http://example.com
  117. withHttp('https://example.com')
  118. ```
  119. ### `withHttps`
  120. Ensures url protocol is `https`
  121. ```ts
  122. // Result: https://example.com
  123. withHttps('http://example.com')
  124. ```
  125. ### `withProtocol`
  126. Changes url protocol passed as second argument
  127. ```ts
  128. // Result: ftp://example.com
  129. withProtocol('http://example.com', 'ftp://')
  130. ```
  131. ### `withoutProtocol`
  132. Removes url protocol
  133. ```ts
  134. // Result: example.com
  135. withoutProtocol('http://example.com')
  136. ```
  137. ### `isEqual`
  138. Compare two URLs regardless of their slash condition or encoding:
  139. ```ts
  140. // Result: true
  141. isEqual('/foo', 'foo')
  142. isEqual('foo/', 'foo')
  143. isEqual('/foo bar', '/foo%20bar')
  144. // Strict compare
  145. // Result: false
  146. isEqual('/foo', 'foo', { leadingSlash: true })
  147. isEqual('foo/', 'foo', { trailingSlash: true })
  148. isEqual('/foo bar', '/foo%20bar', { encoding: true })
  149. ```
  150. ## License
  151. [MIT](./LICENSE)
  152. Special thanks to Eduardo San Martin Morote ([posva](https://github.com/posva)) for [encoding utlities](https://github.com/vuejs/vue-router-next/blob/v4.0.1/src/encoding.ts)
  153. <!-- Badges -->
  154. [npm-version-src]: https://img.shields.io/npm/v/ufo?style=flat-square
  155. [npm-version-href]: https://npmjs.com/package/ufo
  156. [npm-downloads-src]: https://img.shields.io/npm/dm/ufo?style=flat-square
  157. [npm-downloads-href]: https://npmjs.com/package/ufo
  158. [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/ufo/ci/main?style=flat-square
  159. [github-actions-href]: https://github.com/unjs/ufo/actions?query=workflow%3Aci
  160. [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/ufo/main?style=flat-square
  161. [codecov-href]: https://codecov.io/gh/unjs/ufo
  162. [bundle-src]: https://img.shields.io/bundlephobia/minzip/ufo?style=flat-square
  163. [bundle-href]: https://bundlephobia.com/result?p=ufo