2023-01-03 11:55:46 -08:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 15:34:04 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-14 14:56:35 -07:00
|
|
|
export function cleanSearchTerm(searchTerm: string): string {
|
2019-01-14 13:49:58 -08:00
|
|
|
const lowercase = searchTerm.toLowerCase();
|
|
|
|
const withoutSpecialCharacters = lowercase.replace(
|
2021-04-02 22:29:54 +02:00
|
|
|
/([-!"#$%&'()*+,./\\:;<=>?@[\]^_`{|}~])/g,
|
2019-01-14 13:49:58 -08:00
|
|
|
' '
|
|
|
|
);
|
|
|
|
const whiteSpaceNormalized = withoutSpecialCharacters.replace(/\s+/g, ' ');
|
|
|
|
const byToken = whiteSpaceNormalized.split(' ');
|
|
|
|
const withoutSpecialTokens = byToken.filter(
|
|
|
|
token =>
|
|
|
|
token &&
|
|
|
|
token !== 'and' &&
|
|
|
|
token !== 'or' &&
|
|
|
|
token !== 'not' &&
|
|
|
|
token !== ')' &&
|
|
|
|
token !== '(' &&
|
|
|
|
token !== '+' &&
|
|
|
|
token !== ',' &&
|
|
|
|
token !== 'near'
|
|
|
|
);
|
|
|
|
const withWildcards = withoutSpecialTokens.map(token => `${token}*`);
|
|
|
|
|
|
|
|
return withWildcards.join(' ').trim();
|
|
|
|
}
|