From 0c0c3c2885aeeb993f90e97bce60e00a55f05190 Mon Sep 17 00:00:00 2001 From: Daniel Smilkov Date: Thu, 24 Jul 2014 12:04:31 -0400 Subject: [PATCH] fixed bug when the user specifies both ID and score in the results list --- src/fuse.js | 12 +++++++----- test/fuse-test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/fuse.js b/src/fuse.js index ba771cc..0bd3e04 100644 --- a/src/fuse.js +++ b/src/fuse.js @@ -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 , // 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; diff --git a/test/fuse-test.js b/test/fuse-test.js index 7e369ec..b8bde46 100644 --- a/test/fuse-test.js +++ b/test/fuse-test.js @@ -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); \ No newline at end of file