From c6cdd6be763623b47374bf4aed1b3492b160a574 Mon Sep 17 00:00:00 2001 From: Monica Dinculescu Date: Sun, 11 Jun 2017 09:50:48 -0700 Subject: [PATCH] fix exclamation marks --- emoji-translate.js | 22 ++++++++++++++++++---- test.js | 7 +++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/emoji-translate.js b/emoji-translate.js index 314d4aa7..b5d14122 100644 --- a/emoji-translate.js +++ b/emoji-translate.js @@ -73,7 +73,7 @@ function getAllEmojiForWord(originalWord) { (words && words.indexOf(maybeVerbed) >= 0)) { // If it's a two letter word that got translated to a flag, it's 99% of the // time incorrect, so stop doing that. - if (!(word.length == 2 && allEmoji[emoji].category == 'flags')) { + if (!(word.length <= 3 && allEmoji[emoji].category == 'flags')) { useful.push(allEmoji[emoji].char); } } @@ -147,11 +147,25 @@ function translate(sentence, onlyEmoji) { let translation = ''; let words = sentence.split(' '); for (let i = 0; i < words.length; i++ ) { - let translated = getEmojiForWord(words[i]); + // Punctuation blows. Get all the punctuation at the start and end of the word. + // TODO: stop copy pasting this. + let firstSymbol = ''; + let lastSymbol = ''; + var word = words[i]; + while (SYMBOLS.indexOf(word[0]) != -1) { + firstSymbol += word[0]; + word = word.slice(1, word.length); + } + while (SYMBOLS.indexOf(word[word.length - 1]) != -1) { + lastSymbol += word[word.length - 1]; + word = word.slice(0, word.length - 1); + } + + let translated = getEmojiForWord(word); if (translated) { - translation += translated + ' '; + translation += firstSymbol + translated + lastSymbol + ' '; } else if (!onlyEmoji){ - translation += words[i] + ' ' + translation += firstSymbol + word + lastSymbol + ' ' } } return translation; diff --git a/test.js b/test.js index 7ac7d20a..fd72450a 100644 --- a/test.js +++ b/test.js @@ -52,5 +52,12 @@ test('annoying translations', function(t) { t.notEqual('i am', translate.translate('i am').trim()); t.notEqual('she he is', translate.translate('she he is').trim()); t.notEqual('we they are', translate.translate('we they are').trim()); + + // YES should not have a flag. + t.equal(-1, translate.getAllEmojiForWord('yes').indexOf('πŸ‡ΎπŸ‡ͺ')); + + // Exclamation marks should be preserved + t.equal(2, translate.translate('YES! victory!').match(/!/g).length); + t.equal(2, translate.translate('YES! victory!', true).match(/!/g).length); t.end(); });