Skip to content

Main content

Random Symbol Generator Source Code

Browse the implementation files for Random Symbol Generator.
tool.ts typescript
import { validateChoice } from '../../../tool-runtime/validateChoice';
import {
	symbolCatalog,
	type SymbolCategory
} from '../../reference/html/symbol-catalog';
import { calculateRandomItem } from '../item/tool';

const CATEGORIES = [
	'all',
	'punctuation',
	'mathematics',
	'currency',
	'arrows',
	'geometry'
] as const;
const ALL_SYMBOL_ENTRIES = [
	...new Map(
		Object.values(symbolCatalog)
			.flat()
			.map((entry) => [entry[0], entry] as const)
	).values()
];

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

	const entries =
		category === 'all'
			? ALL_SYMBOL_ENTRIES
			: symbolCatalog[category as SymbolCategory];

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

export const Tool: TypeTool = {
	name: 'Random Symbol Generator',
	slug: 'random/symbol',
	intro:
		'Generate a random punctuation, mathematical, currency, arrow, or geometric symbol.',
	tool: randomSymbol,
	i: [
		{
			example: 'all',
			label: 'Category',
			placeholder: 'Symbol category',
			type: 'select',
			value: 'all',
			options: [
				{ value: 'all', text: 'All', selected: true },
				{ value: 'punctuation', text: 'Punctuation' },
				{ value: 'mathematics', text: 'Mathematics' },
				{ value: 'currency', text: 'Currency' },
				{ value: 'arrows', text: 'Arrows' },
				{ value: 'geometry', text: 'Geometric shapes' }
			]
		}
	],
	o: [
		{
			example: '∞',
			label: 'Random symbol',
			placeholder: 'Random symbol',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Emoji', url: '/random/emoji' },
		{ title: 'Random Letter', url: '/random/letter' },
		{
			title: 'HTML Symbols Reference',
			url: '/reference/html/symbol'
		}
	],
	cite: [
		{
			title: 'Symbol - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Symbol'
		},
		{
			title: 'Unicode character table',
			url: 'https://unicode.org/charts/'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Symbol Generator.

References

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

Similar or alternative tools