Filtering strings or sentences
This works by replacing blacklisted words with grawlix. A grawlix is string of typographical symbols used to replace/censor words.
Syntax
redact(string: string, blacklist: string[], options?: object): string
Parameters
string: string
- Given string/sentence to be filtered.
blacklist: string[]
- Blacklist
options
- Additional options that may be passed.
Options
props | default | description |
---|---|---|
grawlix |
* | Character used to replace the blacklisted word. |
replaceByLength |
true | Determines if the blacklisted word should be replaced by its word length or only once |
caseSensitive |
false | Determines if the check for a blacklisted word in a sentence is case sensitive |
Returns
string
- The resulting sentence/string that has its blacklisted words replaced by a grawlix.
Error
Throws an error if the required parameters are not given to the function.
Import
import { redact } from '@princedev/redact';
Examples
Basic Usage
Basic usage with default options.
const racialBlacklist = blacklist('nigga', 'eskimo');
const redactSentence = redact('Wassup my nigga', racialBlacklist)
redactSentence
would be 'Wassup my *****'.
Grawlix Usage
Usage with changing the grawlix option.
const racialBlacklist = blacklist('nigga', 'eskimo');
const redactSentence = redact('Wassup my nigga', racialBlacklist, {
grawlix: '$',
});
redactSentence
would be 'Wassup my $$$$$'.
Replace by Length Usage
Usage with turning off replace by length
const racialBlacklist = blacklist('nigga', 'eskimo');
const redactSentence = redact('Wassup my nigga', racialBlacklist, {
replaceByLength: false,
});
redactSentence
would be 'Wassup my $'.
Case Sensitive Usage
Usage with turning off replace by length
const racialBlacklist = blacklist('nigga', 'eskimo');
const redactSentence = redact('Wassup my Nigga-nigga', racialBlacklist, {
caseSensitive: true,
});
redactSentence
would be 'Wassup my Nigga-*****'.
Multiple Option Usage
Usage with multiple options.
const racialBlacklist = blacklist('nigga', 'eskimo');
const redactSentence = redact('Wassup my nigga', racialBlacklist, {
grawlix: 'censored',
replaceByLength: false,
caseSensitive: false,
});
redactSentence
would be 'Wassup my censored'.