create_book_title.js

import { mapLoader } from "./data_loaders.js";
import { random } from "./random_utils.js";
import { resolve } from "./string_utils.js";

const BOOK_TITLES = await mapLoader("books.data", ";");

/*
Marked for Ruin
{Sea|Grace} and the {Heart|Tomb|Serpent}
Throne and the Lily
Stars and the Princess
{Tried} for {The State}
The Lightning Veil
Twenty and Eligible
How to Win a Prince
Mists
Lure the Devil
Steel Abyss
Abyss/Nebula
Atomic Chaos
*/

const KEYS = [];
for (const key of BOOK_TITLES.keys()) {
  KEYS.push(key);
}

export function getBookGenres() {
  return KEYS;
}

/**
 * Generates a random fictional book title, optionally filtered by genre.
 *
 * @param {Object} [options={}] - Book title creation options
 * @param {string} [options.genre] - Genre of book to generate, selected from the values returned by
 *    `getBookGenres()`. If not provided, a genre is randomly selected.
 * @returns {string} A generated book title
 */
export function createBookTitle({ genre = random(getBookGenres()) } = {}) {
  return resolve(BOOK_TITLES.get(genre));
}