Skip to content

Instantly share code, notes, and snippets.

@gregberns
Created May 18, 2018 07:11
Show Gist options
  • Save gregberns/5e5b3c2f5075cb07b03c74ea84f844b4 to your computer and use it in GitHub Desktop.
Save gregberns/5e5b3c2f5075cb07b03c74ea84f844b4 to your computer and use it in GitHub Desktop.
Refactor Spectacular - List to String
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(', ')
'...';
}
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;
}
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