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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions entries/jQuery.map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<title>jQuery.map()</title>
<signature>
<added>1.0</added>
<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

</argument>
<argument name="callback" type="Function">
<argument name="elementOfArray" type="Object" />
Expand All @@ -16,7 +16,7 @@
<signature>
<added>1.6</added>
<argument name="object" type="Object">
<desc>The Object to translate.</desc>
<desc>The non-Array-like Object to translate.</desc>
</argument>
<argument name="callback" type="Function">
<argument name="propertyOfObject" type="Object" />
Expand All @@ -28,16 +28,13 @@
<desc>Translate all items in an array or object to new array of items.</desc>
<longdesc>
<p>If you wish to process a jQuery object — for example, <code>$('div').map( callback );</code> — use <a href="/map/">.map()</a> instead. </p>
<p>The <code>$.map()</code> method applies a function to each item in an array or object and maps the results into a new array. <strong>Prior to jQuery 1.6</strong>, <code>$.map()</code> supports traversing <em>arrays only</em>. <strong>As of jQuery 1.6</strong> it also traverses objects.</p>
<p>Array-like objects &#x2014; those with a <code>.length</code> property <em>and</em> a value on the <code>.length - 1</code> index &#x2014; must be converted to actual arrays before being passed to <code>$.map()</code>. The jQuery library provides <a href="/jQuery.makeArray/">$.makeArray()</a> for such conversions.</p>
<p>The <code>$.map()</code> method applies a function to each item in an array or object and maps the results into a new array. <strong>Prior to jQuery 1.6</strong>, <code>$.map()</code> supports traversing <em>arrays and array-like objects only</em>. <strong>As of jQuery 1.6</strong> it also traverses objects.</p>
<p>Array-like objects &#x2014; those with a <code>.length</code> property <em>and</em> a value on the <code>.length - 1</code> index &#x2014; may be passed to <code>$.map()</code>.</p>
<pre><code>
// The following object masquerades as an array.
// The following object is array-like.
var fakeArray = { "length": 2, 0: "Addy", 1: "Subtracty" };

// Therefore, convert it to a real array
var realArray = $.makeArray( fakeArray )

// Now it can be used reliably with $.map()
// It can be used reliably with $.map()
$.map( realArray, function( val, i ) {
// Do something
});
Expand Down
14 changes: 7 additions & 7 deletions entries/jQuery.uniqueSort.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
<title>jQuery.uniqueSort()</title>
<signature>
<added>1.12-and-2.2</added>
<argument name="array" type="Array">
<desc>The Array of DOM elements.</desc>
<argument name="array" type="ArrayLikeObject">
<desc>The Array or an Array-like object of DOM elements.</desc>
</argument>
</signature>
<desc>Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.</desc>
<desc>Sorts an array or an array-like object of DOM elements, in place, with the duplicates removed. Note that this only works on arrays/array-likes of DOM elements, not strings or numbers.</desc>
<longdesc>
<p>The <code>$.uniqueSort()</code> function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the <em>exact same</em> node as one already in the array; two different nodes with identical attributes are not considered to be duplicates. This function only works on plain JavaScript arrays of DOM elements, and is chiefly used internally by jQuery. You probably will never need to use it.</p>
<p>The <code>$.uniqueSort()</code> function searches through an array or an array-like object of DOM elements, sorting the array/array-like, and removing any duplicate nodes. A node is considered a duplicate if it is the <em>exact same</em> node as one already in the input; two different nodes with identical attributes are not considered to be duplicates. This function only works on plain JavaScript arrays/array-like objects of DOM elements, and is chiefly used internally by jQuery. You probably will never need to use it.</p>
<p>Prior to jQuery 3.0, this method was called <code><a href="/jQuery.unique/">jQuery.unique()</a></code>.</p>
<p>As of jQuery 1.4 the results will always be returned in document order.</p>
</longdesc>
<example>
<desc>Removes any duplicate elements from the array of divs.</desc>
<code><![CDATA[
// unique() must take a native array
// uniqueSort() must take a native array
var divs = $( "div" ).get();

// Add 3 elements of class dup too (they are divs)
divs = divs.concat( $( ".dup" ).get() );
$( "div" ).eq( 1 ).text( "Pre-unique there are " + divs.length + " elements." );
$( "div" ).eq( 1 ).text( "Pre-uniqueSort there are " + divs.length + " elements." );

divs = jQuery.uniqueSort( divs );
$( "div" ).eq( 2 ).text( "Post-unique there are " + divs.length + " elements." )
$( "div" ).eq( 2 ).text( "Post-uniqueSort there are " + divs.length + " elements." )
.css( "color", "red" );
]]></code>
<css><![CDATA[
Expand Down