Comparing version 0.4.1 to 0.4.2
@@ -112,3 +112,4 @@ import type { TextCensorStrategy } from './TextCensor'; | ||
* A text censoring strategy that generates replacement strings made up of | ||
* random characters from the set of characters provided. | ||
* random characters from the set of characters provided. The strings never | ||
* contain two of the same character in a row. | ||
* | ||
@@ -120,8 +121,8 @@ * @example | ||
* // Before: 'fuck you!' | ||
* // After: '!##$ you!' | ||
* // After: '!#$# you!' | ||
* ``` | ||
* @param charset - Set of characters from which the replacement string should | ||
* be constructed. Must not be empty. | ||
* be constructed. Must have at least two characters. | ||
* @returns A [[TextCensorStrategy]] for use with the [[TextCensor]]. | ||
*/ | ||
export declare function randomCharFromSetCensorStrategy(charset: string): TextCensorStrategy; |
@@ -145,3 +145,4 @@ "use strict"; | ||
* A text censoring strategy that generates replacement strings made up of | ||
* random characters from the set of characters provided. | ||
* random characters from the set of characters provided. The strings never | ||
* contain two of the same character in a row. | ||
* | ||
@@ -153,6 +154,6 @@ * @example | ||
* // Before: 'fuck you!' | ||
* // After: '!##$ you!' | ||
* // After: '!#$# you!' | ||
* ``` | ||
* @param charset - Set of characters from which the replacement string should | ||
* be constructed. Must not be empty. | ||
* be constructed. Must have at least two characters. | ||
* @returns A [[TextCensorStrategy]] for use with the [[TextCensor]]. | ||
@@ -162,8 +163,19 @@ */ | ||
const chars = [...charset]; | ||
if (chars.length === 0) | ||
throw new Error('The character set passed must not be empty.'); | ||
if (chars.length < 2) | ||
throw new Error('The character set passed must have at least 2 characters.'); | ||
return (ctx) => { | ||
let censored = ''; | ||
for (let i = 0; i < ctx.matchLength; i++) | ||
censored += chars[Math.floor(Math.random() * chars.length)]; | ||
if (ctx.matchLength === 0) | ||
return ''; | ||
let lastIdx = Math.floor(Math.random() * chars.length); | ||
let censored = chars[lastIdx]; | ||
for (let i = 1; i < ctx.matchLength; i++) { | ||
let idx = Math.floor(Math.random() * (chars.length - 1)); | ||
// Transform the distribution for idx from [0, len-1) to | ||
// [0, lastIdx) ∪ (lastIdx, len) to exclude lastIdx while | ||
// ensuring a uniform distribution of generated characters. | ||
if (idx >= lastIdx) | ||
idx++; | ||
lastIdx = idx; | ||
censored += chars[idx]; | ||
} | ||
return censored; | ||
@@ -170,0 +182,0 @@ }; |
@@ -244,3 +244,3 @@ "use strict"; | ||
.addPattern((0, Pattern_1.pattern) `|fuk`) | ||
.addWhitelistedTerm('fickle') | ||
.addWhitelistedTerm('fick') | ||
.addWhitelistedTerm('kung-fu') | ||
@@ -310,8 +310,7 @@ .addWhitelistedTerm('kung fu')) | ||
.setMetadata({ originalWord: 'shit' }) | ||
.addPattern((0, Pattern_1.pattern) `shit`) | ||
.addPattern((0, Pattern_1.pattern) `|shit`) | ||
.addWhitelistedTerm('s hit') | ||
.addWhitelistedTerm('sh it') | ||
.addWhitelistedTerm('shi t') | ||
.addWhitelistedTerm('shitake') | ||
.addWhitelistedTerm('mishit')) | ||
.addWhitelistedTerm('shitake')) | ||
.addPhrase((phrase) => phrase.setMetadata({ originalWord: 'slut' }).addPattern((0, Pattern_1.pattern) `s[s]lut`)) | ||
@@ -318,0 +317,0 @@ .addPhrase((phrase) => phrase.setMetadata({ originalWord: 'spastic' }).addPattern((0, Pattern_1.pattern) `|spastic`)) |
@@ -8,5 +8,8 @@ "use strict"; | ||
['e', '3'], | ||
['i', '1|'], | ||
['i', '1|!'], | ||
['g', '6'], | ||
['o', '0'], | ||
['s', '$'], | ||
['s', '$5'], | ||
['t', '7'], | ||
['z', '2'], | ||
]); |
{ | ||
"name": "obscenity", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Robust, extensible profanity filter.", | ||
@@ -58,3 +58,3 @@ "files": [ | ||
"eslint": "^8.57.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-config-prettier": "^10.0.0", | ||
"eslint-plugin-jest": "^27.9.0", | ||
@@ -64,3 +64,3 @@ "eslint-plugin-prettier": "^4.2.1", | ||
"gen-esm-wrapper": "^1.1.3", | ||
"is-ci": "^3.0.1", | ||
"is-ci": "^4.0.0", | ||
"jest": "^29.7.0", | ||
@@ -67,0 +67,0 @@ "jest-circus": "^29.5.0", |
154402
3463