From b8ae515425a9583690f4612783c32fd39782a96a Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sun, 26 Jun 2022 21:12:10 +0300 Subject: [PATCH] refactor(es modules): modulize utils --- modules/burgs-and-states.js | 1 + modules/cultures-generator.js | 1 + modules/dynamic/editors/cultures-editor.js | 1 + modules/dynamic/editors/religions-editor.js | 1 + modules/dynamic/editors/states-editor.js | 1 + modules/markers-generator.js | 1 + modules/military-generator.js | 1 + modules/names-generator.js | 1 + modules/religions-generator.js | 1 + modules/ui/battle-screen.js | 1 + src/main.ts | 1 + src/types/common.d.ts | 4 +++ {utils => src/utils}/functionUtils.js | 0 .../{languageUtils.js => languageUtils.ts} | 36 +++++++++++-------- src/utils/{unitUtils.js => unitUtils.ts} | 24 +++++++------ 15 files changed, 51 insertions(+), 24 deletions(-) rename {utils => src/utils}/functionUtils.js (100%) rename src/utils/{languageUtils.js => languageUtils.ts} (80%) rename src/utils/{unitUtils.js => unitUtils.ts} (57%) diff --git a/modules/burgs-and-states.js b/modules/burgs-and-states.js index 2471d12d..663993d5 100644 --- a/modules/burgs-and-states.js +++ b/modules/burgs-and-states.js @@ -6,6 +6,7 @@ import {getMiddlePoint} from "@/utils/lineUtils"; import {rn, minmax} from "/src/utils/numberUtils"; import {rand, P, each, gauss, ra, rw, generateSeed} from "@/utils/probabilityUtils"; import {round, splitInTwo} from "@/utils/stringUtils"; +import {trimVowels, getAdjective} from "@/utils/languageUtils"; window.BurgsAndStates = (function () { const generate = function () { diff --git a/modules/cultures-generator.js b/modules/cultures-generator.js index 7f86286e..2d79229e 100644 --- a/modules/cultures-generator.js +++ b/modules/cultures-generator.js @@ -2,6 +2,7 @@ import {TIME} from "/src/config/logging"; import {getColors} from "/src/utils/colorUtils"; import {rn, minmax} from "/src/utils/numberUtils"; import {rand, P, rw, biased} from "@/utils/probabilityUtils"; +import {abbreviate} from "@/utils/languageUtils"; window.Cultures = (function () { let cells; diff --git a/modules/dynamic/editors/cultures-editor.js b/modules/dynamic/editors/cultures-editor.js index c6a32be5..f5a148bc 100644 --- a/modules/dynamic/editors/cultures-editor.js +++ b/modules/dynamic/editors/cultures-editor.js @@ -5,6 +5,7 @@ import {byId} from "/src/utils/shorthands"; import {rn} from "/src/utils/numberUtils"; import {capitalize} from "@/utils/stringUtils"; import {si} from "@/utils/unitUtils"; +import {abbreviate} from "@/utils/languageUtils"; const $body = insertEditorHtml(); addListeners(); diff --git a/modules/dynamic/editors/religions-editor.js b/modules/dynamic/editors/religions-editor.js index 3f8a035b..291ee828 100644 --- a/modules/dynamic/editors/religions-editor.js +++ b/modules/dynamic/editors/religions-editor.js @@ -4,6 +4,7 @@ import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips"; import {byId} from "/src/utils/shorthands"; import {rn} from "/src/utils/numberUtils"; import {si} from "@/utils/unitUtils"; +import {abbreviate} from "@/utils/languageUtils"; const $body = insertEditorHtml(); addListeners(); diff --git a/modules/dynamic/editors/states-editor.js b/modules/dynamic/editors/states-editor.js index c5e2a3e1..dd7ee4a1 100644 --- a/modules/dynamic/editors/states-editor.js +++ b/modules/dynamic/editors/states-editor.js @@ -6,6 +6,7 @@ import {getRandomColor, getMixedColor} from "/src/utils/colorUtils"; import {rn} from "/src/utils/numberUtils"; import {rand, P} from "@/utils/probabilityUtils"; import {si} from "@/utils/unitUtils"; +import {getAdjective} from "@/utils/languageUtils"; const $body = insertEditorHtml(); addListeners(); diff --git a/modules/markers-generator.js b/modules/markers-generator.js index 2e89510e..3b98f2d1 100644 --- a/modules/markers-generator.js +++ b/modules/markers-generator.js @@ -5,6 +5,7 @@ import {rn} from "/src/utils/numberUtils"; import {rand, P, gauss, ra, rw} from "@/utils/probabilityUtils"; import {capitalize} from "@/utils/stringUtils"; import {convertTemperature} from "@/utils/unitUtils"; +import {getAdjective, list} from "@/utils/languageUtils"; window.Markers = (function () { let config = []; diff --git a/modules/military-generator.js b/modules/military-generator.js index cbc67f78..21012be7 100644 --- a/modules/military-generator.js +++ b/modules/military-generator.js @@ -2,6 +2,7 @@ import {TIME} from "/src/config/logging"; import {rn, minmax} from "/src/utils/numberUtils"; import {rand, gauss, ra} from "@/utils/probabilityUtils"; import {si} from "@/utils/unitUtils"; +import {nth} from "@/utils/languageUtils"; window.Military = (function () { const generate = function () { diff --git a/modules/names-generator.js b/modules/names-generator.js index 69bf6795..cc99f94f 100644 --- a/modules/names-generator.js +++ b/modules/names-generator.js @@ -3,6 +3,7 @@ import {locked} from "/src/scripts/options/lock"; import {tip} from "/src/scripts/tooltips"; import {rand, P, ra} from "@/utils/probabilityUtils"; import {capitalize} from "@/utils/stringUtils"; +import {vowel} from "@/utils/languageUtils"; window.Names = (function () { let chains = []; diff --git a/modules/religions-generator.js b/modules/religions-generator.js index a156bde2..16a72185 100644 --- a/modules/religions-generator.js +++ b/modules/religions-generator.js @@ -4,6 +4,7 @@ import {unique} from "/src/utils/arrayUtils"; import {getRandomColor, getMixedColor} from "/src/utils/colorUtils"; import {rn} from "/src/utils/numberUtils"; import {rand, P, ra, rw, biased} from "@/utils/probabilityUtils"; +import {trimVowels, getAdjective, abbreviate} from "@/utils/languageUtils"; window.Religions = (function () { // name generation approach and relative chance to be selected diff --git a/modules/ui/battle-screen.js b/modules/ui/battle-screen.js index 73da895e..769c5100 100644 --- a/modules/ui/battle-screen.js +++ b/modules/ui/battle-screen.js @@ -4,6 +4,7 @@ import {wiki} from "@/utils/linkUtils"; import {rn, minmax} from "/src/utils/numberUtils"; import {rand, P, Pint} from "@/utils/probabilityUtils"; import {capitalize} from "@/utils/stringUtils"; +import {getAdjective, list} from "@/utils/languageUtils"; export class Battle { constructor(attacker, defender) { diff --git a/src/main.ts b/src/main.ts index caf073b8..1622d64b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -26,6 +26,7 @@ import {rn, minmax, normalize} from "./utils/numberUtils"; import {createTypedArray} from "./utils/arrayUtils"; import {clipPoly} from "@/utils/lineUtils"; import {rand, P, gauss, ra, rw, generateSeed} from "@/utils/probabilityUtils"; +import {getAdjective} from "@/utils/languageUtils"; import {byId} from "./utils/shorthands"; import "./components"; diff --git a/src/types/common.d.ts b/src/types/common.d.ts index 3d2fe1b3..ef2b66a5 100644 --- a/src/types/common.d.ts +++ b/src/types/common.d.ts @@ -1 +1,5 @@ type UnknownObject = {[key: string]: unknown}; + +interface Dict { + [key: string]: T; +} diff --git a/utils/functionUtils.js b/src/utils/functionUtils.js similarity index 100% rename from utils/functionUtils.js rename to src/utils/functionUtils.js diff --git a/src/utils/languageUtils.js b/src/utils/languageUtils.ts similarity index 80% rename from src/utils/languageUtils.js rename to src/utils/languageUtils.ts index 92963994..322a81ac 100644 --- a/src/utils/languageUtils.js +++ b/src/utils/languageUtils.ts @@ -2,19 +2,27 @@ import {P} from "@/utils/probabilityUtils"; // chars that serve as vowels const VOWELS = `aeiouyɑ'əøɛœæɶɒɨɪɔɐʊɤɯаоиеёэыуюяàèìòùỳẁȁȅȉȍȕáéíóúýẃőűâêîôûŷŵäëïöüÿẅãẽĩõũỹąęįǫųāēīōūȳăĕĭŏŭǎěǐǒǔȧėȯẏẇạẹịọụỵẉḛḭṵṳ`; -function vowel(c) { - return VOWELS.includes(c); + +export function vowel(char: string) { + return VOWELS.includes(char); } // remove vowels from the end of the string -function trimVowels(string, minLength = 3) { - while (string.length > minLength && vowel(string.at(-1))) { - string = string.slice(0, -1); +export function trimVowels(str: string, minLength = 3) { + while (str.length > minLength && str.length && vowel(str.at(-1) as string)) { + str = str.slice(0, -1); } - return string; + return str; } -const adjectivizationRules = [ +interface AdjectivizationRule { + name: string; + probability: number; + condition: RegExp; + action: (noun: string) => string; +} + +const adjectivizationRules: AdjectivizationRule[] = [ {name: "guo", probability: 1, condition: new RegExp(" Guo$"), action: noun => noun.slice(0, -4)}, { name: "orszag", @@ -141,7 +149,7 @@ const adjectivizationRules = [ ]; // get adjective form from noun -function getAdjective(noun) { +export function getAdjective(noun: string) { for (const rule of adjectivizationRules) { if (P(rule.probability) && rule.condition.test(noun)) { return rule.action(noun); @@ -150,12 +158,12 @@ function getAdjective(noun) { return noun; // no rule applied, return noun as is } -// get ordinal from integer: 1 => 1st -const nth = n => n + (["st", "nd", "rd"][((((n + 90) % 100) - 10) % 10) - 1] || "th"); +// get English ordinal from integer: 1 => 1st +export const nth = (n: number) => n + (["st", "nd", "rd"][((((n + 90) % 100) - 10) % 10) - 1] || "th"); // get two-letters code (abbreviation) from string -function abbreviate(name, restricted = []) { - const parsed = name.replace("Old ", "O ").replace(/[()]/g, ""); // remove Old prefix and parentheses +export function abbreviate(str: string, restricted: string[] = []) { + const parsed = str.replace("Old ", "O ").replace(/[()]/g, ""); // remove Old prefix and parentheses const words = parsed.split(" "); const letters = words.join(""); @@ -167,8 +175,8 @@ function abbreviate(name, restricted = []) { } // conjunct array: [A,B,C] => "A, B and C" -function list(array) { +export function list(array: string[]) { if (!Intl.ListFormat) return array.join(", "); - const conjunction = new Intl.ListFormat(window.lang || "en", {style: "long", type: "conjunction"}); + const conjunction = new Intl.ListFormat("en", {style: "long", type: "conjunction"}); return conjunction.format(array); } diff --git a/src/utils/unitUtils.js b/src/utils/unitUtils.ts similarity index 57% rename from src/utils/unitUtils.js rename to src/utils/unitUtils.ts index 2938ae9f..2301b30c 100644 --- a/src/utils/unitUtils.js +++ b/src/utils/unitUtils.ts @@ -1,7 +1,8 @@ -import {rn} from "/src/utils/numberUtils"; +import {byId} from "./shorthands"; +import {rn} from "./numberUtils"; // conver temperature from °C to other scales -const temperatureConversionMap = { +const temperatureConversionMap: Dict<(temp: number) => string> = { "°C": temp => rn(temp) + "°C", "°F": temp => rn((temp * 9) / 5 + 32) + "°F", K: temp => rn(temp + 273.15) + "K", @@ -12,13 +13,14 @@ const temperatureConversionMap = { "°Rø": temp => rn((temp * 21) / 40 + 7.5) + "°Rø" }; -export function convertTemperature(temp) { - const scale = temperatureScale.value || "°C"; - return temperatureConversionMap[scale](temp); +export function convertTemperature(temp: number) { + const scale = (byId("temperatureScale") as HTMLInputElement)?.value || "°C"; + const conversionFn = temperatureConversionMap[scale]; + return conversionFn(temp); } // corvert number to short string with SI postfix -export function si(n) { +export function si(n: number) { if (n >= 1e9) return rn(n / 1e9, 1) + "B"; if (n >= 1e8) return rn(n / 1e6) + "M"; if (n >= 1e6) return rn(n / 1e6, 1) + "M"; @@ -28,10 +30,12 @@ export function si(n) { } // convert SI number to integer -export function siToInteger(value) { +export function siToInteger(value: string) { const metric = value.slice(-1); - if (metric === "K") return parseInt(value.slice(0, -1) * 1e3); - if (metric === "M") return parseInt(value.slice(0, -1) * 1e6); - if (metric === "B") return parseInt(value.slice(0, -1) * 1e9); + const number = parseFloat(value.slice(0, -1)); + + if (metric === "K") return rn(number * 1e3); + if (metric === "M") return rn(number * 1e6); + if (metric === "B") return rn(number * 1e9); return parseInt(value); }