Skip to content

Main content

Random Word Generator Source Code

Browse the implementation files for Random Word Generator.
tool.ts typescript
import type { RandomSource } from '../../../tool-runtime/randomSource';
import { calculateRandomItem } from '../item/tool';
import {
	getStarterDictionaryWords,
	type StarterDictionaryWord
} from '../../dictionary/starter';

export const calculateRandomWord = (
	language: string,
	random: RandomSource = Math.random
): StarterDictionaryWord | null => {
	const words = getStarterDictionaryWords(language);
	return calculateRandomItem(words, random);
};

export const randomWord = (
	languageInput: unknown,
	random: RandomSource = Math.random
): string => {
	if (typeof languageInput !== 'string') return '';
	return calculateRandomWord(languageInput, random)?.word ?? '';
};

export const Tool: TypeTool = {
	name: 'Random Word Generator',
	slug: 'random/word',
	intro:
		'Pick a random English, German, French, or Japanese word from the attributed offline starter dictionary.',
	tool: randomWord,
	i: [
		{
			example: 'en',
			label: 'Language',
			placeholder: 'Select a language',
			type: 'select',
			options: [
				{ selected: true, text: 'English', value: 'en' },
				{ text: 'French', value: 'fr' },
				{ text: 'German', value: 'de' },
				{ text: 'Japanese', value: 'ja' }
			]
		}
	],
	o: [
		{
			example: 'cat',
			label: 'Random Word',
			placeholder: 'A random word will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Item Picker', url: '/random/item' },
		{
			title: 'Translate a Word',
			url: '/dictionary/translate'
		}
	],
	cite: [
		{
			title: 'Wiktionary',
			url: 'https://www.wiktionary.org/'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Random Word Generator.

References

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

Similar or alternative tools