Skip to content

Main content

Random Item Picker Source Code

Browse the implementation files for Random Item Picker.
tool.ts typescript
import { calculateRandomItem } from '@tool.io/tools/random/item';
import type { RandomSource } from '@tool.io/tools/random/number';

export { calculateRandomItem };

export const randomItem = (
	input: unknown,
	random: RandomSource = Math.random
): string => {
	if (typeof input !== 'string') return '';

	const values = input
		.split(/\r?\n/)
		.map((value) => value.trim())
		.filter((value) => value !== '');
	const result = calculateRandomItem(values, random);
	return result ?? '';
};

export const Tool: TypeTool = {
	name: 'Random Item Picker',
	slug: 'random/item',
	intro: 'Choose one random item from a line-separated list.',
	tool: randomItem,
	i: [
		{
			example: 'Pizza\nSushi\nTacos',
			help: 'Enter one item per line. Blank lines are ignored and commas remain part of an item.',
			label: 'Items',
			placeholder: 'Enter one item per line',
			render: 'textarea',
			type: 'text'
		}
	],
	o: [
		{
			example: 'Sushi',
			label: 'Random Item',
			placeholder: 'Random item will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [
		{ title: 'Random Number Generator', url: '/random/number' },
		{ title: 'Random Letter Generator', url: '/random/letter' }
	]
};

export default Tool;

Description

Browse the implementation files for Random Item Picker.

References

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

Similar or alternative tools