ajitid / fzf-for-js

Do fuzzy matching using FZF algorithm in JavaScript

Home Page:https://fzf.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'positions' is sometimes undefined

peterbe opened this issue · comments

I just tried installing 0.4.1 and using it. See mdn/yari#4484
Thankfully I manually tested it in my browser too because I managed to stumble into an error that I really struggle to reproduce now. I wasn't able to capture what the input was when it happened.

But basically, the result's .positions was undefined. I would have expected it to be a new Set() at least.

Looking at this code:

positions: positions ?? new Set(),
it seems it tries to fall back to an empty Set but only if it's already null.

The reason I noticed was that I have this code:

function BreadcrumbURI({
  uri,
  positions,
}: {
  uri: string;
  positions: Set<number>;
}) {
  if (positions.size) {
    const chars = uri.split("");
    return (
      <small>
        {chars.map((char, i) => {
          if (positions.has(i)) {
            return <mark key={i}>{char}</mark>;
          } else {
            return <span key={i}>{char}</span>;
          }
        })}
      </small>
    );
  }
....

and so it would fail hard on if (positions.size) { because it's like doing if (undefined.size) {.
My TypeScript is unable to notice because it thinks that FzfResultItem always contains a .positions that is a Set<number>.

User error!
I misinterpreted my own code. So sorry for the distraction.