refactor(es modules): modulize utils

This commit is contained in:
Azgaar 2022-06-26 21:12:10 +03:00
parent 21fc303320
commit b8ae515425
15 changed files with 51 additions and 24 deletions

View file

@ -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";

View file

@ -1 +1,5 @@
type UnknownObject = {[key: string]: unknown};
interface Dict<T> {
[key: string]: T;
}

View file

@ -0,0 +1,77 @@
// extracted d3 code to bypass version conflicts
// https://github.com/d3/d3-array/blob/main/src/group.js
export function rollups(values, reduce, ...keys) {
return nest(values, Array.from, reduce, keys);
}
function nest(values, map, reduce, keys) {
return (function regroup(values, i) {
if (i >= keys.length) return reduce(values);
const groups = new Map();
const keyof = keys[i++];
let index = -1;
for (const value of values) {
const key = keyof(value, ++index, values);
const group = groups.get(key);
if (group) group.push(value);
else groups.set(key, [value]);
}
for (const [key, values] of groups) {
groups.set(key, regroup(values, i));
}
return map(groups);
})(values, 0);
}
function debounce(func, ms) {
let isCooldown = false;
return function () {
if (isCooldown) return;
func.apply(this, arguments);
isCooldown = true;
setTimeout(() => (isCooldown = false), ms);
};
}
function throttle(func, ms) {
let isThrottled = false;
let savedArgs;
let savedThis;
function wrapper() {
if (isThrottled) {
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this, arguments);
isThrottled = true;
setTimeout(function () {
isThrottled = false;
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}
return wrapper;
}
function getBase64(url, callback) {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
const reader = new FileReader();
reader.onloadend = function () {
callback(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.send();
}

View file

@ -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);
}

View file

@ -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);
}