censorBadWords method

Future<String> censorBadWords(
  1. String input
)

Censors bad words in the input by replacing them with asterisks.

Each bad word is replaced with asterisks matching its length. The check is case-insensitive.

input is the text to censor.

Returns the censored text with bad words replaced by asterisks.

Implementation

Future<String> censorBadWords(String input) async {
  await _loadBadWords();
  return input.splitMapJoin(
    RegExp(r'\b\w+\b'),
    onMatch: (match) {
      final word = match.group(0)!;
      return _badWords!.contains(word.toLowerCase()) ? '*' * word.length : word;
    },
    onNonMatch: (nonMatch) => nonMatch,
  );
}