no-profanity
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -5,2 +5,7 @@ import { ProfanityEngine } from "@coffeeandfun/google-profanity-words"; | ||
/** | ||
* Checks if the given test string contains any profanities based on the provided arguments. | ||
* @param {string | { testString: string, options: object }} arguments - The arguments as per documentation where `testString` is the string to test and `options` is an object containing options. Or just a string to test without aditional options. | ||
* @returns {boolean} - A boolean value indicating whether the test string contains profanities or not. | ||
*/ | ||
const isProfane = (args) => { | ||
@@ -11,4 +16,11 @@ const matches = containsProfanities(args); | ||
/** | ||
* Checks if the given test string contains any profanities based on the provided arguments. | ||
* @param {string | { testString: string, options: object }} arguments - The arguments as per documentation where `testString` is the string to test and `options` is an object containing options. Or just a string to test without aditional options. | ||
* @returns {Array<{ word: string, index: number }>} - An array of objects representing the matched profanities and their indices in the test string. | ||
*/ | ||
const containsProfanities = (args) => { | ||
const { testString, wordList } = parseArgs(args); | ||
const { testString, options, wordList } = parseArgs(args); | ||
if (wordList.length === 0) return []; | ||
const matches = []; | ||
@@ -25,2 +37,7 @@ const regex = new RegExp(`\\b(${wordList.join("|")})\\b`, "ig"); | ||
/** | ||
* Replaces profanities in the given test string based on the provided arguments. | ||
* @param {string | { testString: string, options: object }} arguments - The arguments as per documentation where `testString` is the string to test and `options` is an object containing options. Or just a string to test without aditional options. | ||
* @returns {string} - The test string with profanities replaced. | ||
*/ | ||
const replaceProfanities = (args) => { | ||
@@ -30,3 +47,3 @@ const { testString, options } = parseArgs(args); | ||
const replacement = options.replacement ? options.replacement : "*"; | ||
const replacement = options.replacement ?? "*"; | ||
let newString = testString; | ||
@@ -42,2 +59,7 @@ | ||
/** | ||
* Parses the arguments and returns an object containing the test string, options, and word list. | ||
* @param {string | { testString: string, options: object }} arguments - The arguments as per documentation where `testString` is the string to test and `options` is an object containing options. Or just a string to test without aditional options. | ||
* @returns {{ testString: string, options: object, wordList: Array<string> }} - The parsed arguments. | ||
*/ | ||
const parseArgs = (args) => { | ||
@@ -48,5 +70,11 @@ let excludededSet = new Set(); | ||
} | ||
let wordList = baseList.filter((word) => !excludededSet.has(word)); | ||
let wordList = []; | ||
if (args?.options?.emptyList) { | ||
wordList = []; | ||
} else { | ||
wordList = baseList.filter((word) => !excludededSet.has(word)); | ||
} | ||
let options, testString; | ||
if (typeof args === "string") { | ||
@@ -60,2 +88,12 @@ testString = args; | ||
/* | ||
the list option is deprecated as it is unambiguous, | ||
but we still want to support it to be compatible | ||
with the "bad-words" package | ||
*/ | ||
if (typeof options?.list === "object" && options?.list?.length > 0) { | ||
log("The list option is deprecated, please use includes instead.", args); | ||
options.includes = [...(options.list ?? []), ...(options.includes ?? [])]; | ||
} | ||
if (typeof options?.includes === "object" && options?.includes?.length > 0) { | ||
@@ -65,5 +103,26 @@ wordList = [...wordList, ...options.includes]; | ||
if (options?.regex) { | ||
log("The regex option is deprecated, please use preSanitize instead.", args); | ||
options.preSanitize = options.regex; | ||
} | ||
if (options?.preSanitize) { | ||
testString = testString.replace(options.preSanitize, options?.preSanitizeReplacement ?? ""); | ||
} | ||
if (options?.placeHolder) { | ||
log("The placeHolder option is deprecated, please use replacement instead.", args); | ||
options.replacement = options.placeHolder; | ||
} | ||
return { testString, options, wordList }; | ||
}; | ||
const log = (msg, args) => { | ||
if (args?.options?.surpressWarnings) { | ||
return; | ||
} | ||
console.log(msg); | ||
}; | ||
export { isProfane, containsProfanities, replaceProfanities }; |
{ | ||
"name": "no-profanity", | ||
"version": "1.0.3", | ||
"description": "A JavaScript tool to detect and filter profanity", | ||
"version": "1.1.0", | ||
"description": "A JavaScript package to detect and filter profanity", | ||
"main": "./lib/no-profanity.js", | ||
@@ -11,4 +11,3 @@ "directories": { | ||
"scripts": { | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --reporters=default", | ||
"docs": "./node_modules/.bin/documentation readme --section API ./lib/badwords.js" | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --reporters=default" | ||
}, | ||
@@ -15,0 +14,0 @@ "repository": { |
# no-profanity | ||
A JavaScript tool to detect and filter profanity | ||
A JavaScript package to detect and filter profanity. Yes this code, and with code I mean tests, contain profanity. But what do you expect from a test in a no-profanity package. | ||
## Installation | ||
@@ -8,5 +8,7 @@ ``` | ||
``` | ||
## Credit | ||
Thanks to [bad-words](https://github.com/web-mech/badwords) for the original package. This package is a rewrite of that package, with some extra features and a lot of performance improvements. | ||
## Usage | ||
Using the no-profanity library is very simple. You can use it to detect profanity, or to filter profanity from a string. There are some basic options as well, such as overriding the placeholder character, or adding/removing words from the filterlist. | ||
Using the no-profanity package is very simple. You can use it to detect profanity, or to filter profanity from a string. There are some basic options as well, such as overriding the placeholder character, or adding/removing words from the filterlist. | ||
@@ -77,2 +79,13 @@ ### Check if a string contains profanity | ||
You can also remove all words from the filter list so you can start your own, using the `emptyList` property. | ||
```js | ||
replaceProfanities({ | ||
testString: "testable string", | ||
options: { | ||
emptyList: true | ||
} | ||
}); | ||
``` | ||
### Add words to the filterlist | ||
@@ -90,2 +103,33 @@ The options object should contain a property called `includes` which should be an array of strings you want to filter on. | ||
### Pre-sanitize the string | ||
The options object has the option to contain a regex pattern to sanitize the string before checking for profanities. | ||
```js | ||
replaceProfanities({ | ||
testString: "testable string", | ||
options: { | ||
preSanitize: /[^a-zA-Z0-9]/g | ||
} | ||
}); | ||
``` | ||
All matches will be replaced with an empty string. If you want to change the replacement for `preSanitize`, you can use the `preSanitizeReplacement` property. | ||
```js | ||
replaceProfanities({ | ||
testString: "tabs are the best", | ||
options: { | ||
preSanitizeReplacement: "spaces", | ||
preSanitize: /\btabs\b/, | ||
} | ||
}); | ||
``` | ||
## bad-words package | ||
This package is the replacement for the `bad-words` package which is outdated and slow. According to a handful of benchmarks, this package is about 150 times as fast. | ||
See also [this](other-packages.md) page for more information and the migration guide. | ||
Most options from the `bad-words` package are usable in the `options` object to be used in this package. However, the `replaceRegex` option is not supported. | ||
## Contributing | ||
@@ -95,2 +139,2 @@ Any contributions are highly appreciated. If you want to contribute, please fork the repository and create a pull request. If you have any questions, feel free to create an issue. | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. With MIT comes the freedom to use the code for whatever you want, but a credit would be nice. | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. With MIT comes the freedom to use the code for whatever you want, but a credit would be appreciated. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16511
120
136
8