z-pattern-matching / z

Pattern Matching for Javascript
https://z-pattern-matching.github.io/
Apache License 2.0
1.72k stars 50 forks source link

Help, with documentation. #55

Closed nandotorterolo closed 1 year ago

nandotorterolo commented 5 years ago

Reamde says

matches([1])(
  (a, b,  tail)      => 'Will not match here',
  (a = 2, tail = []) => 'Will not match here',
  (a = 1, tail)      => 'Will match here, tail = []'
)

but

     let a = Z.matches([1])(
            (a = 1, tail) => {
                return 'a = 1, b = []'
            }
        );
        console.log("a");
        console.log(a); // undefined

        let b = Z.matches([1])(
            (b, tail = []) => {
                return 'a = 1, b = []'
            }
        );
        console.log("b");
        console.log(b); //  a = 1, b = []
leonardiwagner commented 5 years ago

Hello @nandotorterolo! Gracias for reporting that!

I'd say this is a bug on library rather than a documentation error

Your second example works because you explicitly tell tail =[] , and the first doesn't because Z is understanding to match tail = undefined, the problem is in this line: https://github.com/z-pattern-matching/z/blob/master/src/matchArray.js#L20

Comparing last match argument (in that case tail) with the actual value: deepEqual(matchArgs[matchArgs.length - 1].value, [])

I think we need to swap:

Then it works fine! However, doing this breaks a test. We just need to check if that really breaks something, or we just need to update the tests.

I'm busy today, but if until tomorrow if we don't receive any fix for that, I'll try to fix this bug myself, thanks again!