feat: cyrillic like query support (#1078)

This commit is contained in:
Elliot DeNolf
2022-09-06 16:47:57 -04:00
committed by GitHub
parent 2ee4c7ad72
commit b7e5828adc
3 changed files with 23 additions and 2 deletions

View File

@@ -97,7 +97,7 @@ export const sanitizeQueryValue = (schemaType: SchemaType, path: string, operato
}
if (operator === 'like' && typeof formattedValue === 'string') {
const $regex = wordBoundariesRegex(formattedValue)
const $regex = wordBoundariesRegex(formattedValue);
formattedValue = { $regex };
}
}

View File

@@ -1,7 +1,12 @@
export default (input: string): RegExp => {
const words = input.split(' ');
// Regex word boundaries that work for cyrillic characters - https://stackoverflow.com/a/47062016/1717697
const wordBoundaryBefore = '(?:(?<=[^\\p{L}\\p{N}])|^)';
const wordBoundaryAfter = '(?=[^\\p{L}\\p{N}]|$)';
const regex = words.reduce((pattern, word, i) => {
return `${pattern}(?=.*\\b${word}.*\\b)${i + 1 === words.length ? '.+' : ''}`;
return `${pattern}(?=.*${wordBoundaryBefore}${word}.*${wordBoundaryAfter})${i + 1 === words.length ? '.+' : ''}`;
}, '');
return new RegExp(regex, 'i');
};

View File

@@ -373,6 +373,22 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1);
});
it('like - cyrillic characters', async () => {
const post1 = await createPost({ title: 'Тест' });
const { status, result } = await client.find<Post>({
query: {
title: {
like: 'Тест',
},
},
});
expect(status).toEqual(200);
expect(result.docs).toEqual([post1]);
expect(result.totalDocs).toEqual(1);
});
it('like - partial word match', async () => {
const post = await createPost({ title: 'separate words should partially match' });