From 58fb93b06e2ad90dd38bc44042c12c730dd85612 Mon Sep 17 00:00:00 2001 From: andwilley <31580078+andwilley@users.noreply.github.com> Date: Tue, 7 Aug 2018 02:41:04 -0700 Subject: [PATCH] Add example for custom equality function with multiple arguments to README (#29) * Update README.md * Update README.md --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 4962610..1ecb3e9 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,38 @@ Here is the expected [flow](http://flowtype.org) type signature for a custom equ type EqualityFn = (a: mixed, b: mixed) => boolean; ``` +#### Custom equality function with multiple arguments + +If the function you want to memoize takes multiple arguments, your custom equality function will be called once for each argument and will be passed each argument's new value and last value. + +```js +import memoizeOne from 'memoize-one'; + +const makeCountObj = (first, second, third) => ({ + first: first.count, + second: second.count, + third: third.count, +}); + +const areCountPropertiesEqual = (newArg, lastArg) => newArg.count === lastArg.count; +// runs once for first's new and last values, once for second's, etc. + +const memoizedMakeCountObj = memoizeOne(makeCountObj, areCountPropertiesEqual); + +const result1 = memoizedMakeCountObj( + {a: '?', count: 1}, + {a: '$', count: 2}, + {a: '#', count: 3} +); +const result2 = memoizedMakeCountObj( + {b: null, count: 1}, + {b: null, count: 2}, + {b: null, count: 3} +); + +result1 === result2; // true - same reference +``` + ## Installation ```bash