fix: implement the same word boundary search as the like query (#1038)

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Wesley
2022-09-01 19:03:21 +02:00
committed by GitHub
parent 32a4e8e9b9
commit c3a0bd8625
7 changed files with 76 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import mongoose, { SchemaType } from 'mongoose';
import { createArrayFromCommaDelineated } from './createArrayFromCommaDelineated';
import { getSchemaTypeOptions } from './getSchemaTypeOptions';
import wordBoundariesRegex from '../utilities/wordBoundariesRegex';
export const sanitizeQueryValue = (schemaType: SchemaType, path: string, operator: string, val: any): unknown => {
let formattedValue = val;
@@ -96,12 +97,8 @@ export const sanitizeQueryValue = (schemaType: SchemaType, path: string, operato
}
if (operator === 'like' && typeof formattedValue === 'string') {
const words = formattedValue.split(' ');
const regex = words.reduce((pattern, word, i) => {
return `${pattern}(?=.*\\b${word}.*\\b)${i + 1 === words.length ? '.+' : ''}`;
}, '');
formattedValue = { $regex: new RegExp(regex), $options: 'i' };
const $regex = wordBoundariesRegex(formattedValue)
formattedValue = { $regex };
}
}