mapbox / osm-compare

Functions that identify what changed during a feature edit on OpenStreetMap.
ISC License
38 stars 15 forks source link

Add profanity comparator #193

Closed manoharuss closed 6 years ago

manoharuss commented 6 years ago

Adds a compare function to flag profanity in name tags by comparing with entries on https://github.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words

manoharuss commented 6 years ago

@amishas157 need your help debugging this. I observed that the regex.test works as expected for the correct fixtures.

Compare function works as expected https://github.com/mapbox/osm-compare/blob/6ef604ef87517804c0508295b50a55438d2317cf/comparators/profanity.js#L35-L36

But in tests/test_compare_function.js, I am seeing that result is returning undefined. https://github.com/mapbox/osm-compare/blob/profanity-comparator/tests/test_compare_function.js#L34

Here is a terminal log.

$ node tests/test_compare_function.js tests/fixtures/profanity.json
Regex check is true
undefined
Flags profanity in name changes
expected [object Object]
actual
Test FAILED! Actual is not expected!

undefined
Does not flag profanity
expected false
actual
Test FAILED! Actual is not expected!

Regex check is true
undefined
Flags profanity
expected [object Object]
actual
Test FAILED! Actual is not expected!
amishas157 commented 6 years ago

@manoharuss found out the following issue while debugging

-    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)) {
-          console.log('Regex check is true');
-          return {'result:profanity': true};
}
-      }
-      return false;
-    });
+    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;

In the first snippet, the result will be assigned to incidents array and therefore should be carried over so to return the result from the function. So all the incidents needs to filtered where result is false.