GithubHelp home page GithubHelp logo

metonym / format-fuse.js Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 270 KB

Utility to format matching fuse.js results for easier text highlighting

License: MIT License

TypeScript 100.00%
fusejs text-highlighting

format-fuse.js's People

Contributors

dependabot-preview[bot] avatar metonym avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

chmac

format-fuse.js's Issues

Seems to break on an array of strings

Slightly modifying the example from the readme like so:

const results = format([
  {
    item: {
      titles: ["Monster 1959", "Second Title"],
      author: { firstName: "David", lastName: "Maine" },
    },
    matches: [
      {
        indices: [[1, 2]],
        value: "Monster 1959",
        key: "title",
        arrayIndex: 0,
      },
    ],
  },
]);

I think the code doesn't follow the refIndex property from fuse on title.

When using Fuse's useExtendedSearch, format-fuse can return overlapping matches

Performing a Fuse extended search for 'sao paulo' on an item that contains the text 'SAO PAULO' yields matches for [ [ 0, 2 ], [ 1, 2 ], [ 4, 8 ] ], and format-fuse returns them as SAO, AO an PAULO. Rendering those as shown in the readme causes the UI to show "SAOAO PAULO". Ideally, the resulting value would eliminate overlapping matches.

Example:

const Fuse = require('fuse.js');
const formatFuseJs = require('format-fuse.js').default;
const util = require('util');

const r = new Fuse([{ name: 'SAO PAULO' }], {
    keys: ['name'],
    includeMatches: true,
    includeScore: true,
    useExtendedSearch: true,
})
    .search('sao paulo')
    .map(s => ({ ...s, formatted: formatFuseJs([s])[0] }));

console.log(util.inspect(r, { showHidden: false, depth: null, colors: true }));

Actual:

[
  {
    item: { name: 'SAO PAULO' },
    refIndex: 0,
    matches: [
      {
        indices: [ [ 0, 2 ], [ 1, 2 ], [ 4, 8 ] ],
        value: 'SAO PAULO',
        key: 'name'
      }
    ],
    score: 0.06403390388180329,
    formatted: {
      name: [
        { text: 'SAO', matches: true },
        { text: 'AO', matches: true },
        { text: ' ', matches: false },
        { text: 'PAULO', matches: true }
      ]
    }
  }
]

Expected:

[
  {
    item: { name: 'SAO PAULO' },
    refIndex: 0,
    matches: [
      {
        indices: [ [ 0, 2 ], [ 1, 2 ], [ 4, 8 ] ],
        value: 'SAO PAULO',
        key: 'name'
      }
    ],
    score: 0.06403390388180329,
    formatted: {
      name: [
        { text: 'SAO', matches: true },
        { text: ' ', matches: false },
        { text: 'PAULO', matches: true }
      ]
    }
  }
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.