Skip to content

Commit 5a2db15

Browse files
jasnellitaloacasas
authored andcommitted
doc: add documentation for url.format(URL[, options]);
Backport-of: #10857
1 parent 7aaa960 commit 5a2db15

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

doc/api/url.md

+42
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,47 @@ The formatting process operates as follows:
198198
string, an [`Error`][] is thrown.
199199
* `result` is returned.
200200

201+
## url.format(URL[, options])
202+
203+
> Stability: 1 - Experimental
204+
205+
* `URL` {URL} A [WHATWG URL][] object
206+
* `options` {Object}
207+
* `auth` {Boolean} `true` if the serialized URL string should include the
208+
username and password, `false` otherwise. Defaults to `true`.
209+
* `fragment` {Boolean} `true` if the serialized URL string should include the
210+
fragment, `false` otherwise. Defaults to `true`.
211+
* `search` {Boolean} `true` if the serialized URL string should include the
212+
search query, `false` otherwise. Defaults to `true`.
213+
* `unicode` (Boolean) `true` if Unicode characters appearing in the host
214+
component of the URL string should be encoded directly as opposed to being
215+
Punycode encoded. Defaults to `false`.
216+
217+
Returns a customizable serialization of a URL String representation of a
218+
[WHATWG URL][] object.
219+
220+
The URL object has both a `toString()` method and `href` property that return
221+
string serializations of the URL. These are not, however, customizable in
222+
any way. The `url.format(URL[, options])` method allows for basic customization
223+
of the output.
224+
225+
For example:
226+
227+
```js
228+
const myURL = new URL('https://a:b@你好你好?abc#foo');
229+
230+
console.log(myURL.href);
231+
// Prints https://a:b@xn--6qqa088eba/?abc#foo
232+
233+
console.log(myURL.toString());
234+
// Prints https://a:b@xn--6qqa088eba/?abc#foo
235+
236+
console.log(url.format(myURL, {fragment: false, unicode: true, auth: false}));
237+
// Prints 'https://你好你好?abc'
238+
```
239+
240+
*Note*: This variation of the `url.format()` method is currently considered to
241+
be experimental.
201242

202243
## url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
203244
<!-- YAML
@@ -712,3 +753,4 @@ console.log(myURL.origin);
712753
[`url.parse()`]: #url_url_parse_urlstring_parsequerystring_slashesdenotehost
713754
[`url.format()`]: #url_url_format_urlobject
714755
[Punycode]: https://tools.ietf.org/html/rfc5891#section-4.4
756+
[WHATWG URL]: #url_the_whatwg_url_api

tools/doc/type-parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const typeMap = {
3737
'http.Server': 'http.html#http_class_http_server',
3838
'http.ServerResponse': 'http.html#http_class_http_serverresponse',
3939
'Iterator': jsDocPrefix +
40-
'Reference/Iteration_protocols#The_iterator_protocol'
40+
'Reference/Iteration_protocols#The_iterator_protocol',
41+
'URL': 'url.html#url_the_whatwg_url_api'
4142
};
4243

4344
module.exports = {

0 commit comments

Comments
 (0)