2019-04-24 12:39:15 +02:00
|
|
|
export class HelperFunctions {
|
|
|
|
|
|
|
|
public static scroll() {
|
2020-05-21 15:17:28 +02:00
|
|
|
HelperFunctions.scrollTo(0,0);
|
|
|
|
}
|
|
|
|
public static scrollTo(x,y) {
|
2019-04-24 12:39:15 +02:00
|
|
|
if (typeof document !== 'undefined') {
|
2020-05-21 15:17:28 +02:00
|
|
|
window.scrollTo(x,y);
|
2019-04-24 12:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 12:51:39 +02:00
|
|
|
|
2020-05-21 15:17:28 +02:00
|
|
|
public static scrollToId(elementId:string) {
|
|
|
|
if (typeof document !== 'undefined' && document.getElementById(elementId)) {
|
2022-02-16 11:01:41 +01:00
|
|
|
document.getElementById(elementId).scrollIntoView({behavior: "smooth"});
|
2020-05-21 15:17:28 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-16 11:01:41 +01:00
|
|
|
|
2019-11-22 09:51:17 +01:00
|
|
|
public static copy(obj: any): any {
|
|
|
|
let copy;
|
|
|
|
|
|
|
|
// Handle the 3 simple types, and null or undefined
|
|
|
|
if (null == obj || "object" != typeof obj) return obj;
|
|
|
|
|
|
|
|
// Handle Date
|
|
|
|
if (obj instanceof Date) {
|
|
|
|
copy = new Date();
|
|
|
|
copy.setTime(obj.getTime());
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Array
|
|
|
|
if (obj instanceof Array) {
|
|
|
|
copy = [];
|
|
|
|
for (let i = 0, len = obj.length; i < len; i++) {
|
|
|
|
copy[i] = HelperFunctions.copy(obj[i]);
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Map
|
|
|
|
if (obj instanceof Map) {
|
2019-11-24 17:28:25 +01:00
|
|
|
return new Map(obj.entries());
|
2019-11-22 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Object
|
|
|
|
if (obj instanceof Object) {
|
|
|
|
copy = {};
|
|
|
|
for (let attr in obj) {
|
|
|
|
if (obj.hasOwnProperty(attr)) {
|
|
|
|
copy[attr] = HelperFunctions.copy(obj[attr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
2019-11-11 14:27:18 +01:00
|
|
|
}
|
2021-04-21 17:28:35 +02:00
|
|
|
|
|
|
|
public static equals(object1, object2) {
|
|
|
|
return object1 === object2 || JSON.stringify(object1) === JSON.stringify(object2);
|
|
|
|
}
|
2019-11-12 16:54:03 +01:00
|
|
|
|
|
|
|
public static encodeArray(elements: string[]): string[] {
|
|
|
|
let encoded: string[] = [];
|
|
|
|
elements.forEach(element => {
|
|
|
|
encoded.push(encodeURIComponent(element));
|
|
|
|
});
|
|
|
|
return encoded;
|
|
|
|
}
|
2021-04-28 17:03:45 +02:00
|
|
|
|
|
|
|
public static getValues(value: any): any[] {
|
|
|
|
return Object.keys(value).map(key => value[key]);
|
|
|
|
}
|
2022-03-15 22:31:10 +01:00
|
|
|
|
|
|
|
public static sortSDGs(sgd1: string, sdg2: string): number {
|
|
|
|
let splitA: string[] = sgd1.split(".");
|
|
|
|
let numA: number;
|
|
|
|
if(splitA && splitA.length > 0) {
|
|
|
|
numA = +splitA[0];
|
|
|
|
}
|
|
|
|
let splitB: string[] = sdg2.split(".");
|
|
|
|
let numB: number;
|
|
|
|
if(splitB && splitB.length > 0) {
|
|
|
|
numB = +splitB[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(numA && numB) {
|
|
|
|
return numA - numB;
|
|
|
|
} else {
|
|
|
|
return sgd1.localeCompare(sdg2);
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 12:51:39 +02:00
|
|
|
}
|