@@ -21,13 +21,13 @@ Import:
21
21
22
22
``` js
23
23
// CommonJS
24
- const { normalizeURL , joinURL } = require (' ufo' )
24
+ const { normalizeURL , joinURL } = require (" ufo" );
25
25
26
26
// ESM
27
- import { normalizeURL , joinURL } from ' ufo'
27
+ import { normalizeURL , joinURL } from " ufo" ;
28
28
29
29
// Deno
30
- import { parseURL } from ' https://unpkg.com/ufo/dist/index.mjs'
30
+ import { parseURL } from " https://unpkg.com/ufo/dist/index.mjs" ;
31
31
```
32
32
33
33
** Notice:** You may need to transpile package and add URL polyfill for legacy environments
@@ -41,72 +41,72 @@ import { parseURL } from 'https://unpkg.com/ufo/dist/index.mjs'
41
41
- Preserves protocol/host if provided
42
42
43
43
``` ts
44
- normalizeURL (' test?query=123 123#hash, test' )
44
+ normalizeURL (" test?query=123 123#hash, test" );
45
45
// test?query=123%20123#hash,%20test
46
46
47
- normalizeURL (' http://localhost:3000' )
47
+ normalizeURL (" http://localhost:3000" );
48
48
// http://localhost:3000/
49
49
```
50
50
51
51
### ` joinURL `
52
52
53
53
``` ts
54
- joinURL (' a ' , ' /b ' , ' /c ' )
54
+ joinURL (" a " , " /b " , " /c " );
55
55
// a/b/c
56
56
```
57
57
58
58
### ` resolveURL `
59
59
60
60
``` ts
61
- resolveURL (' http://foo.com/foo?test=123#token' , ' bar' , ' baz' )
61
+ resolveURL (" http://foo.com/foo?test=123#token" , " bar" , " baz" );
62
62
// http://foo.com/foo/bar/baz?test=123#token
63
63
```
64
64
65
65
### ` parseURL `
66
66
67
67
``` ts
68
- parseURL (' http://foo.com/foo?test=123#token' )
68
+ parseURL (" http://foo.com/foo?test=123#token" );
69
69
// { protocol: 'http:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
70
70
71
- parseURL (' foo.com/foo?test=123#token' )
71
+ parseURL (" foo.com/foo?test=123#token" );
72
72
// { pathname: 'foo.com/foo', search: '?test=123', hash: '#token' }
73
73
74
- parseURL (' foo.com/foo?test=123#token' , ' https://' )
74
+ parseURL (" foo.com/foo?test=123#token" , " https://" );
75
75
// { protocol: 'https:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
76
76
```
77
77
78
78
### ` stringifyParsedURL `
79
79
80
80
``` ts
81
- const obj = parseURL (' http://foo.com/foo?test=123#token' )
82
- obj .host = ' bar.com'
81
+ const obj = parseURL (" http://foo.com/foo?test=123#token" );
82
+ obj .host = " bar.com" ;
83
83
84
- stringifyParsedURL (obj )
84
+ stringifyParsedURL (obj );
85
85
// http://bar.com/foo?test=123#token
86
86
```
87
87
88
88
### ` withQuery `
89
89
90
90
``` ts
91
- withQuery (' /foo?page=a' , { token: ' secret' })
91
+ withQuery (" /foo?page=a" , { token: " secret" });
92
92
// /foo?page=a&token=secret
93
93
```
94
94
95
95
### ` getQuery `
96
96
97
97
``` ts
98
- getQuery (' http://foo.com/foo?test=123&unicode=%E5%A5%BD' )
98
+ getQuery (" http://foo.com/foo?test=123&unicode=%E5%A5%BD" );
99
99
// { test: '123', unicode: '好' }
100
100
```
101
101
102
102
### ` parseFilename `
103
103
104
104
``` ts
105
105
// Result: filename.ext
106
- parseFilename (' http://example.com/path/to/filename.ext' )
106
+ parseFilename (" http://example.com/path/to/filename.ext" );
107
107
108
108
// Result: undefined
109
- parseFilename (' /path/to/.hidden-file' , { strict: true })
109
+ parseFilename (" /path/to/.hidden-file" , { strict: true });
110
110
```
111
111
112
112
### ` $URL `
@@ -122,7 +122,7 @@ Implementing URL interface with improvements:
122
122
- Punycode support for host encoding
123
123
124
124
``` ts
125
- new $URL (' http://localhost:3000/hello?world=true' )
125
+ new $URL (" http://localhost:3000/hello?world=true" );
126
126
// { protocol: 'http:', host: 'localhost:3000', auth: '', pathname: '/hello', query: { world: 'true' }, hash: '' }
127
127
```
128
128
@@ -131,14 +131,14 @@ new $URL('http://localhost:3000/hello?world=true')
131
131
Ensures url ends with a trailing slash.
132
132
133
133
``` ts
134
- withTrailingSlash (' /foo' )
134
+ withTrailingSlash (" /foo" );
135
135
// /foo/
136
136
```
137
137
138
138
Set the second option to ` true ` to support query parameters:
139
139
140
140
``` ts
141
- withTrailingSlash (' /path?query=true' , true )
141
+ withTrailingSlash (" /path?query=true" , true );
142
142
// /path/?query=true
143
143
```
144
144
@@ -147,14 +147,14 @@ withTrailingSlash('/path?query=true', true)
147
147
Ensures url does not ends with a trailing slash.
148
148
149
149
``` ts
150
- withoutTrailingSlash (' /foo/' )
150
+ withoutTrailingSlash (" /foo/" );
151
151
// /foo
152
152
```
153
153
154
154
Set the second option to ` true ` to support query parameters:
155
155
156
156
``` ts
157
- withoutTrailingSlash (' /path/?query=true' , true )
157
+ withoutTrailingSlash (" /path/?query=true" , true );
158
158
// /path?query=true
159
159
```
160
160
@@ -163,10 +163,10 @@ withoutTrailingSlash('/path/?query=true', true)
163
163
Ensures url does not have double slash (except for protocol).
164
164
165
165
``` ts
166
- cleanDoubleSlashes (' //foo//bar//' )
166
+ cleanDoubleSlashes (" //foo//bar//" );
167
167
// /foo/bar/
168
168
169
- cleanDoubleSlashes (' http://example.com/analyze//http://localhost:3000//' )
169
+ cleanDoubleSlashes (" http://example.com/analyze//http://localhost:3000//" );
170
170
// http://example.com/analyze/http://localhost:3000/
171
171
```
172
172
@@ -175,7 +175,7 @@ cleanDoubleSlashes('http://example.com/analyze//http://localhost:3000//')
175
175
Check two paths are equal or not. Trailing slash and encoding are normalized before comparison.
176
176
177
177
``` ts
178
- isSamePath (' /foo' , ' /foo/' )
178
+ isSamePath (" /foo" , " /foo/" );
179
179
// true
180
180
```
181
181
@@ -184,7 +184,7 @@ isSamePath('/foo', '/foo/')
184
184
Check if a path starts with ` ./ ` or ` ../ ` .
185
185
186
186
``` ts
187
- isRelative (' ./foo' )
187
+ isRelative (" ./foo" );
188
188
// true
189
189
```
190
190
@@ -193,7 +193,7 @@ isRelative('./foo')
193
193
Ensures url protocol is ` http `
194
194
195
195
``` ts
196
- withHttp (' https://example.com' )
196
+ withHttp (" https://example.com" );
197
197
// http://example.com
198
198
```
199
199
@@ -202,7 +202,7 @@ withHttp('https://example.com')
202
202
Ensures url protocol is ` https `
203
203
204
204
``` ts
205
- withHttps (' http://example.com' )
205
+ withHttps (" http://example.com" );
206
206
// https://example.com
207
207
```
208
208
@@ -211,7 +211,7 @@ withHttps('http://example.com')
211
211
Changes url protocol passed as second argument
212
212
213
213
``` ts
214
- withProtocol (' http://example.com' , ' ftp://' )
214
+ withProtocol (" http://example.com" , " ftp://" );
215
215
// ftp://example.com
216
216
```
217
217
@@ -220,7 +220,7 @@ withProtocol('http://example.com', 'ftp://')
220
220
Removes url protocol
221
221
222
222
``` ts
223
- withoutProtocol (' http://example.com' )
223
+ withoutProtocol (" http://example.com" );
224
224
// example.com
225
225
```
226
226
@@ -229,19 +229,19 @@ withoutProtocol('http://example.com')
229
229
Compare two URLs regardless of their slash condition or encoding:
230
230
231
231
``` ts
232
- isEqual (' /foo' , ' foo' )
232
+ isEqual (" /foo" , " foo" );
233
233
// true
234
- isEqual (' foo/' , ' foo' )
234
+ isEqual (" foo/" , " foo" );
235
235
// true
236
- isEqual (' /foo bar' , ' /foo%20bar' )
236
+ isEqual (" /foo bar" , " /foo%20bar" );
237
237
// true
238
238
239
239
// Strict compare
240
- isEqual (' /foo' , ' foo' , { leadingSlash: true })
240
+ isEqual (" /foo" , " foo" , { leadingSlash: true });
241
241
// false
242
- isEqual (' foo/' , ' foo' , { trailingSlash: true })
242
+ isEqual (" foo/" , " foo" , { trailingSlash: true });
243
243
// false
244
- isEqual (' /foo bar' , ' /foo%20bar' , { encoding: true })
244
+ isEqual (" /foo bar" , " /foo%20bar" , { encoding: true });
245
245
// false
246
246
```
247
247
@@ -250,11 +250,11 @@ isEqual('/foo bar', '/foo%20bar', { encoding: true })
250
250
Add a fragment (or hash) to a URL:
251
251
252
252
``` ts
253
- withFragment (' /foo' , ' bar' )
253
+ withFragment (" /foo" , " bar" );
254
254
// /foo#bar
255
- withFragment (' /foo#bar' , ' baz' )
255
+ withFragment (" /foo#bar" , " baz" );
256
256
// /foo#baz
257
- withFragment (' /foo#bar' , ' ' )
257
+ withFragment (" /foo#bar" , " " );
258
258
// /foo
259
259
```
260
260
@@ -265,6 +265,7 @@ withFragment('/foo#bar', '')
265
265
Special thanks to Eduardo San Martin Morote ([ posva] ( https://github.com/posva ) ) for [ encoding utilities] ( https://github.com/vuejs/vue-router-next/blob/v4.0.1/src/encoding.ts )
266
266
267
267
<!-- Badges -->
268
+
268
269
[ npm-version-src ] : https://img.shields.io/npm/v/ufo?style=flat&colorA=18181B&colorB=F0DB4F
269
270
[ npm-version-href ] : https://npmjs.com/package/ufo
270
271
[ npm-downloads-src ] : https://img.shields.io/npm/dm/ufo?style=flat&colorA=18181B&colorB=F0DB4F
0 commit comments