Created
March 6, 2020 19:13
-
-
Save sgromkov/76f95905e3df635e4931f04713eeef35 to your computer and use it in GitHub Desktop.
Stick the url with parameters. Covered by unit tests on Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m5=value5'); | |
expect( | |
getUrlWithParams('https://site.com?defaultParam1=1', { | |
param1: undefined, | |
param2: null, | |
param3: '', | |
param4: 'value4', | |
param5: 'value5' | |
}) | |
).toEqual('https://site.com?defaultParam1=1¶m4=value4¶m5=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¶m4=value4¶m5=value5'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment