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

Incorrect evalution order #30

Open
irok opened this issue Jul 18, 2018 · 1 comment
Open

Incorrect evalution order #30

irok opened this issue Jul 18, 2018 · 1 comment

Comments

@irok
Copy link

irok commented Jul 18, 2018

The following code seems to be working properly.

jSmart = require('jsmart');
data = {
  array: [1, 2, 3],
  count: (array) => array.length
};

tmpl = new jSmart('{count($array)}');
console.log(tmpl.fetch(data));    // -> 3

tmpl = new jSmart('{if count($array)}OK{else}NG{/if}');
console.log(tmpl.fetch(data));    // -> OK

However, the following code does not work as expected.

tmpl = new jSmart('{if count($array) > 0}OK{else}NG{/if}');
console.log(tmpl.fetch(data));    // -> NG

Apparently, it seems that count modifier is working instead of my count method.

tmpl = new jSmart('{count($array)}');
console.log(tmpl.fetch({
  array: [1, 2, 3],
  count: (array) => 'My count method'
}));
// -> 3

Can I use my count method?

@irok
Copy link
Author

irok commented Jul 20, 2018

My count method worked as follows.

jSmart = require('jsmart');
delete jSmart.prototype.modifiers.count;    // disable count modifier

tmpl = new jSmart('{count($array)}');
console.log(tmpl.fetch({
  array: [1, 2, 3],
  count: (array) => 'My count method'
}));
// -> My count method

But, another problem was discovered.

jSmart = require('jsmart');
delete jSmart.prototype.modifiers.count;

data = {
  array: [1, 2, 3],
  count: (arg) => JSON.stringify({
    typeof: typeof arg,
    value: arg
  })
};

tmpl = new jSmart('{count($array)}');
console.log(tmpl.fetch(data));
// -> {"typeof":"object","value":[1,2,3]}

There is no problem so far.

tmpl = new jSmart('{count($array) > 0}');
console.log(tmpl.fetch(data));
// -> {"typeof":"boolean","value":false}

Why become a boolean?

tmpl = new jSmart('{count($array) == $array}');
console.log(tmpl.fetch(data));
// -> {"typeof":"boolean","value":true}

true!?
It seems that there is an error in the evaluation order.
Can you fix it?

@irok irok changed the title method with the same name as modifier doesn't work Incorrect evalution order Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant