Signal-Desktop/ts/util/cleanSearchTerm.ts

28 lines
824 B
TypeScript
Raw Permalink Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export function cleanSearchTerm(searchTerm: string): string {
2019-01-14 21:49:58 +00:00
const lowercase = searchTerm.toLowerCase();
const withoutSpecialCharacters = lowercase.replace(
/([-!"#$%&'()*+,./\\:;<=>?@[\]^_`{|}~])/g,
2019-01-14 21:49:58 +00: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();
}