assessment-model/src/app/utils.ts
2023-12-01 17:11:31 +00:00

151 lines
3.7 KiB
TypeScript

import { Rating } from "./db/schema/property";
export function convertDaysToWorkingWeeks(days: number | null) {
if (days === null) {
return "-";
}
const workingDaysPerWeek = 5;
// Convert days to working weeks
const workingWeeks = days / workingDaysPerWeek;
// Determine the range
let lowerBound = Math.floor(workingWeeks);
let upperBound = Math.ceil(workingWeeks);
// Adjust if the fraction is very small, you might not count it as a full extra week
if (workingWeeks - lowerBound < 0.2) {
upperBound = lowerBound;
}
if (lowerBound === 0 && upperBound === 1) {
return "1 week";
}
if (lowerBound === 1 && upperBound === 1) {
return "1-2 weeks";
}
// Format the output
return lowerBound === upperBound
? `${lowerBound} weeks`
: `${lowerBound}-${upperBound} weeks`;
}
export const getEpcColorClass = (letter: string) => {
switch (letter.toUpperCase()) {
case "A":
return "bg-epc_a";
case "B":
return "bg-epc_b";
case "C":
return "bg-epc_c";
case "D":
return "bg-epc_d";
case "E":
return "bg-epc_e";
case "F":
return "bg-epc_f";
case "G":
return "bg-epc_d";
default:
return "bg-gray-500";
}
};
export const getRating = (rating: number | null): Rating => {
if (rating == null) {
return "N/A";
}
if (![1, 2, 3, 4, 5].includes(rating)) {
throw new Error("Rating should be between 1 and 5.");
}
const ratingMap: { [key in 1 | 2 | 3 | 4 | 5]: string } = {
1: "Very poor",
2: "Poor",
3: "Average",
4: "Good",
5: "Very good",
};
return ratingMap[rating as 1 | 2 | 3 | 4 | 5] as Rating; // You can assert that rating is one of these values since you've already checked it
};
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 >= 92) {
return "A";
} else if (sapPoints >= 81) {
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 | Date): 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(1))
: Number(number.toPrecision(4));
const formattedNumber: string = (
roundedNumber / Math.pow(1000, suffixIndex)
).toFixed(1);
return formattedNumber + suffixes[suffixIndex];
}
export function roundToDecimalPlaces(
number: number,
decimalPlaces: number
): number {
const factor = 10 ** decimalPlaces;
return Math.round(number * factor) / factor;
}