From fdf33e465f805f644fe1a6c277e6ed60e1c07805 Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Mon, 21 Mar 2022 18:36:00 +0100 Subject: [PATCH 1/7] add regex string filtering capabilities --- packages/react-core/src/filters/FilterTypes.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/react-core/src/filters/FilterTypes.js b/packages/react-core/src/filters/FilterTypes.js index d170570da..edf1dbf39 100644 --- a/packages/react-core/src/filters/FilterTypes.js +++ b/packages/react-core/src/filters/FilterTypes.js @@ -50,8 +50,9 @@ function stringSearch(filterValues, featureValue, params = {}) { const normalizedFeatureValue = normalize(featureValue, params); const stringRegExp = filterValues .map((filterValue) => { - let stringRegExp = escapeRegExp(normalize(filterValue, params)); + let stringRegExp = normalize(filterValue, params); + if (!params.useRegExp) stringRegExp = escapeRegExp(stringRegExp); if (params.mustStart) stringRegExp = `^${stringRegExp}`; if (params.mustEnd) stringRegExp = `${stringRegExp}$`; From 91902e240b9378c78faf8d5e4857d12e1c2852a9 Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Mon, 21 Mar 2022 18:36:11 +0100 Subject: [PATCH 2/7] add test for feature --- packages/react-core/__tests__/filters/FilterTypes.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/react-core/__tests__/filters/FilterTypes.test.js b/packages/react-core/__tests__/filters/FilterTypes.test.js index 6821f75cb..44e1a1e5e 100644 --- a/packages/react-core/__tests__/filters/FilterTypes.test.js +++ b/packages/react-core/__tests__/filters/FilterTypes.test.js @@ -91,6 +91,11 @@ describe('FilterTypes', () => { // Multiples filter values (OR) expect(stringSearchFilter(['_test_', 'carto'], featureValue)).toBe(true); + + // Regexp filtering + expect(stringSearchFilter(['\\w+RT\\w'], featureValue, { useRegExp: true })).toBe( + true + ); }); }); }); From 586a5b51366200e1577820b98b44a33f79e1e1b7 Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Tue, 22 Mar 2022 11:14:10 +0100 Subject: [PATCH 3/7] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51062a72d..1cc81d0c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Not released - Implement multiple operationColumn in core functions [#347](https://github.com/CartoDB/carto-react/pull/347) +- Add regex string filtering capabilities [#362](https://github.com/CartoDB/carto-react/pull/362) ## 1.2 From 29ad4bf38c0191c2f1409d4a529d5a8f4171d1f3 Mon Sep 17 00:00:00 2001 From: Sergio Clebal Date: Tue, 22 Mar 2022 11:20:59 +0100 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cc81d0c1..30673f9fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Not released - Implement multiple operationColumn in core functions [#347](https://github.com/CartoDB/carto-react/pull/347) -- Add regex string filtering capabilities [#362](https://github.com/CartoDB/carto-react/pull/362) +- Add RegExp string filtering capabilities [#362](https://github.com/CartoDB/carto-react/pull/362) ## 1.2 From 77a48425be58cfec7bfac01ca84bcbcf085d2c49 Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Tue, 22 Mar 2022 12:31:17 +0100 Subject: [PATCH 5/7] address review comment from @bbecquet --- packages/react-core/src/filters/FilterTypes.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/react-core/src/filters/FilterTypes.js b/packages/react-core/src/filters/FilterTypes.js index edf1dbf39..0df8cf927 100644 --- a/packages/react-core/src/filters/FilterTypes.js +++ b/packages/react-core/src/filters/FilterTypes.js @@ -52,9 +52,11 @@ function stringSearch(filterValues, featureValue, params = {}) { .map((filterValue) => { let stringRegExp = normalize(filterValue, params); - if (!params.useRegExp) stringRegExp = escapeRegExp(stringRegExp); - if (params.mustStart) stringRegExp = `^${stringRegExp}`; - if (params.mustEnd) stringRegExp = `${stringRegExp}$`; + if (!params.useRegExp) { + stringRegExp = escapeRegExp(stringRegExp); + if (params.mustStart) stringRegExp = `^${stringRegExp}`; + if (params.mustEnd) stringRegExp = `${stringRegExp}$`; + } return stringRegExp; }) From a8d990318b47828c5f0f644e38b2469f25c8c8e3 Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Tue, 22 Mar 2022 14:34:21 +0100 Subject: [PATCH 6/7] avoid side effect on `normalize` --- packages/react-core/src/filters/FilterTypes.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/react-core/src/filters/FilterTypes.js b/packages/react-core/src/filters/FilterTypes.js index 0df8cf927..8f03b821b 100644 --- a/packages/react-core/src/filters/FilterTypes.js +++ b/packages/react-core/src/filters/FilterTypes.js @@ -50,15 +50,15 @@ function stringSearch(filterValues, featureValue, params = {}) { const normalizedFeatureValue = normalize(featureValue, params); const stringRegExp = filterValues .map((filterValue) => { - let stringRegExp = normalize(filterValue, params); + let formattedValue = filterValue; if (!params.useRegExp) { - stringRegExp = escapeRegExp(stringRegExp); - if (params.mustStart) stringRegExp = `^${stringRegExp}`; - if (params.mustEnd) stringRegExp = `${stringRegExp}$`; + formattedValue = escapeRegExp(normalize(filterValue, params)); + if (params.mustStart) formattedValue = `^${formattedValue}`; + if (params.mustEnd) formattedValue = `${formattedValue}$`; } - return stringRegExp; + return formattedValue; }) .join('|'); From d1c423b1d1d793ae89579d0ab64764df5ed0e3fa Mon Sep 17 00:00:00 2001 From: "Juan D. Jara" Date: Tue, 22 Mar 2022 15:59:55 +0100 Subject: [PATCH 7/7] addres review comment from @clebal --- .../react-core/src/filters/FilterTypes.js | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/react-core/src/filters/FilterTypes.js b/packages/react-core/src/filters/FilterTypes.js index 8f03b821b..2f3452391 100644 --- a/packages/react-core/src/filters/FilterTypes.js +++ b/packages/react-core/src/filters/FilterTypes.js @@ -48,21 +48,18 @@ function closedOpen(filterValues, featureValue) { // FilterTypes.STRING_SEARCH function stringSearch(filterValues, featureValue, params = {}) { const normalizedFeatureValue = normalize(featureValue, params); - const stringRegExp = filterValues - .map((filterValue) => { - let formattedValue = filterValue; + const stringRegExp = params.useRegExp + ? filterValues + : filterValues.map((filterValue) => { + let stringRegExp = escapeRegExp(normalize(filterValue, params)); - if (!params.useRegExp) { - formattedValue = escapeRegExp(normalize(filterValue, params)); - if (params.mustStart) formattedValue = `^${formattedValue}`; - if (params.mustEnd) formattedValue = `${formattedValue}$`; - } + if (params.mustStart) stringRegExp = `^${stringRegExp}`; + if (params.mustEnd) stringRegExp = `${stringRegExp}$`; - return formattedValue; - }) - .join('|'); + return stringRegExp; + }); - const regex = new RegExp(stringRegExp, 'g'); + const regex = new RegExp(stringRegExp.join('|'), params.caseSensitive ? 'g' : 'gi'); return !!normalizedFeatureValue.match(regex); } @@ -75,9 +72,7 @@ function escapeRegExp(value) { } function normalize(data, params) { - let normalizedData = '' + data; - - if (!params.caseSensitive) normalizedData = normalizedData.toLocaleLowerCase(); + let normalizedData = String(data); if (!params.keepSpecialCharacters) normalizedData = normalizedData.normalize('NFD').replace(normalizeRegExp, '');