Add the ability to specify a comparator for array.unique()
for objects only
#860
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was looking at #859 and thought that it seemed like something that belonged in the core of Joi, not as a plugin/extension and had a pretty good idea of how I would implement it. This PR contains that initial implementation.
What It Does
It allows a user to specify a comparator function which is used only for objects.
In the event that a comparator isn't provided it just defaults to the standard behavior using
Hoek.deepEqual()
so the change has a default and also is backwards compatible.What It Doesn't Do
It doesn't allow a comparator to be provided for types other than
object
. Originally I wanted to make the parameter justcomparator
and use it for everything, but once I started implementing this I realized I couldn't do that without a noticeable decrease in performance because of how it works right now using an object map.The only cases I can think of where a comparator function would be useful for things other than objects is when the data needs to be modified, which that could just be done prior to validation. For example, if an array of numbers needs to have
Math.floor()
run on each value and in that case I imagine you would want the value to come out of validation already run throughMath.floor()
anyways. Others would be string manipulation and such like lowercase comparisons, etc.