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

Remote suggestions should appear after local/prefetch suggestions #40

Merged
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
49 changes: 24 additions & 25 deletions src/js/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,35 +214,34 @@ var Dataset = (function() {
},

_processRemoteSuggestions: function(callback, matchedItems) {
var that = this;

return function(data) {
var remoteAndLocalSuggestions = {}, dedupedSuggestions = [];

//convert remoteSuggestions to object
utils.each(data, function(index, item) {
if (utils.isString(item)) {
remoteAndLocalSuggestions[item] = { value: item };
} else {
remoteAndLocalSuggestions[item.value] = item;
}
});
//convert remote suggestions to object
utils.each(data, function(i, remoteItem) {
var isDuplicate = false;

//dedup local and remote suggestions
utils.each(matchedItems, function(index, item) {
if (remoteAndLocalSuggestions[item.value]) {
return true;
}
if (utils.isString(item)) {
remoteAndLocalSuggestions[item] = { value: item };
} else {
remoteAndLocalSuggestions[item.value] = item;
}
});
remoteItem = utils.isString(remoteItem) ?
{ value: remoteItem } : remoteItem;

// checks for duplicates
utils.each(matchedItems, function(i, localItem) {
if (remoteItem.value === localItem.value) {
isDuplicate = true;

// break out of each loop
return false;
}
});

!isDuplicate && matchedItems.push(remoteItem);

//convert combined suggestions object back into an array
utils.each(remoteAndLocalSuggestions, function(index, item) {
dedupedSuggestions.push(item);
// if we're at the limit, we no longer need to process
// the remote results and can break out of the each loop
return matchedItems.length < that.limit;
});
callback && callback(dedupedSuggestions);

callback && callback(matchedItems);
};
},

Expand Down
16 changes: 10 additions & 6 deletions test/dataset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,16 @@ describe('Dataset', function() {
});

it('concatenates local and remote results and dedups them', function() {
var localSuggestions = [expectedItemHash.cake, expectedItemHash.coffee];
var remoteSuggestions = [expectedItemHash.coconut, expectedItemHash.cake]; // not actually used, does not work, no remote request triggered, this is already in the local storage
var func = this.dataset._processRemoteSuggestions(function(items) {
expect(items).toEqual([expectedItemHash.coconut, expectedItemHash.cake, expectedItemHash.coffee]);
}, localSuggestions);
func(remoteSuggestions);
var local = [expectedItemHash.cake, expectedItemHash.coffee],
remote = [expectedItemHash.coconut, expectedItemHash.cake];

this.dataset._processRemoteSuggestions(function(items) {
expect(items).toEqual([
expectedItemHash.cake,
expectedItemHash.coffee,
expectedItemHash.coconut
]);
}, local)(remote);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's the drugs, but I'm not following this (remote) part of the test. Otherwise the whole thing looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

_prcessRemoteSuggestions returns a function that's expected to be called with the results of the remote request. You wrote the code!

Copy link
Contributor

Choose a reason for hiding this comment

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

😦 clearly it's past my bedtime…

});

it('sorts results: local first, then remote, sorted by graph weight / score within each local/remote section', function() {
Expand Down