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;
|
}
|
}
|