DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
valeriavg profile image
Valeria

What about this one?

const lineChecker = (line, isFirstLine) => {
  if(!line?.trim()) return '<br/>'
  const tag = isFirstLine? 'h1':'p'
  return `<${tag}>${line}</${tag}>`
};
// Trim is needed to handle newlines & whitespaces
lineChecker('   \n   ',false) // <br/>
Enter fullscreen mode Exit fullscreen mode