diff --git a/src/core/services/tests/utils/url.test.ts b/src/core/services/tests/utils/url.test.ts index 50b3fea57..fe3ee07b3 100644 --- a/src/core/services/tests/utils/url.test.ts +++ b/src/core/services/tests/utils/url.test.ts @@ -65,6 +65,17 @@ describe('CoreUrlUtilsProvider', () => { 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', () => { const originalUrl = 'https://moodle.org'; const params = { diff --git a/src/core/services/utils/url.ts b/src/core/services/utils/url.ts index f0148b84c..eb52e2838 100644 --- a/src/core/services/utils/url.ts +++ b/src/core/services/utils/url.ts @@ -64,18 +64,18 @@ export class CoreUrlUtilsProvider { const urlAndAnchor = url.split('#'); url = urlAndAnchor[0]; - let separator = url.indexOf('?') != -1 ? '&' : '?'; + let separator = url.indexOf('?') !== -1 ? '&' : '?'; for (const key in params) { let value = params[key]; - if (boolToNumber && typeof value == 'boolean') { + if (boolToNumber && typeof value === 'boolean') { // Convert booleans to 1 or 0. value = value ? '1' : '0'; } - // Ignore objects. - if (typeof value != 'object') { + // Ignore objects and undefined. + if (typeof value !== 'object' && value !== undefined) { url += separator + key + '=' + value; separator = '&'; }