2019-09-05 16:02:18 +02:00
|
|
|
import {UrlSegment} from '@angular/router';
|
|
|
|
|
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());
|
2020-07-07 15:50:49 +02: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
|
2020-07-07 15:50:49 +02:00
|
|
|
if (year < yearMin || year > yearMax)
|
2020-06-05 14:56:28 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
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;
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
class: "doi" | "pmc" | "pmid" | "handle" | "ORCID" = null;
|
|
|
|
id: string;
|
|
|
|
|
|
|
|
public static getDOIsFromString(str: string): string[] {
|
|
|
|
var DOIs: string[] = [];
|
|
|
|
var words: string[] = str.split(" ");
|
|
|
|
|
|
|
|
for (var i = 0; i < words.length; i++) {
|
|
|
|
if (DOI.isValidDOI(words[i]) && DOIs.indexOf(words[i]) == -1) {
|
2020-03-03 15:32:43 +01:00
|
|
|
DOIs.push(words[i]);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
return DOIs;
|
|
|
|
}
|
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) {
|
2020-03-03 15:32:43 +01:00
|
|
|
if (Identifier.isValidDOI(id)) {
|
|
|
|
identifiers.push({"class": "doi", "id": id})
|
|
|
|
} else if (Identifier.isValidORCID(id)) {
|
|
|
|
identifiers.push({"class": "ORCID", "id": id})
|
|
|
|
} else if (Identifier.isValidPMCID(id)) {
|
|
|
|
identifiers.push({"class": "pmc", "id": id})
|
|
|
|
} else if (Identifier.isValidPMID(id)) {
|
|
|
|
identifiers.push({"class": "pmid", "id": id})
|
|
|
|
} else if (Identifier.isValidHANDLE(id)) {
|
|
|
|
identifiers.push({"class": "handle", "id": id})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return identifiers;
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
public static isValidDOI(str: string): boolean {
|
2020-03-03 15:32:43 +01:00
|
|
|
var exp1 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])\S)+)\b/g
|
|
|
|
var exp2 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])[[:graph:]])+)\b/g
|
2020-06-05 14:56:28 +02:00
|
|
|
return (str.match(exp1) != null || str.match(exp2) != 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;
|
|
|
|
return str.match(exp) != null;
|
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
|
|
|
}
|
|
|
|
}
|
2020-06-05 14:56:28 +02:00
|
|
|
|
|
|
|
export class StringUtils {
|
2020-05-07 13:28:55 +02:00
|
|
|
public static urlPrefix(url: string): string {
|
2020-06-05 14:56:28 +02:00
|
|
|
if (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
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static containsWord(text: string, word: string):boolean {
|
|
|
|
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
|
|
|
}
|
|
|
|
public static isOpenAIREID(id:string){
|
|
|
|
if(id && id.length == 46){
|
|
|
|
let exp1 = /^.{12}::([0-9a-z]{32})$/g;
|
|
|
|
return (id.match(exp1)!=null);
|
|
|
|
}
|
|
|
|
return false;
|
2020-06-05 14:56:28 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|