refactor(es modules): migrate numberUtils

This commit is contained in:
Azgaar 2022-06-26 02:11:36 +03:00
parent b425a9daf6
commit ad252b54e6
63 changed files with 97 additions and 56 deletions

View file

@ -7675,8 +7675,6 @@
<script src="utils/commonUtils.js"></script>
<script src="utils/nodeUtils.js"></script>
<script src="utils/numberUtils.js"></script>
<script src="utils/polyfills.js"></script>
<script src="utils/probabilityUtils.js"></script>
<script src="utils/stringUtils.js"></script>
<script src="utils/languageUtils.js"></script>

View file

@ -1,4 +1,6 @@
window.handleZoom = function (isScaleChanged, isPositionChanged) {
import {rn} from "/src/utils/numberUtils";
export function handleZoom(isScaleChanged, isPositionChanged) {
viewbox.attr("transform", `translate(${viewX} ${viewY}) scale(${scale})`);
if (isPositionChanged) drawCoordinates();
@ -21,7 +23,7 @@ window.handleZoom = function (isScaleChanged, isPositionChanged) {
ctx.setTransform(scale, 0, 0, scale, viewX, viewY);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
}
};
}
// active zooming feature
export function invokeActiveZooming() {

View file

@ -2,6 +2,7 @@ import {TIME} from "/src/config/logging";
import {findCell} from "/src/utils/graphUtils";
import {layerIsOn} from "./ui/layers";
import {getColors, getRandomColor, getMixedColor} from "/src/utils/colorUtils";
import {rn, minmax} from "/src/utils/numberUtils";
window.BurgsAndStates = (function () {
const generate = function () {

View file

@ -1,5 +1,6 @@
import {TIME} from "/src/config/logging";
import {getColors} from "/src/utils/colorUtils";
import {rn, minmax} from "/src/utils/numberUtils";
window.Cultures = (function () {
let cells;

View file

@ -1,4 +1,5 @@
import {findCell} from "/src/utils/graphUtils";
import {rn} from "/src/utils/numberUtils";
// update old .map version to the current one
export function resolveVersionConflicts(version) {

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {byId} from "/src/utils/shorthands";
import {rn} from "/src/utils/numberUtils";
const $body = insertEditorHtml();
addListeners();

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {byId} from "/src/utils/shorthands";
import {rn} from "/src/utils/numberUtils";
const $body = insertEditorHtml();
addListeners();

View file

@ -3,6 +3,7 @@ import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {byId} from "/src/utils/shorthands";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
import {rn} from "/src/utils/numberUtils";
const $body = insertEditorHtml();
addListeners();

View file

@ -2,6 +2,7 @@ import {rollups} from "../../../utils/functionUtils.js";
import {isWater} from "/src/utils/graphUtils";
import {tip} from "/src/scripts/tooltips";
import {byId} from "/src/utils/shorthands";
import {rn} from "/src/utils/numberUtils";
const entitiesMap = {
states: {

View file

@ -1,6 +1,7 @@
import {getGridPolygon} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils";
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
// download map as SVG
async function saveSVG() {

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {calculateVoronoi, findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {tip} from "/src/scripts/tooltips";
import {rn, minmax} from "/src/utils/numberUtils";
function quickLoad() {
ldb.get("lastMap", blob => {

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
// functions to save project as .map file

View file

@ -1,3 +1,5 @@
import {rn} from "/src/utils/numberUtils";
window.Lakes = (function () {
const setClimateData = function (h) {
const cells = pack.cells;

View file

@ -1,6 +1,7 @@
import {TIME} from "/src/config/logging";
import {getFriendlyHeight} from "./ui/general";
import {last} from "/src/utils/arrayUtils";
import {rn} from "/src/utils/numberUtils";
window.Markers = (function () {
let config = [];

View file

@ -1,4 +1,5 @@
import {TIME} from "/src/config/logging";
import {rn, minmax} from "/src/utils/numberUtils";
window.Military = (function () {
const generate = function () {

View file

@ -1,4 +1,5 @@
import {TIME} from "/src/config/logging";
import {rn} from "/src/utils/numberUtils";
window.OceanLayers = (function () {
let cells, vertices, pointsN, used;

View file

@ -1,4 +1,5 @@
import {getPackPolygon} from "/src/utils/graphUtils";
import {rn, minmax} from "/src/utils/numberUtils";
window.ReliefIcons = (function () {
const ReliefIcons = function () {

View file

@ -2,6 +2,7 @@ import {TIME} from "/src/config/logging";
import {findAll} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils";
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
import {rn} from "/src/utils/numberUtils";
window.Religions = (function () {
// name generation approach and relative chance to be selected

View file

@ -1,5 +1,6 @@
import {TIME} from "/src/config/logging";
import {last} from "/src/utils/arrayUtils";
import {rn} from "/src/utils/numberUtils";
window.Rivers = (function () {
const generate = function (allowErosion = true) {

View file

@ -1,4 +1,5 @@
import {findCell} from "/src/utils/graphUtils";
import {rn} from "/src/utils/numberUtils";
window.Submap = (function () {
const isWater = (pack, id) => pack.cells.h[id] < 20;

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
window.ThreeD = (function () {
const options = {

View file

@ -1,5 +1,6 @@
import {last} from "/src/utils/arrayUtils";
import {tip} from "/src/scripts/tooltips";
import {rn, minmax} from "/src/utils/numberUtils";
export class Battle {
constructor(attacker, defender) {

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor} from "/src/utils/colorUtils";
import {rn} from "/src/utils/numberUtils";
export function editBiomes() {
if (customization) return;

View file

@ -1,5 +1,6 @@
import {findCell} from "/src/utils/graphUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editBurg(id) {
if (customization) return;

View file

@ -1,6 +1,7 @@
import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function overviewBurgs() {
if (customization) return;

View file

@ -1,5 +1,6 @@
import {getPackPolygon} from "/src/utils/graphUtils";
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editCoastline(node = d3.event.target) {
if (customization) return;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {byId} from "/src/utils/shorthands";
import {tip} from "/src/scripts/tooltips";
import {rn, minmax, normalize} from "/src/utils/numberUtils";
// clear elSelected variable
export function unselect() {

View file

@ -1,4 +1,5 @@
import {findCell} from "/src/utils/graphUtils";
import {rn} from "/src/utils/numberUtils";
export function showEPForRoute(node) {
const points = [];

View file

@ -1,5 +1,6 @@
import {clearMainTip} from "/src/scripts/tooltips";
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editEmblem(type, id, el) {
if (customization) return;

View file

@ -1,5 +1,5 @@
import {findCell, findGridCell} from "/src/utils/graphUtils";
import {MOBILE} from "/src/constants";
import {rn} from "/src/utils/numberUtils";
// fit full-screen map if window is resized
window.addEventListener("resize", function (e) {

View file

@ -3,6 +3,7 @@ import {findGridCell, findGridAll, findCell, getPackPolygon, getGridPolygon} fro
import {last, createTypedArray} from "/src/utils/arrayUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {byId} from "/src/utils/shorthands";
import {rn, minmax, lim} from "/src/utils/numberUtils";
export function editHeightmap(options) {
const {mode, tool} = options || {};

View file

@ -1,5 +1,6 @@
import {findGridCell, getGridPolygon} from "/src/utils/graphUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editIce() {
if (customization) return;

View file

@ -1,5 +1,6 @@
import {getPackPolygon} from "/src/utils/graphUtils";
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editLake() {
if (customization) return;

View file

@ -5,6 +5,7 @@ import {last} from "/src/utils/arrayUtils";
import {stored, store} from "/src/utils/shorthands";
import {tip} from "/src/scripts/tooltips";
import {byId} from "/src/utils/shorthands";
import {rn, minmax, normalize} from "/src/utils/numberUtils";
let presets = {};
restoreCustomPresets(); // run on-load

View file

@ -1,6 +1,7 @@
import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editMarker(markerI) {
if (customization) return;

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function overviewMilitary() {
if (customization) return;

View file

@ -1,5 +1,6 @@
import {unique} from "/src/utils/arrayUtils";
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editNamesbase() {
if (customization) return;

View file

@ -3,6 +3,7 @@ import {last} from "/src/utils/arrayUtils";
import {lock, locked} from "/src/scripts/options/lock";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {byId, stored} from "/src/utils/shorthands";
import {rn, minmax} from "/src/utils/numberUtils";
$("#optionsContainer").draggable({handle: ".drag-trigger", snap: "svg", snapMode: "both"});
$("#exitCustomization").draggable({handle: "div"});

View file

@ -3,6 +3,7 @@ import {findAll, findCell, getPackPolygon, isLand} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {getRandomColor} from "/src/utils/colorUtils";
import {rn} from "/src/utils/numberUtils";
export function editProvinces() {
if (customization) return;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editRegiment(selector) {
if (customization) return;

View file

@ -1,6 +1,7 @@
import {findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function overviewRegiments(state) {
if (customization) return;

View file

@ -1,6 +1,7 @@
import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editReliefIcon() {
if (customization) return;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {getPackPolygon, findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function createRiver() {
if (customization) return;

View file

@ -1,5 +1,6 @@
import {findCell, getPackPolygon} from "/src/utils/graphUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editRiver(id) {
if (customization) return;

View file

@ -1,5 +1,6 @@
"use strict";
function overviewRivers() {
import {rn} from "/src/utils/numberUtils";
export function overviewRivers() {
if (customization) return;
closeDialogs("#riversOverview, .stable");
if (!layerIsOn("toggleRivers")) toggleRivers();

View file

@ -1,4 +1,5 @@
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editRoute(onClick) {
if (customization) return;

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
// add available filters to lists
{

View file

@ -1,5 +1,6 @@
import {byId} from "/src/utils/shorthands";
import {clearMainTip} from "/src/scripts/tooltips";
import {rn, minmax} from "/src/utils/numberUtils";
window.UISubmap = (function () {
byId("submapPointsInput").addEventListener("input", function () {

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function showBurgTemperatureGraph(id) {
const b = pack.burgs[id];

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {tip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
// module to control the Tools options (click to edit, to re-geenerate, tp add)

View file

@ -1,4 +1,5 @@
import {tip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editWorld() {
if (customization) return;

View file

@ -2,6 +2,7 @@ import {restoreDefaultEvents} from "/src/scripts/events";
import {findAll, findCell, getPackPolygon} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils";
import {tip, showMainTip, clearMainTip} from "/src/scripts/tooltips";
import {rn} from "/src/utils/numberUtils";
export function editZones() {
closeDialogs();

View file

@ -22,11 +22,14 @@ import {
isLand,
shouldRegenerateGrid
} from "./utils/graphUtils";
import {rn, minmax, normalize} from "./utils/numberUtils";
import {byId} from "./utils/shorthands";
import "./components";
addGlobalListeners();
const d3 = window.d3;
window.fmg = {
modules: {}
};
@ -51,8 +54,8 @@ rulers = new Rulers();
biomesData = Biomes.getDefault();
nameBases = Names.getNameBases(); // cultures-related data
color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
// color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
// lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
// voronoi graph extension, cannot be changed after generation
graphWidth = +byId("mapWidthInput").value;

View file

@ -1,3 +1,5 @@
import {rn} from "../utils/numberUtils";
export function drawLegend(name: string, data: unknown[]) {
legend.selectAll("*").remove(); // fully redraw every time
legend.attr("data", data.join("|")); // store data

View file

@ -1,5 +1,6 @@
import {findCell} from "/src/utils/graphUtils";
import {last} from "/src/utils/arrayUtils";
import {rn} from "/src/utils/numberUtils";
export class Rulers {
constructor() {

View file

@ -1,10 +1,16 @@
import {TIME} from "../config/logging";
import {createTypedArray} from "./arrayUtils";
import {rn} from "./numberUtils";
import {byId} from "./shorthands";
const Delaunator = window.Delaunator;
const Voronoi = window.Voronoi;
const graphWidth = window.graphWidth;
const graphHeight = window.graphHeight;
// check if new grid graph should be generated or we can use the existing one
export function shouldRegenerateGrid(grid) {
const cellsDesired = +byId("pointsInput").dataset.cells;
const cellsDesired = Number(byId("pointsInput")?.dataset.cells);
if (cellsDesired !== grid.cellsDesired) return true;
const newSpacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2);

21
src/utils/numberUtils.ts Normal file
View file

@ -0,0 +1,21 @@
// round value to d decimals
export function rn(value: number, decimals: number = 0) {
const multiplier = Math.pow(10, decimals);
return Math.round(value * multiplier) / multiplier;
}
export function minmax(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}
// return value clamped to [0, 100]
export function lim(value: number) {
return minmax(value, 0, 100);
}
// normalization function
export function normalize(val: number, min: number, max: number) {
return minmax((val - min) / (max - min), 0, 1);
}
// import {rn, minmax, lim, normalize} from '/src/utils/numberUtils';

View file

@ -1,5 +1,4 @@
"use strict";
// FMG helper functions
import {rn} from "/src/utils/numberUtils";
// clip polygon by graph bbox
function clipPoly(points, secure = 0) {

View file

@ -1,22 +0,0 @@
"use strict";
// FMG utils related to numbers
// round value to d decimals
function rn(v, d = 0) {
const m = Math.pow(10, d);
return Math.round(v * m) / m;
}
function minmax(value, min, max) {
return Math.min(Math.max(value, min), max);
}
// return value in range [0, 100]
function lim(v) {
return minmax(v, 0, 100);
}
// normalization function
function normalize(val, min, max) {
return minmax((val - min) / (max - min), 0, 1);
}

View file

@ -1,16 +0,0 @@
"use strict";
// replaceAll
if (String.prototype.replaceAll === undefined) {
String.prototype.replaceAll = function (str, newStr) {
if (Object.prototype.toString.call(str).toLowerCase() === "[object regexp]") return this.replace(str, newStr);
return this.replace(new RegExp(str, "g"), newStr);
};
}
// flat
if (Array.prototype.flat === undefined) {
Array.prototype.flat = function () {
return this.reduce((acc, val) => (Array.isArray(val) ? acc.concat(val.flat()) : acc.concat(val)), []);
};
}

View file

@ -1,5 +1,4 @@
"use strict";
// FMG utils related to randomness
import {rn, minmax} from "/src/utils/numberUtils";
// random number in a range
function rand(min, max) {

View file

@ -1,5 +1,4 @@
"use strict";
// FMG utils related to strings
import {rn} from "/src/utils/numberUtils";
// round numbers in string to d decimals
function round(s, d = 1) {

View file

@ -1,5 +1,4 @@
"use strict";
// FMG utils related to units
import {rn} from "/src/utils/numberUtils";
// conver temperature from °C to other scales
const temperatureConversionMap = {