From f94e90dc3867677e065acfb94927a27c1fbd6029 Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Sun, 12 Jan 2025 11:53:04 +0530 Subject: [PATCH] remove unused utils --- src/Utils/utils.ts | 259 +---------------------------------- src/components/Form/Utils.ts | 20 --- 2 files changed, 2 insertions(+), 277 deletions(-) diff --git a/src/Utils/utils.ts b/src/Utils/utils.ts index 0d918a4398e..5b6af0ac5a6 100644 --- a/src/Utils/utils.ts +++ b/src/Utils/utils.ts @@ -1,84 +1,15 @@ import { differenceInMinutes, format } from "date-fns"; import html2canvas from "html2canvas"; -import { t } from "i18next"; -import { toast } from "sonner"; import { AREACODES, IN_LANDLINE_AREA_CODES } from "@/common/constants"; import phoneCodesJson from "@/common/static/countryPhoneAndFlags.json"; import dayjs from "@/Utils/dayjs"; import { Time } from "@/Utils/types"; -import { DoseRange, Timing } from "@/types/emr/medicationRequest"; import { Patient } from "@/types/emr/newPatient"; import { PatientModel } from "@/types/emr/patient"; -import { Code } from "@/types/questionnaire/code"; import { Quantity } from "@/types/questionnaire/quantity"; -interface ApacheParams { - age: number; - organFailure: boolean; - temperatureC: number; - heartRate: number; - respiratoryRate: number; - sodium: number; - potassium: number; - creatinine: number; - acuteRenalFailure: boolean; - hematocrit: number; - wbcCount: number; - glasgowComaScore: number; - fiO2: number; -} - -export const calculateApache2Score = (apacheParams: ApacheParams): number => { - const { - age, - organFailure, - temperatureC, - heartRate, - respiratoryRate, - sodium, - potassium, - creatinine, - acuteRenalFailure, - hematocrit, - wbcCount, - glasgowComaScore, - fiO2, - } = apacheParams; - - const ageScore = age < 65 ? 1 : 0; - const organFailureScore = organFailure ? 1 : 0; - const temperatureScore = temperatureC < 37.5 ? 1 : 0; - const heartRateScore = heartRate < 60 ? 1 : 0; - const respiratoryRateScore = respiratoryRate < 12 ? 1 : 0; - const sodiumScore = sodium < 135 ? 1 : 0; - const potassiumScore = potassium < 3.5 ? 1 : 0; - const creatinineScore = creatinine < 0.7 ? 1 : 0; - const acuteRenalFailureScore = acuteRenalFailure ? 1 : 0; - const hematocritScore = hematocrit < 0.45 ? 1 : 0; - const wbcCountScore = wbcCount < 10 ? 1 : 0; - const glasgowComaScoreScore = glasgowComaScore < 6 ? 1 : 0; - const fiO2Score = fiO2 < 0.7 ? 1 : 0; - - const totalScore = - ageScore + - organFailureScore + - temperatureScore + - heartRateScore + - respiratoryRateScore + - sodiumScore + - potassiumScore + - creatinineScore + - acuteRenalFailureScore + - hematocritScore + - wbcCountScore + - glasgowComaScoreScore + - fiO2Score; - - return totalScore; -}; - const DATE_FORMAT = "DD/MM/YYYY"; const TIME_FORMAT = "hh:mm A"; const DATE_TIME_FORMAT = `${TIME_FORMAT}; ${DATE_FORMAT}`; @@ -100,16 +31,10 @@ export const formatDateTime = (date: DateLike, format?: string) => { return obj.format(DATE_TIME_FORMAT); }; -export const formatDate = (date: DateLike, format = DATE_FORMAT) => - formatDateTime(date, format); - export const formatTimeShort = (time: Time) => { return format(new Date(`1970-01-01T${time}`), "h:mm a").replace(":00", ""); }; -export const formatTime = (date: DateLike, format = TIME_FORMAT) => - formatDateTime(date, format); - export const relativeDate = (date: DateLike, withoutSuffix = false) => { const obj = dayjs(date); return `${obj.fromNow(withoutSuffix)}${ @@ -167,11 +92,6 @@ function _isAppleDevice() { */ export const isAppleDevice = _isAppleDevice(); -/** - * `true` if device is an iOS device, else `false` - */ -export const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); - /** * Conditionally concatenate classes. An alternate replacement for `clsx`. * @@ -180,6 +100,8 @@ export const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); *
* // "md:flex p-0" * ``` + * + * @deprecated Use `cn` from `@/lib/utils` instead. */ export const classNames = (...classes: (string | boolean | undefined)[]) => { return classes.filter(Boolean).join(" "); @@ -193,42 +115,6 @@ export const getPincodeDetails = async (pincode: string, apiKey: string) => { return data.records[0]; }; -export const includesIgnoreCase = (str1: string, str2: string) => { - if (!str1 || !str2) return false; - const lowerCaseStr1 = str1.toLowerCase(); - const lowerCaseStr2 = str2.toLowerCase(); - return ( - lowerCaseStr1.includes(lowerCaseStr2) || - lowerCaseStr2.includes(lowerCaseStr1) - ); -}; - -export const getExperienceSuffix = (date?: Date) => { - if (!date) return "0 Years"; - - const today = new Date(); - - let m = (today.getFullYear() - date.getFullYear()) * 12; - m -= date.getMonth(); - m += today.getMonth(); - - let str = ""; - - const years = Math.floor(m / 12); - const months = m % 12; - - if (years) str += `${years} years `; - if (months) str += `${months} months`; - - return str; -}; - -export const formatCurrency = (price: number) => - price?.toLocaleString("en-IN", { - style: "currency", - currency: "INR", - }); - export const isUserOnline = (user: { last_login: DateLike }) => { return user.last_login ? dayjs().subtract(5, "minutes").isBefore(user.last_login) @@ -378,20 +264,6 @@ const getRelativeDateSuffix = (abbreviated: boolean) => { }; }; -export const patientAgeInYears = (obj: PatientModel) => { - const start = dayjs( - obj.date_of_birth - ? new Date(obj.date_of_birth) - : new Date(obj.year_of_birth!, 0, 1), - ); - - const end = dayjs( - obj.death_datetime ? new Date(obj.death_datetime) : new Date(), - ); - - return end.diff(start, "years"); -}; - export const formatPatientAge = ( obj: PatientModel | Patient, abbreviated = false, @@ -429,29 +301,6 @@ export const formatPatientAge = ( return `${day}${suffixes.day}`; }; -export const compareBy = (key: keyof T) => { - return (a: T, b: T) => { - return a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : 0; - }; -}; - -export const compareByDateString = (key: keyof T) => { - return (a: T, b: T) => { - const aV = new Date(a[key] as string); - const bV = new Date(b[key] as string); - return aV < bV ? -1 : aV > bV ? 1 : 0; - }; -}; - -export const isValidUrl = (url?: string) => { - try { - new URL(url ?? ""); - return true; - } catch { - return false; - } -}; - export const mergeQueryOptions = ( selected: T[], queryOptions: T[], @@ -466,13 +315,6 @@ export const mergeQueryOptions = ( ]; }; -export const properRoundOf = (value: number) => { - if (value % 1 === 0) { - return value.toFixed(); - } - return value.toFixed(2); -}; - /** * A utility method to format an array of string to human readable format. * @@ -492,58 +334,6 @@ export const humanizeStrings = (strings: readonly string[], empty = "") => { return `${items.reverse().join(", ")} and ${last}`; }; -export type ValueDescription = { - till?: number; - text: React.ReactNode; - className?: string; - color?: string; -}; - -export const getValueDescription = ( - valueDescriptions: ValueDescription[], - value: number, -) => { - return valueDescriptions.find((vd) => (vd.till || 0) >= (value || 0)); -}; - -export const rangeValueDescription = (range: { - low?: number; - high?: number; -}) => { - const results: ValueDescription[] = []; - - if (range.low != null) { - results.push({ - till: range.low, - text: "Low", - className: "text-red-500", - }); - } - - results.push({ - till: range.high, - text: "Normal", - className: "text-green-500", - }); - - if (range.high != null) { - results.push({ - text: "High", - className: "text-red-500", - }); - } - - return results; -}; - -export const celsiusToFahrenheit = (celsius: number) => { - return (celsius * 9) / 5 + 32; -}; - -export const fahrenheitToCelsius = (fahrenheit: number) => { - return ((fahrenheit - 32) * 5) / 9; -}; - /** * Although same as `Objects.keys(...)`, this provides better type-safety. */ @@ -551,21 +341,6 @@ export const keysOf = (obj: T) => { return Object.keys(obj) as (keyof T)[]; }; -// Utility to check if a value is "empty" -export const isEmpty = (value: unknown) => { - return value === "" || value == undefined; -}; - -// equivalent to lodash omitBy -export function omitBy>( - obj: T, - predicate: (value: unknown) => boolean, -): Partial { - return Object.fromEntries( - Object.entries(obj).filter(([_, value]) => !predicate(value)), - ) as Partial; -} - export const properCase = (str: string) => { return str .split("_") @@ -580,33 +355,12 @@ export const getMonthStartAndEnd = (date: Date) => { }; }; -export const displayCode = (code?: Code) => { - if (!code) return "N/A"; - - return code.display ?? code.code; -}; - export const displayQuantity = (quantity?: Quantity) => { if (!quantity) return "N/A"; return [quantity.value ?? "N/A", quantity.unit].join(" "); }; -// TODO: make it generic -export const displayDoseRange = (range?: DoseRange) => { - if (!range) return "N/A"; - - return ([range.low, range.high] as Quantity[]) - .map(displayQuantity) - .join(" - "); -}; - -export const displayTiming = (timing?: Timing) => { - if (!timing || !timing.repeat) return "N/A"; - - return `${timing.repeat.frequency} every ${timing.repeat.period} ${timing.repeat.period_unit}`; -}; - /** * Returns hours and minutes between two dates. * @@ -642,15 +396,6 @@ export const saveElementAsImage = async (id: string, filename: string) => { link.click(); }; -export const copyToClipboard = async (content: string) => { - try { - await navigator.clipboard.writeText(content); - toast.success(t("copied_to_clipboard")); - } catch (err) { - toast.error(t("copying_is_not_allowed")); - } -}; - export const conditionalAttribute = ( condition: boolean, attributes: Record, diff --git a/src/components/Form/Utils.ts b/src/components/Form/Utils.ts index 457c3fc9dd3..21dc9d8b8aa 100644 --- a/src/components/Form/Utils.ts +++ b/src/components/Form/Utils.ts @@ -14,23 +14,3 @@ export type FormReducer = ( prevState: FormState, action: FormAction, ) => FormState; -export type FormDraft = { timestamp: number; form: FormDetails }; - -export const formReducer = ( - state: FormState, - action: FormAction, -): FormState => { - switch (action.type) { - case "set_form": - return { ...state, form: action.form }; - case "set_errors": - return { ...state, errors: action.errors }; - case "set_field": - return { - form: { ...state.form, [action.name]: action.value }, - errors: { ...state.errors, [action.name]: action.error }, - }; - case "set_state": - return action.state; - } -};