135 lines
3.7 KiB
TypeScript
135 lines
3.7 KiB
TypeScript
export class Dates {
|
|
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
|
|
if(year < 1000 || year > 3000 )
|
|
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));
|
|
myDate.setMonth(+date.substring(5,7)-1);
|
|
myDate.setDate(+date.substring(8,11))
|
|
return myDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class DOI{
|
|
|
|
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){
|
|
DOIs.push(words[i]);
|
|
}
|
|
}
|
|
return DOIs;
|
|
}
|
|
public static isValidDOI(str:string):boolean{
|
|
|
|
var exp1 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])\S)+)\b/g
|
|
var exp2 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])[[:graph:]])+)\b/g
|
|
if(str.match(exp1)!=null || str.match(exp2)!=null){
|
|
// console.log("It's a DOI");
|
|
return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
}
|
|
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(''));
|
|
}
|
|
private emailValidator(email : any): boolean {
|
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
}
|