fixed bug when the user specifies both ID and score in the results list

This commit is contained in:
Daniel Smilkov 2014-07-24 12:04:31 -04:00
parent 9ea17d582e
commit 0c0c3c2885
2 changed files with 45 additions and 5 deletions

View File

@ -393,19 +393,21 @@
return rawResults[i].item;
};
// Helper function, here for speed-up, which returns the idenfifer (via deepValue),
// Helper function, here for speed-up, which replaces the item with its value (via deepValue),
// if the options specifies it,
var getValue = options.id ? function(i) {
return Utils.deepValue(getItem(i), options.id);
var replaceValue = options.id ? function(i) {
rawResults[i].item = Utils.deepValue(rawResults[i].item, options.id);
} : function(i) {
return getItem(i);
return
};
// From the results, push into a new array only the item identifier (if specified)
// of the entire item. This is because we don't want to return the <rawResults>,
// since it contains other metadata;
for (var i = 0, len = rawResults.length; i < len; i++) {
results.push(getValue(i));
// replace the item with its value, which can be its id if the options specifies it
replaceValue(i);
results.push(getItem(i));
}
return results;

View File

@ -301,4 +301,42 @@ vows.describe('Only include ID in results list, with "ISBN"').addBatch({
},
}
}
}).export(module);
vows.describe('Include both ID and score in results list').addBatch({
'Options:': {
topic: function() {
var books = [{
"ISBN": "0765348276",
"title": "Old Man's War",
"author": "John Scalzi"
}, {
"ISBN": "0312696957",
"title": "The Lock Artist",
"author": "Steve Hamilton"
}];
var options = {
keys: ["title", "author"],
id: "ISBN",
includeScore:true
}
var fuse = new Fuse(books, options)
return fuse;
},
'When searching for the term "Stve"': {
topic: function(fuse) {
var result = fuse.search("Stve");
return result;
},
'we get a list containing 1 item': function(result) {
assert.equal(result.length, 1);
},
'whose value is the ISBN of the book': function(result) {
assert.equal(result[0].item, '0312696957');
},
'and has a score different than zero': function(result) {
assert.isNotZero(result[0].score);
}
}
}
}).export(module);