-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (70 loc) · 2.16 KB
/
Copy pathindex.js
File metadata and controls
81 lines (70 loc) · 2.16 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const data = require('./data/build.json');
class Wash {
/**
* Creates a new instance of WashYourMouthOutWithSoap.
* @constructor
*/
constructor () {
this.locales = Object.keys(data);
}
/**
* Normalize case and remove excess whitespace from input string.
* @param {string} phrase Input phrase
* @return {string} Cleaned phrase
*/
static clean (phrase) {
return phrase
.toLowerCase()
.replace(/[\s+]+/g, ' ');
}
/**
* Split input phrase into an array of tokens both with and without
* punctuation.
* @param {string} phrase Input phrase
* @return {array} Array of tokens
*/
static tokenize (phrase) {
const withPunctuation = phrase
.replace('/ {2,}/', ' ')
.split(' ');
const withoutPunctuation = phrase
.replace(/[^\w\s]/g, '')
.replace('/ {2,}/', ' ')
.split(' ');
return withPunctuation.concat(withoutPunctuation);
}
/**
* Returns an array of supported locales.
* @return {array} Array of ISO 639-1 locales.
*/
supported () {
return this.locales;
}
/**
* Returns an array of bad words for the specified locale.
* @param {string} locale ISO 639-1 locale code
* @return {Array} Array of bad words
*/
words (locale) {
return data[locale];
}
/**
* Checks an arbitrary input string against the bad word list for the
* specified locale.
* @param {string} locale ISO 639-1 locale code
* @param {string} phase Input phrase
* @return {boolean} Does the phrase contain a bad word?
*/
check (locale, phrase) {
// Check to see if locale is supported. If not, return false.
if (typeof data[locale] === 'undefined') return false;
// Clean and tokenize user input
const tokens = Wash.tokenize(Wash.clean(phrase));
// Check against list
for (let i in tokens) {
if (this.words(locale).indexOf(tokens[i]) !== -1) return true;
}
return false;
}
}
module.exports = new Wash();