Skip to main content
Remove tag
Source Link

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. 

The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isNotEmptyString(item: string | null | undefined): boolean {
  return !['', null, undefined].includes(item);
}

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isNotEmptyString(item: string | null | undefined): boolean {
  return !['', null, undefined].includes(item);
}

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. 

The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isNotEmptyString(item: string | null | undefined): boolean {
  return !['', null, undefined].includes(item);
}
added 4 characters in body
Source Link

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isEmptyStringisNotEmptyString(item: string | null | undefined): boolean {
  return !['', null, undefined].includes(item);
}

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isEmptyString(item: string | null | undefined): boolean {
  return ['', null, undefined].includes(item);
}

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isNotEmptyString(item: string | null | undefined): boolean {
  return !['', null, undefined].includes(item);
}
Source Link

Optimizing - formatting a person's name

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up with and I'm wondering if there are ways I can make this better. The code was adapted from another function written by a senior developer at my company. I'm trying to expand my way of problem-solving for conditional situations.

export function getLastNameFirstNameFullUserName(
  user: { firstName?: string; lastName?: string; middleName?: string },
  includeMiddleName = false,
): string {
  const { firstName, lastName, middleName } = user;

  const userNameArray: string[] = [];

  if (isNotEmptyString(lastName)) {
    useLastNameWithComma(firstName, includeMiddleName, middleName)
      ? userNameArray.push(`${lastName},`)
      : userNameArray.push(`${lastName}`);
  }

  isNotEmptyString(firstName) && userNameArray.push(`${firstName}`);

  includeMiddleName && isNotEmptyString(middleName) && userNameArray.push(`${middleName}`);

  return userNameArray.join(' ');
}

function useLastNameWithComma(firstName: string, includeMiddleName: boolean, middleName?: string): boolean {
  if (isNotEmptyString(firstName)) {
    return true;
  }

  if (includeMiddleName && isNotEmptyString(middleName)) {
    return true;
  }

  return false;
}

export function isEmptyString(item: string | null | undefined): boolean {
  return ['', null, undefined].includes(item);
}