Project

General

Profile

1
import {UrlSegment} from '@angular/router';
2
import {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
  public static urlPrefix(url: string): string {
191
    if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//")) {
192
      return "";
193
    } else {
194
      return "//";
195
    }
196
  }
197
  
198
  public static quote(params: string): string {
199
    return '"' + params + '"';
200
  }
201
  
202
  public static unquote(params: string): string {
203
    if (params.length > 2 && (params[0] == '"' && params[params.length - 1] == '"') || (params[0] == "'" && params[params.length - 1] == "'")) {
204
      params = params.substring(1, params.length - 1);
205
    }
206
    return params;
207
  }
208
  
209
  public static URIEncode(params: string): string {
210
    return encodeURIComponent(params);
211
  }
212
  
213
  public static URIDecode(params: string): string {
214
    return decodeURIComponent(params);
215
  }
216
  
217
  public static b64DecodeUnicode(str) {
218
    return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) {
219
      return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
220
    }).join(''));
221
  }
222
  
223
  private emailValidator(email: any): boolean {
224
    return !!email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$");
225
  }
226
  
227
  public static urlValidator(): ValidatorFn {
228
    return  Validators.pattern('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.' +
229
      '[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.' +
230
      '[a-zA-Z0-9]+\.[^\s]{2,}');
231
  }
232
  
233
  public static sliceString(mystr, size: number): string {
234
    const sliced = String(mystr).substr(0, size);
235
    return sliced + (String(mystr).length > size ? '...' : '');
236
  }
237
  
238
  /**
239
   * Splits a text to words base on a list of separators. Returns the words of the text including the separators.
240
   * DO NOT TOUCH, IT WORKS
241
   *
242
   * @param text
243
   * @param separators
244
   */
245
  public static split(text: string, separators: string[]): string[] {
246
    let words: (string | string[])[] = [text];
247
    separators.forEach(separator => {
248
      words.forEach((word, index) => {
249
        if (typeof word === "string" && separators.indexOf(word) === -1) {
250
          let tokens: string[] = word.split(separator).filter(value => value !== '');
251
          if (tokens.length > 1) {
252
            words[index] = [];
253
            tokens.forEach((token, i) => {
254
              (<string[]>(words[index])).push(token);
255
              if (i !== (tokens.length - 1)) {
256
                (<string[]>(words[index])).push(separator);
257
              }
258
            });
259
          }
260
        }
261
      });
262
      words = [].concat.apply([], words);
263
    });
264
    return <string []>words;
265
  }
266
  
267
  public static capitalize(value: string): string {
268
    return value.charAt(0).toUpperCase() + value.slice(1);
269
  }
270
  
271
  /**
272
   * Checks if a text contains a word
273
   */
274
  public static containsWord(text: string, word: string):boolean {
275
    return (text && text.toLowerCase().includes(word));
276
  }
277
  
278
  public static URLSegmentsToPath(segments: UrlSegment[]): string {
279
    let path = '';
280
    segments.forEach(route => {
281
      path += '/' + route.path;
282
    })
283
    return path;
284
  }
285
  
286
  public static isEuropeanCountry(country: string) {
287
    let countries = ["Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
288
      "Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece", "Hungary", "Iceland", "Ireland",
289
      "Italy", "Kosovo", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "The Netherlands", "Norway", "Poland",
290
      "Portugal", "Romania", "Russia", "San Marino", "Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City",
291
    ];
292
    return (country && countries.indexOf(country) != -1);
293
  }
294
  public static isOpenAIREID(id:string){
295
    if(id && id.length == 46){
296
      let exp1 = /^.{12}::([0-9a-z]{32})$/g;
297
      return (id.match(exp1)!=null);
298
    }
299
    return false;
300
  }
301
}
(21-21/21)