diff --git a/README.md b/README.md index 3212dab..c88df9f 100644 --- a/README.md +++ b/README.md @@ -13,22 +13,21 @@ npm install easy-emojis ## usage ```javascript -const { countryCodeToFlag, emojiCountryCode } = require('easy-emojis'); -countryCodeToFlag('US'); // returns 'πŸ‡ΊπŸ‡Έ' -flagToCountryCode('πŸ‡ΊπŸ‡Έ'); // returns 'US' +import * as EasyEmojis from 'easy-emojis'; +EasyEmojis.countryCodeToFlag('US'); // returns 'πŸ‡ΊπŸ‡Έ' +EasyEmojis.flagToCountryCode('πŸ‡ΊπŸ‡Έ'); // returns 'US' +EasyEmojis.letterToEmoji('S'); // returns 'πŸ‡Έ' +EasyEmojis.emojiToLetter('πŸ‡Έ') // returns 'S' -const { letterToEmoji, emojiToLetter, getRandomEmoji } = require('easy-emojis'); -letterToEmoji('S'); // returns 'πŸ‡Έ' -emojiToLetter('πŸ‡Έ') // returns 'S' +EasyEmojis.getRandomEmoji(); // should return a random emoji -const { getRandomEmoji } = require('easy-emojis'); -getRandomEmoji(); // should return a random emoji -``` -## alternative importing -```javascript -import * as EasyEmojis from 'easy-emojis'; -EasyEmojis.countryCodeToFlag('US'); // returns 'πŸ‡ΊπŸ‡Έ' +EasyEmojis.getEmojiByName('red apple'); // should return '🍎' +EasyEmojis.getEmojiByShortName(':apple:'); // should return '🍎' + +// getEmoji both accepts name and short name +EasyEmojis.getEmoji('red apple'); // should return '🍎' +EasyEmojis.getEmoji(':apple:'); // should return '🍎' ``` ### Run tests diff --git a/package-lock.json b/package-lock.json index da165f6..37e8b7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "easy-emojis", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "easy-emojis", - "version": "1.0.3", + "version": "1.0.4", "license": "MIT", "devDependencies": { "@types/jest": "^27.4.1", diff --git a/package.json b/package.json index 8ba9031..725c5a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-emojis", - "version": "1.0.3", + "version": "1.0.4", "description": "A simple library to create emojis (flag or letter)", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -9,11 +9,11 @@ "build": "tsc", "format": "prettier --write \"src/**/*.ts\"", "lint": "tslint -p tsconfig.json", - "prepare" : "npm run build", - "prepublishOnly" : "npm test && npm run lint", - "preversion" : "npm run lint", - "version" : "npm run format && git add -A src", - "postversion" : "git push && git push --tags" + "prepare": "npm run build", + "prepublishOnly": "npm test && npm run lint", + "preversion": "npm run lint", + "version": "npm run format && git add -A src", + "postversion": "git push && git push --tags" }, "repository": { "type": "git", @@ -40,4 +40,4 @@ "tslint-config-prettier": "^1.18.0", "typescript": "^4.6.4" } -} +} \ No newline at end of file diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index cb4375c..16084a0 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,36 +1,100 @@ -import emojis from '../emojis'; -import {countryCodeToFlag, emojiToLetter, flagToCountryCode, letterToEmoji, getRandomEmoji} from '../index'; - -test('countryCodeToEmoji', () => { - expect(countryCodeToFlag('US')).toBe('πŸ‡ΊπŸ‡Έ'); -}); - -test('flagToCountryCode', () => { - expect(flagToCountryCode('πŸ‡ΊπŸ‡Έ')).toBe('US'); -}); - -test('letterToEmoji', () => { - expect(letterToEmoji('A')).toBe('πŸ‡¦'); -}); - -test('letterToEmoji with input more than single character', () => { - expect(() => { - letterToEmoji('AS'); - }).toThrow('letterToEmoji: letter must be a single character'); -}); - -test('letterToEmoji with input less than single character', () => { - expect(() => { - letterToEmoji(''); - }).toThrow('letterToEmoji: letter must be a single character'); -}); - -test('emojiToLetter', () => { - expect(emojiToLetter('πŸ‡¦')).toBe('A'); -}); - -test('should return a different emoji on subsequent calls', () => { - const emoji1 = getRandomEmoji(); - const emoji2 = getRandomEmoji(); - expect(emoji1).not.toEqual(emoji2); -}); +import { countryCodeToFlag, emojiToLetter, flagToCountryCode, letterToEmoji, getRandomEmoji, getEmojiByShortName, getEmojiByName } from '../index'; + +describe('index', () => { + beforeAll(() => { + // Mock console.error and console.warn to prevent actual logging during tests + console.error = jest.fn(); + console.warn = jest.fn(); + }); + + it('countryCodeToEmoji', () => { + expect(countryCodeToFlag('US')).toBe('πŸ‡ΊπŸ‡Έ'); + }); + + it('flagToCountryCode', () => { + expect(flagToCountryCode('πŸ‡ΊπŸ‡Έ')).toBe('US'); + }); + + it('letterToEmoji', () => { + expect(letterToEmoji('A')).toBe('πŸ‡¦'); + }); + + it('letterToEmoji with input more than single character', () => { + expect(() => { + letterToEmoji('AS'); + }).toThrow('letterToEmoji: letter must be a single character'); + }); + + it('letterToEmoji with input less than single character', () => { + expect(() => { + letterToEmoji(''); + }).toThrow('letterToEmoji: letter must be a single character'); + }); + + it('emojiToLetter', () => { + expect(emojiToLetter('πŸ‡¦')).toBe('A'); + }); + + it('should return a different emoji on subsequent calls', () => { + const emoji1 = getRandomEmoji(); + const emoji2 = getRandomEmoji(); + expect(emoji1).not.toEqual(emoji2); + }); + + it('should return the correct emoji for a valid short name', () => { + const emojiShortName = ':apple:'; + const result = getEmojiByShortName(emojiShortName); + expect(result).toBe('🍎'); + }); + + it('should return an empty string and log an error for an invalid short name', () => { + const invalidShortName = 'invalid-emoji'; + const result = getEmojiByShortName(invalidShortName); + expect(result).toBe(''); + expect(console.error).toHaveBeenCalledWith( + 'you didnt pass a short name. Please pass correct short name like :apple: or use getEmojiByName' + ); + }); + + it('should return an empty string and log a warning for a short name that is not found', () => { + const nonExistentShortName = ':grape:'; + const result = getEmojiByShortName(nonExistentShortName); + expect(result).toBe(''); + expect(console.warn).toHaveBeenCalledWith( + 'emoji not found with name: ', nonExistentShortName + ); + }); + + it('should return the correct emoji for a valid emoji name', () => { + const emojiName = 'RED Apple'; + const result = getEmojiByName(emojiName); + expect(result).toBe('🍎'); + }); + + it('should return an empty string and log a warning for an invalid emoji name', () => { + const invalidEmojiName = 'Grape'; + const result = getEmojiByName(invalidEmojiName); + expect(result).toBe(''); + expect(console.warn).toHaveBeenCalledWith( + "emoji not found with name: ", invalidEmojiName + ); + }); + + it('should return an empty string and log a warning for a null emoji name', () => { + const nullEmojiName = null; + const result = getEmojiByName(nullEmojiName as unknown as string); + expect(result).toBe(''); + expect(console.warn).toHaveBeenCalledWith( + "emoji not found with name: ", nullEmojiName + ); + }); + + it('should return an empty string and log a warning for an undefined emoji name', () => { + const undefinedEmojiName = undefined; + const result = getEmojiByName(undefinedEmojiName as unknown as string); + expect(result).toBe(''); + expect(console.warn).toHaveBeenCalledWith( + "emoji not found with name: ", undefinedEmojiName + ); + }) +}) diff --git a/src/__tests__/utils.test.ts b/src/__tests__/utils.test.ts new file mode 100644 index 0000000..c893d5e --- /dev/null +++ b/src/__tests__/utils.test.ts @@ -0,0 +1,39 @@ +import { isShortName } from '../utils'; + +describe('isShortName', () => { + it('should return true if the name contains colons at the beginning and end', () => { + const name = ':example:'; + const result = isShortName(name); + expect(result).toBe(true); + }); + + it('should return false if the name does not contain colons at the beginning and end', () => { + const name = 'example'; + const result = isShortName(name); + expect(result).toBe(false); + }); + + it('should return false if the name contains a colon at the beginning but not at the end', () => { + const name = ':example'; + const result = isShortName(name); + expect(result).toBe(false); + }); + + it('should return false if the name contains a colon at the end but not at the beginning', () => { + const name = 'example:'; + const result = isShortName(name); + expect(result).toBe(false); + }); + + it('should return true if the name contains multiple colons at the beginning and end', () => { + const name = ':::example:::'; + const result = isShortName(name); + expect(result).toBe(true); + }); + + it('should return false if the name is an empty string', () => { + const name = ''; + const result = isShortName(name); + expect(result).toBe(false); + }); +}); diff --git a/src/index.ts b/src/index.ts index 45fe45b..84cd56e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import emojis from "./emojis"; +import { isShortName } from "./utils"; // offset between uppercase ascii and regional indicator symbols const OFFSET = 127397; @@ -24,3 +25,35 @@ export const getRandomEmoji = (): string => { const randomIndex = Math.floor(Math.random() * emojis.length); return emojis[randomIndex].unicode; } + +export const getEmoji = (emoji: string): string => { + if (isShortName(emoji)) { + return getEmojiByShortName(emoji); + } else { + return getEmojiByName(emoji); + } +} + +export const getEmojiByName = (emojiName: string): string => { + const foundEmoji = emojis.find(emoji => emoji.name === emojiName?.toLowerCase()); + if (foundEmoji) { + return foundEmoji.emoji + } else { + console.warn("emoji not found with name: ", emojiName) + return "" + } +} + +export const getEmojiByShortName = (emojiShortName: string): string => { + if (!isShortName(emojiShortName.toLowerCase())) { + console.error("you didnt pass a short name. Please pass correct short name like :apple: or use getEmojiByName") + return "" + } + const foundEmoji = emojis.find(emoji => emoji.shortname === emojiShortName?.toLowerCase()); + if (foundEmoji) { + return foundEmoji.emoji + } else { + console.warn("emoji not found with name: ", emojiShortName) + return "" + } +} \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..169b7ad --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,3 @@ +export function isShortName(name: string) { + return /:.+:/.test(name); +} \ No newline at end of file