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

document that `all` on an empty set returns `false` #105

Open panzi opened 3 years ago

panzi commented 3 years ago

In every other language all() (or every()) returns true for an empty set (see e.g. every() in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every#description). This is because it is defined as (except with short circuiting):

let every = (array, func) => array.reduce((a, b) => a && func(b), true);

https://github.com/jwadhams/json-logic-js/blob/c1dd82f5b15d8a553bb7a0cfa841ab8a11a9c227/logic.js#L317-L319

I know that it can't be changed now, because that would break existing code. But I suspect there is code out there that assumes that all will return true on an empty set and is already broken. What I think needs to happen is that this is mentioned in the documentation as a big fat warning, so that people are aware of this unexpected behavior and write code accordingly.