grafana/public/app/features/templating/variable.ts
Tobias Skarhed 0ef4060b98
Chore: Fix about 200 noImplicitAny errors (#18067)
* Fix 200 ish errors

* Add interface
2019-07-16 11:35:42 +02:00

56 lines
1.6 KiB
TypeScript

import _ from 'lodash';
import { assignModelProperties } from 'app/core/utils/model_utils';
/*
* This regex matches 3 types of variable reference with an optional format specifier
* \$(\w+) $var1
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
*/
export const variableRegex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?::(\w+))?}/g;
// Helper function since lastIndex is not reset
export const variableRegexExec = (variableString: string) => {
variableRegex.lastIndex = 0;
return variableRegex.exec(variableString);
};
export interface Variable {
setValue(option: any): any;
updateOptions(): any;
dependsOn(variable: any): any;
setValueFromUrl(urlValue: any): any;
getValueForUrl(): any;
getSaveModel(): any;
}
export type CtorType = new (...args: any[]) => {};
export interface VariableTypes {
[key: string]: {
name: string;
ctor: CtorType;
description: string;
supportsMulti?: boolean;
};
}
export let variableTypes: VariableTypes = {};
export { assignModelProperties };
export function containsVariable(...args: any[]) {
const variableName = args[args.length - 1];
args[0] = _.isString(args[0]) ? args[0] : Object['values'](args[0]).join(' ');
const variableString = args.slice(0, -1).join(' ');
const matches = variableString.match(variableRegex);
const isMatchingVariable =
matches !== null
? matches.find(match => {
const varMatch = variableRegexExec(match);
return varMatch !== null && varMatch.indexOf(variableName) > -1;
})
: false;
return !!isMatchingVariable;
}