JavaScript: Link processing

Last update: 04 Nov 23:20
1
Student
100%
Completion rate

url.js

Implement an abstraction for working with URLs. It should extract and change URL parts. Its interface:

  • make(url), a constructor that creates a URL
  • setProtocol(data, protocol), setter that sets a communication protocol
  • getProtocol(data), getter (selector) that returns a communication protocol
  • setHost(data, host), setter that sets a host
  • getHost(data), getter that returns a host
  • setPath(data, path), setter that sets a URL path
  • getPath(data), getter that returns a URL path
  • setQueryParam(data, key, value), setter that sets query parameters
  • getQueryParam(data, paramName, default = null), getter that returns query parameters. The third parameter of the function is the default value, which is returned when no such parameter found in the query
  • toString(data), getter that transforms URL to string
const url = make('https://hexlet.io/community?q=low');

setProtocol(url, 'http:');
toString(url); // 'http://hexlet.io/community?q=low'

setPath(url, '/404');
toString(url); // 'http://hexlet.io/404?q=low'

setQueryParam(url, 'page', 5);
toString(url); // 'http://hexlet.io/404?q=low&page=5'

setQueryParam(url, 'q', 'high');
toString(url); // 'http://hexlet.io/404?q=high&page=5'

Tips

  • Use the standard URL: URL
  • set() and 'get()' methods

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.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests