Files
30-seconds-of-code/snippets/fuzzySearch.md
2018-02-22 09:30:13 +05:30

667 B

fuzzySearch

Determines if the pattern matches with str

Loops through str and determines if it contains all characters of pattern and in the correct order. Both the strings are converted to lower case.

Adapted from here.

const fuzzySearch = (pattern, str) =>
	[...str].reduce(
		(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex]  || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
	) === pattern.length ? true : false;
fuzzySearch('rt','Rohit'); // true
fuzzySearch('tr','Rohit'); // false