function format
Usage in Deno
import { format } from "node:url";
format(urlObject: URL,options?: URLFormatOptions,): string
The url.format() method returns a formatted URL string derived from urlObject.
const url = require('node:url'); url.format({ protocol: 'https', hostname: 'example.com', pathname: '/some/path', query: { page: 1, format: 'json', }, }); // => 'https://example.com/some/path?page=1&format=json'
If urlObject is not an object or a string, url.format() will throw a TypeError.
The formatting process operates as follows:
- A new empty string
resultis created. - If
urlObject.protocolis a string, it is appended as-is toresult. - Otherwise, if
urlObject.protocolis notundefinedand is not a string, anErroris thrown. - For all string values of
urlObject.protocolthat do not end with an ASCII colon (:) character, the literal string:will be appended toresult. - If either of the following conditions is true, then the literal string
//will be appended toresult:urlObject.slashesproperty is true;urlObject.protocolbegins withhttp,https,ftp,gopher, orfile;
- If the value of the
urlObject.authproperty is truthy, and eitherurlObject.hostorurlObject.hostnameare notundefined, the value ofurlObject.authwill be coerced into a string and appended toresultfollowed by the literal string@. - If the
urlObject.hostproperty isundefinedthen:- If the
urlObject.hostnameis a string, it is appended toresult. - Otherwise, if
urlObject.hostnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.portproperty value is truthy, andurlObject.hostnameis notundefined: * The literal string:is appended toresult, and * The value ofurlObject.portis coerced to a string and appended toresult.
- If the
- Otherwise, if the
urlObject.hostproperty value is truthy, the value ofurlObject.hostis coerced to a string and appended toresult. - If the
urlObject.pathnameproperty is a string that is not an empty string:- If the
urlObject.pathnamedoes not start with an ASCII forward slash (/), then the literal string'/'is appended toresult. - The value of
urlObject.pathnameis appended toresult.
- If the
- Otherwise, if
urlObject.pathnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.searchproperty isundefinedand if theurlObject.queryproperty is anObject, the literal string?is appended toresultfollowed by the output of calling thequerystringmodule'sstringify()method passing the value ofurlObject.query. - Otherwise, if
urlObject.searchis a string:- If the value of
urlObject.searchdoes not start with the ASCII question mark (?) character, the literal string?is appended toresult. - The value of
urlObject.searchis appended toresult.
- If the value of
- Otherwise, if
urlObject.searchis notundefinedand is not a string, anErroris thrown. - If the
urlObject.hashproperty is a string:- If the value of
urlObject.hashdoes not start with the ASCII hash (#) character, the literal string#is appended toresult. - The value of
urlObject.hashis appended toresult.
- If the value of
- Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
urlObject: URL
A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse().
optional
options: URLFormatOptions
string
format(urlObject: UrlObject | string): string
The url.format() method returns a formatted URL string derived from urlObject.
const url = require('node:url'); url.format({ protocol: 'https', hostname: 'example.com', pathname: '/some/path', query: { page: 1, format: 'json', }, }); // => 'https://example.com/some/path?page=1&format=json'
If urlObject is not an object or a string, url.format() will throw a TypeError.
The formatting process operates as follows:
- A new empty string
resultis created. - If
urlObject.protocolis a string, it is appended as-is toresult. - Otherwise, if
urlObject.protocolis notundefinedand is not a string, anErroris thrown. - For all string values of
urlObject.protocolthat do not end with an ASCII colon (:) character, the literal string:will be appended toresult. - If either of the following conditions is true, then the literal string
//will be appended toresult:urlObject.slashesproperty is true;urlObject.protocolbegins withhttp,https,ftp,gopher, orfile;
- If the value of the
urlObject.authproperty is truthy, and eitherurlObject.hostorurlObject.hostnameare notundefined, the value ofurlObject.authwill be coerced into a string and appended toresultfollowed by the literal string@. - If the
urlObject.hostproperty isundefinedthen:- If the
urlObject.hostnameis a string, it is appended toresult. - Otherwise, if
urlObject.hostnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.portproperty value is truthy, andurlObject.hostnameis notundefined: * The literal string:is appended toresult, and * The value ofurlObject.portis coerced to a string and appended toresult.
- If the
- Otherwise, if the
urlObject.hostproperty value is truthy, the value ofurlObject.hostis coerced to a string and appended toresult. - If the
urlObject.pathnameproperty is a string that is not an empty string:- If the
urlObject.pathnamedoes not start with an ASCII forward slash (/), then the literal string'/'is appended toresult. - The value of
urlObject.pathnameis appended toresult.
- If the
- Otherwise, if
urlObject.pathnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.searchproperty isundefinedand if theurlObject.queryproperty is anObject, the literal string?is appended toresultfollowed by the output of calling thequerystringmodule'sstringify()method passing the value ofurlObject.query. - Otherwise, if
urlObject.searchis a string:- If the value of
urlObject.searchdoes not start with the ASCII question mark (?) character, the literal string?is appended toresult. - The value of
urlObject.searchis appended toresult.
- If the value of
- Otherwise, if
urlObject.searchis notundefinedand is not a string, anErroris thrown. - If the
urlObject.hashproperty is a string:- If the value of
urlObject.hashdoes not start with the ASCII hash (#) character, the literal string#is appended toresult. - The value of
urlObject.hashis appended toresult.
- If the value of
- Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
urlObject: UrlObject | string
A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse().
string