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

Add match structure object for safer HTML wrapping of strings #17

Open
wants to merge 3 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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ console.log(matches);
// [ '<b>a<c>o<n>ing', 'a mighty <b>ear <c>a<n>oe' ]
```

Chewbacca: sometimes you don't want to get unsafely wrapped html structures,
and you'd rather do it yourself later. Prepending and appending strings is
cool until you use React and don't want XSS. You can tell fuzzy.js to return
small 'match info' objects to suit your needs better.

```javascript
var list = ['rey', 'kylo ren', 'finn'];
var options = { returnMatchInfo: true };
var results = fuzzy.filter('ren', list, options);
console.log(results);
// [ { string:
// [ {match: true, char: 'r'},
// {match: true, char: 'e'},
// {match: false, char: 'y'} ],
// score: 4,
// index: 0,
// original: 'rey' },
// { string:
// [ {match: false, char: 'k'},
// {match: false, char: 'y'},
// {match: false, char: 'l'},
// {match: false, char: 'o'},
// {match: false, char: ' '},
// {match: true, char: 'r'},
// {match: true, char: 'e'},
// {match: false, char: 'n'} ],
// score: 4,
// index: 1,
// original: 'kylo ren' } ]
```

## Examples
Check out the html files in the [examples](https://github.com/mattyork/fuzzy/tree/master/examples) directory.

Expand Down
25 changes: 21 additions & 4 deletions lib/fuzzy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ fuzzy.test = function(pattern, string) {

// If `pattern` matches `string`, wrap each matching character
// in `opts.pre` and `opts.post`. If no match, return null
// If `opts.returnMatchInfo` is true, return an array of objects
// with boolean match info.
// `opts.pre` and `opts.post` can be used with `opts.returnMatchInfo`
// if necessary.
fuzzy.match = function(pattern, string, opts) {
opts = opts || {};
var patternIdx = 0
Expand All @@ -45,18 +49,21 @@ fuzzy.match = function(pattern, string, opts) {
, pre = opts.pre || ''
// suffix
, post = opts.post || ''
, returnMatchInfo = opts.returnMatchInfo || false
// String to compare against. This might be a lowercase version of the
// raw string
, compareString = opts.caseSensitive && string || string.toLowerCase()
, ch, compareChar;
, ch, compareChar, chMatch;

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

// For each character in the string, either add it to the result
// or wrap in template if it's the next string in the pattern
for(var idx = 0; idx < len; idx++) {
chMatch = false;
ch = string[idx];
if(compareString[idx] === pattern[patternIdx]) {
chMatch = true;
ch = pre + ch + post;
patternIdx += 1;

Expand All @@ -66,12 +73,22 @@ fuzzy.match = function(pattern, string, opts) {
currScore = 0;
}
totalScore += currScore;
result[result.length] = ch;

// assemble match info object if requested
if(returnMatchInfo) {
result[result.length] = {match: chMatch, char: ch};
} else {
result[result.length] = ch;
}
}

// return rendered string if we have a match for every char
// return rendered string/array if we have a match for every char
if(patternIdx === pattern.length) {
return {rendered: result.join(''), score: totalScore};
// join if not returning match info object
if(!returnMatchInfo) {
result = result.join('');
}
return {rendered: result, score: totalScore};
}

return null;
Expand Down
18 changes: 18 additions & 0 deletions test/fuzzy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ describe('fuzzy', function(){
})[0].string;
expect(rendered).to.eql('c<a>c<b>c');
});
it('should optionally return an object surrounding each character', function(){
var fuzzyMatched = fuzzy.filter('a', ['a'], {
returnMatchInfo: true
})[0].string;
expect(fuzzyMatched).to.eql([{match: true, char: 'a'}]);

fuzzyMatched = fuzzy.filter('a', ['ab'], {
returnMatchInfo: true
})[0].string;
expect(fuzzyMatched).to.eql([{match: true, char: 'a'}, {match: false, char: 'b'}]);

fuzzyMatched = fuzzy.filter('a', ['ab'], {
returnMatchInfo: true
, pre: "tah"
, post: "zah!"
})[0].string;
expect(fuzzyMatched).to.eql([{match: true, char: 'tahazah!'}, {match: false, char: 'b'}]);
})
it('should use optional func to get string out of array entry', function() {
var arr = [{arg: 'hizzahpooslahp'}, {arg: 'arppg'}];
expect(fuzzy.filter('poop', arr, {
Expand Down