grafana/public/app/features/variables/adhoc/urlParser.ts
Hugo Häggmark 00a9af00fc
Templating: removes old Angular variable system and featureToggle (#24779)
* Chore: initial commit

* Tests: fixes MetricsQueryEditor.test.tsx

* Tests: fixes cloudwatch/specs/datasource.test.ts

* Tests: fixes stackdriver/specs/datasource.test.ts

* Tests: remove refrences to CustomVariable

* Refactor: moves DefaultVariableQueryEditor

* Refactor: moves utils

* Refactor: moves types

* Refactor: removes variableSrv

* Refactor: removes feature toggle newVariables

* Refactor: removes valueSelectDropDown

* Chore: removes GeneralTabCtrl

* Chore: migrates RowOptions

* Refactor: adds RowOptionsButton

* Refactor: makes the interface more explicit

* Refactor: small changes

* Refactor: changed type as it can be any variable type

* Tests: fixes broken test

* Refactor: changes after PR comments

* Refactor: adds loading state and call to onChange in componentDidMount
2020-06-04 13:44:48 +02:00

53 lines
1.3 KiB
TypeScript

import { AdHocVariableFilter } from 'app/features/variables/types';
import { UrlQueryValue } from '@grafana/data';
import { isArray, isString } from 'lodash';
export const toUrl = (filters: AdHocVariableFilter[]): string[] => {
return filters.map(filter =>
toArray(filter)
.map(escapeDelimiter)
.join('|')
);
};
export const toFilters = (value: UrlQueryValue): AdHocVariableFilter[] => {
if (isArray(value)) {
const values = value as any[];
return values.map(toFilter).filter(isFilter);
}
const filter = toFilter(value);
return filter === null ? [] : [filter];
};
function escapeDelimiter(value: string) {
return value.replace(/\|/g, '__gfp__');
}
function unescapeDelimiter(value: string) {
return value.replace(/__gfp__/g, '|');
}
function toArray(filter: AdHocVariableFilter): string[] {
return [filter.key, filter.operator, filter.value];
}
function toFilter(value: string | number | boolean | undefined | null): AdHocVariableFilter | null {
if (!isString(value) || value.length === 0) {
return null;
}
const parts = value.split('|').map(unescapeDelimiter);
return {
key: parts[0],
operator: parts[1],
value: parts[2],
condition: '',
};
}
function isFilter(filter: AdHocVariableFilter | null): filter is AdHocVariableFilter {
return filter !== null && isString(filter.value);
}