mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
grafana/data: Deprecate kbn.regexEscape and move to grafana/data (#60869)
grafana/data: Deprecate kbn.regexEscape and move escapeRegex to grafana/data
This commit is contained in:
@@ -5,21 +5,21 @@ const specialMatcher = '([\\' + specialChars.join('\\') + '])';
|
|||||||
const specialCharEscape = new RegExp(specialMatcher, 'g');
|
const specialCharEscape = new RegExp(specialMatcher, 'g');
|
||||||
const specialCharUnescape = new RegExp('(\\\\)' + specialMatcher, 'g');
|
const specialCharUnescape = new RegExp('(\\\\)' + specialMatcher, 'g');
|
||||||
|
|
||||||
export const escapeStringForRegex = (value: string) => {
|
export function escapeStringForRegex(value: string) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value.replace(specialCharEscape, '\\$1');
|
return value.replace(specialCharEscape, '\\$1');
|
||||||
};
|
}
|
||||||
|
|
||||||
export const unEscapeStringFromRegex = (value: string) => {
|
export function unEscapeStringFromRegex(value: string) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value.replace(specialCharUnescape, '$2');
|
return value.replace(specialCharUnescape, '$2');
|
||||||
};
|
}
|
||||||
|
|
||||||
export function stringStartsAsRegEx(str: string): boolean {
|
export function stringStartsAsRegEx(str: string): boolean {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
@@ -95,7 +95,11 @@ export function toFloatOrUndefined(value: string): number | undefined {
|
|||||||
return isNaN(v) ? undefined : v;
|
return isNaN(v) ? undefined : v;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const toPascalCase = (string: string) => {
|
export function toPascalCase(string: string) {
|
||||||
const str = camelCase(string);
|
const str = camelCase(string);
|
||||||
return str.charAt(0).toUpperCase() + str.substring(1);
|
return str.charAt(0).toUpperCase() + str.substring(1);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export function escapeRegex(value: string): string {
|
||||||
|
return value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&');
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
TimeRange,
|
TimeRange,
|
||||||
ValueFormatterIndex,
|
ValueFormatterIndex,
|
||||||
rangeUtil,
|
rangeUtil,
|
||||||
|
escapeRegex,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
|
|
||||||
const kbn = {
|
const kbn = {
|
||||||
@@ -24,7 +25,11 @@ const kbn = {
|
|||||||
s: 1,
|
s: 1,
|
||||||
ms: 0.001,
|
ms: 0.001,
|
||||||
} as { [index: string]: number },
|
} as { [index: string]: number },
|
||||||
regexEscape: (value: string): string => value.replace(/[\\^$*+?.()|[\]{}\/]/g, '\\$&'),
|
/** @deprecated since 9.4, use grafana/data */
|
||||||
|
regexEscape: (value: string): string => {
|
||||||
|
deprecationWarning('kbn.ts', 'kbn.regexEscape()', 'escapeRegex from @grafana/data');
|
||||||
|
return escapeRegex(value);
|
||||||
|
},
|
||||||
|
|
||||||
/** @deprecated since 7.2, use grafana/data */
|
/** @deprecated since 7.2, use grafana/data */
|
||||||
roundInterval: (interval: number) => {
|
roundInterval: (interval: number) => {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { isArray, map, replace } from 'lodash';
|
import { isArray, map, replace } from 'lodash';
|
||||||
|
|
||||||
import { dateTime, Registry, RegistryItem, textUtil } from '@grafana/data';
|
import { dateTime, Registry, RegistryItem, textUtil, escapeRegex } from '@grafana/data';
|
||||||
import { VariableType } from '@grafana/schema';
|
import { VariableType } from '@grafana/schema';
|
||||||
import kbn from 'app/core/utils/kbn';
|
|
||||||
import { ALL_VARIABLE_VALUE } from 'app/features/variables/constants';
|
import { ALL_VARIABLE_VALUE } from 'app/features/variables/constants';
|
||||||
|
|
||||||
import { VariableValue, VariableValueSingle } from '../types';
|
import { VariableValue, VariableValueSingle } from '../types';
|
||||||
@@ -81,15 +80,15 @@ export const formatRegistry = new Registry<FormatRegistryItem>(() => {
|
|||||||
description: 'Values are regex escaped and multi-valued variables generate a (<value>|<value>) expression',
|
description: 'Values are regex escaped and multi-valued variables generate a (<value>|<value>) expression',
|
||||||
formatter: (value) => {
|
formatter: (value) => {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return kbn.regexEscape(value);
|
return escapeRegex(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const escapedValues = value.map((item) => {
|
const escapedValues = value.map((item) => {
|
||||||
if (typeof item === 'string') {
|
if (typeof item === 'string') {
|
||||||
return kbn.regexEscape(item);
|
return escapeRegex(item);
|
||||||
} else {
|
} else {
|
||||||
return kbn.regexEscape(String(item));
|
return escapeRegex(String(item));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -100,7 +99,7 @@ export const formatRegistry = new Registry<FormatRegistryItem>(() => {
|
|||||||
return '(' + escapedValues.join('|') + ')';
|
return '(' + escapedValues.join('|') + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
return kbn.regexEscape(`${value}`);
|
return escapeRegex(`${value}`);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { map, find, filter, indexOf } from 'lodash';
|
import { map, find, filter, indexOf } from 'lodash';
|
||||||
|
|
||||||
import { ScopedVars } from '@grafana/data';
|
import { escapeRegex, ScopedVars } from '@grafana/data';
|
||||||
import { TemplateSrv } from '@grafana/runtime';
|
import { TemplateSrv } from '@grafana/runtime';
|
||||||
import kbn from 'app/core/utils/kbn';
|
|
||||||
|
|
||||||
import queryPart from './query_part';
|
import queryPart from './query_part';
|
||||||
import { InfluxQuery, InfluxQueryTag } from './types';
|
import { InfluxQuery, InfluxQueryTag } from './types';
|
||||||
@@ -201,10 +200,10 @@ export default class InfluxQueryModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return kbn.regexEscape(value);
|
return escapeRegex(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const escapedValues = map(value, kbn.regexEscape);
|
const escapedValues = map(value, escapeRegex);
|
||||||
return '(' + escapedValues.join('|') + ')';
|
return '(' + escapedValues.join('|') + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { reduce } from 'lodash';
|
import { reduce } from 'lodash';
|
||||||
|
|
||||||
import kbn from 'app/core/utils/kbn';
|
import { escapeRegex } from '@grafana/data';
|
||||||
|
|
||||||
function renderTagCondition(tag: { operator: any; value: string; condition: any; key: string }, index: number) {
|
function renderTagCondition(tag: { operator: any; value: string; condition: any; key: string }, index: number) {
|
||||||
// FIXME: merge this function with influx_query_model/renderTagCondition
|
// FIXME: merge this function with influx_query_model/renderTagCondition
|
||||||
@@ -48,7 +48,7 @@ export class InfluxQueryBuilder {
|
|||||||
query = 'SHOW MEASUREMENTS';
|
query = 'SHOW MEASUREMENTS';
|
||||||
if (withMeasurementFilter) {
|
if (withMeasurementFilter) {
|
||||||
// we do a case-insensitive regex-based lookup
|
// we do a case-insensitive regex-based lookup
|
||||||
query += ' WITH MEASUREMENT =~ /(?i)' + kbn.regexEscape(withMeasurementFilter) + '/';
|
query += ' WITH MEASUREMENT =~ /(?i)' + escapeRegex(withMeasurementFilter) + '/';
|
||||||
}
|
}
|
||||||
} else if (type === 'FIELDS') {
|
} else if (type === 'FIELDS') {
|
||||||
measurement = this.target.measurement;
|
measurement = this.target.measurement;
|
||||||
|
|||||||
Reference in New Issue
Block a user