yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
export class UtilSOP {
  public static readonly dayArray: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
 
  /**
   * Format the date into string format (1-Jan-2021).
   *
   * @param date Date to format.
   * @returns The date string in format (1-Jan-2021).
   */
  public static getDateString(date: Date): string {
    const actualDateDay = date.getDate(); // Careful not to use getDay() as that returns day of the week
    const actualDateMonthShort = date.toLocaleString('en-us', { month: 'short' }); // Format = Jan, Feb ....
    const actualDateYear = date.getFullYear();
    const actualDateStr = `${actualDateDay}-${actualDateMonthShort}-${actualDateYear}`;
 
    return actualDateStr;
  }
 
  public static getDaysString(dayNumbers: number[]): string[] {
    return dayNumbers.map((x: number) => UtilSOP.dayArray[x]);
  }
 
  public static getInterfaceKeysAsStringArray(interfaceObj: any): string[] {
    const arr: string[] = [];
    for (const [key, _value] of Object.entries(interfaceObj)) {
      arr.push(key);
    }
 
    return arr;
  }
 
  public static getInterfaceObjectAsKeyValueArray(interfaceObj: any): string[] {
    const arr: string[] = [];
    for (const [key, value] of Object.entries(interfaceObj)) {
      arr.push(`${key} = "${value}"`);
    }
 
    return arr;
  }
 
  /**
   * More precise rounding of numbers.
   * https://www.jacklmoore.com/notes/rounding-in-javascript/
   *
   * @param num Number to round
   * @param decimals Number of decimals to round
   * @returns Rounded number
   */
  public static roundNumber(num: number, decimals: number): number {
    const a = Number(`${num}e${decimals}`);
 
    return Number(`${Math.round(a)}e-${decimals}`);
  }
 
  /**
   * Retrieve all values (ignoring the keys) from the array of interface and output the values as string array.
   *
   * @param arrKeyValues Array of interface such as [{Name: X1}, {Name: X2}]
   * @returns The string array in the form [X1, X2]
   */
  public static transformInterfaceValuesToStringArray(arrKeyValues: any[]): string[] {
    const values: string[] = [];
 
    // For each object in the array, retrieve the values delimited by comma
    // Example: [{Name: X1}, {Name: X2}] => [X1, X2]
    for (const keyValue of arrKeyValues) {
      for (const [, entryvalue] of Object.entries(keyValue)) {
        values.push(entryvalue as string);
      }
    }
    return values;
  }
}