effector/patronum

Incorrect values for operators `and` and `or` operators

Closed this issue · 1 comments

Hi! I found a bug in the operators and and or.

If operator contains an array of one Store, it will return a non-Boolean value. But the type of the variable will be Store<boolean>.

For example:

const $storeOne = createStore('hello');
const $storeTwo = createStore(100);

const $andIncorrect = and($storeOne); 
// $andIncorrect: Store<bolean>
// $andIncorrect.watch(value => console.log(value)) => 'hello'

const $andCorrect = and($storeOne, $storeTwo); 
// $andCorrect: Store<bolean>
// $andCorrect.watch(value => console.log(value)) => true

Example: https://share.effector.dev/QhZwNUkL

This happens because the initial value is not set in the reduce method. And it returns the first value of the array.

I have found a solution and I can prepare a PR with a fix and tests. I want to start contributing to open source 😄
I think it's enough to add an initial value from first Store in array. What do you think? https://share.effector.dev/BlTcdKlA

values.reduce(
  (all, current) => Boolean(all) && Boolean(current),
  Boolean(values[0]),
),

The or operator has the same problem and solution

@sagdeev Thank you for reporting!