Skip to content
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

Keep original list order #16771

Open
wants to merge 6 commits into
base: gh-pages
Choose a base branch
from
Open
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
74 changes: 61 additions & 13 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,23 @@ _.prototype = {

if (value.length >= this.minChars && this._list.length > 0) {
this.index = -1;
// Populate list with options that match
this.ul.innerHTML = "";

this._list
.filter(function(item) {
return me.filter(item, value);
})
.sort(this.sort)
.every(function(text, i) {
me.ul.appendChild(me.item(text, value));

return i < me.maxItems - 1;
});
// Populate list with options that match
this.ul.innerHTML = "";

var filteredAndSortedList =
this._list
.filter(function(item) {
return me.filter(item, value);
});

$.mergeSort(filteredAndSortedList ,this.sort);

filteredAndSortedList
.every(function(text, i) {
me.ul.appendChild(me.item(text, value));

return i < me.maxItems - 1;
});

if (this.ul.children.length === 0) {
this.close();
Expand Down Expand Up @@ -354,6 +358,50 @@ $.regExpEscape = function (s) {
return s.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
}

$.mergeSort = function(array, compareFn) {

msort(array, 0, array.length);

function msort(array, begin, end) {
var size = end - begin;
if (size < 2) return;

var begin_right = begin + Math.floor(size / 2);

msort(array, begin, begin_right);
msort(array, begin_right, end);
merge(array, begin, begin_right, end);
}

function merge(array, begin, begin_right, end) {
for (; begin < begin_right; ++begin) {
if (compareFn(array[begin], array[begin_right]) > 0) {
var v = array[begin];
array[begin] = array[begin_right];
insert(array, begin_right, end, v);
}
}
}

function insert(array, begin, end, v) {
while (begin + 1 < end && compareFn(array[begin + 1], v) < 0) {
swap(array, begin, begin + 1);
++begin;
}
array[begin] = v;
}

function swap(array, a, b) {
var tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}

}




// Initialization

function init() {
Expand Down
13 changes: 7 additions & 6 deletions awesomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/awesompleteShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,12 @@ var shared = {
expect(li.getAttribute('aria-selected')).toBe('true');
expect(shared.awesompleter.status.textContent).toBe(li.textContent);
}
},
getListFromResults: function (awesompleter){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like an awesomplete API needs some tweaks (not only for tests). See my comment here #16725 (comment)

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator

Choose a reason for hiding this comment

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

Idea about splitting list setter/getter API #16790 (comment)

return [].slice.call(awesompleter.ul.children)
.map(function(value){
return value.textContent;
});
}

};
17 changes: 16 additions & 1 deletion test/awesompleteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe("awesomplete",function(){
shared.awesompleter = awesompleter;
})


it("should have a non-empty list", shared.expectListLengthToBe(12));

it("should have 3 min chars", shared.expectMinCharsToBe(3));
Expand All @@ -79,7 +80,21 @@ describe("awesomplete",function(){
it("should autocomplete when a suggestion is selected", shared.expectSelectingFirstSuggestionToWorkWith('test'));

it("should autocomplete the first suggestion when autoFirst is true", shared.expectAutoFirstToWorkWith('test'));
})

// see http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11
// Chrome's native Array.prototype.sort does not perform stable sorts with arrays above 10 items.
it("should perform a stable sort inside evaluation chain",function(){
var origList = ["New York","Staten Island","Jamaica","Schenectady","Flushing","White Plains","Great Neck","Yonkers","Utica","Elmira","Binghamton"];

awesompleter = new Awesomplete(dummyInput,{minChars:0, maxItems:20, sort: function(a,b){ return 0;}});
awesompleter.list = origList;
awesompleter.evaluate();

expect(shared.getListFromResults(awesompleter)).toEqual(origList);
});


});
});

describe("awesomplete helpers",function(){
Expand Down