fzy.js
A javascript port of fzy's fuzzy finder scoring algorithm
Try it out online!
Installation
npm i --save fzy.js
Usage
score(needle, haystack)
var fzy = require('fzy.js')
fzy.score("amuser", "app/models/user.rb")
fzy.score("amuser", "app/models/customer.rb")
positions(needle, haystack)
fzy.positions("amuser", "app/models/user.rb")
fzy.positions("amuser", "app/models/customer.rb")
NB: score
and positions
must be called with matching needle and haystack,
doing otherwise is undefined. The caller needs to check that there is a match.
Full Example
var { sortBy, escapeRegExp } = require("lodash");
var fzy = require("fzy.js");
var list = [
"app/models/user.rb",
"app/models/order.rb",
"app/models/customer.rb"
];
var query = "amuser";
var regex = new RegExp(query.split('').map(escapeRegExp).join(".*"));
list = list.filter((s) => s.match(regex));
list = sortBy(list, (s) => -fzy.score(query, s));
list.slice(0, 10);
list.forEach((s) => {
var padded = "";
var p = fzy.positions(query, s);
for(var i = 0; i < query.length; i++) {
padded = padded.padEnd(p[i], ' ') + query[i];
}
console.log(s);
console.log(padded);
console.log("");
});