Git Product home page Git Product logo

typescript-fuzzy-search's Introduction

typescript-fuzzy-search

A simple typescript function I spent way too much time on. It searches for a string in a string array but fuzzily.

Fuzzy

// Basic functionality with long sentences
let testCase7 = ["I love to watch movies.", "I like to read books.", "I enjoy playing football."];
res = fuzzyStringSearch(testCase7, "I love reading books.");
console.assert(res === "I like to read books."); // Expected: true

Characters closer to the beginning are more valued

let testCase_5 = ["ABCD", "BCDA", "CDAB", "DABC"];
res = fuzzyStringSearch(testCase_5, "AB");
console.assert(res === "ABCD", res, " === ABCD"); // Expected: true

Shorter strings are prioritized:

let testCase6 = ["ABCD", "B", "AB", "BCDA", "CDAB", "DABC"];
res = fuzzyStringSearch(testCase6, "AB");
console.assert(res === "AB", res, "=== AB"); // Expected: true

More tests/examples

TS Playground

let res: string | null = ""

const test = (fuzzyStringSearch: Function) => {
// Basic functionality
let testCase1 = ["apple", "banana", "orange", "pineapple"];
res = fuzzyStringSearch(testCase1, "apple");
console.assert(res === "apple", res); // Expected: true

// Order of letters doesn't matter
let testCase2 = ["sv", "sx", "sv-FI", "es-SV"];
res = fuzzyStringSearch(testCase2, "sv-SE");
console.assert(res === "sv-FI", res, "=== sv-FI"); // Expected: true

// Non-case sensitive match
let testCase3 = ["Dog", "Cat", "Bird", "Fish"];
res = fuzzyStringSearch(testCase3, "dog");
console.assert(res === "Dog", res); // Expected: true

// Exhaustive search - More accurate match even with additional letters
let testCase_5 = ["ABCD", "BCDA", "CDAB", "DABC"];
res = fuzzyStringSearch(testCase_5, "AB");
console.assert(res === "ABCD", res, " === ABCD"); // Expected: true

//  Prioritize better and shorter strings
let testCase6 = ["ABCD", "B", "AB", "BCDA", "CDAB", "DABC"];
res = fuzzyStringSearch(testCase6, "AB");
console.assert(res === "AB", res, "=== AB"); // Expected: true

// Invalid inputs - haystack is not an array
res = fuzzyStringSearch("hello", "h");
console.assert(res === null, res, " === null"); // Expected: true

// Invalid inputs - needle is not a string
res = fuzzyStringSearch(["h"], true);
console.assert(res === null, res, ); // Expected: true

// Basic functionality with long sentences
let testCase7 = ["I love to watch movies.", "I enjoy playing football.", "Cooking is my favorite pastime.", "I like to read books."];
res = fuzzyStringSearch(testCase7, "I love reading books.");
console.assert(res === "I like to read books.", res, "I like to read books."); // Expected: true

// Case-insensitive match in sentences
let testCase8 = ["Don't cry because it's over, smile because it happened.", "In three words I can sum up everything I've learned about life: it goes on."];
res = fuzzyStringSearch(testCase8, "don't CRY because it's OVER, SMILE because it happened.");
console.assert(res === "Don't cry because it's over, smile because it happened.", res); // Expected: true

// Test with special characters in the haystack and needle
let testCase9 = ["return -2*root;", "console.log(delta)", "function solve(){ }", "{ key: value }"]
res = fuzzyStringSearch(testCase9, "return (delta >= 0) ? -2*root : console.log(delta);");
console.assert(res === "console.log(delta)", "console.log(delta) ===", res); // Expected: true

// Test with more than one possible matches, with and without exhaustive search
let testCase10 = [
  "A If you don't know where you're going, any road'll take you there", 
  "B If you don't know where you want to go, then it doesn't matter which path you take."];
res = fuzzyStringSearch(testCase10, "If you don't know where you're going, any road will get you there.");
console.assert(res === "A If you don't know where you're going, any road'll take you there"); // Expected: true

res = fuzzyStringSearch(testCase10, "then it doesn't matter.");
console.assert(res === "B If you don't know where you want to go, then it doesn't matter which path you take."); // Expected: true
}


console.time("fuzzy");
for (var i = 0; i < 10e2; i++) {
  // Note that this one will make one assert false because it's not exhaustive
  test((haystack: string[], needle: string) => fuzzyStringSearch(haystack, needle))
}
console.timeEnd("fuzzy");


console.time("fuzzy exhaustive");
for (var i = 0; i < 10e2; i++) {
  test((haystack: string[], needle: string) => fuzzyStringSearch(haystack, needle, false, true))
}
console.timeEnd("fuzzy exhaustive");; i++) {
  test((haystack: string[], needle: string) => fuzzyStringSearch(haystack, needle, false, true))
}
console.timeEnd("fuzzy exhaustive");

typescript-fuzzy-search's People

Contributors

henke443 avatar

Stargazers

 avatar

Watchers

 avatar

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.