Skip to content

Main content

Random Kanji Generator Source Code

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

const KANJI = {
	numbers: [...'一二三四五六七八九十百千万円'],
	nature: [
		...'日月火水木金土山川田天空雨石花草竹林森貝魚鳥虫犬'
	],
	people: [...'人子女男父母友先生名年力心手足目耳口'],
	directions: [...'上下左右中大小本正早入出立休見']
} as const;

const CATEGORIES = [
	'all',
	'numbers',
	'nature',
	'people',
	'directions'
] as const;

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

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

export const Tool: TypeTool = {
	name: 'Random Kanji Generator',
	slug: 'random/kanji',
	intro:
		'Generate a random kanji from a curated introductory set.',
	tool: randomKanji,
	i: [
		{
			example: 'all',
			label: 'Category',
			placeholder: 'Kanji category',
			type: 'select',
			value: 'all',
			options: [
				{
					value: 'all',
					text: 'All introductory kanji',
					selected: true
				},
				{ value: 'numbers', text: 'Numbers and counting' },
				{ value: 'nature', text: 'Nature' },
				{ value: 'people', text: 'People and body' },
				{
					value: 'directions',
					text: 'Directions and basic actions'
				}
			]
		}
	],
	o: [
		{
			example: '山',
			label: 'Random kanji',
			placeholder: 'Random kanji',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Hiragana', url: '/random/hiragana' },
		{ title: 'Random Katakana', url: '/random/katakana' },
		{
			title: 'Random Chinese Character',
			url: '/random/chinese-character'
		}
	],
	cite: [
		{
			title: 'Kanji - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Kanji'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Kanji Generator.

References

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

Similar or alternative tools