node-emoji
Advanced tools
Comparing version 1.4.3 to 1.5.0
@@ -12,2 +12,22 @@ /*jslint node: true*/ | ||
/** | ||
* Removes colons on either side | ||
* of the string if present | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
var trim = function(str) { | ||
var colonIndex = str.indexOf(':'); | ||
if (colonIndex > -1) { | ||
// :emoji: (http://www.emoji-cheat-sheet.com/) | ||
if (colonIndex === str.length - 1) { | ||
str = str.substring(0, colonIndex); | ||
return trim(str); | ||
} else { | ||
str = str.substr(colonIndex + 1); | ||
return trim(str); | ||
} | ||
} | ||
return str; | ||
} | ||
/** | ||
* Emoji namespace | ||
@@ -37,6 +57,3 @@ */ | ||
Emoji.get = function get(emoji) { | ||
if (emoji.indexOf(':') > -1) { | ||
// :emoji: (http://www.emoji-cheat-sheet.com/) | ||
emoji = emoji.substr(1, emoji.length-2); | ||
} | ||
emoji = trim(emoji); | ||
@@ -93,1 +110,20 @@ return Emoji._get(emoji); | ||
} | ||
/** | ||
* return an collection of potential emoji matches | ||
* @param {string} str | ||
* @return {Array.<Object>} | ||
*/ | ||
Emoji.search = function search(str) { | ||
var emojiKeys = Object.keys(Emoji.emoji); | ||
var matcher = trim(str) | ||
var matchingKeys = emojiKeys.filter(function(key) { | ||
return key.toString().indexOf(matcher) === 0; | ||
}); | ||
return matchingKeys.map(function(key) { | ||
return { | ||
key: key, | ||
emoji: Emoji._get(key), | ||
}; | ||
}); | ||
} |
{ | ||
"name": "node-emoji", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"description": "simple emoji support for node.js projects", | ||
@@ -5,0 +5,0 @@ "author": "Daniel Bugl <daniel.bugl@touchlay.com>", |
@@ -24,6 +24,7 @@ # node-emoji | ||
emoji.random() // returns a random emoji + key, e.g. `{ emoji: '❤️', key: 'heart' }` | ||
emoji.search('cof') // returns an array of objects with matching emoji's. `[{ emoji: '☕️', key: 'coffee' }, { emoji: ⚰', key: 'coffin'}]` | ||
``` | ||
## Adding new emoji | ||
Emoji come from js-emoji (Thanks a lot :thumbsup:). You can get a JSON file with all emoji here: https://github.com/omnidan/node-emoji/blob/master/lib/emoji.json | ||
Emoji come from js-emoji (Thanks a lot :thumbsup:). You can get a JSON file with all emoji here: https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json | ||
@@ -30,0 +31,0 @@ To update the list or add custom emoji, clone this repository and put them into `lib/emojifile.js`. |
@@ -80,2 +80,35 @@ /*jslint node: true*/ | ||
}); | ||
describe("search(str)", function () { | ||
it("should return partially matched emojis", function () { | ||
var matchingEmojis = emoji.search("cof"); | ||
matchingEmojis.length.should.not.eql(0); | ||
matchingEmojis.forEach(function(emoji) { | ||
emoji.key.should.match(/^cof/); | ||
}); | ||
}); | ||
it("should only include emojies that begin with the search", function () { | ||
var matchingEmojis = emoji.search("ca"); | ||
matchingEmojis.length.should.not.eql(0); | ||
matchingEmojis.forEach(function(emoji) { | ||
var index = emoji.key.indexOf("ca"); | ||
index.should.be.exactly(0); | ||
}); | ||
}); | ||
it("should match when you include the colon", function () { | ||
var matchingEmojis = emoji.search(":c"); | ||
matchingEmojis.length.should.not.eql(0); | ||
matchingEmojis.forEach(function(emoji) { | ||
var index = emoji.key.indexOf("c"); | ||
index.should.be.exactly(0); | ||
}); | ||
}); | ||
it("should return an empty array when no matching emojis are found", function () { | ||
var matchingEmojis = emoji.search("notAnEmoji"); | ||
matchingEmojis.length.should.be.exactly(0); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147419
1544
44