mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
refactor(es modules): modulize utils
This commit is contained in:
parent
21fc303320
commit
b8ae515425
15 changed files with 51 additions and 24 deletions
|
|
@ -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 () {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
|
|
|
|||
|
|
@ -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 () {
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
|
|
|
|||
4
src/types/common.d.ts
vendored
4
src/types/common.d.ts
vendored
|
|
@ -1 +1,5 @@
|
|||
type UnknownObject = {[key: string]: unknown};
|
||||
|
||||
interface Dict<T> {
|
||||
[key: string]: T;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue