From b2a7891b08a934976d1e487495d69d875f3fdd02 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 9 Mar 2019 23:50:46 -0800 Subject: [PATCH 1/3] move stringToJsRegex --- packages/grafana-ui/src/utils/index.ts | 1 + .../grafana-ui/src/utils/stringUtils.test.ts | 15 +++++++++++++++ packages/grafana-ui/src/utils/stringUtils.ts | 13 +++++++++++++ public/app/core/time_series2.ts | 5 ++--- public/app/core/utils/kbn.ts | 16 ++++------------ .../features/templating/datasource_variable.ts | 4 ++-- public/app/features/templating/query_variable.ts | 4 ++-- public/app/plugins/panel/table/renderer.ts | 5 ++--- 8 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 packages/grafana-ui/src/utils/stringUtils.test.ts create mode 100644 packages/grafana-ui/src/utils/stringUtils.ts diff --git a/packages/grafana-ui/src/utils/index.ts b/packages/grafana-ui/src/utils/index.ts index c5f4b2c5b1b..a5ae301710b 100644 --- a/packages/grafana-ui/src/utils/index.ts +++ b/packages/grafana-ui/src/utils/index.ts @@ -2,4 +2,5 @@ export * from './processTimeSeries'; export * from './valueFormats/valueFormats'; export * from './colors'; export * from './namedColorsPalette'; +export * from './stringUtils'; export { getMappedValue } from './valueMappings'; diff --git a/packages/grafana-ui/src/utils/stringUtils.test.ts b/packages/grafana-ui/src/utils/stringUtils.test.ts new file mode 100644 index 00000000000..a6b35461d78 --- /dev/null +++ b/packages/grafana-ui/src/utils/stringUtils.test.ts @@ -0,0 +1,15 @@ +import { stringToJsRegex } from '@grafana/ui'; + +describe('stringToJsRegex', () => { + it('should parse the valid regex value', () => { + const output = stringToJsRegex('/validRegexp/'); + expect(output).toBeInstanceOf(RegExp); + }); + + it('should throw error on invalid regex value', () => { + const input = '/etc/hostname'; + expect(() => { + stringToJsRegex(input); + }).toThrow(); + }); +}); diff --git a/packages/grafana-ui/src/utils/stringUtils.ts b/packages/grafana-ui/src/utils/stringUtils.ts new file mode 100644 index 00000000000..12433623a6a --- /dev/null +++ b/packages/grafana-ui/src/utils/stringUtils.ts @@ -0,0 +1,13 @@ +export function stringToJsRegex(str: string): RegExp { + if (str[0] !== '/') { + return new RegExp('^' + str + '$'); + } + + const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$')); + + if (!match) { + throw new Error(`'${str}' is not a valid regular expression.`); + } + + return new RegExp(match[1], match[2]); +} diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index 23a0a0c19ea..10c91c148d7 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -1,7 +1,6 @@ -import kbn from 'app/core/utils/kbn'; import { getFlotTickDecimals } from 'app/core/utils/ticks'; import _ from 'lodash'; -import { getValueFormat } from '@grafana/ui'; +import { getValueFormat, stringToJsRegex } from '@grafana/ui'; function matchSeriesOverride(aliasOrRegex, seriesAlias) { if (!aliasOrRegex) { @@ -9,7 +8,7 @@ function matchSeriesOverride(aliasOrRegex, seriesAlias) { } if (aliasOrRegex[0] === '/') { - const regex = kbn.stringToJsRegex(aliasOrRegex); + const regex = stringToJsRegex(aliasOrRegex); return seriesAlias.match(regex) != null; } diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index 43886fafd07..dd79d552277 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -1,5 +1,5 @@ import _ from 'lodash'; -import { getValueFormat, getValueFormatterIndex, getValueFormats } from '@grafana/ui'; +import { getValueFormat, getValueFormatterIndex, getValueFormats, stringToJsRegex } from '@grafana/ui'; const kbn: any = {}; @@ -228,18 +228,10 @@ kbn.slugifyForUrl = str => { .replace(/ +/g, '-'); }; +/** deprecated since 6.1, use grafana/ui */ kbn.stringToJsRegex = str => { - if (str[0] !== '/') { - return new RegExp('^' + str + '$'); - } - - const match = str.match(new RegExp('^/(.*?)/(g?i?m?y?)$')); - - if (!match) { - throw new Error(`'${str}' is not a valid regular expression.`); - } - - return new RegExp(match[1], match[2]); + console.warn('Migrate stringToJsRegex to grafana/ui, '); + return stringToJsRegex(str); }; kbn.toFixed = (value, decimals) => { diff --git a/public/app/features/templating/datasource_variable.ts b/public/app/features/templating/datasource_variable.ts index 4424720c7f8..2b326cb1c5c 100644 --- a/public/app/features/templating/datasource_variable.ts +++ b/public/app/features/templating/datasource_variable.ts @@ -1,5 +1,5 @@ -import kbn from 'app/core/utils/kbn'; import { Variable, containsVariable, assignModelProperties, variableTypes } from './variable'; +import { stringToJsRegex } from '@grafana/ui'; export class DatasourceVariable implements Variable { regex: any; @@ -47,7 +47,7 @@ export class DatasourceVariable implements Variable { if (this.regex) { regex = this.templateSrv.replace(this.regex, null, 'regex'); - regex = kbn.stringToJsRegex(regex); + regex = stringToJsRegex(regex); } for (let i = 0; i < sources.length; i++) { diff --git a/public/app/features/templating/query_variable.ts b/public/app/features/templating/query_variable.ts index 0aec1d8f412..8208345528b 100644 --- a/public/app/features/templating/query_variable.ts +++ b/public/app/features/templating/query_variable.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; -import kbn from 'app/core/utils/kbn'; import { Variable, containsVariable, assignModelProperties, variableTypes } from './variable'; +import { stringToJsRegex } from '@grafana/ui'; function getNoneOption() { return { text: 'None', value: '', isNone: true }; @@ -148,7 +148,7 @@ export class QueryVariable implements Variable { options = []; if (this.regex) { - regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex')); + regex = stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex')); } for (i = 0; i < metricNames.length; i++) { const item = metricNames[i]; diff --git a/public/app/plugins/panel/table/renderer.ts b/public/app/plugins/panel/table/renderer.ts index e9bf89f45fe..6b6189f7482 100644 --- a/public/app/plugins/panel/table/renderer.ts +++ b/public/app/plugins/panel/table/renderer.ts @@ -1,7 +1,6 @@ import _ from 'lodash'; import moment from 'moment'; -import kbn from 'app/core/utils/kbn'; -import { getValueFormat, getColorFromHexRgbOrName, GrafanaThemeType } from '@grafana/ui'; +import { getValueFormat, getColorFromHexRgbOrName, GrafanaThemeType, stringToJsRegex } from '@grafana/ui'; export class TableRenderer { formatters: any[]; @@ -35,7 +34,7 @@ export class TableRenderer { for (let i = 0; i < this.panel.styles.length; i++) { const style = this.panel.styles[i]; - const regex = kbn.stringToJsRegex(style.pattern); + const regex = stringToJsRegex(style.pattern); if (column.text.match(regex)) { column.style = style; From d13eebfe52706f153a778b9e1ce902970fdf4c0a Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 10 Mar 2019 10:19:02 -0700 Subject: [PATCH 2/3] move to string.ts --- packages/grafana-ui/src/utils/index.ts | 2 +- .../src/utils/{stringUtils.test.ts => string.test.ts} | 0 packages/grafana-ui/src/utils/{stringUtils.ts => string.ts} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename packages/grafana-ui/src/utils/{stringUtils.test.ts => string.test.ts} (100%) rename packages/grafana-ui/src/utils/{stringUtils.ts => string.ts} (100%) diff --git a/packages/grafana-ui/src/utils/index.ts b/packages/grafana-ui/src/utils/index.ts index a5ae301710b..d1c6deb096c 100644 --- a/packages/grafana-ui/src/utils/index.ts +++ b/packages/grafana-ui/src/utils/index.ts @@ -2,5 +2,5 @@ export * from './processTimeSeries'; export * from './valueFormats/valueFormats'; export * from './colors'; export * from './namedColorsPalette'; -export * from './stringUtils'; +export * from './string'; export { getMappedValue } from './valueMappings'; diff --git a/packages/grafana-ui/src/utils/stringUtils.test.ts b/packages/grafana-ui/src/utils/string.test.ts similarity index 100% rename from packages/grafana-ui/src/utils/stringUtils.test.ts rename to packages/grafana-ui/src/utils/string.test.ts diff --git a/packages/grafana-ui/src/utils/stringUtils.ts b/packages/grafana-ui/src/utils/string.ts similarity index 100% rename from packages/grafana-ui/src/utils/stringUtils.ts rename to packages/grafana-ui/src/utils/string.ts From f7f124c56b7a8bd5f987f932d1cbc6d640c8de44 Mon Sep 17 00:00:00 2001 From: ryan Date: Sun, 10 Mar 2019 10:37:44 -0700 Subject: [PATCH 3/3] reuse deprecationWarning --- .../ColorPicker/warnAboutColorPickerPropsDeprecation.ts | 4 ++-- packages/grafana-ui/src/utils/deprecationWarning.ts | 6 ++++++ packages/grafana-ui/src/utils/index.ts | 1 + packages/grafana-ui/src/utils/propDeprecationWarning.ts | 6 ------ public/app/core/utils/kbn.ts | 3 ++- 5 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 packages/grafana-ui/src/utils/deprecationWarning.ts delete mode 100644 packages/grafana-ui/src/utils/propDeprecationWarning.ts diff --git a/packages/grafana-ui/src/components/ColorPicker/warnAboutColorPickerPropsDeprecation.ts b/packages/grafana-ui/src/components/ColorPicker/warnAboutColorPickerPropsDeprecation.ts index b8919c682e2..fb4e454d2f9 100644 --- a/packages/grafana-ui/src/components/ColorPicker/warnAboutColorPickerPropsDeprecation.ts +++ b/packages/grafana-ui/src/components/ColorPicker/warnAboutColorPickerPropsDeprecation.ts @@ -1,9 +1,9 @@ -import propDeprecationWarning from '../../utils/propDeprecationWarning'; +import deprecationWarning from '../../utils/deprecationWarning'; import { ColorPickerProps } from './ColorPickerPopover'; export const warnAboutColorPickerPropsDeprecation = (componentName: string, props: ColorPickerProps) => { const { onColorChange } = props; if (onColorChange) { - propDeprecationWarning(componentName, 'onColorChange', 'onChange'); + deprecationWarning(componentName, 'onColorChange', 'onChange'); } }; diff --git a/packages/grafana-ui/src/utils/deprecationWarning.ts b/packages/grafana-ui/src/utils/deprecationWarning.ts new file mode 100644 index 00000000000..3182f232638 --- /dev/null +++ b/packages/grafana-ui/src/utils/deprecationWarning.ts @@ -0,0 +1,6 @@ +const deprecationWarning = (file: string, oldName: string, newName: string) => { + const message = `[Deprecation warning] ${file}: ${oldName} is deprecated. Use ${newName} instead`; + console.warn(message); +}; + +export default deprecationWarning; diff --git a/packages/grafana-ui/src/utils/index.ts b/packages/grafana-ui/src/utils/index.ts index d1c6deb096c..a2acc828752 100644 --- a/packages/grafana-ui/src/utils/index.ts +++ b/packages/grafana-ui/src/utils/index.ts @@ -3,4 +3,5 @@ export * from './valueFormats/valueFormats'; export * from './colors'; export * from './namedColorsPalette'; export * from './string'; +export * from './deprecationWarning'; export { getMappedValue } from './valueMappings'; diff --git a/packages/grafana-ui/src/utils/propDeprecationWarning.ts b/packages/grafana-ui/src/utils/propDeprecationWarning.ts deleted file mode 100644 index a277395c090..00000000000 --- a/packages/grafana-ui/src/utils/propDeprecationWarning.ts +++ /dev/null @@ -1,6 +0,0 @@ -const propDeprecationWarning = (componentName: string, propName: string, newPropName: string) => { - const message = `[Deprecation warning] ${componentName}: ${propName} is deprecated. Use ${newPropName} instead`; - console.warn(message); -}; - -export default propDeprecationWarning; diff --git a/public/app/core/utils/kbn.ts b/public/app/core/utils/kbn.ts index dd79d552277..d747fa37f57 100644 --- a/public/app/core/utils/kbn.ts +++ b/public/app/core/utils/kbn.ts @@ -1,5 +1,6 @@ import _ from 'lodash'; import { getValueFormat, getValueFormatterIndex, getValueFormats, stringToJsRegex } from '@grafana/ui'; +import deprecationWarning from '@grafana/ui/src/utils/deprecationWarning'; const kbn: any = {}; @@ -230,7 +231,7 @@ kbn.slugifyForUrl = str => { /** deprecated since 6.1, use grafana/ui */ kbn.stringToJsRegex = str => { - console.warn('Migrate stringToJsRegex to grafana/ui, '); + deprecationWarning('kbn.ts', 'kbn.stringToJsRegex()', '@grafana/ui'); return stringToJsRegex(str); };