Writing the helper functions
I extracted my code from my naive function into helper functions and ran the tests.
All tests failed! Immediately I knew there was work to be done. But because tests output the differences between your expected output and the actual output, resolving this is easy.
After fixing the bugs, our functions are defined as follows:
Now all test cases for the helper functions are passing.
But the main replaceMultiValueFilters
still fails its tests.
What's happening? Fortunately we have eliminated potential problem
areas with reasonably high confidence.
Upon taking a closer look at the code, I noticed a very subtle problem:
Can you spot it? The code is designed to take our
input filters and replace each filter one at a time
in a loop. However, I was using filters.replace
instead of filter.replace
. This means each time
I called replace, I was calling it on the original
filter instead of our "under construction" filter.
This is why my tests of two field paths or more were failing.
I didn't catch this myself! In fact, my tests did.
When I refactored this logic into _replaceValues
and wrote tests for it, I discovered it was working just fine.
This left one remaining spot to check in my code, and potential
hours worth of debugging over a small small typo were saved.
After changing this line:
The function worked perfectly and passed all our test cases.