Testing purified functions

When we considered the following roundFix special function, which required us to use the state to accumulate the differences due to rounding, we produced a new version by providing the current state as an added parameter and by having the function return two values—the rounded one and the updated state:

const roundFix2 = (a, n) => {  let r = a > 0 ? Math.ceil(n) : Math.floor(n);  a += n - r;  return {a, r};};

This function is now pure, but testing it requires validating not only the returned values but also the updated states. We can base our tests on the experiments we did previously. Once again, we have to use toBeCloseTo() for dealing with floating-point numbers, but we can use toBe() with integers, which produces ...

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.