2019-09-05 16:02:18 +02:00
|
|
|
import {UrlSegment} from '@angular/router';
|
2022-09-23 12:25:08 +02:00
|
|
|
import {AbstractControl, UntypedFormGroup, ValidationErrors, ValidatorFn, Validators} from "@angular/forms";
|
2022-06-09 10:21:25 +02:00
|
|
|
import {Stakeholder, StakeholderEntities} from "../monitor/entities/stakeholder";
|
2021-11-12 13:07:55 +01:00
|
|
|
import {CommunityInfo} from "../connect/community/communityInfo";
|
|
|
|
import {properties} from "../../../environments/environment";
|
2022-06-03 12:01:55 +02:00
|
|
|
import {OpenaireEntities} from "./properties/searchFields";
|
2019-09-05 16:02:18 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
export class Dates {
|
2020-02-17 15:19:14 +01:00
|
|
|
public static yearMin = 1800;
|
|
|
|
public static yearMax = (new Date().getFullYear()) + 10;
|
2020-02-24 14:38:08 +01:00
|
|
|
public static currentYear = (new Date().getFullYear());
|
2021-02-12 12:31:12 +01:00
|
|
|
|
|
|
|
public static isValidYear(yearString, yearMin = this.yearMin, yearMax = this.yearMax) {
|
2017-12-19 13:53:46 +01:00
|
|
|
// First check for the pattern
|
2020-06-05 14:56:28 +02:00
|
|
|
if (!/^\d{4}$/.test(yearString))
|
|
|
|
return false;
|
|
|
|
var year = parseInt(yearString, 10);
|
|
|
|
|
|
|
|
// Check the ranges of month and year
|
2021-11-12 13:07:55 +01:00
|
|
|
return !(year < yearMin || year > yearMax);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
//format YYYY-MM-DD
|
2020-06-05 14:56:28 +02:00
|
|
|
public static isValidDate(dateString: string) {
|
|
|
|
// First check for the pattern
|
|
|
|
if (!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(dateString))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Parse the date parts to integers
|
|
|
|
var parts = dateString.split("-");
|
|
|
|
var day = parseInt(parts[2], 10);
|
|
|
|
var month = parseInt(parts[1], 10);
|
|
|
|
var year = parseInt(parts[0], 10);
|
|
|
|
if (!this.isValidYear(parts[0])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the ranges of month and year
|
|
|
|
if (month == 0 || month > 12)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
|
|
|
|
|
|
// Adjust for leap years
|
|
|
|
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
|
|
|
|
monthLength[1] = 29;
|
|
|
|
|
|
|
|
// Check the range of the day
|
|
|
|
return day > 0 && day <= monthLength[month - 1];
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static getDateToday(): Date {
|
2017-12-19 13:53:46 +01:00
|
|
|
var myDate = new Date();
|
|
|
|
return myDate;
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static getDateToString(myDate: Date): string {
|
|
|
|
var date: string = myDate.getFullYear() + "-";
|
|
|
|
date += ((myDate.getMonth() + 1) < 10) ? "0" + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
|
|
|
|
date += "-";
|
|
|
|
date += (myDate.getDate() < 10) ? "0" + myDate.getDate() : myDate.getDate();
|
|
|
|
return date;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static getDateXMonthsAgo(x: number): Date {
|
2017-12-19 13:53:46 +01:00
|
|
|
var myDate = new Date();
|
|
|
|
myDate.setMonth(myDate.getMonth() - x);
|
|
|
|
return myDate;
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static getDateXYearsAgo(x: number): Date {
|
2017-12-19 13:53:46 +01:00
|
|
|
var myDate = new Date();
|
|
|
|
myDate.setFullYear(myDate.getFullYear() - x);
|
|
|
|
return myDate;
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static getDateFromString(date: string): Date {
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
var myDate = new Date();
|
2020-06-05 14:56:28 +02:00
|
|
|
myDate.setFullYear(+date.substring(0, 4));
|
|
|
|
myDate.setMonth(((date.length > 5) ? (+date.substring(5, 7) - 1) : (0)));
|
|
|
|
myDate.setDate(((date.length > 8) ? (+date.substring(8, 11)) : (1)));
|
2017-12-19 13:53:46 +01:00
|
|
|
return myDate;
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2020-07-30 11:31:15 +02:00
|
|
|
public static getDate(dateString: string): Date {
|
|
|
|
let date = new Date(dateString);
|
|
|
|
if (Object.prototype.toString.call(date) === "[object Date]") {
|
|
|
|
if (isNaN(date.getTime())) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 12:31:12 +01:00
|
|
|
|
|
|
|
public static timeSince(date: Date) {
|
|
|
|
|
|
|
|
let seconds = Math.floor((new Date().getTime() - new Date(date).getTime()) / 1000);
|
|
|
|
|
|
|
|
let interval = seconds / (365*24*60*60);
|
|
|
|
|
|
|
|
if (interval > 1) {
|
|
|
|
let years = Math.floor(interval);
|
|
|
|
return (years > 1?(years + ' years ago'):'a year ago');
|
|
|
|
}
|
|
|
|
interval = seconds / (7*24*60*60);
|
|
|
|
if (interval > 1) {
|
|
|
|
let weeks = Math.floor(interval);
|
|
|
|
return (weeks > 1?(weeks + ' weeks ago'):'a week ago');
|
|
|
|
}
|
|
|
|
interval = seconds / (24*60*60);
|
|
|
|
if (interval > 1) {
|
|
|
|
let days = Math.floor(interval);
|
|
|
|
return (days > 1?(days + ' days ago'):'a day ago');
|
|
|
|
}
|
|
|
|
interval = seconds / (60*60);
|
|
|
|
if (interval > 1) {
|
|
|
|
let hours = Math.floor(interval);
|
|
|
|
return (hours > 1?(hours + ' hours ago'):'an hour ago');
|
|
|
|
}
|
|
|
|
interval = seconds / 60;
|
|
|
|
if (interval > 1) {
|
|
|
|
let minutes = Math.floor(interval);
|
|
|
|
return (minutes > 1?(minutes + ' minutes ago'):'a minute ago');
|
|
|
|
}
|
|
|
|
seconds = Math.floor(interval);
|
|
|
|
return (seconds > 1?(seconds + ' seconds ago'):' just now');
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-06-05 14:56:28 +02:00
|
|
|
export class DOI {
|
|
|
|
|
|
|
|
public static getDOIsFromString(str: string): string[] {
|
2020-03-03 15:32:43 +01:00
|
|
|
return Identifier.getDOIsFromString(str);
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidDOI(str: string): boolean {
|
2020-03-03 15:32:43 +01:00
|
|
|
return Identifier.isValidDOI(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:56:28 +02:00
|
|
|
export class Identifier {
|
2023-10-06 11:22:00 +02:00
|
|
|
class: "doi" | "pmc" | "pmid" | "handle" | "ORCID" | "re3data" | "swhid" = null;
|
2020-06-05 14:56:28 +02:00
|
|
|
id: string;
|
|
|
|
|
|
|
|
public static getDOIsFromString(str: string): string[] {
|
|
|
|
var DOIs: string[] = [];
|
|
|
|
var words: string[] = str.split(" ");
|
|
|
|
|
|
|
|
for (var i = 0; i < words.length; i++) {
|
2022-02-21 15:29:03 +01:00
|
|
|
let id = words[i];
|
|
|
|
if (DOI.isValidDOI(id) ) {
|
|
|
|
id = Identifier.getRawDOIValue(id);
|
|
|
|
if( DOIs.indexOf(id) == -1){
|
|
|
|
DOIs.push(id);
|
|
|
|
}
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
return DOIs;
|
|
|
|
}
|
2022-02-21 15:29:03 +01:00
|
|
|
public static getRawDOIValue(id: string): string {
|
|
|
|
if(id.indexOf("doi.org")!=-1 && id.split("doi.org/").length > 1){
|
|
|
|
id = id.split("doi.org/")[1];
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
public static getIdentifiersFromString(str: string): Identifier[] {
|
|
|
|
let identifiers: Identifier[] = [];
|
|
|
|
let words: string[] = str.split(" ");
|
|
|
|
|
|
|
|
for (let id of words) {
|
|
|
|
if (id.length > 0) {
|
2021-01-13 12:58:06 +01:00
|
|
|
let identifier: Identifier = this.getIdentifierFromString(id);
|
2021-02-12 12:31:12 +01:00
|
|
|
if (identifier) {
|
2021-01-13 12:58:06 +01:00
|
|
|
identifiers.push(identifier);
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return identifiers;
|
|
|
|
}
|
2021-02-12 12:31:12 +01:00
|
|
|
|
2021-04-26 11:42:41 +02:00
|
|
|
public static getIdentifierFromString(pid: string,strict:boolean = true): Identifier {
|
2021-01-13 12:58:06 +01:00
|
|
|
if (Identifier.isValidDOI(pid)) {
|
2022-02-21 15:29:03 +01:00
|
|
|
pid = Identifier.getRawDOIValue(pid);
|
2021-01-13 12:58:06 +01:00
|
|
|
return {"class": "doi", "id": pid};
|
|
|
|
} else if (Identifier.isValidORCID(pid)) {
|
2023-12-01 15:16:25 +01:00
|
|
|
return {"class": "ORCID", "id": Identifier.getRawORCID(pid)};
|
2021-01-13 12:58:06 +01:00
|
|
|
} else if (Identifier.isValidPMCID(pid)) {
|
|
|
|
return {"class": "pmc", "id": pid};
|
|
|
|
} else if (Identifier.isValidPMID(pid)) {
|
|
|
|
return {"class": "pmid", "id": pid};
|
|
|
|
} else if (Identifier.isValidHANDLE(pid)) {
|
|
|
|
return {"class": "handle", "id": pid};
|
2022-05-16 16:01:35 +02:00
|
|
|
} else if (Identifier.isValidRe3Data(pid)) {
|
|
|
|
return {"class": "re3data", "id": pid};
|
2023-10-06 11:22:00 +02:00
|
|
|
} else if (Identifier.isValidSwhId(pid)) {
|
|
|
|
return {"class": "swhid", "id": pid};
|
2021-01-13 12:58:06 +01:00
|
|
|
}
|
2021-04-26 11:42:41 +02:00
|
|
|
//set it as a doi, to catch the case that doi has not valid format
|
|
|
|
return (strict?null:{"class": "doi", "id": pid});
|
2021-01-13 12:58:06 +01:00
|
|
|
}
|
2021-02-11 11:20:10 +01:00
|
|
|
|
2022-05-30 09:39:10 +02:00
|
|
|
public static getPIDFromIdentifiers(identifiers: Map<string, string[]>): Identifier {
|
2023-10-06 11:22:00 +02:00
|
|
|
let classes:string [] = ["doi", "handle", "pmc", "pmid", "re3data", "swhid"];
|
2023-07-25 12:24:24 +02:00
|
|
|
if(identifiers) {
|
|
|
|
for (let cl of classes) {
|
|
|
|
if (identifiers.get(cl)) {
|
|
|
|
for (let pid of identifiers.get(cl)) {
|
|
|
|
let identifier = Identifier.getIdentifierFromString(pid);
|
|
|
|
if (identifier) {
|
|
|
|
return identifier;
|
2021-02-11 11:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 12:24:24 +02:00
|
|
|
}
|
2021-02-11 11:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 12:31:26 +02:00
|
|
|
return null;
|
2021-02-11 11:20:10 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidDOI(str: string): boolean {
|
2021-04-26 11:42:41 +02:00
|
|
|
//keep only exp3?
|
|
|
|
let exp1 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])\S)+)\b/g;
|
|
|
|
let exp2 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])[[:graph:]])+)\b/g;
|
2021-05-19 13:24:10 +02:00
|
|
|
let exp3 = /\b(10[.]*)\b/g;
|
2021-04-26 11:42:41 +02:00
|
|
|
return (str.match(exp1) != null || str.match(exp2) != null || str.match(exp3) != null);
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidORCID(str: string): boolean {
|
|
|
|
let exp = /\b\d{4}-\d{4}-\d{4}-(\d{3}X|\d{4})\b/g;
|
2023-12-01 15:16:25 +01:00
|
|
|
return str.match(exp) != null || this.getRawORCID(str).match(exp) != null;
|
|
|
|
}
|
|
|
|
public static getRawORCID(id: string): string {
|
|
|
|
if(id.indexOf("orcid.org")!=-1 && id.split("orcid.org/").length > 1){
|
|
|
|
id = id.split("orcid.org/")[1];
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
public static isValidPMID(str: string): boolean {
|
|
|
|
let exp = /^\d*$/g;
|
|
|
|
return str.match(exp) != null;
|
|
|
|
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidPMCID(str: string): boolean {
|
|
|
|
let exp = /^(PMC\d{7})$/g;
|
|
|
|
return str.match(exp) != null;
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidHANDLE(str: string): boolean {
|
|
|
|
let exp = /^[0-9a-zA-Z-]*\/[0-9a-zA-Z-]*$/g;
|
|
|
|
return str.match(exp) != null;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-05-16 16:01:35 +02:00
|
|
|
|
|
|
|
public static isValidRe3Data(str: string): boolean {
|
|
|
|
let exp = /(r3d[1-9]\d{0,8})/g;
|
|
|
|
return str.match(exp) != null;
|
|
|
|
}
|
2023-10-06 11:22:00 +02:00
|
|
|
|
|
|
|
public static isValidSwhId(str: string): boolean {
|
|
|
|
// let exp = /swh:1:(snp|rel|rev|dir|cnt):[0-9a-f]{40}/g;
|
|
|
|
let exp = /swh:1:snp:[0-9a-f]{40}/g;
|
|
|
|
return str.match(exp) != null;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
export class StringUtils {
|
2021-02-01 11:14:08 +01:00
|
|
|
|
2023-03-27 14:56:42 +02:00
|
|
|
public static urlRegex = 'https?:\\/\\/(?:www(2?)\\.|(?!www(2?)))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www(2?)\\.' +
|
|
|
|
'[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?:\\/\\/(?:www(2?)\\.|(?!www(2?)))[a-zA-Z0-9]+\\.[^\\s]{2,}|www(2?)\\.' +
|
|
|
|
'[a-zA-Z0-9]+\\.[^\\s]{2,}';
|
2021-02-01 11:14:08 +01:00
|
|
|
|
2022-01-13 14:46:22 +01:00
|
|
|
public static routeRegex = '^[a-zA-Z0-9\/][a-zA-Z0-9\/-]*$';
|
2023-06-26 10:31:22 +02:00
|
|
|
|
|
|
|
public static jsonRegex = /^[\],:{}\s]*$/;
|
2022-01-13 14:46:22 +01:00
|
|
|
|
2020-05-07 13:28:55 +02:00
|
|
|
public static urlPrefix(url: string): string {
|
2023-12-01 16:13:16 +01:00
|
|
|
if (!url || url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//")) {
|
2020-05-07 13:28:55 +02:00
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return "//";
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static quote(params: string): string {
|
|
|
|
return '"' + params + '"';
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static unquote(params: string): string {
|
|
|
|
if (params.length > 2 && (params[0] == '"' && params[params.length - 1] == '"') || (params[0] == "'" && params[params.length - 1] == "'")) {
|
|
|
|
params = params.substring(1, params.length - 1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
return params;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static URIEncode(params: string): string {
|
|
|
|
return encodeURIComponent(params);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static URIDecode(params: string): string {
|
|
|
|
return decodeURIComponent(params);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2021-01-21 16:15:53 +01:00
|
|
|
public static validateEmails(emails: string): boolean {
|
|
|
|
return (emails.split(',')
|
2021-02-12 12:31:12 +01:00
|
|
|
.map(email => Validators.email(<AbstractControl>{value: email.trim()}))
|
2021-01-21 16:15:53 +01:00
|
|
|
.find(_ => _ !== null) === undefined);
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:56:28 +02:00
|
|
|
public static b64DecodeUnicode(str) {
|
|
|
|
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
|
|
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
|
|
}).join(''));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
private emailValidator(email: any): boolean {
|
|
|
|
return !!email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$");
|
2018-07-09 14:07:54 +02:00
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
2021-11-12 13:07:55 +01:00
|
|
|
public static getLogoUrl(result: Stakeholder | CommunityInfo): string {
|
2022-06-27 14:50:21 +02:00
|
|
|
if(result && result.logoUrl) {
|
2022-06-26 22:30:54 +02:00
|
|
|
return (result.isUpload)?(properties.utilsService + '/download/' + result.logoUrl):result.logoUrl;
|
|
|
|
} else {
|
|
|
|
return "assets/common-assets/placeholder.png";
|
|
|
|
}
|
2021-11-12 13:07:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:14:08 +01:00
|
|
|
public static isValidUrl(url: string): boolean {
|
|
|
|
return new RegExp(this.urlRegex).test(url);
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:02:58 +01:00
|
|
|
public static urlValidator(): ValidatorFn {
|
2021-03-03 19:04:53 +01:00
|
|
|
return Validators.pattern(StringUtils.urlRegex);
|
2020-12-16 18:02:58 +01:00
|
|
|
}
|
2023-06-26 10:31:22 +02:00
|
|
|
|
|
|
|
public static jsonValidator(error: string = ''): ValidatorFn {
|
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
|
|
if(control.value) {
|
|
|
|
let test = control.getRawValue().replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, '');
|
|
|
|
if(!new RegExp(this.jsonRegex).test(test)) {
|
|
|
|
return {error: 'Please provide a valid JSON.' + error}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
}
|
2020-12-16 18:02:58 +01:00
|
|
|
|
2022-02-24 17:42:05 +01:00
|
|
|
public static validatorType(options: string[]): ValidatorFn {
|
|
|
|
return (control: AbstractControl): { [key: string]: boolean } | null => {
|
|
|
|
if (options.filter(type => type === control.value).length === 0) {
|
|
|
|
return {'type': false};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:46:22 +01:00
|
|
|
public static validRoute(pages: any[], field: string, initial: string = null): ValidatorFn {
|
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
|
|
if(control.value) {
|
|
|
|
if(!new RegExp(this.routeRegex).test(control.value)) {
|
|
|
|
return {error: 'Route should contain only letters or numbers, e.g /route or route'}
|
|
|
|
}
|
|
|
|
if(pages && pages.length > 0 && control.value !== initial) {
|
|
|
|
const forbidden = pages.filter(page => page[field].replace('/', '') === control.value.replace('/', '')).length > 0;
|
|
|
|
return forbidden ? {error: 'This route is used by an other page'} : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-23 14:23:12 +02:00
|
|
|
public static sliceString(mystr, size: number): string {
|
|
|
|
const sliced = String(mystr).substr(0, size);
|
|
|
|
return sliced + (String(mystr).length > size ? '...' : '');
|
|
|
|
}
|
2019-12-20 12:03:48 +01:00
|
|
|
|
2020-02-07 14:05:07 +01:00
|
|
|
/**
|
|
|
|
* Splits a text to words base on a list of separators. Returns the words of the text including the separators.
|
|
|
|
* DO NOT TOUCH, IT WORKS
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @param separators
|
|
|
|
*/
|
|
|
|
public static split(text: string, separators: string[]): string[] {
|
|
|
|
let words: (string | string[])[] = [text];
|
|
|
|
separators.forEach(separator => {
|
|
|
|
words.forEach((word, index) => {
|
2020-06-05 14:56:28 +02:00
|
|
|
if (typeof word === "string" && separators.indexOf(word) === -1) {
|
2020-02-07 14:05:07 +01:00
|
|
|
let tokens: string[] = word.split(separator).filter(value => value !== '');
|
2020-06-05 14:56:28 +02:00
|
|
|
if (tokens.length > 1) {
|
2020-02-07 14:05:07 +01:00
|
|
|
words[index] = [];
|
|
|
|
tokens.forEach((token, i) => {
|
|
|
|
(<string[]>(words[index])).push(token);
|
2020-06-05 14:56:28 +02:00
|
|
|
if (i !== (tokens.length - 1)) {
|
2020-02-07 14:05:07 +01:00
|
|
|
(<string[]>(words[index])).push(separator);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
words = [].concat.apply([], words);
|
|
|
|
});
|
|
|
|
return <string []>words;
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:03:48 +01:00
|
|
|
public static capitalize(value: string): string {
|
|
|
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a text contains a word
|
|
|
|
*/
|
2021-02-12 12:31:12 +01:00
|
|
|
public static containsWord(text: string, word: string): boolean {
|
2020-06-05 14:56:28 +02:00
|
|
|
return (text && text.toLowerCase().includes(word));
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:02:18 +02:00
|
|
|
public static URLSegmentsToPath(segments: UrlSegment[]): string {
|
|
|
|
let path = '';
|
|
|
|
segments.forEach(route => {
|
|
|
|
path += '/' + route.path;
|
|
|
|
})
|
|
|
|
return path;
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isEuropeanCountry(country: string) {
|
|
|
|
let countries = ["Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
|
2019-12-03 11:45:28 +01:00
|
|
|
"Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece", "Hungary", "Iceland", "Ireland",
|
2020-06-05 14:56:28 +02:00
|
|
|
"Italy", "Kosovo", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "The Netherlands", "Norway", "Poland",
|
|
|
|
"Portugal", "Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City",
|
2019-12-03 11:45:28 +01:00
|
|
|
];
|
2020-06-05 14:56:28 +02:00
|
|
|
return (country && countries.indexOf(country) != -1);
|
2020-06-16 18:42:51 +02:00
|
|
|
}
|
2021-02-12 12:31:12 +01:00
|
|
|
|
|
|
|
public static isOpenAIREID(id: string) {
|
|
|
|
if (id && id.length == 46) {
|
2020-06-16 18:42:51 +02:00
|
|
|
let exp1 = /^.{12}::([0-9a-z]{32})$/g;
|
2021-02-12 12:31:12 +01:00
|
|
|
return (id.match(exp1) != null);
|
2020-06-16 18:42:51 +02:00
|
|
|
}
|
|
|
|
return false;
|
2020-06-05 14:56:28 +02:00
|
|
|
}
|
2021-03-12 15:49:12 +01:00
|
|
|
public static HTMLToString( html:string){
|
|
|
|
try {
|
|
|
|
html = html.replace(/ /g, ' ');
|
|
|
|
html = html.replace(/(\r\n|\n|\r| +(?= ))|\s\s+/gm, " ");
|
|
|
|
html = html.replace(/<[^>]*>/g, '');
|
|
|
|
}catch( e){
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
}
|
2022-04-05 15:45:38 +02:00
|
|
|
|
|
|
|
public static inValidYearValidator(minYear, maxYear): ValidatorFn {
|
|
|
|
return (control: AbstractControl): {[key: string]: any} | null => {
|
2023-05-10 10:51:59 +02:00
|
|
|
return ((control.value && !Dates.isValidYear(control.value, minYear, maxYear)) ?
|
|
|
|
{error: 'Year must be between ' + minYear + ' and ' + maxYear + '.'} : null);
|
2022-04-05 15:45:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-23 12:25:08 +02:00
|
|
|
public static fromYearAfterToYearValidator: ValidatorFn = (control: UntypedFormGroup): ValidationErrors | null => {
|
2022-04-05 15:45:38 +02:00
|
|
|
const yearFrom = control.get('yearFrom');
|
|
|
|
const yearTo = control.get('yearTo');
|
2023-05-10 10:51:59 +02:00
|
|
|
return ((yearFrom && yearTo && (parseInt(yearFrom.value, 10) > parseInt(yearTo.value, 10))) ?
|
|
|
|
{error: 'Starting year must be greater than or equal to ending year.' } : null);
|
2022-04-05 15:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static rangeRequired( enabled:boolean): ValidatorFn {
|
|
|
|
return (control: AbstractControl): ValidationErrors | null => {
|
|
|
|
const yearFrom = control.get('yearFrom');
|
|
|
|
const yearTo = control.get('yearTo');
|
2023-05-10 10:51:59 +02:00
|
|
|
return ((yearFrom && yearTo && enabled && (yearFrom.value == "" || yearTo.value == "")) ? { error: 'Both starting and ending year are required' } : null);
|
2022-04-05 15:45:38 +02:00
|
|
|
};
|
|
|
|
}
|
2022-06-03 12:01:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
public static getEntityName(entityType: string, plural: boolean = false): string {
|
|
|
|
if (entityType == "publication") {
|
|
|
|
return plural ? OpenaireEntities.PUBLICATIONS : OpenaireEntities.PUBLICATION;
|
|
|
|
} else if (entityType == "dataset") {
|
|
|
|
return plural ? OpenaireEntities.DATASETS : OpenaireEntities.DATASET;
|
|
|
|
} else if (entityType == "software") {
|
|
|
|
return plural ? OpenaireEntities.SOFTWARE : OpenaireEntities.SOFTWARE_SINGULAR;
|
|
|
|
} else if (entityType == "other" || entityType == "orp") {
|
|
|
|
return plural ? OpenaireEntities.OTHER : OpenaireEntities.OTHER_SINGULAR;
|
|
|
|
} else if (entityType == "result") {
|
|
|
|
return plural ? OpenaireEntities.RESULTS : OpenaireEntities.RESULT;
|
|
|
|
} else if (entityType == "project") {
|
|
|
|
return plural ? OpenaireEntities.PROJECTS : OpenaireEntities.PROJECT;
|
|
|
|
} else if (entityType == "organization") {
|
|
|
|
return plural ? OpenaireEntities.ORGANIZATIONS : OpenaireEntities.ORGANIZATION;
|
|
|
|
} else if (entityType == "dataprovider") {
|
|
|
|
return plural ? OpenaireEntities.DATASOURCES : OpenaireEntities.DATASOURCE;
|
|
|
|
} else if (entityType == "service") {
|
|
|
|
return plural ? OpenaireEntities.SERVICES : OpenaireEntities.SERVICE;
|
|
|
|
} else if (entityType == "community") {
|
|
|
|
return plural ? OpenaireEntities.COMMUNITIES : OpenaireEntities.COMMUNITY;
|
|
|
|
}
|
|
|
|
return entityType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static getEntityFileName(entityType: string): string {
|
|
|
|
if (entityType == "publication" || entityType == "publications") {
|
|
|
|
return OpenaireEntities.PUBLICATIONS_FILE;
|
|
|
|
} else if (entityType == "dataset" || entityType == "datasets") {
|
|
|
|
return OpenaireEntities.DATASETS_FILE;
|
|
|
|
} else if (entityType == "software") {
|
|
|
|
return OpenaireEntities.SOFTWARE_FILE;
|
|
|
|
} else if (entityType == "other" || entityType == "orp") {
|
|
|
|
return OpenaireEntities.OTHER_FILE;
|
|
|
|
} else if (entityType == "result" || entityType == "results") {
|
|
|
|
return OpenaireEntities.RESULTS_FILE;
|
|
|
|
} else if (entityType == "project" || entityType == "projects") {
|
|
|
|
return OpenaireEntities.PROJECTS_FILE;
|
|
|
|
} else if (entityType == "organization" || entityType == "organizations") {
|
|
|
|
return OpenaireEntities.ORGANIZATIONS_FILE;
|
|
|
|
} else if (entityType == "dataprovider" || entityType == "dataproviders" || entityType == "datasource" || entityType == "datasources") {
|
|
|
|
return OpenaireEntities.DATASOURCES_FILE;
|
|
|
|
} else if (entityType == "service" || entityType == "services") {
|
|
|
|
return OpenaireEntities.SERVICES_FILE;
|
|
|
|
}
|
|
|
|
return entityType.toLowerCase().replace(" ", "-");
|
|
|
|
}
|
2022-06-09 10:21:25 +02:00
|
|
|
|
|
|
|
public static getStakeholderType(stakeholderType: string, plural: boolean = false): string {
|
|
|
|
if(stakeholderType == "funder") {
|
|
|
|
return plural ? StakeholderEntities.FUNDERS : StakeholderEntities.FUNDER;
|
|
|
|
} else if(stakeholderType == "ri") {
|
|
|
|
return plural ? StakeholderEntities.RIS : StakeholderEntities.RI;
|
|
|
|
} else if(stakeholderType == "organization") {
|
|
|
|
return plural ? StakeholderEntities.ORGANIZATIONS : StakeholderEntities.ORGANIZATION;
|
|
|
|
} else if(stakeholderType == "project") {
|
|
|
|
return plural ? StakeholderEntities.PROJECTS : StakeholderEntities.PROJECT;
|
|
|
|
}
|
|
|
|
return stakeholderType;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|