From a529f5eb68e2096006de7d4f942271686cf1c7d5 Mon Sep 17 00:00:00 2001 From: Bruno PIERRE Date: Fri, 17 Dec 2021 17:49:34 +0100 Subject: [PATCH 1/5] Fix splice on non Array Without this fix it does that TypeError: results.splice is not a function at Function.jQuery.uniqueSort (https://code.jquery.com/jquery-git.js:664:12) at jQuery.fn.init.find (https://code.jquery.com/jquery-git.js:2394:27) at gocusihafe.js:3:4 Reproduced here: https://jsbin.com/gocusihafe/1/edit?html,css,js,console,output --- src/selector/uniqueSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/selector/uniqueSort.js b/src/selector/uniqueSort.js index 127cc70683..b01a06266a 100644 --- a/src/selector/uniqueSort.js +++ b/src/selector/uniqueSort.js @@ -80,7 +80,7 @@ jQuery.uniqueSort = function( results ) { } } while ( j-- ) { - results.splice( duplicates[ j ], 1 ); + Array.prototype.splice.call( results, duplicates[ j ] , 1 ); } } From 0290812c086a24ffcf4af9a3569da3cd5388d682 Mon Sep 17 00:00:00 2001 From: Bruno PIERRE Date: Mon, 10 Jan 2022 09:48:50 +0100 Subject: [PATCH 2/5] fix TypeError: results.splice is not a function Fixed by creating a var file for splice as for other Array methods --- src/selector/uniqueSort.js | 3 ++- src/var/splice.js | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 src/var/splice.js diff --git a/src/selector/uniqueSort.js b/src/selector/uniqueSort.js index b01a06266a..e9438485c4 100644 --- a/src/selector/uniqueSort.js +++ b/src/selector/uniqueSort.js @@ -1,6 +1,7 @@ import jQuery from "../core.js"; import document from "../var/document.js"; import sort from "../var/sort.js"; +import splice from "../var/splice.js"; var hasDuplicate; @@ -80,7 +81,7 @@ jQuery.uniqueSort = function( results ) { } } while ( j-- ) { - Array.prototype.splice.call( results, duplicates[ j ] , 1 ); + splice.call( results, duplicates[ j ], 1 ); } } diff --git a/src/var/splice.js b/src/var/splice.js new file mode 100644 index 0000000000..7b3661cd13 --- /dev/null +++ b/src/var/splice.js @@ -0,0 +1,3 @@ +import arr from "./arr.js"; + +export default arr.splice; From 3e03d0620f7b00fe983bfaf47d96ac89d39ec3fa Mon Sep 17 00:00:00 2001 From: Bruno PIERRE Date: Mon, 17 Jan 2022 17:05:12 +0100 Subject: [PATCH 3/5] Fix test of uniqueSort for Arrayish Object There was a bug in the test the input was modified to the good result before the second way of testing with not true Array object. --- test/unit/selector.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/unit/selector.js b/test/unit/selector.js index 41d9d0f70a..b00869a84e 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -1894,9 +1894,7 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) { } } Arrayish.prototype = { - slice: [].slice, - sort: [].sort, - splice: [].splice + sliceForTestOnly: [].slice }; var i, tests, @@ -1958,8 +1956,9 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) { jQuery.each( tests, function( label, test ) { var length = test.length || test.input.length; - assert.deepEqual( jQuery.uniqueSort( test.input ).slice( 0, length ), test.expected, label + " (array)" ); - assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).slice( 0, length ), test.expected, label + " (quasi-array)" ); + //we duplicate the test.input, because if not it is modified by the uniqueSort and the second test becomes worthless + assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ), test.expected, label + " (array)" ); + assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ), test.expected, label + " (quasi-array)" ); } ); } ); From fb0b6cba22e6caacaca2300f089bcd41d049bec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 24 Jan 2022 17:55:05 +0100 Subject: [PATCH 4/5] Apply suggestions from code review --- test/unit/selector.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/selector.js b/test/unit/selector.js index b00869a84e..cef9061649 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -1956,8 +1956,9 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) { jQuery.each( tests, function( label, test ) { var length = test.length || test.input.length; - //we duplicate the test.input, because if not it is modified by the uniqueSort and the second test becomes worthless - assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ), test.expected, label + " (array)" ); + // We duplicate `test.input` because otherwise it is modified by `uniqueSort` + // and the second test becomes worthless. + assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ), test.expected, label + " (array)" ); assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ), test.expected, label + " (quasi-array)" ); } ); } ); From 927a0ef8b74de7cfd90de7daab45e5cc77dc41cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Mon, 24 Jan 2022 18:22:13 +0100 Subject: [PATCH 5/5] Update test/unit/selector.js --- test/unit/selector.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/unit/selector.js b/test/unit/selector.js index cef9061649..ab22261fd0 100644 --- a/test/unit/selector.js +++ b/test/unit/selector.js @@ -1958,8 +1958,10 @@ QUnit.test( "jQuery.uniqueSort", function( assert ) { var length = test.length || test.input.length; // We duplicate `test.input` because otherwise it is modified by `uniqueSort` // and the second test becomes worthless. - assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ), test.expected, label + " (array)" ); - assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ), test.expected, label + " (quasi-array)" ); + assert.deepEqual( jQuery.uniqueSort( test.input.slice( 0 ) ).slice( 0, length ), + test.expected, label + " (array)" ); + assert.deepEqual( jQuery.uniqueSort( new Arrayish( test.input ) ).sliceForTestOnly( 0, length ), + test.expected, label + " (quasi-array)" ); } ); } );