DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
siddharthshyniben profile image
Siddharth

Simplified one liner, with default props

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

Note that this isn't a very scalable solution. Scaling this will need a complete rewrite.

Collapse
 
siddharthshyniben profile image
Siddharth • Edited

The bigger answer is "it depends". If this is it (which probably isn't), you can use my solution. If not there are many ways of making this extensible, like mapping conditions and tags in an array so we can simply add an item to the array to add more elements. Or we could refactor the wrapping in elements to a custom function. There are many more ways we can do this, I'm just mentioning the ones which come to mind