2020-07-31 04:58:58 +02:00
|
|
|
import {UIElement} from "./UI/UIElement";
|
|
|
|
import {DropDown} from "./UI/Input/DropDown";
|
|
|
|
import {State} from "./State";
|
|
|
|
import Locale from "./UI/i18n/Locale";
|
|
|
|
|
2020-07-24 01:12:57 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-07-24 14:46:25 +02:00
|
|
|
|
|
|
|
public static Upper(str : string){
|
|
|
|
return str.substr(0,1).toUpperCase() + str.substr(1);
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
|
|
|
static DoEvery(millis: number, f: (() => void)) {
|
2020-08-08 02:16:42 +02:00
|
|
|
if (State.runningFromConsole) {
|
2020-07-31 17:11:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
|
|
|
f();
|
|
|
|
Utils.DoEvery(millis, f);
|
|
|
|
}
|
|
|
|
, millis)
|
|
|
|
}
|
|
|
|
|
2020-08-08 02:16:42 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-08-22 13:02:31 +02:00
|
|
|
|
|
|
|
public static NoEmpty(array: string[]): string[]{
|
|
|
|
const ls: string[] = [];
|
|
|
|
for (const t of array) {
|
|
|
|
if (t === "") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ls.push(t);
|
|
|
|
}
|
|
|
|
return ls;
|
|
|
|
}
|
2020-08-08 02:16:42 +02:00
|
|
|
|
2020-07-31 04:58:58 +02:00
|
|
|
public static CreateLanguagePicker(label: string | UIElement = "") {
|
|
|
|
|
|
|
|
return new DropDown(label, State.state.layoutToUse.data.supportedLanguages.map(lang => {
|
|
|
|
return {value: lang, shown: lang}
|
|
|
|
}
|
|
|
|
), Locale.language);
|
|
|
|
}
|
2020-08-26 00:21:34 +02:00
|
|
|
|
|
|
|
public static EllipsesAfter(str : string, l : number = 100){
|
|
|
|
if(str.length <= l){
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return str.substr(0, l - 3)+"...";
|
|
|
|
}
|
2020-08-26 15:36:04 +02:00
|
|
|
|
|
|
|
public static Dedup(arr: string[]):string[]{
|
|
|
|
if(arr === undefined){
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const newArr = [];
|
|
|
|
for (const string of arr) {
|
|
|
|
if(newArr.indexOf(string) < 0){
|
|
|
|
newArr.push(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newArr;
|
|
|
|
}
|
2020-08-27 00:08:00 +02:00
|
|
|
|
|
|
|
public static MergeTags(a :any, b: any){
|
|
|
|
const t = {};
|
|
|
|
for (const k in a) {
|
|
|
|
t[k] = a[k];
|
|
|
|
}
|
|
|
|
for (const k in b) {
|
|
|
|
t[k] = b[k];
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
2020-07-31 04:58:58 +02:00
|
|
|
|
2020-07-24 14:46:25 +02:00
|
|
|
}
|