GitHub 6219★

Remove empty lines of a text document

JavaScript version

const removeEmptyLines = (str) =>
str
.split(/\r?\n/)
.filter((line) => line.trim() !== '')
.join('\n');

TypeScript version

const removeEmptyLines = (str: string): string =>
str
.split(/\r?\n/)
.filter((line) => line.trim() !== '')
.join('\n');

Examples

removeEmptyLines(`red

green
blue

yellow
`
);

/* Output */
/*
red
green
blue
yellow
*/
Follow me on and to get more useful contents.