'Shorter way to return propertype enum in typescript [closed]
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?
Solution 1:[1]
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
Solution 2:[2]
public CustomerEditType(customer: Customer): CustomerEditType {
return (customer.company? (customer.vatNumber? CustomerEditType.COMPANYVAT: CustomerEditType.COMPANYNOVAT) : CustomerEditType.PRIVATE));
}
Explanation: this is a short if else statement:
boolenaStatement? ifTrue : else
Using this way you can nest statements:
statement1? (statement2? statement2True: statement2false) : statement1False
more info: Shorthand for if-else statement
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | s_ofia |
| Solution 2 |
