Tagged template to get rid of first new-line

47 Views Asked by At

I have a function like this:

export default ({ subDomain }) => `
{
    mutation ${upperCase(subDomain)} {
        ${lowerCase(subDomain)} {
            ok
        }
    }
}
`

However it returns a string that starts with a new line.

I don't want to write it like this:

export default ({ subDomain }) => `{
    mutation ${upperCase(subDomain)} {
        ${lowerCase(subDomain)} {
            ok
        }
    }
}
`

Is there a way to use a tag this template, to get rid of this new line?

Something like:

export default ({ subDomain }) => trim`
{
    mutation ${upperCase(subDomain)} {
        ${lowerCase(subDomain)} {
            ok
        }
    }
}
`
1

There are 1 best solutions below

0
On

Why not just put .trim() after the template literal?

const fn = (arg) => `
{
    mutation ${arg} {
      ...
    }
}
`.trim();

console.log(fn('abc'));