From 0eebd2929b17ec2438667e3ca93ba27d62c72eed Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Tue, 4 Mar 2025 09:00:37 +0530 Subject: [PATCH] FIX-1847 added methods in string utils in order to utilise in our code. --- src/Utils/Input/utils.jsx | 3 ++- src/Utils/stringUtils.js | 47 +++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/Utils/Input/utils.jsx b/src/Utils/Input/utils.jsx index f0cb75ac7..f9a3b0137 100644 --- a/src/Utils/Input/utils.jsx +++ b/src/Utils/Input/utils.jsx @@ -1,4 +1,5 @@ import PropTypes from "prop-types"; +import { isEmpty } from "../stringUtils"; /** * Utility function to render a truncated value with an optional title. @@ -15,7 +16,7 @@ export const renderTruncatedValue = function ( maxLength, placeholder ) { - if (!displayName || displayName === "0") return placeholder; + if (isEmpty(displayName)) return placeholder; const truncateTextWithDottedEnds = function (text, maxLength) { if (!text || text.length <= maxLength) return text; diff --git a/src/Utils/stringUtils.js b/src/Utils/stringUtils.js index 0bf7aaf11..6693a420f 100644 --- a/src/Utils/stringUtils.js +++ b/src/Utils/stringUtils.js @@ -3,8 +3,8 @@ * @param {string} str String whose first letter is to be capitalized * @returns A string with first letter capitalized */ -export const capitalizeFirstLetter = (str) => { - if (str === null || str === undefined) { +export const capitalizeFirstLetter = (str) => { + if (str === null || str === undefined) { return ""; } if (typeof str !== "string") { @@ -12,7 +12,7 @@ export const capitalizeFirstLetter = (str) => { } if (str.length === 0) { return ""; - } + } return str.charAt(0).toUpperCase() + str.slice(1); }; @@ -22,15 +22,34 @@ export const capitalizeFirstLetter = (str) => { * @returns A string with first letter lower cased */ -export const toLowerCaseFirstLetter = (str) => { +export const toLowerCaseFirstLetter = (str) => { if (str === null || str === undefined) { - return ""; - } - if (typeof str !== "string") { - throw new TypeError("Input must be a string"); - } - if (str.length === 0) { - return ""; - } - return str.charAt(0).toLowerCase() + str.slice(1); - }; \ No newline at end of file + return ""; + } + if (typeof str !== "string") { + throw new TypeError("Input must be a string"); + } + if (str.length === 0) { + return ""; + } + return str.charAt(0).toLowerCase() + str.slice(1); +}; + +/** + * Checks if a string is null, undefined, or empty (including strings with only whitespace). + * @param {string} str - The string to check. + * @returns {boolean} - Returns true if the string is null, undefined, or empty. + */ +export const isEmpty = (str) => { + // Check if string is null, undefined, or empty (including whitespace only) + return str === null || str === undefined || str.trim().length === 0; +}; + +/** + * Checks if a string is not null, undefined, and not empty (including strings with only whitespace). + * @param {string} str - The string to check. + * @returns {boolean} - Returns true if the string is not null, undefined, and not empty. + */ +export const isNotEmpty = (str) => { + return !isEmpty(str); +};