Skip to content

Commit

Permalink
feat(reset): Add reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoee committed Mar 7, 2017
1 parent e52efc6 commit e2cb907
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ filter.remove('b00b');
filter.remove(['b00b', 'b@@b']);
```

### filter.reset()

Reset word list by using default dictionary (also remove word that manually add)

### filter.clearList()

Clear all profanity words
Expand Down
29 changes: 28 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var fs = require('fs');
var words = JSON.parse(fs.readFileSync('./dictionary/default.json', 'utf8'));
var wordDictionary = [];
wordDictionary['default'] = JSON.parse(fs.readFileSync('./dictionary/default.json', 'utf8'));
var words = JSON.parse(JSON.stringify(wordDictionary['default']));

/**
* LeoProfanity
Expand Down Expand Up @@ -63,6 +65,22 @@ var LeoProfanity = {
return replacementWord;
},

/**
* Get word dictionary
* Now, we only have default dictionary
* (private)
*
* @param {string} dictionaryName
*/
getDictionary: function(dictionaryName) {
var result = [];
if (wordDictionary[dictionaryName] !== 'undefined') {
result = JSON.parse(JSON.stringify(wordDictionary[dictionaryName]));
}

return result;
},

/**
* Return all profanity words
*
Expand Down Expand Up @@ -168,6 +186,15 @@ var LeoProfanity = {
return this;
},

/**
* Reset word list by using default dictionary (also remove word that manually add)
*/
reset: function() {
words = this.getDictionary('default');

return this;
},

/**
* Clear word list
*/
Expand Down
14 changes: 14 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ describe('Profanity filter', function() {
expect(filter.list()).to.not.include('boobs');
});

it('reset', function() {
// reset
filter.reset();

// prepare data to test by adding new 2 bad words
var numberOfCurrentWords = filter.list().length;
filter.add(['badword1', 'badword2']);
expect(filter.list().length).to.equal(numberOfCurrentWords + 2);

// reset
filter.reset();
expect(filter.list().length).to.equal(numberOfCurrentWords);
});

it('clearList', function() {
filter.clearList();
expect(filter.list()).to.be.empty;
Expand Down

0 comments on commit e2cb907

Please sign in to comment.