-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
removed lodash imports and dependencies and wrote js equivalents #9116
base: develop
Are you sure you want to change the base?
Changes from all commits
7f75a18
cc88800
f13a324
17931f8
de43d86
1a136f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { Stack, alert, defaultModules } from "@pnotify/core"; | ||
import * as PNotifyMobile from "@pnotify/mobile"; | ||
import { camelCase, startCase } from "lodash-es"; | ||
|
||
import { camelCase, startCase } from "./utils"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use absolute imports |
||
|
||
defaultModules.set(PNotifyMobile, {}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
import { PatientModel } from "@/components/Patient/models"; | ||
|
||
import { AREACODES, IN_LANDLINE_AREA_CODES } from "@/common/constants"; | ||
|
@@ -544,3 +546,46 @@ export const fahrenheitToCelsius = (fahrenheit: number) => { | |
export const keysOf = <T extends object>(obj: T) => { | ||
return Object.keys(obj) as (keyof T)[]; | ||
}; | ||
|
||
// Capitalize the first letter of each word in a string, handling edge cases | ||
export const startCase = (str: string): string => { | ||
if (!str) return ""; | ||
|
||
return str | ||
.toLowerCase() | ||
.replace(/\s+/g, " ") | ||
.trim() | ||
.split(" ") | ||
.map((word) => (word ? word[0].toUpperCase() + word.slice(1) : "")) | ||
.join(" "); | ||
}; | ||
SwanandBhuskute marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Converts a string to camelCase format, first word - lowercase and each subsequent word - uppercase letter, with no spaces. | ||
export const camelCase = (str: string) => { | ||
if (!str) return ""; | ||
return str | ||
.trim() | ||
.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) | ||
.replace(/^[A-Z]/, (c) => c.toLowerCase()); | ||
}; | ||
SwanandBhuskute marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export const useDebounce = ( | ||
callback: (...args: string[]) => void, | ||
delay: number, | ||
) => { | ||
const callbackRef = useRef(callback); | ||
useEffect(() => { | ||
callbackRef.current = callback; | ||
}, [callback]); | ||
|
||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
const debouncedCallback = (...args: string[]) => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
timeoutRef.current = setTimeout(() => { | ||
callbackRef.current(...args); | ||
}, delay); | ||
}; | ||
return debouncedCallback; | ||
}; | ||
SwanandBhuskute marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+572
to
+591
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a hook so move it to src/hooks |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import _ from "lodash"; | ||
import { set } from "lodash-es"; | ||
import { useCallback, useReducer } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
|
@@ -93,7 +91,32 @@ export default function ShowInvestigation(props: ShowInvestigationProps) { | |
|
||
const handleValueChange = (value: any, name: string) => { | ||
const changedFields = { ...state.changedFields }; | ||
set(changedFields, name, value); | ||
const keys = name.split("."); | ||
let current = changedFields; | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
|
||
// Protect against prototype pollution by skipping unsafe keys - crai | ||
if (key === "__proto__" || key === "constructor" || key === "prototype") { | ||
continue; | ||
} | ||
|
||
// Use Object.create(null) to prevent accidental inheritance from Object prototype - coderabbit | ||
current[key] = current[key] || Object.create(null); | ||
current = current[key]; | ||
} | ||
|
||
const lastKey = keys[keys.length - 1]; | ||
|
||
// Final key assignment, ensuring no prototype pollution vulnerability - coderabbit | ||
if ( | ||
lastKey !== "__proto__" && | ||
lastKey !== "constructor" && | ||
lastKey !== "prototype" | ||
) { | ||
current[lastKey] = value; | ||
} | ||
|
||
Comment on lines
+95
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extract this function to utils and reuse for ./Table component as well |
||
dispatch({ type: "set_changed_fields", changedFields }); | ||
}; | ||
|
||
|
@@ -151,15 +174,19 @@ export default function ShowInvestigation(props: ShowInvestigationProps) { | |
}; | ||
|
||
const handleUpdateCancel = useCallback(() => { | ||
const changedValues = _.chain(state.initialValues) | ||
.map((val: any, _key: string) => ({ | ||
id: val?.id, | ||
initialValue: val?.notes || val?.value || null, | ||
value: val?.value || null, | ||
notes: val?.notes || null, | ||
})) | ||
.reduce((acc: any, cur: any) => ({ ...acc, [cur.id]: cur }), {}) | ||
.value(); | ||
const changedValues = Object.keys(state.initialValues).reduce( | ||
(acc: any, key: any) => { | ||
const val = state.initialValues[key]; | ||
acc[key] = { | ||
id: val?.id, | ||
initialValue: val?.notes || val?.value || null, | ||
value: val?.value || null, | ||
notes: val?.notes || null, | ||
}; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
dispatch({ type: "set_changed_fields", changedFields: changedValues }); | ||
}, [state.initialValues]); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this file