From 7670bf8fdd5119fe9b55c4e4e7918ebd76fbfa09 Mon Sep 17 00:00:00 2001 From: Ryan Reynolds Date: Wed, 1 Feb 2017 14:49:20 -0800 Subject: [PATCH] adds function to check partial strings within submitted text update readme and add tests Check input --- README.md | 8 ++++++++ lib/swearjar.js | 25 ++++++++++++++++++++++++- test/test.js | 17 +++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c7895e9..1f6b524 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/swearjar.js b/lib/swearjar.js index 723d28b..d61baf6 100644 --- a/lib/swearjar.js +++ b/lib/swearjar.js @@ -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 @@ -20,6 +42,7 @@ var swearjar = { } }, + // Only checks if a whole word within string is profane profane: function (text) { var profane = false; @@ -66,7 +89,7 @@ var swearjar = { var fullPath = path.join(basePath, relativePath); this._badWords = require(fullPath); }, - + setBadWords: function (badWords) { this._badWords = badWords || {}; } diff --git a/test/test.js b/test/test.js index f6debd7..4e1f0b8 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () {