DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
machinesrental profile image
machinesrental • Edited

"fat" version:

const lineChecker = (line, isFirstLine) => {
  if(typeof line !== 'string' || line === '') return '<br />';
  const tag = isFirstLine ? 'h1' : 'p';
  return `<${tag}>${line}</${tag}>;
}
Enter fullscreen mode Exit fullscreen mode

minimal:

const lineChecker = (line, isFirstLine) => (typeof line !== 'string' || line === '') ? '<br />' : isFirstLine ? `<h1>${line}</h1>` : `<p>${line}</p>`
Enter fullscreen mode Exit fullscreen mode