fuzzysort icon indicating copy to clipboard operation
fuzzysort copied to clipboard

Why "scoreFn" is only invoked when you define "keys" but not with "key"?

Open jfmdev opened this issue 1 month ago • 1 comments

If I do:

const items = [{ name: "abc" }, { name: "def" }, { name: "ghi" }];
fuzzysort.go('bc', items, {
  key: 'name',
  scoreFn: result => {
    console.log("Hi!");
    return result.score * 2;
  }
});

The scoreFn function is never called.

But if I use keys: ['name'] instead of key: 'name' it works perfectly:

const items = [{ name: "abc" }, { name: "def" }, { name: "ghi" }];
fuzzysort.go('bc', items, {
  keys: ['name'],
  scoreFn: result => {
    console.log("Hi!");
    return result.score * 2;
  }
});

Any reason why the custom score function is only invoked when multiple keys are defined?

jfmdev avatar Nov 14 '25 19:11 jfmdev

scoreFn is unnecessary unless you're using keys. scoreFn is for when you need score information about individual keys. otherwise just remap the score normally results.forEach(r => r.score *= 2)

i agree it seems inconsistent.. but most users don't even need to know scoreFn exists. it doesn't work in the key case for performance / simplicity of the code

farzher avatar Nov 15 '25 01:11 farzher