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 URLsetProtocol(data, protocol)
, setter that sets a communication protocolgetProtocol(data)
, getter (selector) that returns a communication protocolsetHost(data, host)
, setter that sets a hostgetHost(data)
, getter that returns a hostsetPath(data, path)
, setter that sets a URL pathgetPath(data)
, getter that returns a URL pathsetQueryParam(data, key, value)
, setter that sets query parametersgetQueryParam(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 querytoString(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.