diff --git a/src/utils/filtering/globalsearch.ts b/src/utils/filtering/globalsearch.ts new file mode 100644 index 0000000..4b4d646 --- /dev/null +++ b/src/utils/filtering/globalsearch.ts @@ -0,0 +1,23 @@ +import filteringUtils from '../../config/filtering/index.json'; + +export function buildGlobalSearchFilterString( + fields: string[], + template: string, + keywords: string[], +): string { + const strArray: string[] = []; + + keywords.forEach((keyword: string) => { + const fieldStrArray: string[] = fields.map((field: string) => + template + .replace('', field) + .replace( + '', + `'${encodeURIComponent(keyword.replace(/'/g, "''"))}'`, + ), + ); + strArray.push(`(${fieldStrArray.join(` ${filteringUtils.or_operator} `)})`); + }); + + return strArray.join(` ${filteringUtils.and_operator} `); +}