Skip to content

exception when doing search with outOfOrder > 0 #79

@danielbush

Description

@danielbush

Hi,
I was using uFuzzy.filter but was trying the outOfOrder option with uFuzzy.search and ran into an error. I may not be using this library correctly, only just been playing with it (I struggle a bit with the docs - I would like to replicate the kind of fuzzy search that vscode uses). Anyway here is a repro which I did in bun (but the original error occurred in a svelte project using pnpm / node 24).

import uFuzzy from "@leeoniya/ufuzzy";

const haystack = [
    "Settings...",
    "Navigate outline...",
    "Insert katex...",
    "Hide",
    "Async menu items demo...",
    "Show permanent inline notification",
    "Show transient inline notification",
    "Show alert",
    "Confirm",
    "blank item 1",
    "blank item 2",
    "blank item 3",
    "blank item 4",
    "blank item 5",
    "blank item 6",
    "blank item 7",
    "blank item 8",
];

const ufuzzy = new uFuzzy({});

function search(haystack: string[], needle: string, outOfOrder: number) {
    const [idxs] = ufuzzy.search(haystack, needle, outOfOrder);
    if (!idxs) {
        console.log("No match found");
        return;
    }
    console.log(idxs)
    ufuzzy.info(idxs, haystack, needle);
}

// search(haystack, "i", 1);
// search(haystack, "it", 1);
// search(haystack, "ite", 1);
// search(haystack, "item", 1);
// search(haystack, "item ", 1);
search(haystack, "item a", 1);

Output:

[ 4, 9, 10, 11, 12, 13, 14, 15, 16 ]
495 | 
496 |                   // the matched parts are [full, junk, term, junk, term, junk]
497 |                   let m = mhstr.match(query);
498 | 
499 |                   // leading junk
500 |                   let start = m.index + m[1].length;
                     ^
TypeError: null is not an object (evaluating 'm.index')
      at info (/Users/danb/work/debug/node_modules/@leeoniya/ufuzzy/dist/uFuzzy.cjs:500:16)
      at search (/Users/danb/work/debug/main.ts:32:12)
      at /Users/danb/work/debug/main.ts:40:1
      at loadAndEvaluateModule (2:1)

Bun v1.2.23 (macOS x64)
error: script "dev" exited with code 1

It's crashing at line: ufuzzy.info(idxs, haystack, needle);

The other commented out console lines don't throw and outOfOrder=0 doesn't throw.

@leeoniya/ufuzzy@1.0.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions