• Node.js Video Tutorials

NodeJS - url.domainToUnicode() Method



The NodeJS url.domainToUnicode() method of URL class returns Unicode for the ASCII values present in the domain. This method performs the inverse operation to the url.domainToASCII() method.

The Unicode is a modern standard for the consistent encoding, representation, and handling of text expressed in most of the world’s writing system.

Syntax

Following is the syntax of the NodeJS url.domainToUnicode() method of class URL

url.domainToUnicode(domain)

Parameters

  • domain: This parameter specifies a string that will be converted to Unicode.

Return Value

This method returns the Unicode serialization of the domain.

Example

If the domain we pass to the NodeJS url.domainToUnicode() method contains ASCII characters, it will convert those characters to Unicode.

In the following example, we are passing a domain string with ASCII characters to url.domainToUnicode() method.

const url = require('node:url');

let domain = url.domainToUnicode('xn--ewyork-vwa.com');
console.log(domain);

Output

On executing the above program, it will generate the following output

ñewyork.com

Example

In the following example, we are passing a domain string with ASCII characters of Japanese characters to the domainToUnicode() method.

const url = require('node:url');

let domain = url.domainToUnicode('xn--6qq79v.com');
console.log(domain);

Output

On executing the above program, it will generate the following output

你好.com

Example

If we pass an invalid domain name to the domainToUnicode() method, it returns an empty string.

In the following example, we are trying to pass an invalid domain to the domainToUnicode() method.

const url = require('node:url');

let domain = url.domainToUnicode('xn--iñvalid.com');
console.log(domain);

Output

On executing the above program, it will generate the following output

//Returns an empty string
nodejs_url_module.htm
Advertisements