random name generator functions

This commit is contained in:
koehr 2022-01-22 18:26:55 +01:00
parent cf8a9b0538
commit 479df6d408
4 changed files with 182 additions and 5 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,123 @@
function chance (fraction) {
return Math.random() < fraction
}
function randInt (max) {
// simplified because it will always be min=0
return Math.round(Math.random() * max)
}
function randElement (list) {
const max = list.length - 1
const index = randInt(max)
return list[index]
}
module.exports = function RandomGenerators (dicts) {
const adjectives = dicts['adjectives']
const adverbs = dicts['adverbs']
const palindromes = dicts['palindrome-sentences']
const housingNouns = dicts['nouns-housing']
const transportNouns = dicts['nouns-transportations']
const alliterations = dicts['alliterations']
const people = dicts['given-names']
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
// TODO: prepositions? number words?
function randomLetterCombination () {
let output = ''
for (let idx = 1 + randInt(3); idx; idx--) output += randElement(letters)
return output
}
function randomSuffixedName (suffix) {
let person = randElement(people)
if (person.endsWith('s') || person.endsWith('x') || person.endsWith('z')) {
person += `' ${suffix}`
} else {
person += `'s ${suffix}`
}
return person
}
// This version always returns a named star
function genNamedStar () {
return randomSuffixedName('Star')
}
function genStar () {
if (chance(0.1)) { // 10% chance for a personalized name
return randomSuffixedName('Star')
}
const rndN = randInt(99999)
const rndL = randomLetterCombination()
const lettersFirst = chance(0.5)
const output = lettersFirst ? `${rndL}-${rndN}` : `${rndN}-${rndL}`
return output
}
function genPlanet () {
if (chance(0.1)) { // 10% chance for Palindromes!
return randElement(palindromes)
}
let output = ''
const includeAdjective = chance(0.8)
if (includeAdjective) {
const includeAdverb = chance(0.2)
if (includeAdverb) output += randElement(adverbs) + ' '
output += randElement(adjectives) + ' '
}
output += randElement(housingNouns)
return output
}
function genStation () {
if (chance(0.1)) { // 10% chance for an alliteration!
return randElement(alliterations)
}
if (chance(0.5)) { // 50% chance for something like Amal's hut
const designation = randElement(housingNouns)
return randomSuffixedName(designation)
}
let output = ''
const includeAdjective = chance(0.8)
if (includeAdjective) {
const includeAdverb = chance(0.2)
if (includeAdverb) output += randElement(adverbs) + ' '
output += randElement(adjectives) + ' '
}
output += randElement(housingNouns)
return output
}
function genStarship () {
if (chance(0.1)) { // 10% chance for an alliteration!
return randElement(alliterations)
}
if (chance(0.5)) { // 50% chance for something like Mel's steamship
const designation = randElement(transportNouns)
return randomSuffixedName(designation)
}
let output = ''
const includeAdjective = chance(0.8)
if (includeAdjective) {
const includeAdverb = chance(0.2)
if (includeAdverb) output += randElement(adverbs) + ' '
output += randElement(adjectives) + ' '
}
output += randElement(transportNouns)
return output
}
return { genStar, genNamedStar, genPlanet, genStation, genStarship }
}

View file

@ -1,13 +1,38 @@
const dicts = require('./dicts.js')
const randomGenerators = require('./generators.js')
const {
//genStar,
genNamedStar,
genPlanet,
genStation,
genStarship
} = randomGenerators(dicts)
const generators = {
'random-star-name': genNamedStar,
'random-planet-name': genPlanet,
'random-station-name': genStation,
'random-starship-name': genStarship,
}
// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
const handler = async (event) => {
try {
const subject = event.queryStringParameters.name || 'World'
// path looks like this: /.netlify/functions/random-names/foo
const generatorKey = event.path.slice(33) // cheapo path segmentation
const generator = generators[generatorKey]
if (!generator) return {
statusCode: 404,
body: JSON.stringify({ statusCode: 404, body: `Unkown generator "${generatorKey}".` }),
}
const name = generator()
return {
statusCode: 200,
body: JSON.stringify({ message: `Hello ${subject}`, path: event.path }),
// // more keys you can return:
// headers: { "headerName": "headerValue", ... },
// isBase64Encoded: true,
body: JSON.stringify({ name }),
}
} catch (error) {
return { statusCode: 500, body: error.toString() }

View file

@ -0,0 +1,28 @@
const { handler } = require('./random-names.js')
function generateEvent (path) {
// see https://docs.netlify.com/functions/build-with-javascript/#format
return {
path: `/.netlify/functions/random-names/${path}`,
httpMethod: 'GET',
headers: {},
queryStringParameters: {},
body: null,
isBase64Encoded: false,
}
}
(function test () {
const events = {
randomStarName: generateEvent('random-star-name'),
randomPlanetName: generateEvent('random-planet-name'),
randomStationName: generateEvent('random-station-name'),
randomStarshipName: generateEvent('random-starship-name'),
}
Object.keys(events).map(async (key) => {
const event = events[key]
const result = await handler(event)
console.log(key, result.statusCode, result.body)
})
}())