Blog Games GitHub Twitter Mastodon
[ up a level ]

Junkrat Name Generator

Press this button to generate:

This is built with my handy grammar based text generator. Check it out!
Also suggest more “junk” and “rat” synonyms at me.

var grammar = {};
grammar.main = "@junk@rat";

grammar.junk = ["Junk", "Trash", "Rubbish", "Garbage", "Debris", 
                "Waste", "Dross", "Litter", "Refuse", "Scrap"];

grammar.rat  = ["Vermin", "Rat", "Rodent", "Vermin", "Wretch", 
                "Vole", "Hamster", "Gerbil", "Gopher"];
var maxDepth = 100; // Maximum number of replacements before giving up.
var grammar = [];
var lib;

// Look up an element in the story segments table.
function seg(g, name, p1) {
    if     (typeof(g[p1]) == "undefined") return "@" + p1;
    else if(typeof(g[p1]) == "string")    return g[p1];
    else                                  return pick(g[p1]);

// Helper functions for array selection and indefinite article.
var pick = (arr) => arr[Math.floor(Math.random() * arr.length)];
var article = (m, o) => ("aeiouAEIOU".indexOf(m[2]) == -1?"a ":"an ") + m[2];

// The heavy lifting!
function generate(txt, g=grammar) {
    var i = 0; //max depth of 100; avoid infinite loops!
    while(txt.indexOf("@")>=0&&i++<maxDepth) txt = txt.replace(/@([a-zA-Z0-9]+)/g, seg.bind(this,g));
    if(i>=maxDepth) txt += "<p style='color:red;'>[Hit maximum iteration depth ("+maxDepth+")]</p>";
    txt=txt.replace(/\%\s[a-zA-Z]/g, article); 
    return txt;
function gen() {
    txt = generate("@main", grammar);       //<---THE IMPORTANT BIT!
    document.querySelector("#result").innerHTML = "<div class='block'>"+txt+"</div>";
[ up a level ]