Skip to content

Instantly share code, notes, and snippets.

@sgromkov
Created March 6, 2020 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgromkov/76f95905e3df635e4931f04713eeef35 to your computer and use it in GitHub Desktop.
Save sgromkov/76f95905e3df635e4931f04713eeef35 to your computer and use it in GitHub Desktop.
Stick the url with parameters. Covered by unit tests on Jest
const paramsReducer = function paramsReducer(acc, param) {
const splittedParam = param.split('=');
const key = splittedParam[0];
const value = splittedParam[1];
return Object.assign({}, acc, { [key]: value });
};
/**
* Returns the url with the pasted parameters
* @param {string} url Url to which you want to stick the params
* @param {Object} params Parameters that are stuck to the given url
* @returns {string} Url with the pasted parameters
*/
export default function getUrlWithParams(url, params = {}) {
if (typeof url !== 'string') {
throw new Error('url should be a String');
}
const separatorIndex = url.indexOf('?');
const defaultPath = (separatorIndex === -1)
? url
: url.substring(0, separatorIndex);
const defaultParams = (separatorIndex === -1)
? {}
: url.substring(separatorIndex + 1).split('&').reduce(paramsReducer, {});
const joinedParams = Object.assign({}, defaultParams, params);
const clearParamsKeys = Object.keys(joinedParams).filter((key) => {
const value = joinedParams[key];
return (typeof value !== 'undefined' && value !== null && value !== '');
});
const urlParams = clearParamsKeys.map((key) => `${key}=${joinedParams[key]}`);
const separator = urlParams.length !== 0 ? '?' : '';
const resultUrl = `${defaultPath}${separator}${urlParams.join('&')}`;
return resultUrl;
}
import getUrlWithParams from './getUrlWithParams';
describe('getUrlWithParams()', () => {
test('should return an empty string if the url is not passed', () => {
expect(getUrlWithParams('')).toEqual('');
});
test('should return an unchanged url if parameters are not passed', () => {
expect(getUrlWithParams('https://site.com')).toEqual('https://site.com');
expect(getUrlWithParams('https://site.com?')).toEqual('https://site.com');
expect(getUrlWithParams('https://site.com?defaultParam1=1')).toEqual('https://site.com?defaultParam1=1');
expect(getUrlWithParams('https://site.com', {})).toEqual('https://site.com');
expect(getUrlWithParams('https://site.com?', {})).toEqual('https://site.com');
expect(getUrlWithParams('https://site.com?defaultParam1=1', {})).toEqual('https://site.com?defaultParam1=1');
expect(getUrlWithParams(
'https://site.com?defaultParam1=1&defaultParam2=2', {})
).toEqual('https://site.com?defaultParam1=1&defaultParam2=2');
expect(
getUrlWithParams('https://site.com', {
param1: undefined,
param2: null,
param3: ''
})
).toEqual('https://site.com');
expect(
getUrlWithParams('https://site.com?defaultParam=1', {
param1: undefined,
param2: null,
param3: ''
})
).toEqual('https://site.com?defaultParam=1');
});
test('should return the url that is linked with the parameters', () => {
expect(
getUrlWithParams('https://site.com', {
param1: undefined,
param2: null,
param3: '',
param4: 'value4',
param5: 'value5'
})
).toEqual('https://site.com?param4=value4&param5=value5');
expect(
getUrlWithParams('https://site.com?defaultParam1=1', {
param1: undefined,
param2: null,
param3: '',
param4: 'value4',
param5: 'value5'
})
).toEqual('https://site.com?defaultParam1=1&param4=value4&param5=value5');
expect(
getUrlWithParams('https://site.com?defaultParam1=1&defaultParam2=2', {
param1: undefined,
param2: null,
param3: '',
param4: 'value4',
param5: 'value5'
})
).toEqual('https://site.com?defaultParam1=1&defaultParam2=2&param4=value4&param5=value5');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment