0

Using ticks understands a line break, but I'd prefer to use them to insert my variables in to the string, so the only way i have figured out how to have a cleaner code while still printing on the same line is to use a combination of backticks and + operators

        message.reply(`**${id}**) `
        + `Email Address: ${email_address}, `
        + `Twitch Account: ${twitch_account}, `
        + `Discord Account: ${discord_account}, `
        + `YouTube Account: ${youtube_account}, `
        + `Minecraft Account: ${minecraft_account}, `
        + `Birthday: ${birthday}, `
        + `Registered: ${registered}, `
        + `Subscribed: ${subscribed}, `
        + `Sub Date: ${sub_date}, `
        + `Sub Streak: ${sub_streak}, `
        + `Administrator: ${admin}`);

this solves the issue but doesn't look as clean as I'd like it to be, is there a more conventional way to accomplish this task while still using ticks?

Also, removing the + causes every variable to be called as a function and returns TypeError:

TypeError: email_address is not a function
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Shujin
  • 27
  • 5
  • And you likely do not want the ) in this line `message.reply(\`**${id}**)` – mplungjan Aug 01 '19 at 13:04
  • 1
    @mplungjan I'm not sure that I agree with your duplicate. The question isn't about how to use template literals in general. It's about how to leverage them for a specific purpose. – zzzzBov Aug 01 '19 at 13:12
  • Almost a dupe of https://stackoverflow.com/questions/27678052/usage-of-the-backtick-character-in-javascript – mplungjan Aug 01 '19 at 13:13

2 Answers2

0

Try this:

 message.reply(`**${id}** 
            Email Address: ${email_address}, 
            Twitch Account: ${twitch_account}, 
            Discord Account: ${discord_account}, 
            YouTube Account: ${youtube_account}, 
            Minecraft Account: ${minecraft_account}, 
            Birthday: ${birthday},
            Registered: ${registered}, 
            Subscribed: ${subscribed}, 
            Sub Date: ${sub_date}, 
            Sub Streak: ${sub_streak}, 
            Administrator: ${admin}`
            .split(/\n/).map(str => str.trim()).join(" ")); 

Like this

// mock:
const message = {
  reply: (str) => console.log(str)
}  

message.reply(`**${"id"}** 
                Email Address: ${"email_address"}, 
                Twitch Account: ${"twitch_account"}, 
                Discord Account: ${"discord_account"},`
                .split(/\n/)
                .map(str => str.trim())
                .join(" ")); 
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Assuming that the overhead of an additional function call doesn't matter for performance you can write all your lines in one string and then replace the newlines with spaces:

`**${id}**
Email Address: ${email_address},
...
`.replace(/(\n|\r\n?)/g, ' ')

As an alternative, since you have a list of key-value pairs, it would also make sense to build the output from an object:

const reply = `**${id}**` +
  Object.entries({
    'Email Address':     email_address,
    'Twitch Account':    twitch_account,
    'Discord Account':   discord_account,
    'YouTube Account':   youtube_account,
    'Minecraft Account': minecraft_account,
    'Birthday':          birthday,
    'Registered':        registered,
    'Subscribed':        subscribed,
    'Sub Date':          sub_date,
    'Sub Streak':        sub_streak,
    'Administrator':     admin
  })
  .map(([key, value]) => `${key}: ${value}`)
  .join(',')

message.reply(reply)
zzzzBov
  • 174,988
  • 54
  • 320
  • 367