Skip to content

Main content

Random Katakana Generator Source Code

Browse the implementation files for Random Katakana Generator.
tool.ts typescript
import { validateChoice } from '../../../tool-runtime/validateChoice';
import { calculateRandomItem } from '../item/tool';

const SETS = {
	basic: [
		...'アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン'
	],
	voiced: [
		...'ガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ'
	]
} as const;

const SET_CHOICES = ['all', 'basic', 'voiced'] as const;

export const randomKatakana = (setInput: unknown): string => {
	const set = validateChoice(setInput, SET_CHOICES);
	if (set === null) return '';

	return (
		calculateRandomItem(
			set === 'all' ? Object.values(SETS).flat() : SETS[set]
		) ?? ''
	);
};

export const Tool: TypeTool = {
	name: 'Random Katakana Generator',
	slug: 'random/katakana',
	intro:
		'Generate a random basic or voiced katakana character.',
	tool: randomKatakana,
	i: [
		{
			example: 'all',
			label: 'Character set',
			placeholder: 'Katakana set',
			type: 'select',
			value: 'all',
			options: [
				{
					value: 'all',
					text: 'Basic and voiced',
					selected: true
				},
				{ value: 'basic', text: 'Basic' },
				{ value: 'voiced', text: 'Voiced and semi-voiced' }
			]
		}
	],
	o: [
		{
			example: 'カ',
			label: 'Random katakana',
			placeholder: 'Random katakana',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Hiragana', url: '/random/hiragana' },
		{ title: 'Random Kanji', url: '/random/kanji' },
		{ title: 'Random Letter', url: '/random/letter' }
	],
	cite: [
		{
			title: 'Katakana - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Katakana'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Katakana Generator.

References

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

Similar or alternative tools