codsworth-namegen/src/main/js/main.js

133 lines
12 KiB
JavaScript
Raw Normal View History

2021-04-28 13:50:44 +02:00
const {$, doAfterLoad, footer, header, nav} = window.fwdekker;
2020-10-01 21:00:19 +02:00
import {jaroWinkler} from "./jaroWinkler";
2020-10-01 20:26:11 +02:00
2020-10-01 21:00:19 +02:00
/**
* Number of similar results to show when given a name.
*
* @type {number}
*/
2020-10-01 20:26:11 +02:00
const similarCount = 25;
2020-10-01 21:00:19 +02:00
/**
* Number of random results to keep in memory.
*
* @type {number}
*/
2020-10-01 20:26:11 +02:00
const randomCount = 25;
2020-10-01 21:00:19 +02:00
/**
* List of all names known by Codsworth.
*
* @type {string[]}
*/
const names = ["Aaliyah", "Aaron", "Abigail", "Abram", "Abrams", "Adalyn", "Adam", "Adams", "Addison", "Adeline", "Adrian", "Ahn", "Aideen", "Aiden", "Akira", "Al", "Alaina", "Alan", "Alana", "Alex", "Alexander", "Alexandra", "Alexis", "Alice", "Allen", "Allison", "Altman", "Alyssa", "Amanda", "Amelia", "Anderson", "Andonov", "Andrew", "Andy", "Angel", "Angela", "Anna", "Annabelle", "Anthony", "Archer", "Aria", "Arianna", "Asdf", "Ash", "Asher", "Ashford", "Ashley", "Asimov", "Assface", "Atkins", "Atkinson", "Atom", "Atomic", "Atticus", "Aubrey", "Audrey", "Austin", "Autumn", "Ava", "Avery", "Bacon", "Badass", "Bailey", "Baker", "Banner", "Bannerman", "Bannion", "Barnes", "Barron", "Barry", "Bash", "Bastard", "Battosai", "Batty", "Baudoin", "Bell", "Bella", "Ben", "Benjamin", "Bennell", "Bennett", "Bentley", "Bery", "Bill", "Billy", "Bilodeau", "Black", "Blaise", "Blake", "Blaze", "Bob", "Bobbi", "Bobby", "Bogdanove", "Bond", "Boob", "Boobie", "Boobies", "Boom", "Boston", "Bowman", "Bradley", "Braun", "Brayden", "Brenda", "Brenna", "Brian", "Brianna", "Brick", "Brigner", "Brock", "Brody", "Brooklyn", "Brooks", "Browder", "Brown", "Browne", "Bruce", "Bruckheimer", "Bryan", "Bryant", "Buffy", "Bulger", "Bull", "Burgess", "Burke", "Burt", "Butch", "Butler", "Caden", "Cal", "Caleb", "Callie", "Camden", "Camilla", "Campbell", "Capable", "Carey", "Carnow", "Carofano", "Caroline", "Carson", "Carter", "Carver", "Casserly", "Castle", "Catherine", "Cha", "Chapin", "Chappy", "Charles", "Charlie", "Charlotte", "Chase", "Cheng", "Cherise", "Cherry", "CherryBomb", "Chloe", "Chopper", "Chris", "Christian", "Christiane", "Christopher", "Church", "Claire", "Clara", "Clark", "Clarke", "Claw", "Clemens", "Clunk", "Cobb", "Cobra", "Cock", "Cockerham", "Cole", "Colin", "Collins", "Colton", "Conall", "Connor", "Cook", "Cooke", "Cooper", "Cormac", "Corman", "Cornelius", "Cornett", "Corrie", "Corrine", "Corvo", "Corwin", "Cory", "Cosgrove", "Courtenay", "Courtney", "Cox", "Coyle", "Craig", "Crash", "Cruz", "Cubbison", "Cullen", "Cummings", "Dag", "Damien", "Dan", "Dane", "Danger", "Daniel", "Daniels", "Danny", "Darrel", "Darren", "Darryl", "Daryl", "Dave", "David", "Davis", "Dawson", "Deag", "Death", "Deb", "Debbie", "Deborah", "Deckard", "Declan", "Deitrick", "Delaney", "Dell", "Dennis", "Denton", "Deschain", "Deth", "Diana", "Diane", "DiAngelo", "Diaz", "Dick", "DiMaggio", "Dinolt", "DJ", "Django", "Dom", "Domics", "Dominguez", "Dominic", "Don", "Donald", "Dong", "DongJun", "Donnie", "Donny", "Doug", "Douglas", "Dragon", "Draper", "Dulany", "Dutch", "Duvall", "Dylan", "Eagle", "East", "Ed", "Eddie", "Eddy", "Edwards", "Elena", "Eli", "Eliana", "Elijah", "Elizabeth", "Ella", "Ellie", "Elliot", "Elliott", "Emil", "Emile", "Emily", "Emma", "Erectus", "Eric", "Erik", "Erin", "Ernest", "Ernie", "Ethan", "Eugene", "Eva", "Evan", "Evans", "Evelyn", "Everdeen", "Everett", "Felipe", "Ferret", "Fifi", "Finn", "Finster", "Fiona", "Fisher", "Flag", "Flagg", "Flash", "Flores", "Flynn", "Foster", "Fox", "Fragile", "Francis", "Franke", "Freeman", "Frost", "Fuck", "Fucker", "Fuckface", "Furiosa", "Fury", "Gabriel", "Gabriella", "Gardiner", "Gary", "Gavin", "Gene", "George", "Georgia", "Gerry", "Gianna", "Gil", "Glen", "Gomez", "Gonzalez", "Goose", "Gordon", "Graber", "Grace", "Grant", "Gravato", "Gray", "Grayson", "Greco", "Green", "Greene", "Grey", "Griffin", "Grognak", "Gryphon", "Gutierrez", "Guy", "Hackman", "Hailey", "Hall", "Ham", "Hamilton", "Hamm", "Hammer", "Hammett", "Hamrick", "Han", "Hannah", "Hans", "Harden", "Hardi", "Harold", "Harper", "Harris", "Harry", "Haschart", "Hasenbuhler", "Hastings", "Hawk", "Hawke", "Hazel", "Henning", "Henry", "Herbert", "Hicks", "Hill", "Hines", "Holly", "Holmes", "Hook", "Hopgood", "Houle", "Howard", "Hudson", "Hughes", "Humongous", "Humungus", "Hyun", "Ian", "Ilya", "Imperator", "Indiana", "Indy", "Isaac", "Isabella", "Isabelle", "Isaiah", "Ist", "Istvan", "J.P.", "Jack", "Jackson", "Jacob", "Jag", "Jake", "James", "Jangjoon", "Jasmine", "Jason", "Jay", "Jayce", "Jayden", "Jeff", "Jeffery", "Jen", "Jenkins", "Je
/**
* Returns a random element from the given array.
*
* @param array {*[]} the array to choose a random element from
* @returns {*} a random element from the given array
*/
const getRandomFromArray = array => array[Math.floor(Math.random() * array.length)];
/**
* Finds strings similar to the given string using the Jaro-Winkler algorithm.
*
* @param string {string} the string to find similar strings of
* @param candidates {string[]} the strings to compare to the given string
* @param count {number} the number of similar strings to return
* @return {string[]} the `count` most similar candidates
*/
const jaroWinklerFinder = (string, candidates, count) =>
candidates
.map(candidate => [candidate, jaroWinkler(string, candidate, false)])
.sort((a, b) => b[1] - a[1])
.map(it => it[0])
.slice(0, count);
/**
* A cyclical buffer that overwrites old entries once it's full in a cyclical manner.
*/
class CircularBuffer {
/**
* Constructs a new cyclical buffer.
*
* @param size {number} the maximum number of elements that can go in the buffer
*/
2020-10-01 20:26:11 +02:00
constructor(size) {
2020-10-01 21:00:19 +02:00
this.size = size;
this.index = 0; // Points to next insertion
this.entries = [];
2020-10-01 20:26:11 +02:00
}
2020-10-01 21:00:19 +02:00
/**
* Adds a new entry to the buffer, potentially overwriting the oldest entry.
*
* @param {*} entry the entry to add; can be any type, including `undefined`
*/
add(entry) {
this.entries[this.index % this.size] = entry;
this.index++;
2020-10-01 20:26:11 +02:00
}
2020-10-01 21:00:19 +02:00
/**
* Returns the entries in order of insertion.
*
* @return {*[]} the entries in order of insertion
*/
getEntriesInOrder() {
2020-10-01 20:26:11 +02:00
const out = [];
2020-10-01 21:00:19 +02:00
for (let i = 0; i < this.size && i < this.index; i++)
out.unshift(this.entries[(this.index - i - 1) % this.size]);
2020-10-01 20:26:11 +02:00
2020-10-01 21:00:19 +02:00
return out;
2020-10-01 20:26:11 +02:00
}
}
// Load template
doAfterLoad(() => {
2021-04-19 09:20:47 +02:00
$("#nav").appendChild(nav("/Tools/Codsworth Name Generator/"));
2020-10-01 20:26:11 +02:00
$("#header").appendChild(header({
title: "Codsworth Name Generator",
description: `Choose a fitting name for your Fallout 4 character that Codsworth can pronounce`
}));
$("#footer").appendChild(footer({
2022-02-28 17:07:36 +01:00
vcsURL: "https://git.fwdekker.com/tools/codsworth-namegen/",
2020-10-01 20:26:11 +02:00
version: "v%%VERSION_NUMBER%%"
}));
2021-04-28 13:50:44 +02:00
$("main").classList.remove("hidden");
2021-11-14 10:56:02 +01:00
$("[autofocus]").focus();
2020-10-01 20:26:11 +02:00
});
// Event handlers
doAfterLoad(() => {
const generateSimilar = () => {
const name = $("#similarInput").value;
if (name === undefined || name === "")
return;
2020-10-01 21:00:19 +02:00
$("#similarNames").value = jaroWinklerFinder(name, names, similarCount).join("\n");
2020-10-01 20:26:11 +02:00
};
2020-10-01 21:00:19 +02:00
$("#generateSimilar").addEventListener("click", generateSimilar);
$("#similarInput").addEventListener("keypress", event => {
if (event.key === "Enter") {
2020-10-01 20:26:11 +02:00
generateSimilar();
2020-10-01 21:00:19 +02:00
event.preventDefault();
2020-10-01 20:26:11 +02:00
}
});
2020-10-01 21:00:19 +02:00
const randomBuffer = new CircularBuffer(randomCount);
2020-10-01 20:26:11 +02:00
$("#generateRandom").onclick = () => {
2020-10-01 21:00:19 +02:00
randomBuffer.add(getRandomFromArray(names));
2020-10-01 20:26:11 +02:00
2020-10-01 21:00:19 +02:00
const randomOutput = $("#randomNames");
randomOutput.value = randomBuffer.getEntriesInOrder().join("\n");
randomOutput.scrollTop = randomOutput.scrollHeight;
2020-10-01 20:26:11 +02:00
};
});