Created
May 18, 2018 07:11
-
-
Save gregberns/5e5b3c2f5075cb07b03c74ea84f844b4 to your computer and use it in GitHub Desktop.
Refactor Spectacular - List to String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildFeaturePreview(vehicle: IVehicle): string { | |
let mpg = this.shouldShowMpg(vehicle) ? `${vehicle.MPGCity} / ${vehicle.MPGHighway} MPG` : null; | |
let features = vehicle.HighlightedFeatures.map(feature => feature.description); | |
return [mpg, | |
...features] | |
.filter(isDefinedAndNotNull) | |
.join(', ') | |
'...'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildFeaturePreview(vehicle: IVehicle): string { | |
let featureList: string[] = []; | |
let features: string = ''; | |
if (shouldShowMpg(vehicle)) { | |
featureList.push(`${vehicle.MPGCity} / ${vehicle.MPGHighway} MPG`); | |
} | |
vehicle.HighlightedFeatures // HighlightedFeatures = [{ .description }] | |
.map(feature => { | |
featureList.push(`${feature.description}`); | |
}); | |
features = featureList.join(', '); | |
features += '...'; | |
return features; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isDefinedAndNotNull(o) { | |
return typeof o !== 'undefined && o !== null; | |
} | |
shouldShowMpg(vehicle: IVehicle): boolean { | |
return ( | |
isDefinedAndNotNull(vehicle) && | |
(isDefinedAndNotNull(vehicle.MPGCity) && vehicle.MPGCity !== 0) && | |
(isDefinedAndNotNull(vehicle.MPGHighway) && vehicle.MPGHighway !== 0) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment