From f13a324edffe23a686388798520c5d3d1ae30d7d Mon Sep 17 00:00:00 2001 From: SwanandBhuskute Date: Thu, 14 Nov 2024 13:26:55 +0000 Subject: [PATCH] enhanced with suggestions 2 --- src/Utils/stringUtils.ts | 13 +++++++------ .../Facility/Investigations/Reports/utils.tsx | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Utils/stringUtils.ts b/src/Utils/stringUtils.ts index 89ab3dce851..dc8bd55cd3f 100644 --- a/src/Utils/stringUtils.ts +++ b/src/Utils/stringUtils.ts @@ -1,11 +1,10 @@ // 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 - .replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, (match, index) => - index === 0 ? match.toLowerCase() : match.toUpperCase(), - ) - .replace(/\s+/g, "") - .replace(/([A-Z])/g, (match) => match.toLowerCase()); + .trim() + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) + .replace(/^[A-Z]/, (c) => c.toLowerCase()); }; // Capitalize the first letter of each word in a string, handling edge cases @@ -23,5 +22,7 @@ export const startCase = (str: string): string => { // Capitalize the first letter of a string export const capitalize = (str: string) => { - return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); + if (!str) return ""; + if (str.length === 1) return str.toUpperCase(); + return str.charAt(0).toUpperCase() + str.slice(1); }; diff --git a/src/components/Facility/Investigations/Reports/utils.tsx b/src/components/Facility/Investigations/Reports/utils.tsx index 12897e4cb3a..349b067bc62 100644 --- a/src/components/Facility/Investigations/Reports/utils.tsx +++ b/src/components/Facility/Investigations/Reports/utils.tsx @@ -7,7 +7,9 @@ const memoize = any>(fn: T): T => { const key = args .map((arg) => typeof arg === "object" - ? JSON.stringify(Object.entries(arg).sort()) + ? arg instanceof Date + ? arg.getTime().toString() + : JSON.stringify(Object.entries(arg).sort()) : String(arg), ) .join("|");