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

Add emoji detector + prio names lib #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions comparators/emoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';
const isNameKey = require('../lib/names.js').isNameKey;

const isEmoji = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?)*/;

module.exports = emoji;

/*
Contains an emoji in one of the name tags
@param {geojson} newVersion
@returns {object|false) returns the detected incident or false
*/
function emoji(newVersion) {
if (!newVersion || !newVersion.properties) return false;

const tags = newVersion.properties;
for (const tag in tags) {
if (!isNameKey(tag)) continue;
const name = tags[tag];
if (isEmoji.test(name)) {
return {message: 'Found emoji in ' + tag};
}
}
return false;
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
rare_critical_feature_created: wrapsync(
require('./comparators/rare_critical_feature_created')
),
emoji: wrapsync(require('./comparators/emoji')),
deprecated_construction_proposal_tag: wrapsync(
require('./comparators/deprecated_construction_proposal_tag.js')
),
Expand Down
20 changes: 20 additions & 0 deletions lib/names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const namePattern = /^(int_|short_|)?(name|wikidata:labels)(:.*)?$/;
const isNameKey = key => namePattern.test(key);
const priorityLanguages = new Set(['en', 'es', 'de', 'fr', 'ru', 'zh']);

function isPrioNameKey(key) {
if (key === 'name') return true;
if (!isNameKey(key)) return false;
const parts = key.split(':');
if (
(parts[0] === 'name' ||
[parts[0], parts[1]].join(':') === 'wikidata:labels') &&
!priorityLanguages.has(parts[parts.length - 1])
)
return false;
return true;
}

exports.isPrioNameKey = isPrioNameKey;
exports.isNameKey = isNameKey;
50 changes: 50 additions & 0 deletions tests/fixtures/emoji.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"compareFunction": "emoji",
"fixtures": [
{
"description": "Finds Emoji",
"newVersion": {
"type": "Feature",
"properties": { "name": "'Give me some 💵'" }
},
"oldVersion": null,
"expectedResult": { "message": "Found emoji in name" }
},
{
"description": "Finds Emoji",
"newVersion": {
"type": "Feature",
"properties": { "name": "If all you have is a 🔨" }
},
"oldVersion": null,
"expectedResult": { "message": "Found emoji in name" }
},
{
"description": "Finds Emoji",
"newVersion": {
"type": "Feature",
"properties": { "name": "Emojis 😂 in OSM" }
},
"oldVersion": null,
"expectedResult": { "message": "Found emoji in name" }
},
{
"description": "Finds Emoji",
"newVersion": {
"type": "Feature",
"properties": { "name": "OSM 😈" }
},
"oldVersion": null,
"expectedResult": { "message": "Found emoji in name" }
},
{
"description": "No Emoji in name",
"newVersion": {
"type": "Feature",
"properties": { "name": "OSM does not need Emojis" }
},
"oldVersion": null,
"expectedResult": false
}
]
}