Doesn't work with inputs that have default value declaration.
ckwong90 opened this issue · comments
Chris Kwong commented
for example:
const foo = (bool, num = 5) => bool ? {num} : {def: "default"}
expect(foo.length).toEqual(2) //Error: foo.length === 1 instead of 2
const memFoo = memoize(foo);
expect(memFoo(true, 5) === memFoo(true, 6)).toBe(false) //Error is true
expect(memFoo(true) === memFoo(true, 6)).toBe(false) //Error is true
Maybe add this to the limitation section in docs with the workaround
const foo = (bool, num) => {
num = num || 5; //initialize default value here
return bool ? {num} : {def: "default"}
}
Ricardo Tomasi commented
Classic:
We've discussed this extensively before and there doesn't seem to be many plausible use cases for the function length property.
https://esdiscuss.org/topic/function-length-and-default-parameters
There are even worse cases: https://medium.com/@robinpokorny/function-length-property-is-not-to-be-trusted-27c3b2d468d5
If using default args you should enforce the variadic strategy with:
const memFoo = memoize(foo, { strategy: memoize.strategies.variadic })
This should definitely be mentioned in the 'Gotchas' section of the README.