Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5266f23

Browse files
authored
Selector: Implement theuniqueSort chainable method
Some APIs, like `.prevAll()`, return elements in the reversed order, causingconfusing behavior when used with wrapping methods (seegh-5149 for more info) To provide an easy workaround, this commit implements a chainable `uniqueSort`method on jQuery objects, an equivalent of `jQuery.uniqueSort`.Fixesgh-5166Closesgh-5168
1 parent716130e commit5266f23

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

‎src/selector/uniqueSort.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import jQuery from "../core.js";
22
importdocumentfrom"../var/document.js";
33
importsortfrom"../var/sort.js";
44
importsplicefrom"../var/splice.js";
5+
importslicefrom"../var/slice.js";
56

67
varhasDuplicate;
78

@@ -87,3 +88,7 @@ jQuery.uniqueSort = function( results ) {
8788

8889
returnresults;
8990
};
91+
92+
jQuery.fn.uniqueSort=function(){
93+
returnthis.pushStack(jQuery.uniqueSort(slice.apply(this)));
94+
};

‎test/unit/selector.js

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,20 +1923,8 @@ QUnit.test( "find in document fragments", function( assert ) {
19231923
assert.strictEqual(elem.length,1,"Selection works");
19241924
});
19251925

1926-
QUnit.test("jQuery.uniqueSort",function(assert){
1927-
assert.expect(14);
1928-
1929-
functionArrayish(arr){
1930-
vari=this.length=arr.length;
1931-
while(i--){
1932-
this[i]=arr[i];
1933-
}
1934-
}
1935-
Arrayish.prototype={
1936-
sliceForTestOnly:[].slice
1937-
};
1938-
1939-
vari,tests,
1926+
functiongetUniqueSortFixtures(){
1927+
vari,
19401928
detached=[],
19411929
body=document.body,
19421930
fixture=document.getElementById("qunit-fixture"),
@@ -1951,7 +1939,7 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
19511939
detached2.appendChild(document.createElement("li")).id="detachedChild"+i;
19521940
}
19531941

1954-
tests={
1942+
return{
19551943
"Empty":{
19561944
input:[],
19571945
expected:[]
@@ -1992,15 +1980,61 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) {
19921980
length:3
19931981
}
19941982
};
1983+
}
1984+
1985+
QUnit.test("jQuery.uniqueSort",function(assert){
1986+
assert.expect(14);
19951987

1996-
jQuery.each(tests,function(label,test){
1997-
varlength=test.length||test.input.length;
1998-
// We duplicate `test.input` because otherwise it is modified by `uniqueSort`
1988+
varfixtures=getUniqueSortFixtures();
1989+
1990+
functionArrayish(arr){
1991+
vari=this.length=arr.length;
1992+
while(i--){
1993+
this[i]=arr[i];
1994+
}
1995+
}
1996+
Arrayish.prototype={
1997+
sliceForTestOnly:[].slice
1998+
};
1999+
2000+
jQuery.each(fixtures,function(label,fixture){
2001+
varlength=fixture.length||fixture.input.length;
2002+
2003+
// We duplicate `fixture.input` because otherwise it is modified by `uniqueSort`
19992004
// and the second test becomes worthless.
2000-
assert.deepEqual(jQuery.uniqueSort(test.input.slice(0)).slice(0,length),
2001-
test.expected,label+" (array)");
2002-
assert.deepEqual(jQuery.uniqueSort(newArrayish(test.input)).sliceForTestOnly(0,length),
2003-
test.expected,label+" (quasi-array)");
2005+
assert.deepEqual(
2006+
jQuery.uniqueSort(fixture.input.slice(0))
2007+
.slice(0,length),
2008+
fixture.expected,
2009+
label+" (array)"
2010+
);
2011+
2012+
assert.deepEqual(
2013+
jQuery.uniqueSort(newArrayish(fixture.input))
2014+
.sliceForTestOnly(0,length),
2015+
fixture.expected,
2016+
label+" (quasi-array)"
2017+
);
2018+
});
2019+
});
2020+
2021+
QUnit.test("uniqueSort()",function(assert){
2022+
assert.expect(28);
2023+
2024+
varfixtures=getUniqueSortFixtures();
2025+
2026+
jQuery.each(fixtures,function(label,fixture){
2027+
varlength=fixture.length||fixture.input.length,
2028+
fixtureInputCopy=fixture.input.slice(0),
2029+
sortedElem=jQuery(fixture.input).uniqueSort();
2030+
2031+
assert.deepEqual(fixture.input,fixtureInputCopy,"Fixture not modified ("+label+")");
2032+
2033+
assert.deepEqual(sortedElem.slice(0,length).toArray(),fixture.expected,label);
2034+
2035+
// Chaining
2036+
assert.ok(sortedEleminstanceofjQuery,"chaining");
2037+
assert.deepEqual(sortedElem.end().toArray(),fixture.input,label);
20042038
});
20052039
});
20062040

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp