Skip to content

Commit d24926a

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 d24926a

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
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
});

yarn.lock

+19-5
Original file line numberDiff line numberDiff line change
@@ -4155,7 +4155,16 @@ agentkeepalive@^3.4.1:
41554155
dependencies:
41564156
humanize-ms "^1.2.1"
41574157

4158-
agentkeepalive@^4.1.3, agentkeepalive@^4.2.1:
4158+
agentkeepalive@^4.1.3:
4159+
version "4.3.0"
4160+
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.3.0.tgz#bb999ff07412653c1803b3ced35e50729830a255"
4161+
integrity sha512-7Epl1Blf4Sy37j4v9f9FjICCh4+KAQOyXgHEwlyBiAQLbhKdq/i2QQU3amQalS/wPhdPzDXPL5DMR5bkn+YeWg==
4162+
dependencies:
4163+
debug "^4.1.0"
4164+
depd "^2.0.0"
4165+
humanize-ms "^1.2.1"
4166+
4167+
agentkeepalive@^4.2.1:
41594168
version "4.2.1"
41604169
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.2.1.tgz#a7975cbb9f83b367f06c90cc51ff28fe7d499717"
41614170
integrity sha512-Zn4cw2NEqd+9fiSVWMscnjyQ1a8Yfoc5oBajLeo5w+YBHgDUcEBY2hS4YpTz6iN5f/2zQiktcuM6tS8x1p9dpA==
@@ -6889,7 +6898,7 @@ delayed-stream@~1.0.0:
68896898
delegates@^1.0.0:
68906899
version "1.0.0"
68916900
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
6892-
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=
6901+
integrity sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==
68936902

68946903
delete-empty@^2.0.0:
68956904
version "2.0.0"
@@ -6905,6 +6914,11 @@ depd@^1.1.2:
69056914
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
69066915
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=
69076916

6917+
depd@^2.0.0:
6918+
version "2.0.0"
6919+
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
6920+
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==
6921+
69086922
dependency-check@^4.1.0:
69096923
version "4.1.0"
69106924
resolved "https://registry.yarnpkg.com/dependency-check/-/dependency-check-4.1.0.tgz#d45405cabb50298f8674fe28ab594c8a5530edff"
@@ -9980,9 +9994,9 @@ inquirer@^7.0.0, inquirer@^7.3.3:
99809994
through "^2.3.6"
99819995

99829996
install-artifact-from-github@^1.3.0:
9983-
version "1.3.1"
9984-
resolved "https://registry.yarnpkg.com/install-artifact-from-github/-/install-artifact-from-github-1.3.1.tgz#eefaad9af35d632e5d912ad1569c1de38c3c2462"
9985-
integrity sha512-3l3Bymg2eKDsN5wQuMfgGEj2x6l5MCAv0zPL6rxHESufFVlEAKW/6oY9F1aGgvY/EgWm5+eWGRjINveL4X7Hgg==
9997+
version "1.3.2"
9998+
resolved "https://registry.yarnpkg.com/install-artifact-from-github/-/install-artifact-from-github-1.3.2.tgz#1a16d9508e40330523a3017ae0d4713ccc64de82"
9999+
integrity sha512-yCFcLvqk0yQdxx0uJz4t9Z3adDMLAYrcGYv546uRXCSvxE+GqNYhhz/KmrGcUKGI/gVLR9n/e/zM9jX/+ASMJQ==
998610000

998710001
internal-slot@^1.0.3:
998810002
version "1.0.3"

0 commit comments

Comments
 (0)