-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create @automattic/js-utils
package and add uniqueBy
#50263
Conversation
More or less copied from 30secondsofcode.org/js/s/unique-elements-by |
This PR does not affect the size of JS and CSS bundles shipped to the user's browser. Generated by performance advisor bot at iscalypsofastyet.com. |
uniqueBy
helper@automattic/js-utils
package and add uniqueBy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
9bbe2ec
to
71b66d6
Compare
extract `uniqueBy` into `js-utils` pkg and use TS remove comment about `null` and `undefined` values
Co-authored-by: sarayourfriend <saram@fastmail.com>
71b66d6
to
5c07bc8
Compare
How's ours different from lodash's? https://lodash.com/docs/4.17.15#uniqBy 😬 |
It serves the same purpose. There's an ongoing initiative (mostly lead by @tyxla) to replace some of lodash with either native JS code (which is probably most of the usages) and/or introduce similar functions where this isn't possible. This PR is mostly motivated by not introducing new usages of lodash for some of my other work where I need this functionality. |
Ours is essentially much simpler 😉 The Lodash one uses I'm also planning to post about some of the motivations here, so stay tuned @ockham 😉 |
For added motivation on rolling out our own, Of course, a lot of the code it pulls in is is already indirectly being used elsewhere in Calypso, but if the goal is to get rid of |
|
||
function uniqueBy< T >( arr: T[], fn: ( a: T, b: T ) => boolean = defaultCompare ): T[] { | ||
return arr.reduce( ( acc, v ) => { | ||
if ( ! acc.some( ( x: any ) => fn( v, x ) ) ) acc.push( v ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like O(n²)
in all cases. Via an Object
/Set
maybe we can make this to always be O(n)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A note that in this case we would need to move to a key function vs. the compare one, but given that we wanted to mimic uniqBy
and not uniqWith
that seems sensible trade-off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry I totally missed that feedback. I'll make sure to have a look at it 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries!
@@ -0,0 +1,10 @@ | |||
const defaultCompare = ( a: any, b: any ): boolean => a === b; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use unknown
instead of any
in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you allowed to do comparison operations on unknown
?
Update: you are! https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgUwGYEMCuAbKBhEBbABxQCcEYBeGAChQC4Y0wBrMEAdzABoYAjexluzABKCgD4YKCuUo8A3ACggA
I didn't know that, unknown
would definitely be preferred in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option, probably the cleanest type-wise, is to make the function generic:
function defaultComp< A, B >( a: A, b: B ) {
return a === b;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I totally missed that feedback. I'll make sure to follow up with a PR!
Changes proposed in this Pull Request
@automattic/js-utils
packageuniqueBy
utility function(mostly) extracted from #50077
Testing instructions