assessment-model/src/app/utils.ts
2023-08-01 18:20:08 +01:00

75 lines
2 KiB
TypeScript

export const serializeBigInt = (_: any, value: any): string | any => {
// Simple utility function to convert BigInts to strings with JSON.stringify
return typeof value === "bigint" ? value.toString() : value;
};
export function sapToEpc(sapPoints: number): string {
if (sapPoints <= 0 || sapPoints > 100) {
throw new Error("SAP points should be between 1 and 100.");
}
if (sapPoints > 91) {
return "A";
} else if (sapPoints > 80) {
return "B";
} else if (sapPoints > 69) {
return "C";
} else if (sapPoints > 55) {
return "D";
} else if (sapPoints > 39) {
return "E";
} else if (sapPoints > 21) {
return "F";
} else {
return "G";
}
}
export function formatDateTime(dateTimeString: string): string {
// Create a new Date object
const dateTime = new Date(dateTimeString);
// Get the various parts of the date
const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
};
return dateTime.toLocaleDateString("en-GB", options);
}
export function formatNumber(number: number): string {
if (number === 0) {
return "0";
}
const suffixes: string[] = ["", "k", "m", "b", "t"];
const suffixIndex: number = Math.floor(Math.log10(Math.abs(number)) / 3);
// Check if the suffix index is within the available suffixes
if (suffixIndex >= suffixes.length) {
return number.toString(); // Return the number as is
}
// Check if the number is smaller and round to 2 decimal places
const roundedNumber: number =
Math.abs(number) < 1000
? Number(number.toFixed(2))
: Number(number.toPrecision(4));
const formattedNumber: string = (
roundedNumber / Math.pow(1000, suffixIndex)
).toString();
return formattedNumber + suffixes[suffixIndex];
}
export function roundToDecimalPlaces(
number: number,
decimalPlaces: number
): number {
const factor = 10 ** decimalPlaces;
return Math.round(number * factor) / factor;
}