Comparing version 0.0.2 to 0.1.0
12
fuzzy.js
@@ -1,6 +0,6 @@ | ||
/* | ||
fuzzy.js v0.0.2 | ||
(c) 2013 Ben Ripkens | ||
License: MIT | ||
*/ | ||
/** | ||
* fuzzy.js v0.1.0 | ||
* (c) 2016 Ben Ripkens | ||
* @license: MIT | ||
*/ | ||
(function (context) { | ||
@@ -81,3 +81,3 @@ 'use strict'; | ||
fuzzy.matchComparator = function matchComparator(m1, m2) { | ||
return m2.score - m1.score; | ||
return (m2.score - m1.score != 0) ? m2.score - m1.score : m1.term.length - m2.term.length; | ||
}; | ||
@@ -84,0 +84,0 @@ |
@@ -1,6 +0,6 @@ | ||
/* | ||
fuzzy.js v0.0.2 | ||
(c) 2013 Ben Ripkens | ||
License: MIT | ||
*/ | ||
(function(context){"use strict";var fuzzy=function fuzzy(term,query){var max=calcFuzzyScore(term,query);var termLength=term.length;if(fuzzy.analyzeSubTerms){for(var i=1;i<termLength&&i<fuzzy.analyzeSubTermDepth;i++){var subTerm=term.substring(i);var score=calcFuzzyScore(subTerm,query);if(score.score>max.score){score.term=term;score.highlightedTerm=term.substring(0,i)+score.highlightedTerm;max=score}}}return max};var calcFuzzyScore=function calcFuzzyScore(term,query){var score=0;var termLength=term.length;var queryLength=query.length;var highlighting="";var ti=0;var previousMatchingCharacter=-2;for(var qi=0;qi<queryLength&&ti<termLength;qi++){var qc=query.charAt(qi);var lowerQc=qc.toLowerCase();for(;ti<termLength;ti++){var tc=term.charAt(ti);if(lowerQc===tc.toLowerCase()){score++;if(previousMatchingCharacter+1===ti){score+=2}highlighting+=fuzzy.highlighting.before+tc+fuzzy.highlighting.after;previousMatchingCharacter=ti;ti++;break}else{highlighting+=tc}}}highlighting+=term.substring(ti,term.length);return{score:score,term:term,query:query,highlightedTerm:highlighting}};fuzzy.matchComparator=function matchComparator(m1,m2){return m2.score-m1.score};fuzzy.analyzeSubTerms=false;fuzzy.analyzeSubTermDepth=10;fuzzy.highlighting={before:"<em>",after:"</em>"};if(typeof module!=="undefined"&&module.exports){module.exports=fuzzy}else if(typeof define==="function"){define(function(){return fuzzy})}else{var previousFuzzy=context.fuzzy;fuzzy.noConflict=function(){context.fuzzy=previousFuzzy;return fuzzy};context.fuzzy=fuzzy}})(this); | ||
/** | ||
* fuzzy.js v0.1.0 | ||
* (c) 2016 Ben Ripkens | ||
* @license: MIT | ||
*/ | ||
(function(context){"use strict";var fuzzy=function fuzzy(term,query){var max=calcFuzzyScore(term,query);var termLength=term.length;if(fuzzy.analyzeSubTerms){for(var i=1;i<termLength&&i<fuzzy.analyzeSubTermDepth;i++){var subTerm=term.substring(i);var score=calcFuzzyScore(subTerm,query);if(score.score>max.score){score.term=term;score.highlightedTerm=term.substring(0,i)+score.highlightedTerm;max=score}}}return max};var calcFuzzyScore=function calcFuzzyScore(term,query){var score=0;var termLength=term.length;var queryLength=query.length;var highlighting="";var ti=0;var previousMatchingCharacter=-2;for(var qi=0;qi<queryLength&&ti<termLength;qi++){var qc=query.charAt(qi);var lowerQc=qc.toLowerCase();for(;ti<termLength;ti++){var tc=term.charAt(ti);if(lowerQc===tc.toLowerCase()){score++;if(previousMatchingCharacter+1===ti){score+=2}highlighting+=fuzzy.highlighting.before+tc+fuzzy.highlighting.after;previousMatchingCharacter=ti;ti++;break}else{highlighting+=tc}}}highlighting+=term.substring(ti,term.length);return{score:score,term:term,query:query,highlightedTerm:highlighting}};fuzzy.matchComparator=function matchComparator(m1,m2){return m2.score-m1.score!=0?m2.score-m1.score:m1.term.length-m2.term.length};fuzzy.analyzeSubTerms=false;fuzzy.analyzeSubTermDepth=10;fuzzy.highlighting={before:"<em>",after:"</em>"};if(typeof module!=="undefined"&&module.exports){module.exports=fuzzy}else if(typeof define==="function"){define(function(){return fuzzy})}else{var previousFuzzy=context.fuzzy;fuzzy.noConflict=function(){context.fuzzy=previousFuzzy;return fuzzy};context.fuzzy=fuzzy}})(this); |
{ | ||
"name": "fuzzy.js", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Approximate (fuzzy) string matching just like you are used to from Sublime Text and others.", | ||
"main": "fuzzy.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha", | ||
"compress": "uglifyjs fuzzy.js --comments > fuzzy.min.js" | ||
}, | ||
"config": { | ||
"ghooks": { | ||
"pre-commit": "npm run test && npm run compress && git add fuzzy.min.js" | ||
} | ||
}, | ||
"repository": { | ||
@@ -21,7 +24,13 @@ "type": "git", | ||
"approximate", | ||
"sublime" | ||
"sublime", | ||
"searching", | ||
"sorting" | ||
], | ||
"author": "Ben Ripkens <bripkens.dev@gmail.com>", | ||
"license": "MIT", | ||
"readmeFilename": "README.md" | ||
"devDependencies": { | ||
"ghooks": "1.0.3", | ||
"mocha": "2.4.5", | ||
"uglify-js": "2.6.2" | ||
} | ||
} |
@@ -1,24 +0,37 @@ | ||
# fuzzy.js [![Build Status](https://secure.travis-ci.org/bripkens/fuzzy.js.png)](https://secure.travis-ci.org/#!/bripkens/fuzzy.js) | ||
# fuzzy.js [![Build Status](https://travis-ci.org/bripkens/fuzzy.js.svg?branch=gh-pages)](https://travis-ci.org/bripkens/fuzzy.js) [![NPM version](https://badge.fury.io/js/fuzzy.js.png)](http://badge.fury.io/js/fuzzy.js) | ||
Fuzzy.js is a tiny library for approximate (fuzzy) string matching. In | ||
contrast to other algorithms like the Levenshtein Distance, | ||
fuzzy.js' algorithm is very lightweight and is aimed to provide an | ||
experience which you may know from editors such as Sublime Text 2, | ||
TextMate and others. | ||
Fuzzy.js is a library for approximate (fuzzy) string matching. | ||
Fuzzy.js is aimed at providing an | ||
experience which you may know from editors such as Sublime Text, | ||
Atom, TextMate and others. | ||
Further information is available through http://bripkens.github.com/fuzzy.js/. | ||
**Demo: http://bripkens.github.com/fuzzy.js/demo** | ||
# Installation | ||
## Installation | ||
You can install fuzzy.js using NPM and Bower just as you would expect: | ||
- Either download the [developer](https://raw.github.com/bripkens/fuzzy.js/master/fuzzy.js) | ||
or the [minified](https://raw.github.com/bripkens/fuzzy.js/master/fuzzy.min.js) | ||
version of fuzzy.js and include it in your HTML file. | ||
- NPM users can `npm install fuzzy.js` | ||
## Usage | ||
``` | ||
npm install fuzzy.js | ||
var query = 'majs'; | ||
var term = 'main.js'; | ||
bower install fuzzy.js | ||
var match = fuzzy(term, query); | ||
console.log(match.score); // 8 | ||
console.log(match.query); // 'majs' | ||
console.log(match.term); // 'main.js' | ||
console.log(match.highlightedTerm); // '<em>m</em><em>a</em>in.<em>j</em><em>s</em>' | ||
// sorting an array of matches | ||
var matches = [ ... ]; | ||
matches.sort(fuzzy.matchComparator); // sorts descending | ||
``` | ||
# License (MIT) | ||
## License (MIT) | ||
Copyright (c) 2012 Ben Ripkens | ||
Copyright (c) 2016 Ben Ripkens | ||
@@ -25,0 +38,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
@@ -1,3 +0,3 @@ | ||
var assert = require("assert"), | ||
fuzzy = require("../fuzzy.js"); | ||
var assert = require("assert"); | ||
var fuzzy = require("../fuzzy.js"); | ||
@@ -58,3 +58,3 @@ describe('fuzzy', function(){ | ||
assert.equal(10, match.score); | ||
assert.equal('Halle<l><u><j><a>', | ||
assert.equal('Halle<l><u><j><a>', | ||
match.highlightedTerm); | ||
@@ -69,3 +69,3 @@ }); | ||
assert.equal(9, match.score); | ||
assert.equal('-----------<H><e><l>--He<l>l<o>', match.highlightedTerm); | ||
@@ -78,7 +78,7 @@ }); | ||
it('should sort fuzzy matches', function() { | ||
var match0 = { score: 1 }, | ||
match1 = { score: 0 }, | ||
match2 = { score: 5 }, | ||
match3 = { score: 2 }, | ||
matches = [match0, match1, match2, match3]; | ||
var match0 = { score: 1 }; | ||
var match1 = { score: 0 }; | ||
var match2 = { score: 5 }; | ||
var match3 = { score: 2 }; | ||
var matches = [match0, match1, match2, match3]; | ||
@@ -89,3 +89,14 @@ matches.sort(fuzzy.matchComparator); | ||
}); | ||
it('should sort matches with equal scores according to term length', function() { | ||
var match0 = { score: 1, term: 'cardealer' }; | ||
var match1 = { score: 1, term: 'car' }; | ||
var match2 = { score: 1, term: 'carpark' }; | ||
var matches = [match0, match1, match2]; | ||
matches.sort(fuzzy.matchComparator); | ||
assert.deepEqual([match1, match2, match0], matches); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
2
43
20351
3
12
340
1