Skip to content

Main content

Random Emoji Generator Source Code

Browse the implementation files for Random Emoji Generator.
tool.ts typescript
import { validateChoice } from '../../../tool-runtime/validateChoice';
import {
	emojiCatalog,
	type EmojiCategory
} from '../../reference/html/emoji-catalog';
import { calculateRandomItem } from '../item/tool';

const CATEGORIES = [
	'all',
	'activities',
	'faces',
	'people',
	'animals',
	'food',
	'travel',
	'objects',
	'symbols',
	'flags'
] as const;
const ALL_EMOJI_ENTRIES = Object.values(emojiCatalog).flat();

export const randomEmoji = (categoryInput: unknown): string => {
	const category = validateChoice(categoryInput, CATEGORIES);
	if (category === null) return '';

	const entries =
		category === 'all'
			? ALL_EMOJI_ENTRIES
			: emojiCatalog[category as EmojiCategory];

	return calculateRandomItem(entries)?.[0] ?? '';
};

export const Tool: TypeTool = {
	name: 'Random Emoji Generator',
	slug: 'random/emoji',
	intro: 'Generate a random emoji from a selected category.',
	tool: randomEmoji,
	i: [
		{
			example: 'all',
			label: 'Category',
			placeholder: 'Emoji category',
			type: 'select',
			value: 'all',
			options: [
				{ value: 'all', text: 'All', selected: true },
				{ value: 'activities', text: 'Activities' },
				{ value: 'faces', text: 'Faces' },
				{ value: 'people', text: 'People and body' },
				{ value: 'animals', text: 'Animals' },
				{ value: 'food', text: 'Food' },
				{ value: 'travel', text: 'Travel' },
				{ value: 'objects', text: 'Objects' },
				{ value: 'symbols', text: 'Symbols' },
				{ value: 'flags', text: 'Flags' }
			]
		}
	],
	o: [
		{
			example: 'πŸŽ‰',
			label: 'Random emoji',
			placeholder: 'Random emoji',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Symbol', url: '/random/symbol' },
		{ title: 'Random Letter', url: '/random/letter' },
		{
			title: 'HTML Emoji Reference',
			url: '/reference/html/emoji'
		}
	],
	cite: [
		{
			title: 'Emoji - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Emoji'
		},
		{
			title: 'Unicode Emoji',
			url: 'https://unicode.org/emoji/'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Emoji Generator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools