JavaScript: URL
Last update: 13 Jan 23:20
0
Students
Url.js
Here you will have to implement the Url
class that could extract parts of it from the HTTP address represented by a string. Export this class as default.
The class must contain a constructor and the following methods:
- constructor takes an HTTP address as a string
getScheme()
returns the data transfer protocol (without a colon)getHostName()
returns the hostnamegetQueryParams()
returns the request parameters as object key-value pairsgetQueryParam()
retrieves the value of the request parameter by name. If the given parameter does not exist, the method returns the value specified by the second parameter (null
by default)
While doing the exercise, you will need to work through the documentation and explore the capabilities of the URL class in order to parse the HTTP address string representation.
Examples
const url = new Url('http://google.com:80?key=value&key2=value2');
url.getScheme(); // 'http'
url.getHostName(); // 'google.com'
url.getQueryParams();
// {
// key: 'value',
// key2: 'value2',
// };
url.getQueryParam('key'); // 'value'
// the second parameter is a default value
url.getQueryParam('key2', 'lala'); // 'value2'
url.getQueryParam('new', 'ehu'); // 'ehu'
url.getQueryParam('new'); // null
Tips
- Do not use deprecated language methods (
parse
,format
, etc.) - Examine the URLSearchParams class methods to handle the query string
For full access to the challenge you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.