zspecza / common-tags

🔖 Useful template literal tags for dealing with strings in ES2015+
Other
1.99k stars 60 forks source link

Objects call their valueOf methods instead of toString methods. #143

Closed dragonfire535 closed 6 years ago

dragonfire535 commented 6 years ago

When converting objects into strings that have a custom toString method, I've noticed the lib calls valueOf instead. I don't know if this is intentional, but it really is confusing if you aren't expecting this behavior.

console.log(stripIndents`
    ${obj} is cool!
`);

Assuming obj has a valueOf that returns "1" and a toString that returns "2", the valueOf gets called, which in normal template literals is not the case.

fatfisz commented 6 years ago

I'll investigate this.

fatfisz commented 6 years ago

Ok, I've got it - it was quite hard to track. I'll prepare a MR, keep your fingers crossed for a fast release!