UNPKG

@okwesil/type-test

Version:

Simple typing test program for the command line

27 lines (23 loc) 787 B
import { readFileSync } from "fs"; import { join } from "path"; import { PROGRAM_DIRECTORY } from "./save.js"; export const textStyles = { bold: text => `\x1b[1m${text}\x1b[0m`, red: text => `\x1b[31m${text}\x1b[0m`, green: text => `\x1b[32m${text}\x1b[0m`, yellow: text => `\x1b[33m${text}\x1b[0m`, } const wordBankFile = join(PROGRAM_DIRECTORY, "words.txt") const WORDS = readFileSync(wordBankFile, { encoding: "utf8" }).split("\n").map(word => word.trim()); /** * * @param {number} wordCount * @returns {string} */ export function generateSampleText(wordCount) { let text = ""; for (let i = 0; i < wordCount; i++) { text += WORDS[Math.round(Math.random() * (WORDS.length - 1))] + " "; } return text.trimEnd(); }