Skip to content

Commit

Permalink
fix(examples): loosen searching examples to match with more flags (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijemmao authored Jan 11, 2024
1 parent 110ad14 commit cb6811a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/controllers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const createSimpleRegExp = (keywords: { text: string }[]) => ({
`${keywords.map((keyword) => `(${createRegExp(keyword.text, true).wordReg.source})`).join('|')}`,
'i'
),
exampleReg: new RegExp(
`${keywords.map((keyword) => `(${createRegExp(keyword.text, true).exampleReg.source})`).join('|')}`,
'i'
),
definitionsReg: new RegExp(
`${keywords.map((keyword) => `(${createRegExp(keyword.text, true).definitionsReg.source})`).join('|')}`,
'i'
Expand All @@ -48,7 +52,7 @@ const constructRegexQuery = ({
? createSimpleRegExp(keywords)
: keywords?.length
? createSimpleRegExp(keywords)
: { wordReg: /^[.{0,}\n{0,}]/, definitionsReg: /^[.{0,}\n{0,}]/ };
: { wordReg: /^[.{0,}\n{0,}]/, exampleReg: /^[.{0,}\n{0,}]/, definitionsReg: /^[.{0,}\n{0,}]/ };

/* Packages the res response with sorting */
export const packageResponse = ({
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const definitionsQuery = ({

/* Regex match query used to later to defined the Content-Range response header */
export const searchExamplesRegexQuery = ({ regex, flags }: { regex: SearchRegExp; flags: Flags }) => ({
$or: [{ igbo: regex.wordReg }, { english: regex?.definitionsReg }],
$or: [{ igbo: regex.exampleReg }, { english: regex?.definitionsReg }],
...(flags.style ? { style: flags.style } : {}),
});
export const searchIgboTextSearch = fullTextSearchQuery;
Expand Down
4 changes: 3 additions & 1 deletion src/shared/utils/createRegExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import diacriticCodes from '../constants/diacriticCodes';

export interface SearchRegExp {
wordReg: RegExp;
exampleReg: RegExp;
definitionsReg: RegExp;
hardDefinitionsReg?: RegExp;
}
Expand Down Expand Up @@ -67,12 +68,13 @@ const createRegExp = (rawSearchWord: string, hardMatch = false): SearchRegExp =>
: `${startWordBoundary}(^${front}${regexWordString}${back}$)${endWordBoundary}`,
'i'
);

const exampleReg = new RegExp(`${startWordBoundary}(${regexWordString})${endWordBoundary}`, 'i');
const definitionsReg = new RegExp(`${startWordBoundary}(${regexWordString})${endWordBoundary}`, 'i');
const hardDefinitionsReg = new RegExp(`${startWordBoundary}(${hardRegexWordString})${endWordBoundary}`, 'i');

return {
wordReg,
exampleReg,
definitionsReg,
hardDefinitionsReg,
};
Expand Down

0 comments on commit cb6811a

Please sign in to comment.