Skip to content

Commit

Permalink
refactor(matchers): remove NfaMatcher
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The NfaMatcher class has been removed. Use the RegExpMatcher instead.
  • Loading branch information
jo3-l committed Jan 5, 2024
1 parent 34d45da commit fd84fdd
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 184 deletions.
149 changes: 77 additions & 72 deletions docs/reference/README.md

Large diffs are not rendered by default.

47 changes: 25 additions & 22 deletions docs/reference/classes/DataSet.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ associating metadata with them.

## Type parameters

| Name | Description |
| :------ | :------ |
| Name | Description |
| :------------- | :----------------------------------------------------------------------------- |
| `MetadataType` | Metadata type for phrases. Note that the metadata type is implicitly nullable. |

## Table of contents
Expand All @@ -33,8 +33,8 @@ associating metadata with them.

#### Type parameters

| Name |
| :------ |
| Name |
| :------------- |
| `MetadataType` |

## Methods
Expand All @@ -53,8 +53,8 @@ const customDataset = new DataSet().addAll(englishDataset);

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :------ | :--------------------------------------- | :------------- |
| `other` | [`DataSet`](DataSet.md)<`MetadataType`\> | Other dataset. |

#### Returns
Expand All @@ -65,7 +65,7 @@ const customDataset = new DataSet().addAll(englishDataset);

[src/dataset/DataSet.ts:29](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L29)

___
---

### addPhrase

Expand All @@ -77,17 +77,20 @@ Adds a phrase to this dataset.

```typescript
const data = new DataSet<{ originalWord: string }>()
.addPhrase((phrase) => phrase.setMetadata({ originalWord: 'fuck' })
.addPattern(pattern`fuck`)
.addPattern(pattern`f[?]ck`)
.addWhitelistedTerm('Afck'))
.addPhrase((phrase) =>
phrase
.setMetadata({ originalWord: 'fuck' })
.addPattern(pattern`fuck`)
.addPattern(pattern`f[?]ck`)
.addWhitelistedTerm('Afck'),
)
.build();
```

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :--- | :------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------- |
| `fn` | (`builder`: [`PhraseBuilder`](PhraseBuilder.md)<`MetadataType`\>) => [`PhraseBuilder`](PhraseBuilder.md)<`MetadataType`\> | A function that takes a [[PhraseBuilder]], adds patterns/whitelisted terms/metadata to it, and returns it. |

#### Returns
Expand All @@ -98,11 +101,11 @@ const data = new DataSet<{ originalWord: string }>()

[src/dataset/DataSet.ts:75](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L75)

___
---

### build

**build**(): `Pick`<[`RegExpMatcherOptions`](../interfaces/RegExpMatcherOptions.md), ``"blacklistedTerms"`` \| ``"whitelistedTerms"``\>
**build**(): `Pick`<[`RegExpMatcherOptions`](../interfaces/RegExpMatcherOptions.md), `"blacklistedTerms"` \| `"whitelistedTerms"`\>

Returns the dataset in a format suitable for usage with the [[RegExpMatcher]].

Expand All @@ -118,13 +121,13 @@ const matcher = new RegExpMatcher({

#### Returns

`Pick`<[`RegExpMatcherOptions`](../interfaces/RegExpMatcherOptions.md), ``"blacklistedTerms"`` \| ``"whitelistedTerms"``\>
`Pick`<[`RegExpMatcherOptions`](../interfaces/RegExpMatcherOptions.md), `"blacklistedTerms"` \| `"whitelistedTerms"`\>

#### Defined in

[src/dataset/DataSet.ts:118](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L118)

___
---

### getPayloadWithPhraseMetadata

Expand All @@ -144,8 +147,8 @@ const phraseMetadata = matchesWithPhraseMetadata[0].phraseMetadata;

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :-------- | :---------------------------------------------- | :---------------------- |
| `payload` | [`MatchPayload`](../interfaces/MatchPayload.md) | Original match payload. |

#### Returns
Expand All @@ -156,7 +159,7 @@ const phraseMetadata = matchesWithPhraseMetadata[0].phraseMetadata;

[src/dataset/DataSet.ts:94](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L94)

___
---

### removePhrasesIf

Expand All @@ -174,8 +177,8 @@ const customDataset = new DataSet<{ originalWord: string }>()

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :---------- | :---------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------- |
| `predicate` | (`phrase`: [`PhraseContainer`](../interfaces/PhraseContainer.md)<`MetadataType`\>) => `boolean` | A predicate that determines whether or not a phrase should be removed. Return `true` to remove, `false` to keep. |

#### Returns
Expand Down
18 changes: 9 additions & 9 deletions docs/reference/classes/ParserError.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ Custom error thrown by the parser when syntactical errors are detected.

#### Parameters

| Name | Type |
| :------ | :------ |
| Name | Type |
| :-------- | :------- |
| `message` | `string` |
| `line` | `number` |
| `column` | `number` |
| `line` | `number` |
| `column` | `number` |

#### Overrides

Expand All @@ -59,7 +59,7 @@ Note that surrogate pairs are counted as 1 column wide, not 2.

[src/pattern/ParserError.ts:16](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/pattern/ParserError.ts#L16)

___
---

### line

Expand All @@ -71,7 +71,7 @@ The line on which the error occurred (one-based).

[src/pattern/ParserError.ts:10](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/pattern/ParserError.ts#L10)

___
---

### message

Expand All @@ -85,11 +85,11 @@ Error.message

node_modules/.pnpm/typescript@5.2.2/node_modules/typescript/lib/lib.es5.d.ts:1068

___
---

### name

`Readonly` **name**: ``"ParserError"``
`Readonly` **name**: `"ParserError"`

#### Overrides

Expand All @@ -99,7 +99,7 @@ Error.name

[src/pattern/ParserError.ts:5](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/pattern/ParserError.ts#L5)

___
---

### stack

Expand Down
26 changes: 13 additions & 13 deletions docs/reference/classes/PhraseBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Builder for phrases.

## Type parameters

| Name |
| :------ |
| Name |
| :------------- |
| `MetadataType` |

## Table of contents
Expand All @@ -31,8 +31,8 @@ Builder for phrases.

#### Type parameters

| Name |
| :------ |
| Name |
| :------------- |
| `MetadataType` |

## Methods
Expand All @@ -45,8 +45,8 @@ Associates a pattern with this phrase.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :-------- | :------------------------------------------------ | :-------------- |
| `pattern` | [`ParsedPattern`](../interfaces/ParsedPattern.md) | Pattern to add. |

#### Returns
Expand All @@ -57,7 +57,7 @@ Associates a pattern with this phrase.

[src/dataset/DataSet.ts:149](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L149)

___
---

### addWhitelistedTerm

Expand All @@ -67,8 +67,8 @@ Associates a whitelisted pattern with this phrase.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :----- | :------- | :----------------------- |
| `term` | `string` | Whitelisted term to add. |

#### Returns
Expand All @@ -79,7 +79,7 @@ Associates a whitelisted pattern with this phrase.

[src/dataset/DataSet.ts:159](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L159)

___
---

### build

Expand All @@ -96,7 +96,7 @@ Builds the phrase, returning a [[PhraseContainer]] for use with the

[src/dataset/DataSet.ts:178](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/dataset/DataSet.ts#L178)

___
---

### setMetadata

Expand All @@ -106,8 +106,8 @@ Associates some metadata with this phrase.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :---------- | :------------- | :--------------- |
| `metadata?` | `MetadataType` | Metadata to use. |

#### Returns
Expand Down
54 changes: 27 additions & 27 deletions docs/reference/classes/RegExpMatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ const matcher = new RegExpMatcher({
```typescript
// 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 "")
]),
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.
Expand All @@ -60,18 +60,18 @@ const doesMatch = matcher.hasMatch('fuck you bitch');
```typescript
// 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'
],
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.
Expand All @@ -80,8 +80,8 @@ console.log(matcher.getAllMatches('fu.....uuuuCK the pen is mightier than the sw

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :-------- | :-------------------------------------------------------------- | :-------------- |
| `options` | [`RegExpMatcherOptions`](../interfaces/RegExpMatcherOptions.md) | Options to use. |

#### Defined in
Expand All @@ -102,10 +102,10 @@ 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`. |
| 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

Expand All @@ -123,7 +123,7 @@ otherwise, their order is unspecified.

[src/matcher/regexp/RegExpMatcher.ts:86](https://github.com/jo3-l/obscenity/blob/1a1f44e/src/matcher/regexp/RegExpMatcher.ts#L86)

___
---

### hasMatch

Expand All @@ -136,8 +136,8 @@ checking the result, though it depends on the implementation.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| Name | Type | Description |
| :------ | :------- | :------------- |
| `input` | `string` | Text to check. |

#### Returns
Expand Down
Loading

0 comments on commit fd84fdd

Please sign in to comment.