badwords icon indicating copy to clipboard operation
badwords copied to clipboard

Issue with symbols / emoji only strings

Open lkgoerges opened this issue 4 years ago • 3 comments

When running the clean() function with a string containing only emojis or symbols I receive the following error:

TypeError: Cannot read property '0' of null
at Filter.clean in bad-words/lib/badwords.js — line 58

As a workaround, I currently add "ABC" at the end of every string and remove it after the cleanup.

Example code:

const ProfanityFilter = require('bad-words');
const profanityFilter = new ProfanityFilter();
profanityFilter.clean('🙂');
profanityFilter.clean('.,-*');

Screenshot_127

lkgoerges avatar Nov 22 '20 05:11 lkgoerges

Getting the same problem. Will try the workaround by @lkgoerges

Fitmavincent avatar Nov 24 '20 04:11 Fitmavincent

For those interested, I'm using this as a workaround for now.

// filename bad-words-hacked.js
const Filter = require('bad-words')

class FilterHacked extends Filter {
    cleanHacked(string) {
        try {
            return this.clean(string);
        } catch {
            const joinMatch = this.splitRegex.exec(string);
            const joinString = (joinMatch && joinMatch[0]) || '';
            return string.split(this.splitRegex).map((word) => {
              return this.isProfane(word) ? this.replaceWord(word) : word;
            }).join(joinString);
        }
      }
}

module.exports = FilterHacked

Example code:

var Filter = require('./bad-words-hacked');
filter = new Filter();
const filteredString = filter.cleanHacked(string);

cbmmd42 avatar Apr 16 '21 23:04 cbmmd42

Looks like this repo is pretty much abandoned. Does anyone have a fork they plan on maintaining?

iandoesallthethings avatar Jan 09 '24 22:01 iandoesallthethings