mercredi 20 novembre 2019

Elegant Way To Format Text With JavaScript?

I'm pulling in e-mails for a user to read and I want them to be able to read the text in the app. So I created a function that filters out, trims and formats the strings. If the string length is greater than 16 it creates a and if it is less than 16 it creates a

. The issue is I keep getting breaks where there aren't any. I tried removing the
from the code but it then becomes unreadable mess of strings. I wanted to see if there was a more elegant way to handle these strings in pure Javascript. Thank you!


function readEmail(e) {
      const contentStr = []
      e.body.replace(/[^\w\s,-:='?]/gi, '')
        .split(/\n/gi)
        .filter(s => s.trim().length)
        .forEach((preStr) => {
          const s = preStr.trim()
          if {
              (s.length > 1 && s.length <= 16)
              contentStr.push(`<h3>${s}</h3><br />`)
          }
          else if{ 
              (s.length > 16)
              contentStr.push(`<p>${s}</p><br />`)
          }
})

Aucun commentaire:

Enregistrer un commentaire