Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escaping null #427

Closed
lazylester opened this issue Jan 10, 2012 · 5 comments
Closed

escaping null #427

lazylester opened this issue Jan 10, 2012 · 5 comments
Labels

Comments

@lazylester
Copy link

I was surprised that _.escape(null) produces the string "null". Wouldn't the "principle of least surprise" suggest that the result should be a blank string, or maybe even null? Of course, I realize that the principle of least surprise is subjective, but I raise this since it might be an oversight rather than the intended and expected operation.

Thanks for underscore, and for your contribution to open source.

Les

@jashkenas
Copy link
Owner

In a dynamically typed language, you can pass any value to any function ... but that doesn't mean you should. The Underscore collection functions have undefined behavior when passed anything other than Arrays or Objects; _.escape has undefined behavior when passed anything that's not a string. This isn't something that should be expected by Underscore itself. You can use:

_.escape(value || "");

@ghost
Copy link

ghost commented Mar 27, 2012

In my opinion _.escape(null) should evaluate to an empty string. The change only adds like 6 characters and it's common for developers to, for example, fetch a model with null attributes and then try to render it in an escaped template.

The current version also causes an inconsistency in _.template where <%= null_value %> evaluates to "" but <%- null_value %> evaluates to "null". It seems more elegant to make this change in _.escape rather than force developers to use <%= _.escape(value || "") =>.

@octatone
Copy link
Contributor

@mattyurka

_.escape escapes all keywords and objects in the same fashion and returns strings. Are you suggesting making exceptions to the behavior per keyword/object type?

http://jsfiddle.net/octatone/FxPLS/

I think the expected behavior for _.escape is to return an escaped string representation of whatever is passed to it.

@ghost
Copy link

ghost commented Mar 27, 2012

@octatone

I was suggesting treating input as (input || '') in _.escape. This would only change undefined behavior. If you want to display NaN, undefined, false or null just pass in (input + '').

But I see how this is inelegant. In my case I have nulls in my models and don't want to spit nulls at the user. My gut tells me this is what other developers would want but I don't really know.

As a side note, in my browser Array.join converts nulls and undefineds into empty strings, which is the cause of the inconsistency I mentioned above. Limiting ourselves to just nulls and undefineds, however, would add more cruft: (_.isNull(input) || _.isUndefined(input)) ? '' : input

@russplaysguitar
Copy link
Contributor

+1
right now i'm adding a wrapper for escape() to my code base that adds || "" so users see "" and not "undefined"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants