From 19e37a2da5f6608bfb6299fc5ee207fd6a86adb9 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Wed, 18 Nov 2015 20:34:22 -0500 Subject: initial commit --- fuse.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 fuse.js (limited to 'fuse.js') diff --git a/fuse.js b/fuse.js new file mode 100644 index 0000000..ad2153f --- /dev/null +++ b/fuse.js @@ -0,0 +1,19 @@ +/** + * Fuse - Lightweight fuzzy-search + * + * Copyright (c) 2012 Kirollos Risk . + * All Rights Reserved. Apache Software License 2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function(){function Searcher(pattern,options){options=options||{};var MATCH_LOCATION=options.location||0,MATCH_DISTANCE=options.distance||100,MATCH_THRESHOLD=options.threshold||.6,pattern=options.caseSensitive?pattern:pattern.toLowerCase(),patternLen=pattern.length;if(patternLen>32){throw new Error("Pattern length is too long")}var matchmask=1<=start;j--){charMatch=pattern_alphabet[text.charAt(j-1)];if(d===0){rd[j]=(rd[j+1]<<1|1)&charMatch}else{rd[j]=(rd[j+1]<<1|1)&charMatch|((last_rd[j+1]|last_rd[j])<<1|1)|last_rd[j+1]}if(rd[j]&matchmask){score=match_bitapScore(d,j-1);if(score<=score_threshold){score_threshold=score;best_loc=j-1;if(best_loc>MATCH_LOCATION){start=Math.max(1,2*MATCH_LOCATION-best_loc)}else{break}}}}if(match_bitapScore(d+1,MATCH_LOCATION)>score_threshold){break}last_rd=rd}return{isMatch:best_loc>=0,score:score}}}function Fuse(list,options){options=options||{};var keys=options.keys;this.search=function(pattern){var searcher=new Searcher(pattern,options),i,j,item,text,dataLen=list.length,bitapResult,rawResults=[],rawResultsLen,existingResult,results=[],compute=null;function analyzeText(text,entity,index){if(text!==undefined&&text!==null&&typeof text==="string"){bitapResult=searcher.search(text);if(bitapResult.isMatch){existingResult=rawResults[index];if(existingResult){existingResult.score=Math.min(existingResult.score,bitapResult.score)}else{rawResults.push({item:entity,score:bitapResult.score})}}}}if(typeof list[0]==="string"){for(i=0;i