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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Profanity detection and filtering library.

## Usage

### swearjar.isSubstringProfane(text, start, partialLength)

Returns true if any substring within the given string contains profanity.

var swearjar = require('swearjar');
swearjar.isSubstringProfane("hello there"); // false
swearjar.isSubstringProfane("hellof-bombthere"); // true

### swearjar.profane(text)

Returns true if the given string contains profanity.
Expand Down
25 changes: 24 additions & 1 deletion lib/swearjar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ var swearjar = {

_badWords: {},

// Checks each substring (starting at length of 3 by default) within a string for profanity
// Use regex and .test against each word in en_US.json for very large strings instead
isSubstringProfane: function(text, startParam, partialLengthParam) {
var start = startParam || 0;
var partialLength = partialLengthParam || 3;
if (typeof text !== 'string') return false;
if (text.length <= partialLength) return this.profane(text);
const partialText = text.slice(start, start + partialLength);
if (this.profane(partialText)) {
return true;
} else {
if(partialLength !== text.length) {
if (start !== text.length - partialLength) {
return this.isSubstringProfane(text, ++start, partialLength);
} else {
return this.isSubstringProfane(text, 0, ++partialLength)
}
}
}
return false;
},

scan: function (text, callback) {
var word, key, match;
var regex = /\w+/g
Expand All @@ -20,6 +42,7 @@ var swearjar = {
}
},

// Only checks if a whole word within string is profane
profane: function (text) {
var profane = false;

Expand Down Expand Up @@ -66,7 +89,7 @@ var swearjar = {
var fullPath = path.join(basePath, relativePath);
this._badWords = require(fullPath);
},

setBadWords: function (badWords) {
this._badWords = badWords || {};
}
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
var assert = require('assert');
var swearjar = require('../lib/swearjar.js');

describe('swearjar.isSubstringProfane', function () {

it('should should detect bad words within a string', function () {
assert.equal(swearjar.isSubstringProfane('i love you john doe'), false);
assert.equal(swearjar.isSubstringProfane('fuckyoujohndoe'), true);
});

it('should detect uppercase bad words within a string', function () {
assert.equal(swearjar.isSubstringProfane('FUCKyoujohndoe'), true);
});

it('should detect mixedcase bad words within a string', function () {
assert.equal(swearjar.isSubstringProfane('FuCkyoujohndoe'), true);
});

});

describe('swearjar.profane', function () {

it('should should detect bad words', function () {
Expand Down