Skip to content

Main content

Cheatsheet Source Code

Browse the implementation files for Cheatsheet.
tool.ts typescript
import Cheatsheet from './Cheatsheet.svelte';
import asciiCheatsheetData from '$tooldata/cheatsheet/ascii/data.json';
import type { CheatsheetDocument } from './types';
import { getCheatsheetHref } from './navigation';
import { availableCheatsheets } from './sheets';
import { subNav } from './subnav';

let cheetSubNav = {
	...subNav,
	groups: [
		...subNav.groups,
		{
			name: 'Cheatsheets',
			items: availableCheatsheets.map((a) => ({
				a: getCheatsheetHref(a.slug),
				name: a.name,
				slug: a.slug
			}))
		}
	]
};

const getCheatsheet = async (slug: string) => {
	const sheet =
		availableCheatsheets.filter(
			(item) => item.slug === slug
		)[0] ?? undefined;

	if (sheet) {
		if (slug === 'ascii') {
			return asciiCheatsheetData as CheatsheetDocument;
		}

		if (sheet.type === 'db') {
			// Try load from db.
			return '404';
		} else {
			// Assume local json sheet.
			try {
				// If we load json without ?raw
				// we can directly use it as a
				// JS object.
				const sheetJSON = await import(
					`$lib/tools/cheatsheet/${sheet.slug}/cheatsheet.json`
				);

				/* Debug */
				//console.log(
				//	'tryLoadCheatsheet (sheet)',
				//	sheetJSON?.default
				//);

				return sheetJSON?.default;
			} catch {
				return 404;
			}
		}
	}
	return undefined;
};

export const cheatsheet = async (
	name: string
	//orientation: string,
	//size: string
) => {
	// Cheatsheet loader

	if (name) {
		const sheet = await getCheatsheet(name);

		if (sheet) {
			return sheet;
		}
	}

	if (!name) {
		return {
			name: 'cheatsheet',
			sheet: {
				groups: [
					{
						items: [...availableCheatsheets]
							.sort((first, second) =>
								first.name.localeCompare(second.name, 'en-US')
							)
							.map((sheet) => ({
								action: 'Open cheatsheet',
								key: sheet.name,
								reference: `/cheatsheet/${sheet.slug}`
							})),
						name: 'Available cheatsheets'
					}
				],
				lastupdate: '2026-07-31',
				meta: [],
				subtitle:
					'Cheatsheets for programming, software, design, science, and common applications.',
				title: 'Available'
			},
			type: 'index',
			version: '0.1.0'
		};
	}

	return {
		error:
			'404 Sorry, the cheatsheet does not exist. Please select one from the list.'
	};
};

export const Tool: TypeTool = {
	name: 'Cheatsheet',
	slug: 'cheatsheet',
	tool: cheatsheet,
	intro:
		'Browse quick-reference guides for tools, programming, design, and science.',
	i: [
		/* Hide, until layout is fixed such that there is an option to display a single input i: [] as flex-col in ToolIO with o: []
		{
			example: 'vs-code',
			label: 'Cheatsheet',
			placeholder: 'Select a cheatsheet',
			type: 'select',
			options: availableCheatsheets.map((item) => {
				return { value: item.slug, text: item.name };
			})
		}
		*/
		/*
		{
			example: 'Landscape',
			label: 'Orientation',
			placeholder: 'Select an orientation such as Portrait',
			type: 'select', // select
			options: [
				{ value: 'portrait', text: 'Portrait', selected: true },
				{ value: 'landscape', text: 'Landscape' }
			]
		},
		{
			example: 'US letter',
			label: 'Paper size',
			placeholder: 'Select a size',
			type: 'select',
			options: [
				{ value: 'a4', text: 'A4', selected: true },
				{ value: 'us-letter', text: 'US letter' }
			]
		}
		*/
		/* TODO Button to print as PDF?
		{
			example: 'Print as PDF',
			label: 'Print as PDF',
			placeholder: 'Print',
			type: 'button'
		},*/
	],
	o: [
		{
			type: 'text',
			placeholder: '...',
			label: 'Cheatsheet',
			render: 'custom',
			renderas: Cheatsheet
		}
	],
	subnav: cheetSubNav,
	cite: [
		{
			title: 'Cheatsheet - Wikipedia',
			url: 'https://en.wikipedia.org/Cheat_sheet'
		}
	]
};

export default Tool;

Description

Browse the implementation files for Cheatsheet.

References

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

Similar or alternative tools