Skip to content

The open alternative to Salesforce, designed for AI.

fix(twenty-shared): honor wildcard parity in rich text ilike filter (#25505)

Abdellatif Anaflous2522a49e44fc
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRichTextFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRichTextFilter.test.ts
index 806b3f7f2a1..bca09dcea6a 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRichTextFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRichTextFilter.test.ts
@@ -28,13 +28,130 @@ describe('isMatchingRichTextFilter', () => {
}),
).toBe(true);
});
+
+ it('should treat underscore as a single character wildcard like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a_c' } },
+ value: 'abc',
+ }),
+ ).toBe(true);
+ });
+
+ it('should not match several characters for underscore', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a_c' } },
+ value: 'abxc',
+ }),
+ ).toBe(false);
+ });
+
+ it('should match a newline with underscore like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a_c' } },
+ value: 'a\nc',
+ }),
+ ).toBe(true);
+ });
+
+ it('should match a newline with percent like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a%c' } },
+ value: 'a\nc',
+ }),
+ ).toBe(true);
+ });
+
+ it('should match an astral character with underscore like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a_c' } },
+ value: 'a\u{1F600}c',
+ }),
+ ).toBe(true);
+ });
+
+ it('should keep an escaped percent literal like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: '100\\%' } },
+ value: '100%',
+ }),
+ ).toBe(true);
+ });
+
+ it('should not let an escaped percent act as a wildcard', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: '100\\%' } },
+ value: '100a',
+ }),
+ ).toBe(false);
+ });
+
+ it('should keep an escaped underscore literal like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a\\_c' } },
+ value: 'a_c',
+ }),
+ ).toBe(true);
+ });
+
+ it('should not let an escaped underscore act as a wildcard', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a\\_c' } },
+ value: 'abc',
+ }),
+ ).toBe(false);
+ });
+
+ it('should keep an escaped backslash literal like SQL ILIKE', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'a\\\\c' } },
+ value: 'a\\c',
+ }),
+ ).toBe(true);
+ });
+
+ it('should reject a trailing escape character', () => {
+ expect(() =>
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 'abc\\' } },
+ value: 'abc\\',
+ }),
+ ).toThrow('LIKE pattern must not end with escape character');
+ });
+
+ it('should not broaden matching with Unicode case folding', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: { ilike: 's' } },
+ value: 'ſ',
+ }),
+ ).toBe(false);
+ });
+
+ it('should not match when the markdown filter has no ilike', () => {
+ expect(
+ isMatchingRichTextFilter({
+ richTextFilter: { markdown: {} },
+ value: 'undefined',
+ }),
+ ).toBe(false);
+ });
});
describe('default', () => {
it('should throw for unexpected filter', () => {
expect(() =>
isMatchingRichTextFilter({
- richTextFilter: {} as any,
+ richTextFilter: {},
value: 'test',
}),
).toThrow('Unexpected value for RICH_TEXT filter');
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingRichTextFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingRichTextFilter.ts
index 08367798c33..87d972b2f1f 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingRichTextFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingRichTextFilter.ts
@@ -1,5 +1,6 @@
import { type RichTextFilter } from '@/types';
-import escapeRegExp from 'lodash.escaperegexp';
+import { convertLikePatternToRegexOrThrow } from '@/utils/filter/utils/convertLikePatternToRegexOrThrow';
+import { isDefined } from '@/utils/validation/isDefined';
export const isMatchingRichTextFilter = ({
richTextFilter,
@@ -10,9 +11,14 @@ export const isMatchingRichTextFilter = ({
}) => {
switch (true) {
case richTextFilter.markdown !== undefined: {
- const escapedPattern = escapeRegExp(richTextFilter.markdown.ilike);
- const regexPattern = escapedPattern.replace(/%/g, '.*');
- const regexCaseInsensitive = new RegExp(`^${regexPattern}$`, 'i');
+ if (!isDefined(richTextFilter.markdown.ilike)) {
+ return false;
+ }
+
+ const regexCaseInsensitive = convertLikePatternToRegexOrThrow({
+ pattern: richTextFilter.markdown.ilike,
+ isCaseInsensitive: true,
+ });
return regexCaseInsensitive.test(value);
}