function escapeRegExp(input: string) { // escape regex characters in the input string to prevent regex format errors return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } /** * Checks if the name is a fuzzy match of the query. * * @example * ```ts * const name = 'John Smith'; * const query = 'js'; * const isMatch = isFuzzyMatch(name, query); * // isMatch: true * ``` */ export function isFuzzyMatch(name: string, query: string) { const pureName = name .trim() .toLowerCase() .split('') .filter(char => char !== ' ') .join(''); const regex = new RegExp( query .split('') .filter(char => char !== ' ') .map(item => `${escapeRegExp(item)}.*`) .join(''), 'i' ); return regex.test(pureName); }