From 95b066c6f5df069010bcfd6e81be2db09d16f6a7 Mon Sep 17 00:00:00 2001
From: FCGudder <-@->
Date: Mon, 27 Aug 2018 07:57:34 +0200
Subject: [PATCH] Factoring out name and surname generation

---
 devNotes/twine JS.txt       | 41 +++++++++++++++++++++----------------
 src/js/slaveGenerationJS.tw | 41 +++++++++++++++++++++----------------
 2 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/devNotes/twine JS.txt b/devNotes/twine JS.txt
index 22737f700c0..e7f362055c6 100644
--- a/devNotes/twine JS.txt	
+++ b/devNotes/twine JS.txt	
@@ -26213,28 +26213,33 @@ window.nationalityToRace = function nationalityToRace(slave) {
 	slave.race = hashChoice(setup.raceSelector[slave.nationality] || setup.raceSelector[""]);
 };
 
+window.generateName = function generateName(nationality, race, male) {
+	const lookup = (male ? setup.malenamePoolSelector : setup.namePoolSelector);
+	const result = jsEither(
+		lookup[nationality + "." + race] || lookup[nationality] ||
+		(male ? setup.whiteAmericanMaleNames : setup.whiteAmericanSlaveNames));
+	/* fallback for males without specific male name sets: return female name */
+	if(male && !result) {
+		return generateName(nationality, race, false);
+	}
+	return result;
+};
+
+window.generateSurname = function generateSurname(nationality, race, male) {
+	const result = jsEither(
+		setup.surnamePoolSelector[nationality + "." + race] ||
+		setup.surnamePoolSelector[nationality] ||
+		setup.whiteAmericanSlaveSurnames);
+	/* TODO: Look up male alternatives if male == true */
+	return result || 0;
+};
+
 window.nationalityToName = function nationalityToName(slave) {
 	const V = State.variables;
 	const male = (slave.genes === "XY");
 
-	if(male) {
-		/* try male pools first */
-		slave.birthName =
-			jsEither(setup.malenamePoolSelector[slave.nationality + "." + slave.race] ||
-				setup.malenamePoolSelector[slave.nationality] ||
-				setup.whiteAmericanMaleNames);
-	}
-	if(!male || !slave.birthName) {
-		/* not male, or male names not defined yet */
-		slave.birthName =
-			jsEither(setup.namePoolSelector[slave.nationality + "." + slave.race] ||
-				setup.namePoolSelector[slave.nationality] ||
-				setup.whiteAmericanSlaveNames);
-	}
-	slave.birthSurname =
-		jsEither(setup.surnamePoolSelector[slave.nationality + "." + slave.race] ||
-			setup.surnamePoolSelector[slave.nationality] ||
-			setup.whiteAmericanSlaveSurnames);
+	slave.birthName = generateName(slave.nationality, slave.race, male);
+	slave.birthSurname = generateSurname(slave.nationality, slave.race, male);
 	slave.slaveName = slave.birthName;
 	slave.slaveSurname = slave.birthSurname;
 	if (V.useFSNames === 1) {
diff --git a/src/js/slaveGenerationJS.tw b/src/js/slaveGenerationJS.tw
index 36bbd5905bb..18b3394a497 100644
--- a/src/js/slaveGenerationJS.tw
+++ b/src/js/slaveGenerationJS.tw
@@ -48,28 +48,33 @@ window.nationalityToRace = function nationalityToRace(slave) {
 	slave.race = hashChoice(setup.raceSelector[slave.nationality] || setup.raceSelector[""]);
 };
 
+window.generateName = function generateName(nationality, race, male) {
+	const lookup = (male ? setup.malenamePoolSelector : setup.namePoolSelector);
+	const result = jsEither(
+		lookup[nationality + "." + race] || lookup[nationality] ||
+		(male ? setup.whiteAmericanMaleNames : setup.whiteAmericanSlaveNames));
+	/* fallback for males without specific male name sets: return female name */
+	if(male && !result) {
+		return generateName(nationality, race, false);
+	}
+	return result;
+};
+
+window.generateSurname = function generateSurname(nationality, race, male) {
+	const result = jsEither(
+		setup.surnamePoolSelector[nationality + "." + race] ||
+		setup.surnamePoolSelector[nationality] ||
+		setup.whiteAmericanSlaveSurnames);
+	/* TODO: Look up male alternatives if male == true */
+	return result || 0;
+};
+
 window.nationalityToName = function nationalityToName(slave) {
 	const V = State.variables;
 	const male = (slave.genes === "XY");
 
-	if(male) {
-		/* try male pools first */
-		slave.birthName =
-			jsEither(setup.malenamePoolSelector[slave.nationality + "." + slave.race] ||
-				setup.malenamePoolSelector[slave.nationality] ||
-				setup.whiteAmericanMaleNames);
-	}
-	if(!male || !slave.birthName) {
-		/* not male, or male names not defined yet */
-		slave.birthName =
-			jsEither(setup.namePoolSelector[slave.nationality + "." + slave.race] ||
-				setup.namePoolSelector[slave.nationality] ||
-				setup.whiteAmericanSlaveNames);
-	}
-	slave.birthSurname =
-		jsEither(setup.surnamePoolSelector[slave.nationality + "." + slave.race] ||
-			setup.surnamePoolSelector[slave.nationality] ||
-			setup.whiteAmericanSlaveSurnames);
+	slave.birthName = generateName(slave.nationality, slave.race, male);
+	slave.birthSurname = generateSurname(slave.nationality, slave.race, male);
 	slave.slaveName = slave.birthName;
 	slave.slaveSurname = slave.birthSurname;
 	if (V.useFSNames === 1) {
-- 
GitLab