openaire-library/utils/HelperFunctions.class.ts

105 lines
2.5 KiB
TypeScript

export class HelperFunctions {
public static scroll() {
HelperFunctions.scrollTo(0,0);
}
public static scrollTo(x,y) {
if (typeof document !== 'undefined') {
window.scrollTo(x,y);
}
}
public static scrollToId(elementId:string) {
if (typeof document !== 'undefined' && document.getElementById(elementId)) {
document.getElementById(elementId).scrollIntoView({behavior: "smooth"});
}
}
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) {
return new Map(obj.entries());
}
// 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.");
}
public static equals(object1, object2) {
return object1 === object2 || JSON.stringify(object1) === JSON.stringify(object2);
}
public static encodeArray(elements: string[]): string[] {
let encoded: string[] = [];
elements.forEach(element => {
encoded.push(encodeURIComponent(element));
});
return encoded;
}
public static getValues(value: any): any[] {
return Object.keys(value).map(key => value[key]);
}
public static getVocabularyLabel(value: any, vocabulary: any) {
if(vocabulary && value in vocabulary) {
return vocabulary[value];
}
return value;
}
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);
}
}
public static swap(array: any[], from, to) {
array.splice(to, 0, array.splice(from, 1)[0]);
}
}