Skip to content

Commit

Permalink
improved formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarkowsky committed Jan 27, 2025
1 parent c6871f1 commit 5cf51d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
24 changes: 19 additions & 5 deletions react-app/src/pages/AccessRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,28 @@ const RequestForm = ({ submitHandler }: { submitHandler: (d: any) => void }) =>
() => getProvider(sso.user?.preferred_username, lookup?.data?.Config?.bcscIdentifier),
[sso.user, lookup],
);

const userIsIdir = provider === 'IDIR';

const namePatterns = [
{
split: /\s/,
join: ' ',
},
{
split: /[_]/,
join: ' ',
},
{
split: /'/,
join: `'`,
},
];

const formMethods = useForm({
defaultValues: {
Provider: provider ?? '',
FirstName: capitalizeFirstLetters(sso.user?.first_name) || '',
LastName: capitalizeFirstLetters(sso.user?.last_name) || '',
FirstName: capitalizeFirstLetters(sso.user?.first_name, namePatterns) || '',
LastName: capitalizeFirstLetters(sso.user?.last_name, namePatterns) || '',
Email: userIsIdir ? sso.user?.email : '',
Notes: '',
Agency: '',
Expand All @@ -69,8 +83,8 @@ const RequestForm = ({ submitHandler }: { submitHandler: (d: any) => void }) =>
useEffect(() => {
formMethods.reset({
Provider: provider ?? '',
FirstName: capitalizeFirstLetters(sso.user?.first_name) || '',
LastName: capitalizeFirstLetters(sso.user?.last_name) || '',
FirstName: capitalizeFirstLetters(sso.user?.first_name, namePatterns) || '',
LastName: capitalizeFirstLetters(sso.user?.last_name, namePatterns) || '',
Email: userIsIdir ? sso.user?.email : '',
Notes: '',
Agency: '',
Expand Down
41 changes: 30 additions & 11 deletions react-app/src/utilities/formatters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,35 @@ export const pidFormatter = (pid: number | string): string => {

export const formatNumber = (num: number) => num.toLocaleString();

export const capitalizeFirstLetters = (name: string) => {
if (!name.length) return '';
const nameParts = name.split(/[_\s]/);
const returnValue = [];
nameParts.forEach((part) => {
if (part.length) {
const lower = part.toLowerCase();
const firstLetter = lower.at(0).toUpperCase();
returnValue.push(firstLetter + lower.slice(1));
}
/**
* @param input The string being capitalized.
* @param patterns A list of objects with split and join values.
* For each object, the string will be split, capitalized, then joined with the provided characters.
* Default splits on spaces and joins with spaces again.
* @returns A string with the capitalized result.
*/
export const capitalizeFirstLetters = (
input: string,
patterns = [
{
split: /[\s]/,
join: ' ',
},
],
) => {
if (!input.length) return '';
let returnValue = input.toLowerCase().trim();
// For each provided pattern, split the string, capitalize each initial letter, then join it again
patterns.forEach((pattern) => {
const parts = returnValue.split(pattern.split);
const result = [];
parts.forEach((part) => {
if (part.length) {
const firstLetter = part.at(0).toUpperCase();
result.push(firstLetter + part.slice(1));
}
});
returnValue = result.join(pattern.join);
});
return returnValue.join(' ');
return returnValue;
};

0 comments on commit 5cf51d5

Please sign in to comment.