Skip to content

Instantly share code, notes, and snippets.

@jackdomleo7
Created February 14, 2023 22:12
Show Gist options
  • Save jackdomleo7/ef28edf483a6064545c6ca9234e59ff5 to your computer and use it in GitHub Desktop.
Save jackdomleo7/ef28edf483a6064545c6ca9234e59ff5 to your computer and use it in GitHub Desktop.
import { fuzzySearch } from './fuzzySearch';
describe('fuzzySearch()', () => {
it('returns true when the searchTerm matches the searchString exactly', () => {
const searchString = 'foo';
const searchTerm = 'foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns false when the searchTerm does not match the searchString exactly', () => {
const searchString = 'foo';
const searchTerm = 'bar';
expect(fuzzySearch(searchString, searchTerm)).toBe(false);
});
it('returns true when the searchTerm matches the searchString exactly (with spaces)', () => {
const searchString = 'foo foo';
const searchTerm = 'foo foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns false when the searchTerm does not match the searchString exactly (with spaces)', () => {
const searchString = 'foo foo';
const searchTerm = 'bar bar';
expect(fuzzySearch(searchString, searchTerm)).toBe(false);
});
it('returns true when the searchTerm is at the beginning of the searchString', () => {
const searchString = 'foo is a strange word';
const searchTerm = 'foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns true when the searchTerm is at the end of the searchString', () => {
const searchString = 'strange word is foo';
const searchTerm = 'foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns true when the searchTerm is somewhere within the searchString', () => {
const searchString = 'strange word foo is';
const searchTerm = 'foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns false when the searchTerm is nowhere within the searchString', () => {
const searchString = 'bar bar wooly sheep';
const searchTerm = 'foo';
expect(fuzzySearch(searchString, searchTerm)).toBe(false);
});
it('returns true when the searchTerm (with spaces) is somewhere within the searchString separated by other words', () => {
const searchString = 'foo is not the same word as bar';
const searchTerm = 'foo bar';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns true when the searchTerm matches the searchString but the searchString characters are separated by another character', () => {
const searchString = 'F*o*o*B*a*r';
const searchTerm = 'FooBar';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns true when the searchTerm matches the searhString exactly differing only in capitalisation', () => {
const searchString = 'FoO';
const searchTerm = 'fOo';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
it('returns true when the searchTerm (prefixed and suffixed with whitespace) matches the searchString (prefixed and suffixed with no whitespace)', () => {
const searchString = 'foo';
const searchTerm = ' foo ';
expect(fuzzySearch(searchString, searchTerm)).toBe(true);
});
});
/**
* Client side fuzzy search to loosely search for a term within a string
* @param searchString - the string that is being searched within
* @param - searchTerm - the term that is being searched for
* @returns - true if loosely matched, false if not matched at all
*/
export function fuzzySearch(searchString: string, searchTerm: string): boolean {
let startMinusOne = -1;
searchTerm = searchTerm.toLowerCase().trim();
searchString = searchString.toLowerCase();
for (let i = 0; i < searchTerm.length; i++) {
if (!~(startMinusOne = searchString.indexOf(searchTerm[i], startMinusOne + 1))) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment