mapcomplete/Utils.ts
Pieter Vander Vennet 17c0d19b7c Small layout tweaks
2020-08-26 00:21:34 +02:00

76 lines
1.8 KiB
TypeScript

import {UIElement} from "./UI/UIElement";
import {DropDown} from "./UI/Input/DropDown";
import {State} from "./State";
import Locale from "./UI/i18n/Locale";
export class Utils {
/**
* Gives a clean float, or undefined if parsing fails
* @param str
*/
static asFloat(str): number {
if (str) {
const i = parseFloat(str);
if (isNaN(i)) {
return undefined;
}
return i;
}
return undefined;
}
public static Upper(str : string){
return str.substr(0,1).toUpperCase() + str.substr(1);
}
static DoEvery(millis: number, f: (() => void)) {
if (State.runningFromConsole) {
return;
}
window.setTimeout(
function () {
f();
Utils.DoEvery(millis, f);
}
, millis)
}
public static NoNull<T>(array: T[]): T[] {
const ls: T[] = [];
for (const t of array) {
if (t === undefined || t === null) {
continue;
}
ls.push(t);
}
return ls;
}
public static NoEmpty(array: string[]): string[]{
const ls: string[] = [];
for (const t of array) {
if (t === "") {
continue;
}
ls.push(t);
}
return ls;
}
public static CreateLanguagePicker(label: string | UIElement = "") {
return new DropDown(label, State.state.layoutToUse.data.supportedLanguages.map(lang => {
return {value: lang, shown: lang}
}
), Locale.language);
}
public static EllipsesAfter(str : string, l : number = 100){
if(str.length <= l){
return str;
}
return str.substr(0, l - 3)+"...";
}
}