Skip to content

Commit

Permalink
fix exclamation marks
Browse files Browse the repository at this point in the history
  • Loading branch information
notwaldorf committed Jun 11, 2017
1 parent e6764c4 commit c6cdd6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 18 additions & 4 deletions emoji-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit c6cdd6b

Please sign in to comment.