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

null/undefined are not falsy when not in the root context #409

Closed
dasilvacontin opened this issue Dec 19, 2014 · 7 comments
Closed

null/undefined are not falsy when not in the root context #409

dasilvacontin opened this issue Dec 19, 2014 · 7 comments

Comments

@dasilvacontin
Copy link
Collaborator

 var Mustache = require('mustache');

 var view = {
    values: ['foo', 'bar', null],
 };

 console.log( Mustache.render("{{#values}}is {{#.}}{{.}}{{/.}}{{^.}}nothing{{/.}}\n{{/values}}", view) );

Renders:

test  node test.js
is foo
is bar
is [object Object]

When it should actually render:

test  node test.js
is foo
is bar
is nothing
@dasilvacontin
Copy link
Collaborator Author

Also, when rending null, it should render something more useful than [object Object] like [null].

@dasilvacontin
Copy link
Collaborator Author

Actually, null has always been intended to be falsy as shown by test/files/falsy.txt, but somehow the test was passing.

@dasilvacontin
Copy link
Collaborator Author

From the lookup function:

Context.prototype.lookup = function (name) {
  var cache = this.cache;

  var value;
  if (name in cache) {
    value = cache[name];
  } else {
    var context = this, names, index;

    while (context) {
      if (name.indexOf('.') > 0) {
        value = context.view;
        names = name.split('.');
        index = 0;

        while (value != null && index < names.length)
          value = value[names[index++]];
      } else if (typeof context.view == 'object') {
        value = context.view[name];
      }

      if (value != null)
        break;

      context = context.parent;
    }

    cache[name] = value;
  }

  if (isFunction(value))
    value = value.call(this.view);

  return value;
};

If you analyse, when a value such as value != null is in a context with no parent, it was getting returned by the lookup function anyways. I'm going to add more tests for falsey values in which their context does have a parent.

@dasilvacontin
Copy link
Collaborator Author

Actually n2, undefined has always been intended to be falsy as shown by test/files/falsy.txt.

It's great to see this, because it's like a past 👍 to stop context lookup when there is actually a key (even if the value is undefined or null) as issued at #397 and suggested at #407. (/cc @janl)

@dasilvacontin
Copy link
Collaborator Author

So the problem is not that null is not considered as falsy. In fact, it's considered as falsy AS LONG as you use it on the ROOT context.

The problem is on the lookup function, so I will fix this on PR #407.

dasilvacontin added a commit to dasilvacontin/mustache.js that referenced this issue Dec 19, 2014
dasilvacontin added a commit to dasilvacontin/mustache.js that referenced this issue Dec 19, 2014
dasilvacontin added a commit to dasilvacontin/mustache.js that referenced this issue Dec 19, 2014
@dasilvacontin
Copy link
Collaborator Author

This gets fixed by PR #407.

@dasilvacontin dasilvacontin added this to the 2.0.0 milestone Dec 20, 2014
@dasilvacontin dasilvacontin changed the title null is not considered falsy when used to open a section null/undefined are not falsy when not in the root context Dec 20, 2014
dasilvacontin added a commit to dasilvacontin/mustache.js that referenced this issue Mar 22, 2015
@phillipj
Copy link
Collaborator

phillipj commented May 5, 2015

Fixed in a020430

@phillipj phillipj closed this as completed May 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants