DEV Community

Discussion on: How would you refactor this JS function?

Collapse
 
rkallan profile image
RRKallan

Assumption:

  • line must is a string and length >= 1 return line
  • isFirstLine is a boolean true htmlTag = h1 else htmlTag = p
const lineChecker = (line = "", isFirstLine = false) => {
    if (typeof line !== "string" || !line.length) return "<br />";

    const elementTag = isFirstLine === true ? "h1" : "p";
    return `<${elementTag}>${line}</${elementTag}>`;
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lgrammel profile image
Lars Grammel

Nice refactoring, I like the tag extraction and the input type checking!

Collapse
 
rkallan profile image
RRKallan

THX.