From 3739b7c103b21da3fc20ea98a441ae2bb285bc01 Mon Sep 17 00:00:00 2001 From: Kirollos Risk Date: Fri, 30 May 2014 09:44:24 -0700 Subject: [PATCH] Fixed #26 --- bower.json | 2 +- package.json | 2 +- src/fuse.js | 19 ++++++++++--------- src/fuse.min.js | 2 +- test/fuse-test.js | 13 +++++++++++++ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bower.json b/bower.json index 8707d32..23722d2 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "fuse.js", - "version": "1.1.2", + "version": "1.1.3", "main": "./src/fuse.js", "ignore": [ "test/" diff --git a/package.json b/package.json index a9c2cb7..121a3ad 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fuse.js", "author": "Kirollos Risk", - "version": "1.1.2", + "version": "1.1.3", "description": "Lightweight fuzzy-search", "license": "Apache", "main": "./src/fuse.js", diff --git a/src/fuse.js b/src/fuse.js index 9b50bcf..ff29c00 100644 --- a/src/fuse.js +++ b/src/fuse.js @@ -260,6 +260,7 @@ this.options.sortFn = options.sortFn || Fuse.defaultOptions.sortFn, this.options.keys = options.keys || Fuse.defaultOptions.keys; this.options.getFn = options.getFn || Fuse.defaultOptions.getFn; + this.options.shouldSort = options.shouldSort || Fuse.defaultOptions.shouldSort; }; Fuse.defaultOptions = { @@ -307,7 +308,7 @@ */ Fuse.prototype.search = function(pattern) { var searcher = new(this.options.searchFn)(pattern, this.options), - i, j, item, text, + i, j, item, list = this.list, dataLen = list.length, options = this.options, @@ -384,18 +385,18 @@ // the raw item, including the score, or simply the item itself, depending // on the specified option var getItem = options.includeScore ? function(i) { - return rawResults[i]; - } : function(i) { - return rawResults[i].item; - }; + return rawResults[i]; + } : function(i) { + return rawResults[i].item; + }; // Helper function, here for speed-up, which returns the idenfifer (via deepValue), // if the options specifies it, var getValue = options.id ? function(i) { - return Utils.deepValue(getItem(i), options.id); - } : function(i) { - return getItem(i); - }; + return Utils.deepValue(getItem(i), options.id); + } : function(i) { + return getItem(i); + }; // 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 , diff --git a/src/fuse.min.js b/src/fuse.min.js index 98afe42..76279fa 100644 --- a/src/fuse.min.js +++ b/src/fuse.min.js @@ -17,4 +17,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -!function(t){function e(t,n){this.list=t,this.options=n=n||{},this.options.sort="sort"in n?n.sort:e.defaultOptions.sort,this.options.includeScore="includeScore"in n?n.includeScore:e.defaultOptions.includeScore,this.options.searchFn=n.searchFn||e.defaultOptions.searchFn,this.options.sortFn=n.sortFn||e.defaultOptions.sortFn,this.options.keys=n.keys||e.defaultOptions.keys,this.options.getFn=n.getFn||e.defaultOptions.getFn}var n=function(t,e){if(e=e||{},this.options=e,this.options.location=e.location||n.defaultOptions.location,this.options.distance="distance"in e?e.distance:n.defaultOptions.distance,this.options.threshold="threshold"in e?e.threshold:n.defaultOptions.threshold,this.options.maxPatternLength=e.maxPatternLength||n.defaultOptions.maxPatternLength,this.pattern=e.caseSensitive?t:t.toLowerCase(),this.patternLen=t.length,this.patternLen>this.options.maxPatternLength)throw new Error("Pattern length is too long");this.matchmask=1<i;)this._bitapScore(e,l+o)<=u?i=o:d=o,o=Math.floor((d-i)/2+i);for(d=o,s=Math.max(1,l-o+1),r=Math.min(l+o,c)+this.patternLen,a=Array(r+2),a[r+1]=(1<=s;n--)if(p=this.patternAlphabet[t.charAt(n-1)],a[n]=0===e?(a[n+1]<<1|1)&p:(a[n+1]<<1|1)&p|((h[n+1]|h[n])<<1|1)|h[n+1],a[n]&this.matchmask&&(g=this._bitapScore(e,n-1),u>=g)){if(u=g,f=n-1,m.push(f),!(f>l))break;s=Math.max(1,2*l-f)}if(this._bitapScore(e+1,l)>u)break;h=a}return{isMatch:f>=0,score:g}};var i={deepValue:function(t,e){for(var n=0,e=e.split("."),i=e.length;i>n;n++){if(!t)return null;t=t[e[n]]}return t}};e.defaultOptions={id:null,caseSensitive:!1,includeScore:!1,shouldSort:!0,searchFn:n,sortFn:function(t,e){return t.score-e.score},getFn:i.deepValue,keys:[]},e.prototype.search=function(t){var e,n,o,s,r,a=new this.options.searchFn(t,this.options),h=this.list,p=h.length,c=this.options,l=this.options.keys,u=l.length,f=[],d={},g=[],m=function(t,e,n){void 0!==t&&null!==t&&"string"==typeof t&&(s=a.search(t),s.isMatch&&(r=d[n],r?r.score=Math.min(r.score,s.score):(d[n]={item:e,score:s.score},f.push(d[n]))))};if("string"==typeof h[0])for(var e=0;p>e;e++)m(h[e],e,e);else for(var e=0;p>e;e++)for(o=h[e],n=0;u>n;n++)m(this.options.getFn(o,l[n]),o,e);c.shouldSort&&f.sort(c.sortFn);for(var L=c.includeScore?function(t){return f[t]}:function(t){return f[t].item},S=c.id?function(t){return i.deepValue(L(t),c.id)}:function(t){return L(t)},e=0,F=f.length;F>e;e++)g.push(S(e));return g},"object"==typeof exports?module.exports=e:"function"==typeof define&&define.amd?define(function(){return e}):t.Fuse=e}(this); \ No newline at end of file +!function(t){function e(t,n){this.list=t,this.options=n=n||{},this.options.sort="sort"in n?n.sort:e.defaultOptions.sort,this.options.includeScore="includeScore"in n?n.includeScore:e.defaultOptions.includeScore,this.options.searchFn=n.searchFn||e.defaultOptions.searchFn,this.options.sortFn=n.sortFn||e.defaultOptions.sortFn,this.options.keys=n.keys||e.defaultOptions.keys,this.options.getFn=n.getFn||e.defaultOptions.getFn,this.options.shouldSort=n.shouldSort||e.defaultOptions.shouldSort}var n=function(t,e){if(e=e||{},this.options=e,this.options.location=e.location||n.defaultOptions.location,this.options.distance="distance"in e?e.distance:n.defaultOptions.distance,this.options.threshold="threshold"in e?e.threshold:n.defaultOptions.threshold,this.options.maxPatternLength=e.maxPatternLength||n.defaultOptions.maxPatternLength,this.pattern=e.caseSensitive?t:t.toLowerCase(),this.patternLen=t.length,this.patternLen>this.options.maxPatternLength)throw new Error("Pattern length is too long");this.matchmask=1<o;)this._bitapScore(e,l+i)<=u?o=i:d=i,i=Math.floor((d-o)/2+o);for(d=i,s=Math.max(1,l-i+1),r=Math.min(l+i,c)+this.patternLen,a=Array(r+2),a[r+1]=(1<=s;n--)if(p=this.patternAlphabet[t.charAt(n-1)],a[n]=0===e?(a[n+1]<<1|1)&p:(a[n+1]<<1|1)&p|((h[n+1]|h[n])<<1|1)|h[n+1],a[n]&this.matchmask&&(g=this._bitapScore(e,n-1),u>=g)){if(u=g,f=n-1,S.push(f),!(f>l))break;s=Math.max(1,2*l-f)}if(this._bitapScore(e+1,l)>u)break;h=a}return{isMatch:f>=0,score:g}};var o={deepValue:function(t,e){for(var n=0,e=e.split("."),o=e.length;o>n;n++){if(!t)return null;t=t[e[n]]}return t}};e.defaultOptions={id:null,caseSensitive:!1,includeScore:!1,shouldSort:!0,searchFn:n,sortFn:function(t,e){return t.score-e.score},getFn:o.deepValue,keys:[]},e.prototype.search=function(t){var e,n,i,s,r,a=new this.options.searchFn(t,this.options),h=this.list,p=h.length,c=this.options,l=this.options.keys,u=l.length,f=[],d={},g=[],S=function(t,e,n){void 0!==t&&null!==t&&"string"==typeof t&&(s=a.search(t),s.isMatch&&(r=d[n],r?r.score=Math.min(r.score,s.score):(d[n]={item:e,score:s.score},f.push(d[n]))))};if("string"==typeof h[0])for(var e=0;p>e;e++)S(h[e],e,e);else for(var e=0;p>e;e++)for(i=h[e],n=0;u>n;n++)S(this.options.getFn(i,l[n]),i,e);c.shouldSort&&f.sort(c.sortFn);for(var m=c.includeScore?function(t){return f[t]}:function(t){return f[t].item},L=c.id?function(t){return o.deepValue(m(t),c.id)}:function(t){return m(t)},e=0,F=f.length;F>e;e++)g.push(L(e));return g},"object"==typeof exports?module.exports=e:"function"==typeof define&&define.amd?define(function(){return e}):t.Fuse=e}(this); \ No newline at end of file diff --git a/test/fuse-test.js b/test/fuse-test.js index bfdfb29..7e369ec 100644 --- a/test/fuse-test.js +++ b/test/fuse-test.js @@ -33,6 +33,19 @@ vows.describe('Flat list of strings: ["Apple", "Orange", "Banana"]').addBatch({ assert.equal(result[0], 1); assert.equal(result[1], 2); }, + }, + 'When performing a fuzzy search for the term "nan"': { + topic: function(fuse) { + var result = fuse.search("nan"); + return result; + }, + 'we get a list of containing 2 items: [2, 1]': function(result) { + assert.equal(result.length, 2); + }, + 'whose values represent the indices of ["Banana", "Orange"]': function(result) { + assert.equal(result[0], 2); + assert.equal(result[1], 1); + }, } } }).export(module);