Skip to content

Commit

Permalink
Merge pull request #51 from smeijer/feature/fix-flags
Browse files Browse the repository at this point in the history
Fix flag identification bug: `Emoji.which(πŸ‡²πŸ‡½)`
  • Loading branch information
omnidan authored Jul 15, 2017
2 parents 88b452c + 101c540 commit 685312e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
48 changes: 33 additions & 15 deletions lib/emoji.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*jslint node: true*/
require('string.prototype.codepointat');
var toArray = require('lodash.toarray');

"use strict";
Expand Down Expand Up @@ -29,6 +28,19 @@ var trim = function(str) {
}
return str;
}

/**
* Adds colons to either side
* of the string
* @param {string} str
* @return {string}
*/
var wrapColons = function(str) {
return (str && str.length > 0) ? ':' + str + ':' : '';
}

var NON_SPACING_MARK = String.fromCharCode(65039); // 65039 - '️' - 0xFE0F;

/**
* Emoji namespace
*/
Expand All @@ -45,7 +57,7 @@ Emoji._get = function _get(emoji) {
if (Emoji.emoji.hasOwnProperty(emoji)) {
return Emoji.emoji[emoji];
}
return ':' + emoji + ':';
return wrapColons(emoji);
};

/**
Expand All @@ -61,17 +73,27 @@ Emoji.get = function get(emoji) {

/**
* get emoji name from code
* @param {string} emoji_code
* @param {string} emoji
* @param {boolean} includeColons should the result include the ::
* @return {string}
*/
Emoji.which = function which(emoji_code) {
for (var prop in Emoji.emoji) {
if (Emoji.emoji.hasOwnProperty(prop)) {
if (Emoji.emoji[prop].codePointAt() === emoji_code.codePointAt()) {
return prop;
}
}
Emoji.which = function which(emoji_code, includeColons) {
var word = emojiToCode[emoji_code];
if (word) {
return includeColons ? wrapColons(word) : word;
}

// Most of the times, the word is already returned by now. Sometimes
// we need to handle the non-spacing-mark. If we haven't returned yet,
// we're going to try the oposite version, with or without the mark.
var endsWithMark = emoji_code[emoji_code.length - 1] === NON_SPACING_MARK;
var alias = endsWithMark
? emoji_code.substr(0, emoji_code.length - 1)
: emoji_code + NON_SPACING_MARK;

word = emojiToCode[alias];

return includeColons ? wrapColons(word) : word;
};

/**
Expand Down Expand Up @@ -150,10 +172,6 @@ Emoji.unemojify = function unemojify(str) {
var words = toArray(str);

return words.map(function(word) {
var emoji_text = emojiToCode[word];
if (emoji_text) {
return ':'+emoji_text+':';
}
return word;
return Emoji.which(word, true) || word;
}).join('');
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"url": "https://github.com/omnidan/node-emoji/issues"
},
"dependencies": {
"lodash.toarray": "^4.4.0",
"string.prototype.codepointat": "^0.2.0"
"lodash.toarray": "^4.4.0"
},
"devDependencies": {
"istanbul": "^0.4.5",
Expand Down
30 changes: 29 additions & 1 deletion test/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,33 @@ describe("emoji.js", function () {
should.exist(coffee);
coffee.should.be.exactly('coffee');
});

it("should work for differently formed characters", function () {
var umbrella = emoji.which('β˜”');
should.exist(umbrella);
umbrella.should.be.exactly('umbrella');
});

it("should return the same name for differently formed characters", function () {
var umbrella1 = emoji.which('β˜”');
should.exist(umbrella1);
var umbrella2 = emoji.which('β˜”οΈ');
should.exist(umbrella2);
umbrella1.should.equal(umbrella2);
});

it("should work for flags", function() {
var mexico = emoji.which('πŸ‡²πŸ‡½');
should.exists(mexico);
mexico.should.be.exactly('flag-mx');

var marocco = emoji.which('πŸ‡²πŸ‡¦');
should.exists(marocco);
marocco.should.be.exactly('flag-ma');

// see issue #21
mexico.should.not.equal(marocco);
});
});

describe("emojify(str)", function () {
Expand All @@ -57,6 +72,13 @@ describe("emoji.js", function () {
should.exist(coffee);
coffee.should.be.exactly('I ❀️ β˜•οΈ! - 😯⭐️😍 ::: test : : πŸ‘+');
});

it("should handle flags correctly", function() {
var flags = emoji.emojify('Mexico :flag-mx: and Marocco :flag-ma: are not the same');
should.exists(flags);
flags.should.be.exactly('Mexico πŸ‡²πŸ‡½ and Marocco πŸ‡²πŸ‡¦ are not the same');
});

it("should leave unknown emoji", function () {
var coffee = emoji.emojify('I :unknown_emoji: :star: :another_one:');
should.exist(coffee);
Expand Down Expand Up @@ -160,6 +182,12 @@ describe("emoji.js", function () {
var coffee = emoji.unemojify('I love πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘©');
should.exist(coffee);
coffee.should.be.exactly('I love :woman-kiss-woman:');
})
});

it("should parse flags correctly", function () {
var flags = emoji.unemojify('The flags of πŸ‡²πŸ‡½ and πŸ‡²πŸ‡¦ are not the same');
should.exists(flags);
flags.should.be.exactly('The flags of :flag-mx: and :flag-ma: are not the same');
});
});
});

0 comments on commit 685312e

Please sign in to comment.