ktsrivastava29 / Remove-Vulgar

An initiative to save kids-our future.
1 stars 1 forks source link

Consider adding `bad words` in seperate file #2

Open AvishrantsSh opened 3 years ago

AvishrantsSh commented 3 years ago

The project you are working on is frankly great!

A nice little tweak might be to add all the explicit words in a separate file to enhance readability. Also, considering that the set of words might get quite large, I think you can consider storing the words in either JSON format or a set object. These two data structures are hashed and offer O(1) lookup, which is pretty neat I guess.

Another suggestion would be to construct regex in one go rather than constructing for each word in the array. Also, the current method does a regex match for every single word again and again, which might not be very efficient perhaps.

replaceText(document.body)

function replaceText(element) {
    if (element.hasChildNodes()) {
        element.childNodes.forEach(replaceText)
    } else if (element.nodeType === Text.TEXT_NODE) {
        const badWords = ["word1", "word2", "word3"]
        const badRegex = new RegExp(badWords.join('|'), 'gi')
        element.textContent = element.textContent.replace(badRegex, " 🥺")
    }
}

Hope it helps @lkiThakur @ktsrivastava29

ktsrivastava29 commented 3 years ago

@AvishrantsSh Thank you very much, sir. Sir, your feedback and recommendations are really valuable to us. We are grateful to you for your assistance to newcomers like us. We'll take a look at your suggestions and try to put them into action as quickly as feasible. We look forward to receiving your advice in the future also.