Skip to content

Commit ab15378

Browse files
committed
Replace re2 with RegExp in timeline and add unit tests
Remove re2 usage and replace it with JavaScript built-in RegExp object. Also add more unit tests to make sure that using RegExp has same expressions as using re2 library. Issue Resolve #3901 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
1 parent 86d42bc commit ab15378

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
238238
- [Multi DataSource] UX enhancement on Data source management stack ([#2521](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2521))
239239
- [Multi DataSource] UX enhancement on Update stored password modal for Data source management stack ([#2532](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2532))
240240
- [Monaco editor] Add json worker support ([#3424](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3424))
241+
- Replace re2 with RegExp in timeline and add unit tests ([#3908](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3908))
241242

242243
### 🐛 Bug Fixes
243244

src/plugins/vis_type_timeline/server/series_functions/label.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ export default new Chainable('label', {
6262
const config = args.byName;
6363
return alter(args, function (eachSeries) {
6464
if (config.regex) {
65-
// not using a standard `import` so that if there's an issue with the re2 native module
66-
// that it doesn't prevent OpenSearch Dashboards from starting up and we only have an issue using Timeline labels
67-
const RE2 = require('re2');
68-
eachSeries.label = eachSeries.label.replace(new RE2(config.regex), config.label);
65+
const regex = new RegExp(config.regex);
66+
eachSeries.label = eachSeries.label.replace(regex, config.label);
6967
} else {
7068
eachSeries.label = config.label;
7169
}

src/plugins/vis_type_timeline/server/series_functions/label.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,24 @@ describe('label.js', () => {
5151
expect(r.output.list[0].label).to.equal('beerative');
5252
});
5353
});
54+
55+
it('can use a regex to capture groups to modify series label', () => {
56+
return invoke(fn, [seriesList, 'beer$2', '(N)(egative)']).then((r) => {
57+
expect(r.output.list[0].label).to.equal('beeregative');
58+
});
59+
});
60+
61+
it('can handle different regex patterns', () => {
62+
const seriesListCopy1 = JSON.parse(JSON.stringify(seriesList));
63+
const seriesListCopy2 = JSON.parse(JSON.stringify(seriesList));
64+
65+
return Promise.all([
66+
invoke(fn, [seriesListCopy1, 'beer$1 - $2', '(N)(egative)']).then((r) => {
67+
expect(r.output.list[0].label).to.equal('beerN - egative');
68+
}),
69+
invoke(fn, [seriesListCopy2, 'beer$1_$2', '(N)(eg.*)']).then((r) => {
70+
expect(r.output.list[0].label).to.equal('beerN_egative');
71+
}),
72+
]);
73+
});
5474
});

0 commit comments

Comments
 (0)