The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string.
If the urlObject is not an object or string, then it will throw a TypeError.
Return value: It returns string derieved from urlObject.
The urlObject can have the following fields or keys:
The formatting process is as follows:
1. Initially, an empty string (‘’ say result) is created and then following parameters are looked for in order.
2. urlObject.protocol: string
- If urlObject.protocol is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If urlObject.protocol do not end with ASCII colon ( : ) then, the literal ‘:’ is appended to the result.
3. urlObject.slashes: boolean
- If either of the following property is true, then literals ‘//’ are appended to the result:
- urlObject.slashaes is true.
- urlObject.protocol is http, https, ftp, gopher, or file, then slashes will be automatically true even if slashes is false.
4. urlObject.auth: string
- If the urlObject.auth is not undefined and urlObject.host or urlObject.hostname is also not undefined then auth is appended to the result with literal ‘@’ irrespective of whether the literal ‘@’ present or not at the end.
5. urlObject.host: string
- If urlObject.host is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If it is undefined then urlObject.hostname is considered.
6. urlObject.hostname: string
- If urlObject.hostname is a string it is appended to the result else if not undefined and not a string then Error is thrown.
- If both host and hostname are defined then host will be given considered.
7. urlObject.port: (number | string)
- If hostname is considered and urlObject.port is defined then literal ‘:’ will be appended to the result along with urlObject.port.
8. urlObject.pathname: string
- If urlObject.pathname is a string but not empty string and not starting with literal ‘/’, then literal ‘/’ is appended to the result.
- urlObject.pathname is appended to the result.
- Else UrlObject.pathname is not a string then Error is thrown.
9. urlObject.search: string
- If urlObject.search is a string but not empty string and not starting with literal ‘?’, then literal ‘?’ is appended to the result.
- urlObject.search is appended to the result.
- If urlObject.search is not a string then Error is thrown.
10. urlObject.query: Object
- If urlObject.query is an Object then literal ‘?’ is appended to the result along with output of calling the querystring module’s stringify() method passing the value of urlObject.query.
- If both urlObject.search and urlObject.query are defined then urlObject.search will only be considered.
11. urlObject.hash: string
- If urlObject.hash is a string but not empty string and not starting with literal ‘#’, then literal ‘#’ is appended to the result.
- urlObject.hash is appended to the result.
- Else urlObject.hash is not a string and is not undefined then Error is thrown.
12. Finally, the result is returned.
NOTE: The above program will compile and run by using the node fileName.js command.
- Why Node.js ?
- Node js | OS
- Node | URL.protocol API
- HTTPS in Node
- Node | URLSearchParams.set()
- Node | URLSearchParams.has()
- Node | URL.href API
- Node | URL.password API
- Node | URL.hostname API
- Node | URL.hash API
- Node | URL.host API
- Node | URL.origin API
- Node | URL.username API
- Node | URL.domainToASCII
- Node | URLSearchParams.get()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.