mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor(es modules): restore old functions with TS
This commit is contained in:
parent
aa214a7826
commit
dea496018c
7 changed files with 55 additions and 47 deletions
|
|
@ -32,41 +32,43 @@ export function rollups<TObject, TKey, TReduce>(
|
|||
return nest(values, Array.from, reduce, keys);
|
||||
}
|
||||
|
||||
export function debounce<T extends (...args: any[]) => any>(func: T, waitFor: number) {
|
||||
let timeout: ReturnType<typeof setTimeout>;
|
||||
return (...args: Parameters<T>): ReturnType<T> => {
|
||||
let result: any;
|
||||
timeout && clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
result = func(...args);
|
||||
}, waitFor);
|
||||
return result;
|
||||
export function debounce(func: Function, ms: number) {
|
||||
let isCooldown = false;
|
||||
|
||||
return function (this: unknown, ...args: unknown[]) {
|
||||
if (isCooldown) return;
|
||||
func.apply(this, args);
|
||||
isCooldown = true;
|
||||
setTimeout(() => (isCooldown = false), ms);
|
||||
};
|
||||
}
|
||||
|
||||
export function throttle(func: Function, waitFor: number = 300) {
|
||||
let inThrottle: boolean;
|
||||
let lastFn: ReturnType<typeof setTimeout>;
|
||||
let lastTime: number;
|
||||
export function throttle(func: Function, ms: number) {
|
||||
let isThrottled = false;
|
||||
let savedArgs: unknown[];
|
||||
let savedThis: unknown;
|
||||
|
||||
return function (this: any) {
|
||||
const context = this;
|
||||
const args = arguments;
|
||||
|
||||
if (!inThrottle) {
|
||||
func.apply(context, args);
|
||||
lastTime = Date.now();
|
||||
inThrottle = true;
|
||||
} else {
|
||||
clearTimeout(lastFn);
|
||||
lastFn = setTimeout(() => {
|
||||
if (Date.now() - lastTime >= waitFor) {
|
||||
func.apply(context, args);
|
||||
lastTime = Date.now();
|
||||
}
|
||||
}, Math.max(waitFor - (Date.now() - lastTime), 0));
|
||||
function wrapper(this: unknown, ...args: unknown[]) {
|
||||
if (isThrottled) {
|
||||
savedArgs = args;
|
||||
savedThis = this;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
func.apply(this, args);
|
||||
isThrottled = true;
|
||||
|
||||
setTimeout(function () {
|
||||
isThrottled = false;
|
||||
if (savedArgs) {
|
||||
wrapper.apply(savedThis, savedArgs);
|
||||
savedArgs = [];
|
||||
savedThis = null;
|
||||
}
|
||||
}, ms);
|
||||
}
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
export function getBase64(url: string, callback: (base64: string | ArrayBuffer | null) => void) {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
import {polygon} from "lineclip";
|
||||
|
||||
const {graphWidth, graphHeight, pack} = window;
|
||||
|
||||
// clip polygon by graph bbox
|
||||
export function clipPoly(points: TPoints) {
|
||||
return polygon(points, [0, 0, graphWidth, graphHeight]);
|
||||
// @ts-expect-error graphWidth/graphWidth are global variables
|
||||
return polygon(points, [0, 0, graphWidth, graphWidth]);
|
||||
}
|
||||
|
||||
// get segment of any point on polyline
|
||||
|
|
@ -40,6 +39,7 @@ export function getSegmentId(points: TPoints, point: TPoint, step = 10) {
|
|||
|
||||
// return center point of common edge of 2 pack cells
|
||||
export function getMiddlePoint(cell1: number, cell2: number) {
|
||||
// @ts-expect-error pack is global variable
|
||||
const {cells, vertices} = pack;
|
||||
|
||||
const commonVertices = cells.v[cell1].filter((vertex: number) =>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
import {ERROR} from "../config/logging";
|
||||
import {rn} from "./numberUtils";
|
||||
|
||||
// round numbers in string to d decimals
|
||||
export function round(str: string, d = 1) {
|
||||
if (!str) {
|
||||
ERROR && console.error("Path is empty", str);
|
||||
return "";
|
||||
}
|
||||
return str.replace(/[\d\.-][\d\.e-]*/g, n => String(rn(+n, d)));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue