Skip to content

Commit

Permalink
use subFilterFirstIndex instead of indexOf(charAt)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipperolet committed Mar 7, 2024
1 parent 06f9e8e commit 23a4e29
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions front/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,28 @@ export function subFilter(a: string, b: string) {
* lexicographic order
*/
export function compareForFuzzySort(query: string, a: string, b: string) {
const distanceToQuery = (s: string) =>
subFilterLastIndex(query, s) - subFilterFirstIndex(query, s);
const distanceA = distanceToQuery(a);
if (distanceA === 0) {
const subFilterFirstIndexA = subFilterFirstIndex(query, a);
if (subFilterFirstIndexA === -1) {
return 1;
}
const distanceB = distanceToQuery(b);
if (distanceB === 0) {

const subFilterFirstIndexB = subFilterFirstIndex(query, b);
if (subFilterFirstIndexB === -1) {
return -1;
}

const subFilterLastIndexA = subFilterLastIndex(query, a);
const subFilterLastIndexB = subFilterLastIndex(query, b);
const distanceA = subFilterLastIndexA - subFilterFirstIndexA;
const distanceB = subFilterLastIndexB - subFilterFirstIndexB;
if (distanceA !== distanceB) {
return distanceA - distanceB;
}
const firstCharA = a.indexOf(query.charAt(0));
const firstCharB = b.indexOf(query.charAt(0));
if (firstCharA !== firstCharB) {
return firstCharA - firstCharB;

if (subFilterFirstIndexA !== subFilterFirstIndexB) {
return subFilterFirstIndexA - subFilterFirstIndexB;
}

if (a.length !== b.length) {
return a.length - b.length;
}
Expand Down

0 comments on commit 23a4e29

Please sign in to comment.