Skip to content

Commit

Permalink
simplified the source code
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrMonier committed Jul 7, 2022
1 parent 54585ea commit c4a4e19
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 86 deletions.
79 changes: 28 additions & 51 deletions src/Filter.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,36 @@
import dictionary from './data/dictionary';

type language =
| 'arabic'
| 'chinese'
| 'czech'
| 'danish'
| 'english'
| 'esperanto'
| 'finnish'
| 'french'
| 'german'
| 'hindi'
| 'hungarian'
| 'italian'
| 'japanese'
| 'korean'
| 'norwegian'
| 'persian'
| 'polish'
| 'portuguese'
| 'russian'
| 'turkish'
| 'swedish'
| 'thai';
export default class Filter {
private words: Set<string>;
/**
*
*/
constructor(config?: {
languages: (
| 'arabic'
| 'chinese'
| 'czech'
| 'danish'
| 'english'
| 'esperanto'
| 'finnish'
| 'french'
| 'german'
| 'hindi'
| 'hungarian'
| 'italian'
| 'japanese'
| 'korean'
| 'norwegian'
| 'persian'
| 'polish'
| 'portuguese'
| 'russian'
| 'turkish'
| 'swedish'
| 'thai'
)[];
}) {
constructor(config?: { languages: language[] }) {
this.words = new Set<string>();
const languagesChecks = new Set<
| 'arabic'
| 'chinese'
| 'czech'
| 'danish'
| 'english'
| 'esperanto'
| 'finnish'
| 'french'
| 'german'
| 'hindi'
| 'hungarian'
| 'italian'
| 'japanese'
| 'korean'
| 'norwegian'
| 'persian'
| 'polish'
| 'portuguese'
| 'russian'
| 'turkish'
| 'swedish'
| 'thai'
>(config?.languages);
const languagesChecks = new Set<language>(config?.languages);
if (languagesChecks.size !== 0) {
languagesChecks.forEach(lang => {
this.words = new Set<string>([...this.words, ...dictionary[lang]]);
Expand All @@ -73,7 +48,9 @@ export default class Filter {

this.words.forEach(word => {
const wordExp = new RegExp(`${word.replace(/(\W)/g, '\\$1')}`, 'gi');
wordExp.test(value) ? profaneWordsCounter++ : null;
if (wordExp.test(value)) {
profaneWordsCounter++;
}
});
return !!profaneWordsCounter;
}
Expand Down
44 changes: 22 additions & 22 deletions src/data/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import Arabic from "./arabic.json";
import Chinese from "./chinese.json";
import Czech from "./czech.json";
import Danish from "./danish.json";
import Esperanto from "./esperanto.json";
import English from "./english.json";
import Finnish from "./finnish.json";
import German from "./german.json";
import Hindi from "./hindi.json";
import Hungarian from "./hungarian.json";
import Korean from "./korean.json";
import Norwegian from "./Norwegian.json";
import Persian from "./persian.json";
import Polish from "./polish.json";
import Portuguese from "./portuguese.json";
import Russian from "./russian.json";
import French from "./french.json";
import Italian from "./italian.json";
import Japanese from "./japanese.json";
import Turkish from "./turkish.json";
import Swedish from "./swedish.json";
import Thai from "./thai.json";
import Arabic from './arabic.json';
import Chinese from './chinese.json';
import Czech from './czech.json';
import Danish from './danish.json';
import Esperanto from './esperanto.json';
import English from './english.json';
import Finnish from './finnish.json';
import German from './german.json';
import Hindi from './hindi.json';
import Hungarian from './hungarian.json';
import Korean from './korean.json';
import Norwegian from './Norwegian.json';
import Persian from './persian.json';
import Polish from './polish.json';
import Portuguese from './portuguese.json';
import Russian from './russian.json';
import French from './french.json';
import Italian from './italian.json';
import Japanese from './japanese.json';
import Turkish from './turkish.json';
import Swedish from './swedish.json';
import Thai from './thai.json';
export default {
arabic: Arabic.words,
chinese: Chinese.words,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import Filter from "./Filter";
import Filter from './Filter';
export { Filter };
24 changes: 12 additions & 12 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Filter } from "../src";
describe("test profanity", () => {
it("return false English", () => {
import { Filter } from '../src';
describe('test profanity', () => {
it('return false English', () => {
const filter = new Filter();
expect(filter.isProfane("he is going home")).toBe(false);
expect(filter.isProfane('he is going home')).toBe(false);
});
it("return true English", () => {
it('return true English', () => {
const filter = new Filter();
expect(filter.isProfane("he is going fucking home")).toBe(true);
expect(filter.isProfane('he is going fucking home')).toBe(true);
});

it("return false Arabic", () => {
const filter = new Filter({ languages: ["arabic"] });
it('return false Arabic', () => {
const filter = new Filter({ languages: ['arabic'] });

expect(filter.isProfane("ذهب محمد الى الحديقة")).toBe(false);
expect(filter.isProfane('ذهب محمد الى الحديقة')).toBe(false);
});
it("return true Arabic", () => {
const filter = new Filter({ languages: ["arabic"] });
it('return true Arabic', () => {
const filter = new Filter({ languages: ['arabic'] });

expect(filter.isProfane("الراجل بتاع السوبر ماركت معرص")).toBe(true);
expect(filter.isProfane('الراجل بتاع السوبر ماركت معرص')).toBe(true);
});
});

0 comments on commit c4a4e19

Please sign in to comment.