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-02-17 15:19:14 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static isValidYear(yearString){
|
|
|
|
// First check for the pattern
|
|
|
|
if(!/^\d{4}$/.test(yearString))
|
|
|
|
return false;
|
|
|
|
var year = parseInt(yearString, 10);
|
|
|
|
|
|
|
|
// Check the ranges of month and year
|
2020-02-17 15:19:14 +01:00
|
|
|
if(year < this.yearMin || year > this.yearMax )
|
2017-12-19 13:53:46 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//format YYYY-MM-DD
|
|
|
|
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];
|
|
|
|
|
|
|
|
}
|
|
|
|
public static getDateToday():Date{
|
|
|
|
var myDate = new Date();
|
|
|
|
return myDate;
|
|
|
|
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
public static getDateXMonthsAgo(x:number):Date{
|
|
|
|
var myDate = new Date();
|
|
|
|
myDate.setMonth(myDate.getMonth() - x);
|
|
|
|
return myDate;
|
|
|
|
|
|
|
|
}
|
|
|
|
public static getDateXYearsAgo(x:number):Date{
|
|
|
|
var myDate = new Date();
|
|
|
|
myDate.setFullYear(myDate.getFullYear() - x);
|
|
|
|
return myDate;
|
|
|
|
|
|
|
|
}
|
|
|
|
public static getDateFromString(date:string):Date{
|
|
|
|
|
|
|
|
var myDate = new Date();
|
|
|
|
myDate.setFullYear(+date.substring(0,4));
|
2019-07-24 14:46:29 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DOI{
|
|
|
|
|
2020-03-03 15:32:43 +01:00
|
|
|
public static getDOIsFromString(str:string):string[]{
|
|
|
|
return Identifier.getDOIsFromString(str);
|
|
|
|
}
|
|
|
|
public static isValidDOI(str:string):boolean{
|
|
|
|
return Identifier.isValidDOI(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Identifier{
|
|
|
|
class: "doi" | "pmc" | "pmid" | "handle"|"ORCID" =null;
|
|
|
|
id:string;
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
public static getDOIsFromString(str:string):string[]{
|
|
|
|
var DOIs:string[] = [];
|
|
|
|
var words:string[] = str.split(" ");
|
|
|
|
|
|
|
|
for(var i=0; i< words.length; i++){
|
2020-03-03 15:32:43 +01:00
|
|
|
if(DOI.isValidDOI(words[i]) && DOIs.indexOf(words[i]) == -1){
|
|
|
|
DOIs.push(words[i]);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
return DOIs;
|
|
|
|
}
|
2020-03-03 15:32:43 +01:00
|
|
|
public static getIdentifiersFromString(str:string):Identifier[]{
|
|
|
|
let identifiers:Identifier[] = [];
|
|
|
|
let words:string[] = str.split(" ");
|
|
|
|
|
|
|
|
for(let id of words){
|
|
|
|
if(id.length > 0 ) {
|
|
|
|
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;
|
|
|
|
}
|
2017-12-19 13:53:46 +01: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
|
|
|
|
return (str.match(exp1)!=null || str.match(exp2)!=null);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
public static isValidPMID(str:string):boolean{
|
|
|
|
let exp =/^\d*$/g;
|
|
|
|
return str.match(exp)!=null;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-03-03 15:32:43 +01:00
|
|
|
}
|
|
|
|
public static isValidPMCID(str:string):boolean{
|
|
|
|
let exp =/^(PMC\d{7})$/g;
|
|
|
|
return str.match(exp)!=null;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-03-03 15:32:43 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
export class StringUtils{
|
|
|
|
public static quote(params: string):string {
|
|
|
|
return '"'+params+'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
public static URIEncode(params: string):string {
|
|
|
|
return encodeURIComponent(params);
|
|
|
|
}
|
|
|
|
public static URIDecode(params: string):string {
|
|
|
|
return decodeURIComponent(params);
|
|
|
|
}
|
|
|
|
public static b64DecodeUnicode(str) {
|
|
|
|
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
|
|
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
|
|
}).join(''));
|
|
|
|
}
|
2018-07-09 14:07:54 +02:00
|
|
|
private emailValidator(email : any): boolean {
|
|
|
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
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) => {
|
|
|
|
if(typeof word === "string" && separators.indexOf(word) === -1) {
|
|
|
|
let tokens: string[] = word.split(separator).filter(value => value !== '');
|
|
|
|
if(tokens.length > 1) {
|
|
|
|
words[index] = [];
|
|
|
|
tokens.forEach((token, i) => {
|
|
|
|
(<string[]>(words[index])).push(token);
|
|
|
|
if(i !== (tokens.length - 1)) {
|
|
|
|
(<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);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-09-05 16:02:18 +02:00
|
|
|
public static URLSegmentsToPath(segments: UrlSegment[]): string {
|
|
|
|
let path = '';
|
|
|
|
segments.forEach(route => {
|
|
|
|
path += '/' + route.path;
|
|
|
|
})
|
|
|
|
return path;
|
|
|
|
}
|
2019-12-03 11:45:28 +01:00
|
|
|
public static isEuropeanCountry(country:string){
|
|
|
|
let countries =["Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
|
|
|
|
"Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece", "Hungary", "Iceland", "Ireland",
|
|
|
|
"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",
|
|
|
|
];
|
|
|
|
return (country && countries.indexOf(country)!= -1);
|
|
|
|
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|