Skip to content

Commit

Permalink
Merge pull request #6 from sinansonmez/feature/add-get-emoji
Browse files Browse the repository at this point in the history
Feature: Get emoji by name or short name
  • Loading branch information
sinansonmez authored Nov 2, 2023
2 parents de3b239 + da4c1ac commit 9cf8f5a
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 58 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -40,4 +40,4 @@
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.6.4"
}
}
}
136 changes: 100 additions & 36 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
})
})
39 changes: 39 additions & 0 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
33 changes: 33 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import emojis from "./emojis";
import { isShortName } from "./utils";

// offset between uppercase ascii and regional indicator symbols
const OFFSET = 127397;
Expand All @@ -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 ""
}
}
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isShortName(name: string) {
return /:.+:/.test(name);
}

0 comments on commit 9cf8f5a

Please sign in to comment.