Skip to content

undefined/null values in interpolated expressions should return "" #1406

@ricardobeat

Description

@ricardobeat

On most templating/string formatting implementations, falsy values result in an empty string:

"#{something} here"
=> " here"

whereas in CoffeeScript you get the .toString() representation:

something = x if y // condition fails
"#{something} here"
=> "undefined here"
something = null
"#{something}"
=> "null"

You can say this matches expectations, coming from js, but it could be better. I have yet to see a case where you actually want to print "undefined", "false" or "null" to a string, so returning an empty string would break expectations in a good way :) If you really want it .toString() and typeof will still be there for you.

As a result, this:

console.log "#{not(i%3) and 'fizz' or ''}#{not(i%5) and 'buzz' or ''}" or i for i in [1..100]

could be written as

console.log "#{'fizz' if !(i%3)}#{'buzz' if !(i%5)}" or i for i in [1..100]

That would at principle mean wrapping interpolated expressions in (expression || ''). That wouldn't work for 0 though. Special delimiters?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions