Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #141 from mapbox/invalid-name
Browse files Browse the repository at this point in the history
Comparator to flag invalid names of features
  • Loading branch information
bkowshik authored Apr 10, 2017
2 parents c662fa8 + d23e9c4 commit 79410cf
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions comparators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,6 @@ Report when a new user creates a new water feature.

### invalid-tag-modification
Reports when a feature's primary tag is modified between newVersion and oldVersion of the feature.

### invalid_name
Report if a values of `name` or `name:*` for a feature have any profanity.
35 changes: 35 additions & 0 deletions comparators/invalid_name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

module.exports = invalidName;

function invalidName(newVersion, oldVersion, callback) {

// Not interested if there isn't a newVersion.
if (!newVersion) return callback(null, {});

var properties = newVersion.properties;
for (var key in properties) {
var naughtyWords;

// TODO: Having English as the default locale for now.
if (key === 'name') {
naughtyWords = require('naughty-words/en.json');
if (naughtyWords.indexOf(properties[key]) !== -1) return callback(null, {'result:invalid_name': true});
} else if (key.indexOf('name') === 0) {
// Splitting 'name:ko' into ['name', 'ko']
var locale = key.split(':')[1];

// Name of the locale's naughty word file.
var filename = 'naughty-words/' + locale + '.json';

try {
naughtyWords = require(filename);
if (naughtyWords.indexOf(properties[key]) !== -1) return callback(null, {'result:invalid_name': true});
} catch (error) {
// TODO: naughty-words for the locale does not exist. Skip for now.
}

}
}
return callback(null, {});
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ module.exports = {
'name_matches_to_wikidata': require('./comparators/name_matches_to_wikidata.js'),
'name_modified': wrapsync(require('./comparators/name_modified.js')),
'osm_landmarks': require('./comparators/osm_landmarks.js'),
'invalid_tag_modification': require('./comparators/invalid_tag_modification')
'invalid_tag_modification': require('./comparators/invalid_tag_modification'),
'invalid_name': require('./comparators/invalid_name')
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"levenshtein": "^1.0.5",
"lodash": "^4.17.3",
"moment": "^2.17.1",
"naughty-words": "^1.0.1",
"osm-landmarks": "^0.3.1",
"queue-async": "^1.2.1",
"request": "^2.72.0",
Expand Down
53 changes: 53 additions & 0 deletions tests/fixtures/invalid_name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"compareFunction": "invalid_name",
"fixtures": [
{
"description": "Test feature with name tag but without profanity",
"expectedResult": {},
"newVersion": {
"type": "Feature",
"properties": {
"name": "Hello, world!"
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test feature with profanity in name tag",
"expectedResult": {"result:invalid_name": true},
"newVersion": {
"type": "Feature",
"properties": {
"name": "xxx"
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test feature with profanity in name:* tag",
"expectedResult": {"result:invalid_name": true},
"newVersion": {
"type": "Feature",
"properties": {
"name:ko": "강간"
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test feature with profanity in name:* tag when naughty-words does not exist for the locale",
"expectedResult": {},
"newVersion": {
"type": "Feature",
"properties": {
"name:kn": "xxx"
},
"geometry": null
},
"oldVersion": null
}
]
}

0 comments on commit 79410cf

Please sign in to comment.