Home > Mobile >  How to look for an item in DB when the type is String | String[] using typescript union types
How to look for an item in DB when the type is String | String[] using typescript union types

Time:11-18

I am calling a method sendEmail that sends an email to a single user or a bunch of users(the type of to email is string | string[])

Is there a cleaner/better way to determine if it's an array or a single user so that I can look for it in the DB accordingly? The code looks quite repetitive now.

  public async sendEmail(
    toEmail: string | string[],
  ): Promise<void> {
    if (!Array.isArray(toEmail)) {
      const user = await this.userRepository.getUserByEmail(toEmail);
      await checkIfPaymentMade(user);
    } else {
      for (const email of toEmail) {
       const user = await this.userRepository.getUserByEmail(toEmail);
        await checkIfPaymentMade(user);
      }
    }
  }

CodePudding user response:

A few other options come to mind

  1. Extract the common code to its own function. There's still some duplication, but just one line instead of however long the helper function needs to be.
public async sendEmail(
  toEmail: string | string[]
): Promise<void> {
  if (!Array.isArray(toEmail)) {
    await doStuff(toEmail);
  } else {
    for (const email of toEmail) {
      await doStuff(email);
    }
  }
}

private async doStuff(toEmail: string): Promise<void> {
  const user = await this.userRepository.getUserByEmail(toEmail);
  await checkIfPaymentMade(user);
}
  1. Have the body of the function only work with standalone strings. If it's an array, recurse.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  if (Array.isArray(toEmail) {
    for (const email of toEmail) {
      await sendEmail(email)
    }
  } else {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
  1. Have the body of the function only work with arrays. If it's not an array, turn it into one first.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  const emailArray = Array.isArray(toEmail) ? toEmail : [toEmail];

  for (const email of emailArray) {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}

CodePudding user response:

If you just want to keep it DRY, here's an option:

TS Playground link

public async sendEmail (recipients: string | string[]): Promise<void> {
  for (const address of Array.isArray(recipients) ? recipients : [recipients]) {
    const user = await this.userRepository.getUserByEmail(address);
    await checkIfPaymentMade(user);
  }
}
  • Related