Let's I have parts of user's address that should be formatted in single string.
Address components are
Street
City
Phone
State
Zip
and should be formatted to string street city, phone, state zip. (2 commas).
Problem is that every field can be null. So if street == null and city == null, then I should have string phone, state zip (1 comma). Problem is in controlling number of spaces and number of commas
How can I avoid and minimize the number of null-inspections?
My current code is
var formatAddress = function(address) {
var retVal = ""
if (address.street || address.city)
{
retVal += address.street ? address.street + " " : ""
retVal += address.city ? address.city : ""
retVal += ", ";
}
retVal += address.phone ? address.phone + ", " : ""
retVal += address.state ? address.state : ""
retVal += address.zip ? " " + address.zip : ""
return retVal
}