This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
profanity.js
46 lines (42 loc) · 1.61 KB
/
profanity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const naughtyWords = require('../forked/naughty-words');
const priorityLanguages = require('../config/priority_languages.json');
const isNameKey = require('../lib/names').isNameKey;
function profanity(newVersion, oldVersion) {
if (newVersion.deleted) return false;
const oldTags = (oldVersion && oldVersion.properties) || {};
const tags = newVersion.properties;
if (!tags) return false;
for (const tag in tags) {
if (!isNameKey(tag)) continue;
// Only consider name tags that have changed from their previous value
if (tags[tag] === oldTags[tag]) continue;
const val = tags[tag];
// Replace punctuation with whitespace
// http://stackoverflow.com/a/25575009
const normalized = val.replace(
/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]/ig,
' '
);
const splitTag = tag.split(':');
const lang = (splitTag[splitTag.length - 1] || '').split('_')[0];
// If there is a language code, check against the matching language,
// Otherwise check priority languages
const languagesToCheck = naughtyWords[lang] ? [lang] : priorityLanguages;
const incidents = languagesToCheck
.map(lang => {
for (let i = 0; i < naughtyWords[lang].length; i++) {
const word = naughtyWords[lang][i];
const regex = new RegExp('(\\s|^)' + word + '(\\s|$)', 'gi');
if (regex.test(normalized)) {
return true;
}
}
return false;
})
.filter(incident => !!incident);
if (incidents.length) return {'result:profanity': true};
return false;
}
}
module.exports = profanity;