Skip to content
Snippets Groups Projects
Commit 218825a6 authored by Miguel Rincon's avatar Miguel Rincon Committed by Enrique Alcántara
Browse files

Replace objectToQueryString with objectToQuery

This change removes objectToQueryString in favor of objectToQuery
parent ca94c201
No related branches found
No related tags found
1 merge request!65309Replace objectToQueryString with objectToQuery
import { objectToQueryString, spriteIcon } from '~/lib/utils/common_utils';
import { spriteIcon } from '~/lib/utils/common_utils';
import { objectToQuery } from '~/lib/utils/url_utility';
import FilteredSearchContainer from './container';
import VisualTokenValue from './visual_token_value';
......@@ -327,7 +328,7 @@ export default class FilteredSearchVisualTokens {
return endpoint;
}
const queryString = objectToQueryString(JSON.parse(endpointQueryParams));
const queryString = objectToQuery(JSON.parse(endpointQueryParams));
return `${endpoint}?${queryString}`;
}
......
......@@ -371,17 +371,6 @@ export const parseIntPagination = (paginationInformation) => ({
previousPage: parseInt(paginationInformation['X-PREV-PAGE'], 10),
});
/**
* Converts object with key-value pairs
* into query-param string
*
* @param {Object} params
*/
export const objectToQueryString = (params = {}) =>
Object.keys(params)
.map((param) => `${param}=${params[param]}`)
.join('&');
export const buildUrlWithCurrentLocation = (param) => {
if (param) return `${window.location.pathname}${param}`;
......
......@@ -501,15 +501,15 @@ export function queryToObject(query, { gatherArrays = false, legacySpacesDecode
/**
* Convert search query object back into a search query
*
* @param {Object} obj that needs to be converted
* @param {Object?} params that needs to be converted
* @returns {String}
*
* ex: {one: 1, two: 2} into "one=1&two=2"
*
*/
export function objectToQuery(obj) {
return Object.keys(obj)
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`)
export function objectToQuery(params = {}) {
return Object.keys(params)
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
.join('&');
}
......
......@@ -11,8 +11,8 @@ import {
GlSprintf,
} from '@gitlab/ui';
import { mapActions, mapState } from 'vuex';
import { objectToQueryString } from '~/lib/utils/common_utils';
import { numberToHumanSize } from '~/lib/utils/number_utils';
import { objectToQuery } from '~/lib/utils/url_utility';
import { s__, __ } from '~/locale';
import Tracking from '~/tracking';
import PackageListRow from '../../shared/components/package_list_row.vue';
......@@ -114,7 +114,7 @@ export default {
!this.groupListUrl || document.referrer.includes(this.projectName)
? this.projectListUrl
: this.groupListUrl; // to avoid security issue url are supplied from backend
const modalQuery = objectToQueryString({ [SHOW_DELETE_SUCCESS_ALERT]: true });
const modalQuery = objectToQuery({ [SHOW_DELETE_SUCCESS_ALERT]: true });
window.location.replace(`${returnTo}?${modalQuery}`);
},
handleFileDelete(file) {
......
......@@ -139,21 +139,6 @@ describe('common_utils', () => {
});
});
describe('objectToQueryString', () => {
it('returns empty string when `param` is undefined, null or empty string', () => {
expect(commonUtils.objectToQueryString()).toBe('');
expect(commonUtils.objectToQueryString('')).toBe('');
});
it('returns query string with values of `params`', () => {
const singleQueryParams = { foo: true };
const multipleQueryParams = { foo: true, bar: true };
expect(commonUtils.objectToQueryString(singleQueryParams)).toBe('foo=true');
expect(commonUtils.objectToQueryString(multipleQueryParams)).toBe('foo=true&bar=true');
});
});
describe('buildUrlWithCurrentLocation', () => {
it('should build an url with current location and given parameters', () => {
expect(commonUtils.buildUrlWithCurrentLocation()).toEqual(window.location.pathname);
......
......@@ -718,6 +718,19 @@ describe('URL utility', () => {
expect(urlUtils.objectToQuery(searchQueryObject)).toEqual('one=1&two=2');
});
it('returns empty string when `params` is undefined, null or empty string', () => {
expect(urlUtils.objectToQuery()).toBe('');
expect(urlUtils.objectToQuery('')).toBe('');
});
it('returns query string with values of `params`', () => {
const singleQueryParams = { foo: true };
const multipleQueryParams = { foo: true, bar: true };
expect(urlUtils.objectToQuery(singleQueryParams)).toBe('foo=true');
expect(urlUtils.objectToQuery(multipleQueryParams)).toBe('foo=true&bar=true');
});
});
describe('cleanLeadingSeparator', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment