Skip to content

Commit

Permalink
discourage using capitalizeWords fn. by being kept as a shared utilit…
Browse files Browse the repository at this point in the history
…y fn. use tailwindcss's alternative
  • Loading branch information
rithviknishad committed Dec 5, 2024
1 parent 8feb118 commit 4d39aa1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
23 changes: 20 additions & 3 deletions src/Utils/Notifications.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Stack, alert, defaultModules } from "@pnotify/core";
import * as PNotifyMobile from "@pnotify/mobile";

import { capitalizeWords, cleanStringForNotifications } from "@/Utils/utils";

defaultModules.set(PNotifyMobile, {});

const notifyStack = new Stack({
Expand Down Expand Up @@ -36,6 +34,25 @@ const notify = (text, type) => {
});
};

/**
* Formats input string to a more human readable format
* @param {string} key - The key to format
* @returns {string} The formatted key
* @example
* formatKey("patient_name") => "Patient Name"
*/
const formatKey = (key) => {
return key
.replace(/[^a-zA-Z0-9]+/g, " ") // Replace non-alphanumeric characters with a space
.trim()
.split(" ")
.map(
(word) =>
word.charAt(0).toLocaleUpperCase() + word.slice(1).toLocaleLowerCase(),
) // Capitalize the first letter of each word and lowercase the rest
.join(" ");
};

const notifyError = (error) => {
let errorMsg = "";
if (typeof error === "string" || !error) {
Expand All @@ -45,7 +62,7 @@ const notifyError = (error) => {
errorMsg = error.detail;
} else {
for (let [key, value] of Object.entries(error)) {
let keyName = capitalizeWords(cleanStringForNotifications(key));
let keyName = formatKey(key);
if (Array.isArray(value)) {
const uniques = [...new Set(value)];
errorMsg += `${keyName} - ${uniques.splice(0, 5).join(", ")}`;
Expand Down
23 changes: 1 addition & 22 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,27 +545,6 @@ export const keysOf = <T extends object>(obj: T) => {
return Object.keys(obj) as (keyof T)[];
};

//removes all symbols except _ and - (one part of startCase)
export const cleanStringForNames = (str: string): string => {
return str.replace(/[^a-zA-Z0-9_-]+/g, " ").trim();
};

export const cleanStringForNotifications = (str: string): string => {
return str.replace(/[^a-zA-Z0-9]+/g, " ").trim();
};

// just capitalizes (as per startCase working) (part of startCase) (only inside Notification.js)
export const capitalizeWords = (str: string): string => {
if (!str) return "";
return str
.split(" ")
.map(
(word) =>
word.charAt(0).toLocaleUpperCase() + word.slice(1).toLocaleLowerCase(),
)
.join(" ");
};

// Utility to check if a value is "empty"
export const isEmpty = (value: unknown) => {
return value === "" || value == undefined;
Expand All @@ -574,7 +553,7 @@ export const isEmpty = (value: unknown) => {
// equivalent to lodash omitBy
export function omitBy<T extends Record<string, unknown>>(
obj: T,
predicate: (value: unknown) => boolean = isEmpty,
predicate: (value: unknown) => boolean,
): Partial<T> {
return Object.fromEntries(
Object.entries(obj).filter(([_, value]) => !predicate(value)),
Expand Down
4 changes: 2 additions & 2 deletions src/components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

import { DraftSection, useAutoSaveReducer } from "@/Utils/AutoSave";
import * as Notification from "@/Utils/Notifications";
import { classNames, omitBy } from "@/Utils/utils";
import { classNames, isEmpty, omitBy } from "@/Utils/utils";

type Props<T extends FormDetails> = {
className?: string;
Expand Down Expand Up @@ -57,7 +57,7 @@ const Form = <T extends FormDetails>({
event.stopPropagation();

if (validate) {
const errors = omitBy(validate(state.form)) as FormErrors<T>;
const errors = omitBy(validate(state.form), isEmpty) as FormErrors<T>;

if (Object.keys(errors).length) {
dispatch({ type: "set_errors", errors });
Expand Down

0 comments on commit 4d39aa1

Please sign in to comment.