Skip to content

How to exclude single character highlights? #793

Closed as not planned
Closed as not planned

Description

@punkpeye

Is there an existing issue for this?

  • I have searched the existing issues

Your question

Image

Analytics word has letters 'n' 't' 's' highlighted.

How do I only highlight sub-strings that are at least X characters long?

Activity

  1. rahuls24 commented on Apr 4, 2025

    @rahuls24

    To highlight only substrings that are at least X characters long, you can use Fuse.js with the minMatchCharLength option.


    🚀 Approach 1: Using minMatchCharLength (Recommended)

    The cleanest way is to set the minMatchCharLength option in your Fuse.js configuration:

    const Fuse = require('fuse.js');
    
    const options = [
      'Open settings -> Agents',
      'Open Usage Analytics',
      'View Dashboard Overview',
      'Access System Logs',
    ];
    
    const fuseOptions = {
      includeMatches: true,
      minMatchCharLength: 2, // Ignores matches shorter than 2 characters
    };
    
    const fuse = new Fuse(options, fuseOptions);
    const searchPattern = 'agents';
    const results = fuse.search(searchPattern);
    
    console.log(results);
    • minMatchCharLength: 2 filters out matches shorter than 2 characters.

    ⚡ Approach 2: Manual Filtering (Workaround)

    If you can’t change the configuration, manually filter out single-character matches after the search:

    const Fuse = require('fuse.js');
    
    const options = [
      'Open settings -> Agents',
      'Open Usage Analytics',
      'View Dashboard Overview',
      'Access System Logs',
    ];
    
    const fuseOptions = {
      includeMatches: true,
      minMatchCharLength: 1,
    };
    
    const fuse = new Fuse(options, fuseOptions);
    const searchPattern = 'agents';
    
    const filteredResults = fuse.search(searchPattern)
      .flatMap(result =>
        result.matches
          .filter(match => match.indices[0][0] !== match.indices[0][1]) // Exclude single-character matches
          .map(match => ({
            item: result.item,
            match: result.item.substring(match.indices[0][0], match.indices[0][1] + 1),
            indices: match.indices
          }))
      );
    
    console.log(filteredResults);

    ⚠️ Which to Use?

    • ✅ Approach 1 is recommended for clean, efficient filtering.
    • 🔧 Approach 2 is for cases where configuration changes aren’t possible.

    📚 References:

  2. github-actions commented on Aug 3, 2025

    @github-actions

    This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 30 days

  3. krisk commented on May 14, 2026

    @krisk
    Owner

    Same underlying bug as #792, just observed from the other side. Shipped a fix in v7.4.0-beta.6 (npm install fuse.js@beta) that restricts highlights to the actual matched window. minMatchCharLength: 2 now reliably filters single-char residuals, which it couldn't do reliably before because multi-character stray runs would slip through anyway. Bonus: ~15% perf win on includeMatches queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions