Last active
January 18, 2021 21:59
-
-
Save SethVandebrooke/b32642c79b0720b3a020a0a9079afc99 to your computer and use it in GitHub Desktop.
Generate fantastical names of worlds, cities, or general places.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePlace(type) { | |
var name = ""; | |
var l = "bcdfghjklmnprstvwxyz"; | |
var v = "aeiou"; | |
var a = ["an","on","al","aph","ah"]; | |
var b = ["or","an","ro","ain","am"]; | |
var c = ["ia","sha","ea","ae","on","ue","a","as","in","na","o","di"]; | |
var d = ["ph","th","sh"]; | |
var sequences = [ | |
[l,a,l,b,c], | |
[l,a,l,b,v,d], | |
[l,a,l,v], | |
[l,b,l,v], | |
[d,a,l,v], | |
[l,v,d] | |
]; | |
var types = { | |
"country": [sequences[0],sequences[1]], | |
"city": [sequences[2]], | |
"planet": [sequences[3],sequences[4]], | |
"name": [sequences[3],sequences[5]] | |
} | |
if (type && type in types) { | |
random(types[type]).forEach(x => name += random(x)); | |
} else { | |
random(sequences).forEach(x => name += random(x)); | |
} | |
return name; | |
} | |
function random(a, b) { | |
if(Array.isArray(a)){ | |
return a[random(a.length)]; | |
} | |
if (typeof a === "object") { | |
var key = random(Object.keys(a)); | |
return b === "key" ? key : b === "both" ? {key:key,value:a[key]} : a[key]; | |
} | |
if (typeof a === "string" && !!a) { | |
return (a.toLowerCase() === "bool") ? (Math.floor(Math.random()*2) == 1) : random(a.split('')); | |
} | |
if (typeof a === "number"){ | |
if(typeof b === "number"){ | |
return random(b-a)+a; | |
} | |
return Math.floor(Math.random()*a); | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment