Skip to content

Latest commit

 

History

History
153 lines (101 loc) · 3.97 KB

RegExpMatcher.md

File metadata and controls

153 lines (101 loc) · 3.97 KB

obscenity / RegExpMatcher

Class: RegExpMatcher

An implementation of the [[Matcher]] interface using regular expressions and string searching methods.

Implements

Table of contents

Constructors

Methods

Constructors

constructor

new RegExpMatcher(options)

Creates a new [[RegExpMatcher]] with the options given.

Example

// Use the options provided by the English preset.
const matcher = new RegExpMatcher({
	...englishDataset.build(),
	...englishRecommendedTransformers,
});

Example

// Simple matcher that only has blacklisted patterns.
const matcher = new RegExpMatcher({
 blacklistedTerms: assignIncrementingIds([
     pattern`fuck`,
     pattern`f?uck`, // wildcards (?)
     pattern`bitch`,
     pattern`b[i]tch` // optionals ([i] matches either "i" or "")
 ]),
});

// Check whether some string matches any of the patterns.
const doesMatch = matcher.hasMatch('fuck you bitch');

Example

// A more advanced example, with transformers and whitelisted terms.
const matcher = new RegExpMatcher({
 blacklistedTerms: [
     { id: 1, pattern: pattern`penis` },
     { id: 2, pattern: pattern`fuck` },
 ],
 whitelistedTerms: ['pen is'],
 blacklistMatcherTransformers: [
     resolveConfusablesTransformer(), // '🅰' => 'a'
     resolveLeetSpeakTransformer(), // '$' => 's'
     foldAsciiCharCaseTransformer(), // case insensitive matching
     skipNonAlphabeticTransformer(), // 'f.u...c.k' => 'fuck'
     collapseDuplicatesTransformer(), // 'aaaa' => 'a'
 ],
});

// Output all matches.
console.log(matcher.getAllMatches('fu.....uuuuCK the pen is mightier than the sword!'));

Parameters

Name Type Description
options RegExpMatcherOptions Options to use.

Defined in

src/matcher/regexp/RegExpMatcher.ts:74

Methods

getAllMatches

getAllMatches(input, sorted?): MatchPayload[]

Returns all matches of blacklisted terms in the text.

If you only need to check for the presence of a match, and do not need more specific information about the matches, use the hasMatch() method, which is typically more efficient.

Parameters

Name Type Default value Description
input string undefined Text to find profanities in.
sorted boolean false Whether the resulting list of matches should be sorted using [[compareMatchByPositionAndId]]. Defaults to false.

Returns

MatchPayload[]

A list of matches of the matcher on the text. The matches are guaranteed to be sorted if and only if the sorted parameter is true, otherwise, their order is unspecified.

Implementation of

Matcher.getAllMatches

Defined in

src/matcher/regexp/RegExpMatcher.ts:87


hasMatch

hasMatch(input): boolean

Checks whether there is a match for any blacklisted term in the text.

This is typically more efficient than calling getAllMatches and checking the result, though it depends on the implementation.

Parameters

Name Type Description
input string Text to check.

Returns

boolean

Implementation of

Matcher.hasMatch

Defined in

src/matcher/regexp/RegExpMatcher.ts:120