jsaway
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

JSAway

npm npm NPM GitHub Repo stars

JS Away is a simple swear filter that was inspired by GoAway by TwiN.

Bad words are sourced from chucknorris-io/swear-words

Installation

Go into your terminal and type:

npm install --save jsaway

Usage

First, import the ProfanityDetector class.

import ProfanityDetector from 'jsaway'

Then, make a variable using the class.

const profDetector = new ProfanityDetector()

From here, you can use simple functions to find and detect profanity in text.

profDetector.has('fuck') // true
profDetector.extract('fuck you.') // 'fuck'
profDetector.extractAll('fuck you ass piece of shit') // ['fuck', 'ass', 'shit ']

JSAway also supports basic leetspeak detection (for example, "t3st" will still be detected).

profDetector.has('$h1+') // true
profDetector.extract('i love eating $h1+') // '$h1+'

You can also use the .bounds() function to find the slice that contains the profanity.

const profanityBounds = profDetector.bounds('piece of shit') // [9, 13]
'piece of shit'.slice(...profanityBounds) // 'shit'

JSAway can also censor your strings, using the .censor() method.

The second argument is the censorCharacter, or the character you want to censor with.

The third argument is includeFirstLetter, which includes the first letter of the profanity.

The fourth argument is includeLastLetter, which is like includeFirstLetter, but for the last letter of the profanity.

profDetector.censor('Fuck you. Fuck this shit.') // **** you. **** this ****.
profDetector.censor('Fuck you. Fuck this shit.', '-', true) // F--- you. F--- this s---.
profDetector.censor('Fuck you. Fuck this shit.', '•', true, true) // F••k you. F••k this s••t.

Customization

JSAway can be customized to only block certain profanities. You can do this buy either editing the list of default profanities, or by creating your own.

To add and remove profanities from the default profanities (see profanities.json for the full list), you can use the ProfanityDetector.addProfanity and ProfanityDetector.removeProfanity functions.

profDetector.addProfanity('test')
profDetector.has('test') // test
profDetector.extract('t3s+') // 't3s+'

profDetector.removeProfanity('test')
profDetector.extract('test') // null

You can also make your entire own list of profanities by simply editing the ProfanityDetector.profanities array.

profDetector.profanities = ['test', 'words']
profDetector.has('test') // true
profDetector.extract('t3s+') // 't3s+'

Example Usage

Message contains blocked words

import ProfanityDetector from 'jsaway'

const profDetector = new ProfanityDetector()

let message = 'Hello there!'
if (!profDetector.has(message)) {
    console.log(message)
} else {
    console.log(`Your message contains blocked words: 
    ${profDetector.extractAll(message).join(', ').slice(0, -2)}`)
}

Package Sidebar

Install

npm i jsaway

Weekly Downloads

0

Version

1.2.1

License

MIT

Unpacked Size

12.6 kB

Total Files

8

Last publish

Collaborators

  • malkist