Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return indices of matched characters #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/fuzzy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ fuzzy.match = function(pattern, str, opts) {
// String to compare against. This might be a lowercase version of the
// raw string
, compareString = opts.caseSensitive && str || str.toLowerCase()
, ch;
, ch
, matchIndices = []
;

pattern = opts.caseSensitive && pattern || pattern.toLowerCase();

Expand All @@ -62,6 +64,7 @@ fuzzy.match = function(pattern, str, opts) {

// consecutive characters should increase the score more than linearly
currScore += 1 + currScore;
matchIndices.push(idx);
} else {
currScore = 0;
}
Expand All @@ -73,7 +76,7 @@ fuzzy.match = function(pattern, str, opts) {
if(patternIdx === pattern.length) {
// if the string is an exact match with pattern, totalScore should be maxed
totalScore = (compareString === pattern) ? Infinity : totalScore;
return {rendered: result.join(''), score: totalScore};
return {rendered: result.join(''), score: totalScore, indices: matchIndices};
}

return null;
Expand All @@ -86,6 +89,7 @@ fuzzy.match = function(pattern, str, opts) {
// string: '<b>lah' // The rendered string
// , index: 2 // The index of the element in `arr`
// , original: 'blah' // The original element in `arr`
// , indices: [0]
// }]
//
// `opts` is an optional argument bag. Details:
Expand Down Expand Up @@ -124,6 +128,7 @@ fuzzy.filter = function(pattern, arr, opts) {
, score: rendered.score
, index: idx
, original: element
, indices: rendered.indices
};
}
return prev;
Expand All @@ -141,4 +146,3 @@ fuzzy.filter = function(pattern, arr, opts) {


}());

5 changes: 5 additions & 0 deletions test/fuzzy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe('fuzzy', function(){
opts.caseSensitive = false;
expect(fuzzy.match('AB', 'AB', opts)).to.not.equal(null);
});
it('should return indices of matching characters', function (){
expect(fuzzy.match('bd','abcde').indices).to.deep.equal([1,3]);
})
xit('should return the same score for matches in the middle as matches at beginning', function(){
// TODO: Dont know how I feel about this. Sublime weights characters that
// appear toward the beginning of the string a bit higher
Expand Down Expand Up @@ -86,11 +89,13 @@ describe('fuzzy', function(){
expect(result[0].string).to.equal('aba');
expect(result[0].index).to.equal(0);
expect(result[0]).to.have.property('score');
expect(result[0].indices).to.deep.equal([0,1]);

// verify second result
expect(result[1].string).to.equal('cacb');
expect(result[1].index).to.equal(2);
expect(result[1]).to.have.property('score');
expect(result[1].indices).to.deep.equal([1,3]);
});
it('should use optional template stringing to wrap each element', function(){
var rendered = fuzzy.filter('a', ['a'], {
Expand Down