MOBILE-4368 core: Don't add undefined params to URL

main
Dani Palou 2023-06-15 12:46:54 +02:00 committed by Alfonso Salces
parent 4eb3594d9b
commit 718f95b403
2 changed files with 15 additions and 4 deletions

View File

@ -65,6 +65,17 @@ describe('CoreUrlUtilsProvider', () => {
expect(url).toEqual(originalUrl); expect(url).toEqual(originalUrl);
}); });
it('doesn\'t add undefined or null params', () => {
const originalUrl = 'https://moodle.org';
const url = urlUtils.addParamsToUrl(originalUrl, {
foo: undefined,
bar: null,
baz: 1,
});
expect(url).toEqual('https://moodle.org?baz=1');
});
it('adds anchor to URL', () => { it('adds anchor to URL', () => {
const originalUrl = 'https://moodle.org'; const originalUrl = 'https://moodle.org';
const params = { const params = {

View File

@ -64,18 +64,18 @@ export class CoreUrlUtilsProvider {
const urlAndAnchor = url.split('#'); const urlAndAnchor = url.split('#');
url = urlAndAnchor[0]; url = urlAndAnchor[0];
let separator = url.indexOf('?') != -1 ? '&' : '?'; let separator = url.indexOf('?') !== -1 ? '&' : '?';
for (const key in params) { for (const key in params) {
let value = params[key]; let value = params[key];
if (boolToNumber && typeof value == 'boolean') { if (boolToNumber && typeof value === 'boolean') {
// Convert booleans to 1 or 0. // Convert booleans to 1 or 0.
value = value ? '1' : '0'; value = value ? '1' : '0';
} }
// Ignore objects. // Ignore objects and undefined.
if (typeof value != 'object') { if (typeof value !== 'object' && value !== undefined) {
url += separator + key + '=' + value; url += separator + key + '=' + value;
separator = '&'; separator = '&';
} }