Skip to content

Commit 4cf724a

Browse files
committed
feat(core utils): regexp_from_wildcard - Create regular expressions from wildcard strings.
This utility can be used for search strings with wildcards like "test-*-xyz". The wildcard can be at the beginning, somewhere in the middle or at the end and multiple wildcards can be used. This method was factored out from removeWildcardClass for broader usage.
1 parent 2e78439 commit 4cf724a

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/core/utils.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ function escapeRegExp(str) {
175175
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
176176
}
177177

178+
/**
179+
* Create a RegExp object of a wildcard search string.
180+
*
181+
* @param {string} wildcard: A search string which can contain wildcards "*".
182+
* The wildcard "*" can be anywhere in the string and
183+
* can also be used multiple times. If no wildcard is
184+
* present the search string is used for an exact match.
185+
186+
* @returns {RegExp}: A RegExp object which can be used to match strings.
187+
*/
188+
function regexp_from_wildcard(wildcard) {
189+
let regexp = wildcard.replace(/[\-\[\]{}()+?.,\\\^$|#\s]/g, "\\$&");
190+
regexp = regexp.replace(/[*]/g, ".*");
191+
regexp = new RegExp(`^${regexp}$`);
192+
return regexp;
193+
}
194+
178195
/**
179196
* Remove classes from a list of targets if they match a specific pattern.
180197
*
@@ -193,10 +210,7 @@ function removeWildcardClass(targets, classes) {
193210
target.classList.remove(classes);
194211
}
195212
} else {
196-
let matcher = classes.replace(/[\-\[\]{}()+?.,\\\^$|#\s]/g, "\\$&");
197-
matcher = matcher.replace(/[*]/g, ".*");
198-
matcher = new RegExp("^" + matcher + "$");
199-
213+
const matcher = regexp_from_wildcard(classes);
200214
for (const target of targets) {
201215
const class_list = (target.getAttribute("class") || "").split(/\s+/);
202216
if (!class_list.length) {
@@ -780,6 +794,7 @@ var utils = {
780794
isObject: isObject,
781795
extend: extend,
782796
findLabel: findLabel,
797+
regexp_from_wildcard: regexp_from_wildcard,
783798
removeWildcardClass: removeWildcardClass,
784799
hideOrShow: hideOrShow,
785800
addURLQueryParameter: addURLQueryParameter,
@@ -807,7 +822,7 @@ var utils = {
807822
date_diff: date_diff,
808823
threshold_list: threshold_list,
809824
is_option_truthy: is_option_truthy,
810-
getCSSValue: dom.get_css_value, // BBB: moved to dom. TODO: Remove in upcoming version.
825+
//getCSSValue: dom.get_css_value, // BBB: moved to dom. TODO: Remove in upcoming version.
811826
elementInViewport: (el) => {
812827
// BBB: Remove with next major version.
813828
console.warn("Deprecated. Use utils.isElementInViewport");

src/core/utils.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ describe("basic tests", function () {
161161
});
162162
});
163163

164+
describe("regexp_from_wildcard", function () {
165+
it("creates regular expressions from wildcard strings", function () {
166+
let regex = utils.regexp_from_wildcard("test-*-tset");
167+
expect(regex.test("test-abcd-defg-tset")).toBe(true);
168+
expect(regex.test("tset-abcd-defg-test")).toBe(false);
169+
170+
regex = utils.regexp_from_wildcard("*-test");
171+
expect(regex.test("abdc-test")).toBe(true);
172+
expect(regex.test("abdc-wxyz")).toBe(false);
173+
174+
regex = utils.regexp_from_wildcard("test-*");
175+
expect(regex.test("test-abcd")).toBe(true);
176+
expect(regex.test("abdc-wxyz")).toBe(false);
177+
178+
regex = utils.regexp_from_wildcard("*-test-*");
179+
expect(regex.test("abcd-test-wxyz")).toBe(true);
180+
expect(regex.test("abcd-efgh-wxyz")).toBe(false);
181+
182+
regex = utils.regexp_from_wildcard("*");
183+
expect(regex.test("abcd")).toBe(true);
184+
});
185+
});
186+
164187
describe("removeWildcardClass", function () {
165188
describe("... with single element", function () {
166189
it("Remove basic class", function () {

0 commit comments

Comments
 (0)