1
|
import {UrlSegment} from '@angular/router';
|
2
|
import {AbstractControl, ValidatorFn, Validators} from "@angular/forms";
|
3
|
|
4
|
export class Dates {
|
5
|
public static yearMin = 1800;
|
6
|
public static yearMax = (new Date().getFullYear()) + 10;
|
7
|
public static currentYear = (new Date().getFullYear());
|
8
|
|
9
|
public static isValidYear(yearString, yearMin=this.yearMin, yearMax=this.yearMax) {
|
10
|
// First check for the pattern
|
11
|
if (!/^\d{4}$/.test(yearString))
|
12
|
return false;
|
13
|
var year = parseInt(yearString, 10);
|
14
|
|
15
|
// Check the ranges of month and year
|
16
|
if (year < yearMin || year > yearMax)
|
17
|
return false;
|
18
|
return true;
|
19
|
}
|
20
|
|
21
|
//format YYYY-MM-DD
|
22
|
public static isValidDate(dateString: string) {
|
23
|
// First check for the pattern
|
24
|
if (!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(dateString))
|
25
|
return false;
|
26
|
|
27
|
// Parse the date parts to integers
|
28
|
var parts = dateString.split("-");
|
29
|
var day = parseInt(parts[2], 10);
|
30
|
var month = parseInt(parts[1], 10);
|
31
|
var year = parseInt(parts[0], 10);
|
32
|
if (!this.isValidYear(parts[0])) {
|
33
|
return false;
|
34
|
}
|
35
|
|
36
|
// Check the ranges of month and year
|
37
|
if (month == 0 || month > 12)
|
38
|
return false;
|
39
|
|
40
|
var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
41
|
|
42
|
// Adjust for leap years
|
43
|
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
|
44
|
monthLength[1] = 29;
|
45
|
|
46
|
// Check the range of the day
|
47
|
return day > 0 && day <= monthLength[month - 1];
|
48
|
|
49
|
}
|
50
|
|
51
|
public static getDateToday(): Date {
|
52
|
var myDate = new Date();
|
53
|
return myDate;
|
54
|
|
55
|
}
|
56
|
|
57
|
public static getDateToString(myDate: Date): string {
|
58
|
var date: string = myDate.getFullYear() + "-";
|
59
|
date += ((myDate.getMonth() + 1) < 10) ? "0" + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
|
60
|
date += "-";
|
61
|
date += (myDate.getDate() < 10) ? "0" + myDate.getDate() : myDate.getDate();
|
62
|
return date;
|
63
|
|
64
|
}
|
65
|
|
66
|
public static getDateXMonthsAgo(x: number): Date {
|
67
|
var myDate = new Date();
|
68
|
myDate.setMonth(myDate.getMonth() - x);
|
69
|
return myDate;
|
70
|
|
71
|
}
|
72
|
|
73
|
public static getDateXYearsAgo(x: number): Date {
|
74
|
var myDate = new Date();
|
75
|
myDate.setFullYear(myDate.getFullYear() - x);
|
76
|
return myDate;
|
77
|
|
78
|
}
|
79
|
|
80
|
public static getDateFromString(date: string): Date {
|
81
|
|
82
|
var myDate = new Date();
|
83
|
myDate.setFullYear(+date.substring(0, 4));
|
84
|
myDate.setMonth(((date.length > 5) ? (+date.substring(5, 7) - 1) : (0)));
|
85
|
myDate.setDate(((date.length > 8) ? (+date.substring(8, 11)) : (1)));
|
86
|
return myDate;
|
87
|
|
88
|
}
|
89
|
|
90
|
public static getDate(dateString: string): Date {
|
91
|
let date = new Date(dateString);
|
92
|
if (Object.prototype.toString.call(date) === "[object Date]") {
|
93
|
if (isNaN(date.getTime())) {
|
94
|
return null;
|
95
|
} else {
|
96
|
return date;
|
97
|
}
|
98
|
} else {
|
99
|
return null;
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
|
104
|
export class DOI {
|
105
|
|
106
|
public static getDOIsFromString(str: string): string[] {
|
107
|
return Identifier.getDOIsFromString(str);
|
108
|
}
|
109
|
|
110
|
public static isValidDOI(str: string): boolean {
|
111
|
return Identifier.isValidDOI(str);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
export class Identifier {
|
116
|
class: "doi" | "pmc" | "pmid" | "handle" | "ORCID" = null;
|
117
|
id: string;
|
118
|
|
119
|
public static getDOIsFromString(str: string): string[] {
|
120
|
var DOIs: string[] = [];
|
121
|
var words: string[] = str.split(" ");
|
122
|
|
123
|
for (var i = 0; i < words.length; i++) {
|
124
|
if (DOI.isValidDOI(words[i]) && DOIs.indexOf(words[i]) == -1) {
|
125
|
DOIs.push(words[i]);
|
126
|
}
|
127
|
}
|
128
|
return DOIs;
|
129
|
}
|
130
|
|
131
|
public static getIdentifiersFromString(str: string): Identifier[] {
|
132
|
let identifiers: Identifier[] = [];
|
133
|
let words: string[] = str.split(" ");
|
134
|
|
135
|
for (let id of words) {
|
136
|
if (id.length > 0) {
|
137
|
let identifier: Identifier = this.getIdentifierFromString(id);
|
138
|
if(identifier) {
|
139
|
identifiers.push(identifier);
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
return identifiers;
|
144
|
}
|
145
|
|
146
|
public static getIdentifierFromString(pid: string): Identifier {
|
147
|
if (Identifier.isValidDOI(pid)) {
|
148
|
return {"class": "doi", "id": pid};
|
149
|
} else if (Identifier.isValidORCID(pid)) {
|
150
|
return {"class": "ORCID", "id": pid};
|
151
|
} else if (Identifier.isValidPMCID(pid)) {
|
152
|
return {"class": "pmc", "id": pid};
|
153
|
} else if (Identifier.isValidPMID(pid)) {
|
154
|
return {"class": "pmid", "id": pid};
|
155
|
} else if (Identifier.isValidHANDLE(pid)) {
|
156
|
return {"class": "handle", "id": pid};
|
157
|
}
|
158
|
return null;
|
159
|
}
|
160
|
|
161
|
public static isValidDOI(str: string): boolean {
|
162
|
var exp1 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])\S)+)\b/g
|
163
|
var exp2 = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&\'<>])[[:graph:]])+)\b/g
|
164
|
return (str.match(exp1) != null || str.match(exp2) != null);
|
165
|
}
|
166
|
|
167
|
public static isValidORCID(str: string): boolean {
|
168
|
let exp = /\b\d{4}-\d{4}-\d{4}-(\d{3}X|\d{4})\b/g;
|
169
|
return str.match(exp) != null;
|
170
|
}
|
171
|
|
172
|
public static isValidPMID(str: string): boolean {
|
173
|
let exp = /^\d*$/g;
|
174
|
return str.match(exp) != null;
|
175
|
|
176
|
}
|
177
|
|
178
|
public static isValidPMCID(str: string): boolean {
|
179
|
let exp = /^(PMC\d{7})$/g;
|
180
|
return str.match(exp) != null;
|
181
|
}
|
182
|
|
183
|
public static isValidHANDLE(str: string): boolean {
|
184
|
let exp = /^[0-9a-zA-Z-]*\/[0-9a-zA-Z-]*$/g;
|
185
|
return str.match(exp) != null;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
export class StringUtils {
|
190
|
|
191
|
public static urlRegex = 'https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.' +
|
192
|
'[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.' +
|
193
|
'[a-zA-Z0-9]+\.[^\s]{2,}';
|
194
|
|
195
|
public static urlPrefix(url: string): string {
|
196
|
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//")) {
|
197
|
return "";
|
198
|
} else {
|
199
|
return "//";
|
200
|
}
|
201
|
}
|
202
|
|
203
|
public static quote(params: string): string {
|
204
|
return '"' + params + '"';
|
205
|
}
|
206
|
|
207
|
public static unquote(params: string): string {
|
208
|
if (params.length > 2 && (params[0] == '"' && params[params.length - 1] == '"') || (params[0] == "'" && params[params.length - 1] == "'")) {
|
209
|
params = params.substring(1, params.length - 1);
|
210
|
}
|
211
|
return params;
|
212
|
}
|
213
|
|
214
|
public static URIEncode(params: string): string {
|
215
|
return encodeURIComponent(params);
|
216
|
}
|
217
|
|
218
|
public static URIDecode(params: string): string {
|
219
|
return decodeURIComponent(params);
|
220
|
}
|
221
|
|
222
|
public static validateEmails(emails: string): boolean {
|
223
|
return (emails.split(',')
|
224
|
.map(email => Validators.email(<AbstractControl>{ value: email.trim() }))
|
225
|
.find(_ => _ !== null) === undefined);
|
226
|
}
|
227
|
|
228
|
public static b64DecodeUnicode(str) {
|
229
|
return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
|
230
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
231
|
}).join(''));
|
232
|
}
|
233
|
|
234
|
private emailValidator(email: any): boolean {
|
235
|
return !!email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$");
|
236
|
}
|
237
|
|
238
|
public static isValidUrl(url: string): boolean {
|
239
|
return new RegExp(this.urlRegex).test(url);
|
240
|
}
|
241
|
|
242
|
public static urlValidator(): ValidatorFn {
|
243
|
return Validators.pattern(this.urlRegex);
|
244
|
}
|
245
|
|
246
|
public static sliceString(mystr, size: number): string {
|
247
|
const sliced = String(mystr).substr(0, size);
|
248
|
return sliced + (String(mystr).length > size ? '...' : '');
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Splits a text to words base on a list of separators. Returns the words of the text including the separators.
|
253
|
* DO NOT TOUCH, IT WORKS
|
254
|
*
|
255
|
* @param text
|
256
|
* @param separators
|
257
|
*/
|
258
|
public static split(text: string, separators: string[]): string[] {
|
259
|
let words: (string | string[])[] = [text];
|
260
|
separators.forEach(separator => {
|
261
|
words.forEach((word, index) => {
|
262
|
if (typeof word === "string" && separators.indexOf(word) === -1) {
|
263
|
let tokens: string[] = word.split(separator).filter(value => value !== '');
|
264
|
if (tokens.length > 1) {
|
265
|
words[index] = [];
|
266
|
tokens.forEach((token, i) => {
|
267
|
(<string[]>(words[index])).push(token);
|
268
|
if (i !== (tokens.length - 1)) {
|
269
|
(<string[]>(words[index])).push(separator);
|
270
|
}
|
271
|
});
|
272
|
}
|
273
|
}
|
274
|
});
|
275
|
words = [].concat.apply([], words);
|
276
|
});
|
277
|
return <string []>words;
|
278
|
}
|
279
|
|
280
|
public static capitalize(value: string): string {
|
281
|
return value.charAt(0).toUpperCase() + value.slice(1);
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* Checks if a text contains a word
|
286
|
*/
|
287
|
public static containsWord(text: string, word: string):boolean {
|
288
|
return (text && text.toLowerCase().includes(word));
|
289
|
}
|
290
|
|
291
|
public static URLSegmentsToPath(segments: UrlSegment[]): string {
|
292
|
let path = '';
|
293
|
segments.forEach(route => {
|
294
|
path += '/' + route.path;
|
295
|
})
|
296
|
return path;
|
297
|
}
|
298
|
|
299
|
public static isEuropeanCountry(country: string) {
|
300
|
let countries = ["Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
|
301
|
"Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece", "Hungary", "Iceland", "Ireland",
|
302
|
"Italy", "Kosovo", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "The Netherlands", "Norway", "Poland",
|
303
|
"Portugal", "Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City",
|
304
|
];
|
305
|
return (country && countries.indexOf(country) != -1);
|
306
|
}
|
307
|
public static isOpenAIREID(id:string){
|
308
|
if(id && id.length == 46){
|
309
|
let exp1 = /^.{12}::([0-9a-z]{32})$/g;
|
310
|
return (id.match(exp1)!=null);
|
311
|
}
|
312
|
return false;
|
313
|
}
|
314
|
}
|