mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
parent
53a3e386c6
commit
48fe3feae7
@ -55,3 +55,34 @@ describe('parseKeyValue', () => {
|
||||
expect(obj).toEqual({ custom: '', custom1: '001', custom2: '002', custom3: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUrlSearchParams', () => {
|
||||
const { location } = window;
|
||||
// @ts-ignore
|
||||
delete window.location;
|
||||
|
||||
window.location = {
|
||||
...location,
|
||||
hash: '#hash',
|
||||
host: 'www.domain.com:9877',
|
||||
hostname: 'www.domain.com',
|
||||
href: 'http://www.domain.com:9877/path/b?var1=a&var2=b&var2=c&var2=d&var3=a&var3=d&z#hash',
|
||||
origin: 'http://www.domain.com:9877',
|
||||
pathname: '/path/b',
|
||||
port: '9877',
|
||||
protocol: 'http:',
|
||||
search: '?var1=a&var2=b&var2=c&var2=d&var3=a&var3=d&z',
|
||||
};
|
||||
|
||||
let expectedParams = {
|
||||
var1: ['a'],
|
||||
var2: ['b', 'c', 'd'],
|
||||
var3: ['a', 'd'],
|
||||
z: true,
|
||||
};
|
||||
|
||||
it('should take into account multi-value and boolean parameters', () => {
|
||||
const params = urlUtil.getUrlSearchParams();
|
||||
expect(params).toStrictEqual(expectedParams);
|
||||
});
|
||||
});
|
||||
|
@ -105,17 +105,21 @@ function appendQueryToUrl(url: string, stringToAppend: string) {
|
||||
/**
|
||||
* Return search part (as object) of current url
|
||||
*/
|
||||
function getUrlSearchParams() {
|
||||
function getUrlSearchParams(): UrlQueryMap {
|
||||
const search = window.location.search.substring(1);
|
||||
const searchParamsSegments = search.split('&');
|
||||
const params: any = {};
|
||||
const params: UrlQueryMap = {};
|
||||
for (const p of searchParamsSegments) {
|
||||
const keyValuePair = p.split('=');
|
||||
if (keyValuePair.length > 1) {
|
||||
// key-value param
|
||||
const key = decodeURIComponent(keyValuePair[0]);
|
||||
const value = decodeURIComponent(keyValuePair[1]);
|
||||
params[key] = value;
|
||||
if (key in params) {
|
||||
params[key] = [...(params[key] as any[]), value];
|
||||
} else {
|
||||
params[key] = [value];
|
||||
}
|
||||
} else if (keyValuePair.length === 1) {
|
||||
// boolean param
|
||||
const key = decodeURIComponent(keyValuePair[0]);
|
||||
|
Loading…
Reference in New Issue
Block a user