dvdzkwsk / react-reformed

Make React forms simple again, see it here:
https://react-reformed.zuko.me/
MIT License
541 stars 32 forks source link

fix(compose): allow composition #16

Closed nytr0gen closed 7 years ago

nytr0gen commented 7 years ago

Hi. The compose function wasn't functioning correctly. It reversed the functions on every call to the returned function (the example below is better than any way I can explain it).

> function compose (..._fns) {
...   return (...args) => {
.....     const [fn, ...fns] = _fns.reverse()
.....     return fns.reduce((acc, f) => f(acc), fn(...args))
.....   }
... }
> const a = v => v*2
> const b = v => v+3
> const ab = compose(a, b)
> ab(1)
8
> ab(1)
5
> ab(1)
8
> ab(1)
5
> function compose_fix (..._fns) {
...   const [fn, ...fns] = _fns.reverse()
...   return (...args) => {
.....     return fns.reduce((acc, f) => f(acc), fn(...args))
.....   }
... }
> const ab2 = compose_fix(a, b)
> ab2(1)
8
> ab2(1)
8
> ab2(1)
8
> ab2(1)
8
dvdzkwsk commented 7 years ago

Oh man, that's hilarious. Great find, I can't imagine the trouble you went through to track that down.

dvdzkwsk commented 7 years ago

Published as v1.1.2 to npm: https://github.com/davezuko/react-reformed/commit/a75d2deefa2d0bdc55764dcf72ee6b030ee1d77b. Thank you!