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 +3 -2
- package/src/append-pathname.ts +21 -0
- package/src/append-search-param.ts +13 -0
- package/src/decode.ts +3 -0
- package/src/encode.ts +3 -0
- package/src/index.ts +14 -0
- package/src/set-hash.ts +7 -0
- package/src/set-host.ts +7 -0
- package/src/set-password.ts +7 -0
- package/src/set-pathname.ts +7 -0
- package/src/set-port.ts +7 -0
- package/src/set-protocol.ts +15 -0
- package/src/set-search-param.ts +13 -0
- package/src/set-search-params.ts +14 -0
- package/src/set-search.ts +7 -0
- package/src/set-username.ts +7 -0
package/package.json
CHANGED
|
@@ -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
package/src/encode.ts
ADDED
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'
|
package/src/set-hash.ts
ADDED
package/src/set-host.ts
ADDED
package/src/set-port.ts
ADDED
|
@@ -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
|
+
}
|