Home > Net >  Shorter way to return propertype enum in typescript
Shorter way to return propertype enum in typescript

Time:02-04

I have a function like this:

  public CustomerEditType(customer: Customer): CustomerEditType {
    if (customer.company) {
      if (customer.vatNumber) {
        return CustomerEditType.COMPANYVAT;
      } else {
        return CustomerEditType.COMPANYNOVAT;
      }
    } else {
      return CustomerEditType.PRIVATE;
    }
  }

The problem I have here is I don't know how to make this function shorter. Maybe an inline return statement? How do I make this function shorter?

CodePudding user response:

Please find the below code.

public CustomerEditType(customer: Customer): CustomerEditType {
    return customer.company ? (customer.vatNumber? CustomerEditType.COMPANYVAT : CustomerEditType.COMPANYNOVAT) : CustomerEditType.PRIVATE;
}

CodePudding user response:

The shortest way would be:

// shortest method
public CustomerEditType(customer: Customer): CustomerEditType {
  return (customer.company) ? ((customer.vatNumber) ? CustomerEditType.COMPANYVAT : CustomerEditType.COMPANYNOVAT) : CustomerEditType.PRIVATE;
}

For more information see: Javascript one line If...else...else if statement

CodePudding user response:

public CustomerEditType(customer: Customer): CustomerEditType {

    return (customer.company? (customer.vatNumber? CustomerEditType.COMPANYVAT: CustomerEditType.COMPANYNOVAT) : CustomerEditType.PRIVATE));
}

  •  Tags:  
  • Related