我想创建一个字符串文字,其可选值为numberOfResidents。
function mapBuildingToComment(building: Building) {
return `
### Building information ###
Street: ${building.address.street}
HouseNumber: ${building.address.houseNumber}
${mapNumberOfResidents(building.numberOfResidents)}
City: ${building.address.city}
`
}
function mapNumberOfResidents(numberOfResidents?: string) {
if (!numberOfResidents) return ''
return `Number of residents: ${numberOfResidents}`
}
我现在的问题是,当numberOfResidents未定义时,输出中有一个空行。
Output:
### Building information ###
Street: Teststreet
HouseNumber: 1
City: Test
如何做到没有空行,门牌号正下方是城市?
1条答案
按热度按时间yqhsw0fo1#
删除换行符,并仅在需要时添加它,如
另一个选择是从
s.replaceAll(/\n\s+(?=\n)/g, '')
结果中删除空行