A shameless rip off of vscode fuzzy scorer algorithm used to search files and stuff.
npm install --save vscode-fuzzy-scorer
import { compareFilePathsByFuzzyScore } from 'vscode-fuzzy-scorer';
const sourceA = '/some/path/fileA.txt';
const sourceB = '/some/path/other/fileB.txt';
const sourceC = '/unrelated/some/path/other/fileC.txt';
const query = 'path fileB';
const result = [sourceA, sourceB, sourceC].sort((r1, r2) =>
compareFilePathsByFuzzyScore({ pathA: r1, pathB: r2, query })
);
console.log(result);
/*
Result:
[
'/some/path/other/fileB.txt' // sourceB
'/some/path/fileA.txt' // sourceA
'/unrelated/some/path/other/fileC.txt' // sourceC
]
*/
or
import { scoreFilePathFuzzy } from 'vscode-fuzzy-scorer';
const path = '/xyz/some/path/someFile123.txt';
const query = 'xyz some';
const result = scoreFilePathFuzzy({ path, query });
console.log(result);
/*
Result:
{
"score": 131098,
"labelMatch": [
{
"start": 0,
"end": 4
}
],
"descriptionMatch": [
{
"start": 1,
"end": 4
}
]
}
*/