url-operator 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "url-operator",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
7
- "lib"
7
+ "lib",
8
+ "src"
8
9
  ],
9
10
  "type": "module",
10
11
  "main": "lib/index.js",
@@ -0,0 +1,21 @@
1
+ import { go } from '@blackglory/prelude'
2
+
3
+ export function appendPathname(url: URL, pathname: string): URL {
4
+ const baseURL = go(() => {
5
+ const baseURL = new URL(url)
6
+ if (!baseURL.pathname.endsWith('/')) {
7
+ baseURL.pathname = `${baseURL.pathname}/`
8
+ }
9
+
10
+ return baseURL
11
+ })
12
+
13
+ const newURL = new URL(
14
+ pathname.replace(/^\/*/, '')
15
+ , baseURL
16
+ )
17
+ newURL.search = baseURL.search
18
+ newURL.hash = baseURL.hash
19
+
20
+ return newURL
21
+ }
@@ -0,0 +1,13 @@
1
+ export function appendSearchParam(
2
+ url: URL
3
+ , name: string
4
+ , value: string | number
5
+ ): URL {
6
+ const newURL = new URL(url)
7
+
8
+ const newSearchParams = new URLSearchParams(newURL.searchParams)
9
+ newSearchParams.append(name, value.toString())
10
+ newURL.search = newSearchParams.toString()
11
+
12
+ return newURL
13
+ }
package/src/decode.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function decode(url: string): string {
2
+ return decodeURI(url)
3
+ }
package/src/encode.ts ADDED
@@ -0,0 +1,3 @@
1
+ export function encode(url: string): string {
2
+ return encodeURI(decodeURI(url))
3
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ export * from './set-protocol.js'
2
+ export * from './set-username.js'
3
+ export * from './set-password.js'
4
+ export * from './set-host.js'
5
+ export * from './set-port.js'
6
+ export * from './set-pathname.js'
7
+ export * from './append-pathname.js'
8
+ export * from './set-search.js'
9
+ export * from './set-search-param.js'
10
+ export * from './set-search-params.js'
11
+ export * from './append-search-param.js'
12
+ export * from './set-hash.js'
13
+ export * from './encode.js'
14
+ export * from './decode.js'
@@ -0,0 +1,7 @@
1
+ export function setHash(url: URL, hash: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.hash = hash
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,7 @@
1
+ export function setHost(url: URL, host: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.host = host
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,7 @@
1
+ export function setPassword(url: URL, password: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.password = password
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,7 @@
1
+ export function setPathname(url: URL, pathname: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.pathname = pathname
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,7 @@
1
+ export function setPort(url: URL, port: number): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.port = port.toString()
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,15 @@
1
+ export function setProtocol(url: URL, protocol: string): URL {
2
+ const oldURL = new URL(url)
3
+
4
+ // WHATWG URL标准在更改协议时有一些怪癖, 故在此通过直接修改字符串来达到目的.
5
+ const newURL = new URL(
6
+ oldURL.href.replace(
7
+ /^(\S+:)\/\//
8
+ , protocol.endsWith(':')
9
+ ? protocol
10
+ : `${protocol}://`
11
+ )
12
+ )
13
+
14
+ return newURL
15
+ }
@@ -0,0 +1,13 @@
1
+ export function setSearchParam(
2
+ url: URL
3
+ , name: string
4
+ , value: string | number
5
+ ): URL {
6
+ const newURL = new URL(url)
7
+
8
+ const newSearchParams = new URLSearchParams(newURL.searchParams)
9
+ newSearchParams.set(name, value.toString())
10
+ newURL.search = newSearchParams.toString()
11
+
12
+ return newURL
13
+ }
@@ -0,0 +1,14 @@
1
+ export function setSearchParams(
2
+ url: URL
3
+ , searchParams: Record<string, string | number>
4
+ ): URL {
5
+ const newURL = new URL(url)
6
+
7
+ const newSearchParams = new URLSearchParams(newURL.searchParams)
8
+ for (const [name, value] of Object.entries(searchParams)) {
9
+ newSearchParams.set(name, value.toString())
10
+ }
11
+ newURL.search = newSearchParams.toString()
12
+
13
+ return newURL
14
+ }
@@ -0,0 +1,7 @@
1
+ export function setSearch(url: URL, search: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.search = search
5
+
6
+ return newURL
7
+ }
@@ -0,0 +1,7 @@
1
+ export function setUsername(url: URL, username: string): URL {
2
+ const newURL = new URL(url)
3
+
4
+ newURL.username = username
5
+
6
+ return newURL
7
+ }