diff --git a/.vscode/settings.json b/.vscode/settings.json index b943dbc..8675ad5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "deno.enable": true + "deno.enable": true, + "deno.unstable": true } \ No newline at end of file diff --git a/db/creatures/fantasy.ts b/data/creatures/fantasy.ts similarity index 100% rename from db/creatures/fantasy.ts rename to data/creatures/fantasy.ts diff --git a/db/creatures/reality.ts b/data/creatures/reality.ts similarity index 100% rename from db/creatures/reality.ts rename to data/creatures/reality.ts diff --git a/functions/generator/name/character.ts b/functions/generator/name/character.ts index 0c7bc93..597a3d0 100644 --- a/functions/generator/name/character.ts +++ b/functions/generator/name/character.ts @@ -1,9 +1,9 @@ import { getRandom } from "../../util/mod.ts"; -import adjectives from "../../../data/adjectives.ts"; +import Adjectives from "../../../data/adjectives.ts"; import Synonyms from "../../../data/synonyms.ts"; -import levels from "../../../data/levels.ts"; -import thirdperson from "../../../data/characterTypes.ts"; +import Levels from "../../../data/levels.ts"; +import ThirdPerson from "../../../data/characterTypes.ts"; /** * Generate a random fantasy character. @@ -13,8 +13,8 @@ export const generateCharacter = (): string => { return `${ Math.random() < 0.9 ? Math.random() > 0.3 - ? `${getRandom(adjectives)} ` - : `${getRandom(Synonyms.level)} ${getRandom(levels)} ` + ? `${getRandom(Adjectives)} ` + : `${getRandom(Synonyms.level)} ${getRandom(Levels)} ` : "" - }${getRandom(thirdperson)}`; + }${getRandom(ThirdPerson)}`; }; diff --git a/functions/generator/name/monster.ts b/functions/generator/name/monster.ts index 5402ebc..1fa1415 100644 --- a/functions/generator/name/monster.ts +++ b/functions/generator/name/monster.ts @@ -1,13 +1,16 @@ import { getRandom } from "../../util/mod.ts"; import { generateName } from "./normal.ts"; -import suffix from "../../../data/suffixesForFantasy.ts" +import Suffix from "../../../data/suffixesForFantasy.ts"; /** * Generate a fantasy name like the stuff in RPGs. * @returns Fantasy name */ -export const generateFantasyName = (): string => { - return `${generateName(3 + Math.floor(Math.random() * 5)).toLowerCase()}${ - getRandom(suffix) - }`; +export const generateFantasyName = ( + length = 3 + Math.floor(Math.random() * 5), +): string => { + const suffix = getRandom(Suffix); + return `${ + generateName(length).slice(0, length - suffix.length).toLowerCase() + }${suffix}`; }; diff --git a/functions/generator/name/normal.ts b/functions/generator/name/normal.ts index 8d359c9..770a389 100644 --- a/functions/generator/name/normal.ts +++ b/functions/generator/name/normal.ts @@ -5,14 +5,14 @@ import after from "../../../data/nameRules.ts" /** * Generate a random name. The name is just a normal word that can be pronounced. - * @param lenn Length of the name to generate. + * @param length Length of the name to generate. * @returns Random name that actually makes sense. */ -export function generateName(lenn?: number): string { - if (!lenn) lenn = 4 + Math.floor(Math.random() * 5); +export function generateName(length?: number): string { + if (!length) length = 4 + Math.floor(Math.random() * 5); let name: string = getRandom(alphabet); - for (let pointer = 0; pointer < lenn - 1; ++pointer) { - if (pointer === lenn - 2) { + for (let pointer = 0; pointer < length - 1; ++pointer) { + if (pointer === length - 2) { name += getRandom( after.find((x) => x.letter === name.charAt(name.length - 1).toUpperCase() @@ -39,5 +39,5 @@ export function generateName(lenn?: number): string { } } } - return name; + return name.toLowerCase(); } diff --git a/functions/generator/name/race.ts b/functions/generator/name/race.ts index 9923115..d9eba10 100644 --- a/functions/generator/name/race.ts +++ b/functions/generator/name/race.ts @@ -1,4 +1,4 @@ -import fantasy from "../../../db/creatures/fantasy.ts"; +import fantasy from "../../../data/creatures/fantasy.ts"; import { capitalize, getRandom } from "../../util/mod.ts"; import { generateFantasyName } from "./monster.ts"; diff --git a/functions/generator/story/penance.ts b/functions/generator/story/penance.ts index ffedbae..7bb6bef 100644 --- a/functions/generator/story/penance.ts +++ b/functions/generator/story/penance.ts @@ -1,5 +1,5 @@ -import fantasyCreatures from "../../../db/creatures/fantasy.ts"; -import realCreatures from "../../../db/creatures/reality.ts"; +import fantasyCreatures from "../../../data/creatures/fantasy.ts"; +import realCreatures from "../../../data/creatures/reality.ts"; const creatures = fantasyCreatures.concat(realCreatures); import { capitalize, getRandom } from "../../util/mod.ts"; @@ -51,7 +51,7 @@ export const penance = (name: string): string => { : `${getRandom(fight)}${rand > 0.5 ? `` : `s`}` } the${Math.random() < 0.5 ? ` ${evil()}` : ``} ${ Math.random() < 0.5 ? generateCharacter() : evilcreature.name - }${Math.random() < 0.5 ? `, ${capitalize(generateFantasyName())},` : ``}${ + }${ Math.random() < 0.5 ? ` with the help of the ${goodcreature.plural}${ Math.random() < 0.5 diff --git a/package.json b/package.json index b1c33c5..750034c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nekooftheabyss/lala", - "version": "3.1.4", + "version": "3.1.5", "description": "A collection of random useful (probably) javascript classes and functions. No it wasn't named after Lala Satalin Deviluke (or it probably was, idk).", "main": "lala.js", "type": "module", diff --git a/test.js b/test.js deleted file mode 100644 index c66601f..0000000 --- a/test.js +++ /dev/null @@ -1,3 +0,0 @@ -import mod from './mod.ts'; - -console.log(mod.generateStory()) \ No newline at end of file diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..aadbf90 --- /dev/null +++ b/test.ts @@ -0,0 +1,210 @@ +import commonEmail from "./data/commonEmail.ts"; + +import { + capitalize, + generateChainMail, + generateCharacter, + generateEmail, + generateFantasyName, + generateName, + generateRace, + generateStory, + generateString, + getRandom, + owoify, + weebify, +} from "./mod.ts"; + +import { Race } from "./functions/generator/name/race.ts"; + +import { + assert, + assertEquals, + assertExists, + assertInstanceOf, + assertStringIncludes, +} from "https://deno.land/std@0.159.0/testing/asserts.ts"; + +Deno.test({ + name: "Generator functions are defined.", + fn() { + assertExists(generateChainMail); + assertExists(generateCharacter); + assertExists(generateEmail); + assertExists(generateFantasyName); + assertExists(generateName); + assertExists(generateRace); + assertExists(generateStory); + assertExists(generateString); + }, +}); + +Deno.test({ + name: "Util functions are defined.", + fn() { + assertExists(capitalize); + assertExists(getRandom); + assertExists(weebify); + assertExists(owoify); + }, +}); + +Deno.test({ + name: "generateChainMail generates a valid, non-empty string.", + fn() { + const chainMail = generateChainMail(); + console.debug(`\t${chainMail}`); + assert(Boolean(chainMail?.length)); + }, +}); + +Deno.test({ + name: + "generateChainMail, when supplied a custom name, includes that name in the result.", + fn() { + const chainMail = generateChainMail("Lala"); + console.debug(`\t${chainMail}`); + assertStringIncludes(chainMail, "Lala"); + }, +}); + +Deno.test({ + name: "generateCharacter generates a valid, non-empty string", + fn() { + const character = generateCharacter(); + console.debug(`\t${character}`); + assert(Boolean(character?.length)); + }, +}); + +Deno.test({ + name: "generateEmail generates a valid, non-empty string", + fn() { + const email = generateEmail(); + console.debug(`\t${email}`); + assert(Boolean(email?.length)); + }, +}); + +Deno.test({ + name: + "generateEmail generates an email from a popular service when parameter passed is true.", + fn() { + const email = generateEmail(true); + console.debug(`\t${email}`); + assert(commonEmail.some((x) => email.split("@")[1] === x)); + }, +}); + +Deno.test({ + name: "generateFantasyName generates a valid, non-empty string", + fn() { + const name = generateFantasyName(); + console.debug(`\t${name}`); + assert(Boolean(name?.length)); + }, +}); + +Deno.test({ + name: + "generateFantasyName generates a name equal in length to supplied parameter", + fn() { + const length = 6 + Math.floor(Math.random() * 17); + const name = generateFantasyName(length); + console.debug( + `\tName:\t\t${name}\n\tLength Passed:\t${length}\n\tLength Got:\t${name.length}`, + ); + assertEquals(name.length, length); + }, +}); + +Deno.test({ + name: "generateName generates a valid, non-empty string", + fn() { + const name = generateName(); + console.debug(`\t${name}`); + assert(Boolean(name?.length)); + }, +}); + +Deno.test({ + name: "generateName generates a name equal in length to supplied parameter", + fn() { + const length = 6 + Math.floor(Math.random() * 17); + const name = generateName(length); + console.debug( + `\tName:\t\t${name}\n\tLength Passed:\t${length}\n\tLength Got:\t${name.length}`, + ); + assertEquals(name.length, length); + }, +}); + +Deno.test({ + name: "generateRace generates an instance of Race", + fn() { + const race = generateRace(); + console.debug( + `\t${race.description}`, + ); + assertInstanceOf(race, Race); + }, +}); + +Deno.test({ + name: "generateRace, when supplied a custom name, uses that name.", + fn() { + const race = generateRace("Lala"); + console.debug(`\t${race.description}`); + assertEquals(race.name, "Lala"); + }, +}); + +Deno.test({ + name: "generateStory generates a valid, non-empty string.", + fn() { + const story = generateStory(); + console.debug(`\t${story}`); + assert(Boolean(story?.length)); + }, +}); + +Deno.test({ + name: + "generateStory, when supplied a custom name, includes that name in the result.", + fn() { + const story = generateStory("Lala"); + console.debug(`\t${story}`); + assertStringIncludes(story, "Lala"); + }, +}); + +Deno.test({ + name: "generateString generates a valid, non-empty string", + fn() { + const str = generateString(); + console.debug(`\t${str}`); + assert(Boolean(str?.length)); + }, +}); + +Deno.test({ + name: + "generateString generates a string equal in length to supplied parameter", + fn() { + const length = 6 + Math.floor(Math.random() * 17); + const str = generateString(length); + console.debug( + `\String:\t\t${str}\n\tLength Passed:\t${length}\n\tLength Got:\t${str.length}`, + ); + assertEquals(str.length, length); + }, +}); + +Deno.test({ + name: "generateString generates a purely alphanumeric string.", + fn() { + const str = generateString(99); + console.debug(`\t${str}`); + assert(/^[a-zA-Z0-9]+$/.test(str)); + }, +});