grafana/public/app/features/variables/adhoc/urlParser.ts
Marcus Andersson a7a1406415
Variables: migrates ad hoc variable type to react/redux. (#22784)
* Refactor: moves all the newVariables part to features/variables directory

* Feature: adds datasource type

* Tests: adds reducer tests

* Tests: covers data source actions with tests

* Chore: reduces strict null errors

* boilerplate that will be replaced by real code.

* added old editor template.

* added initial version of ad hoc editor.

* added working (apart from add) version of the editor.

* Added placeholder for picker.

* Have a working UI. Need to connect it so we refresh the variables on changes.

* variable should be updated now.

* removed console.log

* made the url work.

* cleaned up the adapter.

* added possiblity to create filter directly from table.

* moved infotext from general reducer to extended value of adhoc.

* fixed strict null errors.

* fixed strict null errors.

* fixed issue where remove was displayed before being added.

* fixed issue with fragment key.

* changed so template_src is using the redux variables.

* minor refactorings.

* moved adhoc picker to adhoc variable.

* adding tests for reducer and fixed bug.

* added tests or urlparser.

* added tests for ad hoc actions.

* added more tests.

* added more tests.

* fixed strict null error.

* fixed copy n pase error.

* added utilit for getting new variable index.

* removed console.log

* added location to reducerTester type and created a module type for it.

* changed so we only have one builder pattern.

* fixed tests to use static expected values.

* fixed strict errors.

* fixed more strict errors.

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
2020-03-23 09:00:36 +01:00

53 lines
1.3 KiB
TypeScript

import { AdHocVariableFilter } from 'app/features/templating/variable';
import { UrlQueryValue } from '@grafana/runtime';
import { isString, isArray } 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);
}