Giter Club home page Giter Club logo

Comments (3)

bevacqua avatar bevacqua commented on August 15, 2024 4

it's an optimization shortcut

from fuzzysearch.

pie6k avatar pie6k commented on August 15, 2024

I think I have to admit this was the best explanation of confusing part of the code I have ever seen in my entire life!

from fuzzysearch.

pie6k avatar pie6k commented on August 15, 2024

I've decided to rewrite it as I consider code of this lib hard to understand and also I needed it to return informations about which parts were fuzzy and which were input

Maybe it'll be useful for someone (it's in typescript)

type MatchRoleType = 'input' | 'fuzzy' | 'suggestion';

interface FuzzyMatchData {
  content: string;
  type: MatchRoleType;
}

export interface FuzzyMatchOptions {
  truncateTooLongInput?: boolean;
}

export function fuzzyMatch(
  fuzzyInput: string,
  fullString: string,
  { truncateTooLongInput }: FuzzyMatchOptions = {},
): FuzzyMatchData[] | false {
  // input is longer than fullString and truncating is disabled
  if (fuzzyInput.length > fullString.length && !truncateTooLongInput) {
    return false;
  }

  // truncate if fuzzyinput is longer than fullstring
  if (fuzzyInput.length > fullString.length && truncateTooLongInput) {
    fuzzyInput = fuzzyInput.substr(0, fullString.length);
  }

  // both fuzzy and full string are equal - they match without being fuzzy
  if (fuzzyInput === fullString) {
    return [{ content: fuzzyInput, type: 'input' }];
  }

  const fuzzyMatchData: FuzzyMatchData[] = [];

  const leftFuzzyLetters = fuzzyInput.split('');

  let fuzzyLettersBuffer: string[] = [];
  let matchingLettersBuffer: string[] = [];

  function clearFuzzyBuffer() {
    if (fuzzyLettersBuffer.length > 0) {
      fuzzyMatchData.push({
        content: fuzzyLettersBuffer.join(''),
        type: 'fuzzy',
      });
      fuzzyLettersBuffer = [];
    }
  }

  function clearMatchingBuffer() {
    if (matchingLettersBuffer.length > 0) {
      fuzzyMatchData.push({
        content: matchingLettersBuffer.join(''),
        type: 'input',
      });
      matchingLettersBuffer = [];
    }
  }

  for (let fullStringLetter of fullString) {
    const currentFuzzyLetter = leftFuzzyLetters[0];

    console.log({ currentFuzzyLetter, fullStringLetter });

    // no more input
    if (!currentFuzzyLetter) {
      // clear buffers
      console.log('no more fuzzy letters');
      break;
    }

    // another fuzzy letter to add
    if (fullStringLetter !== currentFuzzyLetter) {
      // make sure to clean matching letters buffer if we have some
      clearMatchingBuffer();
      fuzzyLettersBuffer.push(fullStringLetter);

      continue;
    }

    // match!
    leftFuzzyLetters.shift();
    // clear fuzzy letters buffer
    clearFuzzyBuffer();

    matchingLettersBuffer.push(fullStringLetter);

    if (!leftFuzzyLetters.length) {
      clearMatchingBuffer();
    }
  }

  if (leftFuzzyLetters.length > 0) {
    return false;
  }

  const matchedPart = fuzzyMatchData.map((match) => match.content).join('');

  const suggestionPart = fullString.replace(matchedPart, '');

  if (suggestionPart) {
    fuzzyMatchData.push({ content: suggestionPart, type: 'suggestion' });
  }

  return fuzzyMatchData;
}

[edit]
Actually - I've published it to npm because true/false result was not enough for my case - https://github.com/pie6k/fuzzystring

from fuzzysearch.

Related Issues (14)

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.