From 93bc64b1a0750cfca6216e1a740a80dd81e23f33 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 19 Feb 2018 21:21:10 +0530 Subject: [PATCH] Update fuzzySearch --- snippets/fuzzySearch.md | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/snippets/fuzzySearch.md b/snippets/fuzzySearch.md index 9760f5279..d384e5b41 100644 --- a/snippets/fuzzySearch.md +++ b/snippets/fuzzySearch.md @@ -6,19 +6,10 @@ Loops through `str` and determines if it contains all characters of `patrn` and Taken from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18). ``` js -fuzzySearch = (pattern, string) => { - let patternLength = pattern.length; - let strLength = string.length; - - for (var patternIdx = 0, strIdx = 0;patternIdx !== patternLength && strIdx !== strLength;strIdx++){ - let patternChar = pattern[patternIdx].toLowerCase(); - let strChar = string[strIdx].toLowerCase(); - if (patternChar === strChar) - ++patternIdx; - } - - return patternLength !== 0 && strLength !== 0 && patternIdx === patternLength ? true : false; -}; +const fuzzySearch = (pattern, str) => + [...str].reduce( + (acc, char) => char.toLowerCase() === (pattern[acc] || '').toLowerCase() ? acc + 1 : acc, 0 + ) === pattern.length ? true : false; ```