Skip to content

jQuery.map:jQuery.uniqueSort: Accept array-like input, fix typos #1214

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

Merged
merged 3 commits into from
Jan 9, 2023

Conversation

mgol
Copy link
Member

@mgol mgol commented Dec 19, 2022

1. jQuery.map: Accept array-like input

2. jQuery.uniqueSort: Accept array-like input, fix typos

Apart from array-like inputs being officially allowed in
jQuery.uniqueSort now, in a few places in examples
it used to be referred to as unique instead of uniqueSort.


The second one is particularly interesting. For some reason, there's even an example directly claiming jQuery.uniqueSort doesn't work on array-likes when its code says something completely opposite - especially that it's sometimes used on jQuery collections. We even test it on an "Arrayish" class!

mgol added 2 commits December 19, 2022 13:34
Apart from array-like inputs being officially allowed in
`jQuery.uniqueSort` now, in a few places in examples
it used to be referred to as `unique` instead of `uniqueSort`.
@mgol mgol requested review from timmywil and gibson042 December 19, 2022 13:54
@mgol mgol changed the title jQuery.map: Accept array-like input jQuery.map:jQuery.uniqueSort: Accept array-like input Dec 19, 2022
@mgol mgol changed the title jQuery.map:jQuery.uniqueSort: Accept array-like input jQuery.map:jQuery.uniqueSort: Accept array-like input, fix typos Dec 19, 2022
<argument name="array" type="Array">
<desc>The Array to translate.</desc>
<argument name="array" type="ArrayLikeObject">
<desc>The Array or an Array-like object to translate.</desc>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is true for all versions back to 1.0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timmywil I've just checked. In versions 1.0, 1.1, 1.2, 1.3, 1.4 & 1.5 jQuery.map is written using a for loop that would work with array-likes as well. Moreover, jQuery 1.2 implemented jQuery.fn.map which uses jQuery.map on jQuery objects which are not arrays.

jQuery 1.6 added support for iterating on objects in jQuery.map so it had to detect "arrays" separately. The 1.6 detection code is a bit cryptic:

// jquery objects are treated as arrays
isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ( ( length > 0 && elems[ 0 ] && elems[ length -1 ] ) || length === 0 || jQuery.isArray( elems ) )

but it does detect array-likes. 1.9 extracted that logic and uses:

isArray = isArraylike( elems ),

so even the util name has "array-like" in the name.

I think we can assume that array-likes have been supported since 1.0 even if that was not the original intent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at so many versions

Co-authored-by: Timmy Willison <4timmywil@gmail.com>
@mgol mgol requested a review from timmywil December 20, 2022 21:07
@mgol mgol merged commit 9685f8a into jquery:main Jan 9, 2023
@mgol mgol deleted the array-like-input-map-uniqueSort branch January 9, 2023 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants