DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
rkallan profile image
RRKallan • Edited

Adding the types makes sense.

Only the else if else statement could be replaced. My suggestion based on your example

const parseStringToHtml = (line = "", isFirstLine = false): string => {
    if (!line) return "<br />";

    if (isFirstLine) return `<h1>${line}</h1>`;

    return `<p>${line}</p>`;
};
Enter fullscreen mode Exit fullscreen mode

edited

Collapse
 
siddharthshyniben profile image
Siddharth

I'd suggest putting the line argument in the function first, as line is more important and isFirstLine can have a default value

Thread Thread
 
rkallan profile image
RRKallan

true i just copy paste it and didn't read the arguments.