jwadhams / json-logic-js

Build complex rules, serialize them as JSON, and execute them in JavaScript
MIT License
1.26k stars 140 forks source link

Safari returns problematic values #4

Closed adzap closed 8 years ago

adzap commented 8 years ago

Been using the lib nicely in IE and Chrome but found a serious bug in Safari (v9.1.1).

var logic = {
  "and": [
   { "==": [ {"var": "foo"}, 0 ] },
   { "==": [ {"var": "bar" }, 0] }
  ]
};

var data = {
  "foo": 0,
  "bar": 0
};

jsonLogic.apply(logic, data) // returns 2 in Safari

Change 0 to a 1 returns false. Truthy logic can overcome this issue.

But the real issue is the 'or' operator

var logic = {
  "or": [
   { "==": [ {"var": "foo"}, 0 ] },
   { "==": [ {"var": "bar" }, 0] }
  ]
};

var data = {
  "foo": 1,
  "bar": 1
};
jsonLogic.apply(logic, data) // should return false but returns 2 in Safari, a truthy value. Ug.

Difficult to trace this given the recursion. Any clues?

jwadhams commented 8 years ago

Well that's terrifying. I'm debugging this this morning.

Here's a reproduction JSFiddle: https://jsfiddle.net/jwadhams/1fwv8koa/1/

jwadhams commented 8 years ago

Can you take a look at version 1.0.6?

The root of the problem appears to be that I used a fancy for...in statement to iterate over the arguments to or and and

Moving to an old fashioned for(var i=0 ; i < arguments.length ; i+=1) loop cleared up the issue for me.

adzap commented 8 years ago

Excellent, that seems to have done the trick. Thanks very much.

Thanks a lot for this lib too.

jwadhams commented 8 years ago

You're very welcome. Thanks for the bug report, that definitely would have bitten me when my product got out into the hands of the wider public!