Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ A simple node utility that removes/replaces profane words in given text. Very ea

The module uses the profane words curated and maintained by [Shutterstock Project](https://github.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words).

*Default locale is en-base*
*Default locale is en*

## Supported replacement patterns

| Replacement Pattern | Default replacement Word/Characters | Sample Output |
| --- | --- | --- |
| word | BLEEP | Your ass becomes Your BLEEP |
| character | * | Your ass becomes Your \*\*\* |
| grawlix | - | Your ass becomes You &!# |
| grawlix | - | Your ass becomes You &!# |

*Default replacement pattern is 'star'*

Expand All @@ -25,15 +25,15 @@ The module uses the profane words curated and maintained by [Shutterstock Projec

### cleanser.replace(inputString<string>, replacementPattern<string>, replacementWord<string>)

Replaces the bad words in the given inputString with the replacement pattern and replacement word passed to the function. For more usage examples please see ```javascript example.js```
Replaces the bad words in the given inputString with the replacement pattern and replacement word passed to the function. For more usage examples please see ```javascript example.js```

#### Default replacement

```javascript
var cleanser = require('profanity-cleanser');

// Sets to default locale of en-base
cleanser.setLocale();
// Sets to default locale of en
cleanser.setLocale();

var inputString = "Your ass is shit"

Expand All @@ -46,8 +46,8 @@ var output = cleanser.replace(inputString);
```javascript
var cleanser = require('profanity-cleanser');

// Sets to default locale of en-base
cleanser.setLocale();
// Sets to default locale of en
cleanser.setLocale();

var inputString = "Your ass is shit"

Expand All @@ -59,8 +59,8 @@ var output = cleanser.replace(inputString, 'word', 'FOOBAR');
```javascript
var cleanser = require('profanity-cleanser');

// Sets to default locale of en-base
cleanser.setLocale();
// Sets to default locale of en
cleanser.setLocale();

var inputString = "Your ass is shit"

Expand All @@ -75,12 +75,12 @@ Validates and sets the locale for profanity check based on the array passed. Acc
```javascript
var cleanser = require('profanity-cleanser');

// Sets to default locale of en-base
cleanser.setLocale();
// Sets to default locale of en
cleanser.setLocale();

// Sets the locale to hindi spanish and japanese.
cleanser.setLocale(['hi', 'es', 'ja']);
```
```

### cleanser.getLocale()

Expand All @@ -100,7 +100,7 @@ console.log(output.toString())

### cleanser.addWords([string])

This method allows you to add more words of your choice to the dictionary.
This method allows you to add more words of your choice to the dictionary.

```javascript
var cleanser = require('profanity-cleanser');
Expand All @@ -117,7 +117,7 @@ var output = cleanser.replace(input)

### cleanser.removeWords(string)

This method allows you to remove word from the dictionary.
This method allows you to remove word from the dictionary.

```javascript
var cleanser = require('profanity-cleanser');
Expand Down Expand Up @@ -151,5 +151,5 @@ var output = cleanser.getDictionary();
- Also used @jwils0n's [profanity-filter](https://github.com/jwils0n/profanity-filter) as reference for grawlix implementation.

- Seed data for locale based bad words is obtained from [Shutterstock Project](https://github.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words).


35 changes: 17 additions & 18 deletions filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var _ = require('lodash');
var _ = require('lodash');
var sanitizer = require('./lib/sanitizer.js');
var meta = require('./lib/metadata.js');

Expand All @@ -12,14 +12,14 @@ var grawlixChars = ['!','@','#','$','%','&','*'];

var replacement = {
'character' : function(key, pat){
var keyRepm = _.repeat(pat, key.length);
var keyRepm = _.repeat(pat, key.length);
return keyRepm;
},

'word': function(key, pat){
return pat;
},

'grawlix' : function(key){
var keyReplacement = '',
grawlixLen = grawlixChars.length,
Expand Down Expand Up @@ -49,8 +49,7 @@ module.exports = {
//Now start populating dictionary; before that clear out the dictionary
locale.forEach(function(lang){
// First require the module
var mod = require('./seed/'+lang);
var arrayContents = mod();
var arrayContents = require('naughty-words')[lang];
var otherDictionary = _.concat(dictionary, arrayContents);
dictionary = _.uniq(otherDictionary);
});
Expand All @@ -62,27 +61,27 @@ module.exports = {
{
throw "It appears that locale is not set. Perhaps you forgot to call setLocale ?";
}
return locale;
return locale;
},

/* This method returns a list of available locales supported by the module */
showAvailableLocales: function(){
return meta.getSupportedLocales();
},
/* This method returns a lit of words currently added to the dictionary */

/* This method returns a lit of words currently added to the dictionary */
getDictionary: function(){
return dictionary;
},

/* This method allows you to add more words to the current directory instance incase they are not supported by our seeds*/
addWords: function(inputWordArray){

var sanitizedInputWordArray = sanitizer.sanitizeInputWordArray(inputWordArray);
var otherDictionary = _.concat(dictionary, sanitizedInputWordArray);
dictionary = _.uniq(otherDictionary);
},

/* This method removes words from the dictionary instance */
removeWords: function(inputWords){
if(dictionary.indexOf(inputWords) != -1)
Expand All @@ -94,7 +93,7 @@ module.exports = {

/* This method shows the supported replacement patterns */
showReplacementPatterns: function(){
return validPatterns;
return validPatterns;
},

/* This method replaces the bad words in the string with your replacement characeter */
Expand All @@ -107,7 +106,7 @@ module.exports = {
{
throw "Dictionary is not populated. Perhaps you forgot to call setLocale ?";
}

// Validate the replacementPattern
var input = {};
input.replacementPattern = replacementPattern;
Expand All @@ -127,13 +126,13 @@ module.exports = {
{
keyReplacement = replacement[repPatt.pattern](key, repPatt.word);
var regex = new RegExp('\\b'+ key + '\\b', 'g');
origString = origString.replace(regex, keyReplacement);
lowerStr = origString.toLowerCase();
origString = origString.replace(regex, keyReplacement);
lowerStr = origString.toLowerCase();
}
}
return origString;
}

return origString;

},

};
11 changes: 4 additions & 7 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ var fs = require('fs');
module.exports={

getSupportedLocales: function(){

var fileNames = fs.readdirSync( __dirname + '/../seed');
var validLocales = [];
fileNames.forEach(function(f){
validLocales.push(f.replace(/\.[^/.]+$/, ""));
});

var locales = require('naughty-words');
var validLocales = Object.keys(locales);
return validLocales;
},
};
};
18 changes: 9 additions & 9 deletions lib/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ var meta = require('./metadata.js');
module.exports= {

sanitizeLocales: function(localeArray){

var validLocales = meta.getSupportedLocales();
var locale;
if(typeof localeArray === 'string' || typeof localeArray === 'number')
{
throw "Input to locale should be an array. Eg. ['fr']";
}
}
if(localeArray === undefined)
{
locale = ['en-base']; // default locale is always english-base
}
locale = ['en']; // default locale is always english-base
}
else if(localeArray.length === 0)
{
locale = ['en-base'];
locale = ['en'];
}
else
{
locale = localeArray;
locale = localeArray;
}

// Check that these are valid locales
Expand All @@ -37,7 +37,7 @@ module.exports= {
},

sanitizeInputWordArray: function(inputWordArray){

if(inputWordArray === undefined || typeof inputWordArray === 'string' || typeof inputWordArray === 'number')
{
throw "Input word array should be of a type array. Eg. ['a', 'man']";
Expand All @@ -50,7 +50,7 @@ module.exports= {
var replacementPattern = input.replacementPattern;
var replacementWord = input.replacementWord;
var validPatterns = input.validPatterns;

var returnPat = {};
if(replacementPattern === undefined)
{
Expand Down Expand Up @@ -82,7 +82,7 @@ module.exports= {
}
}
}

returnPat.pattern = replacementPattern;
returnPat.word = replacementWord;
return returnPat ;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"mocha": "^2.4.5"
},
"dependencies": {
"lodash": "^4.6.1"
"lodash": "^4.6.1",
"naughty-words": "^1.0.2"
},
"licenses": [
{
Expand Down
44 changes: 0 additions & 44 deletions seed/ar.js

This file was deleted.

47 changes: 0 additions & 47 deletions seed/cs.js

This file was deleted.

Loading