services/bad-words-filter-service.js

/**
 * AngularJS Service to check if a word matches our bad words list.
 *
 * @module badWordsFilterService
 * @see https://docs.angularjs.org/api/ng/type/angular.Module#service
 */
angular.module('tgaApp').service('badWordsFilterService', ['badWordList',
  function badWordsFilterService(badWordList) {
    /**
     * Check if the word matches anything in the {@link badWordList}
     *
     * @param {string} str
     * @returns {boolean}
     */
    this.checkWord = (str) => {
      if (str === null || angular.isUndefined(str)) {
        return false;
      }
      const s = str.toLowerCase().replace(/[-_\s]/g, '');
      for (let i = 0; i < badWordList.length; i++) {
        if (s === badWordList[i].replace(/[-\s]/g, '')) {
          return true;
        }
      }
      return false;
    };
  },
]);