Skip to content

Commit c4b6d90

Browse files
author
Andika Demas Riyandi
committed
simplify filtering by using foldMap function
1 parent 6a0a997 commit c4b6d90

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src-ui.v3/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/dist-newstyle/
22
/dist/
3+
cabal.project.local*
4+

src-ui.v3/src/Main.hs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import qualified Data.Aeson as J
2424
import qualified Data.Aeson.Types as J
2525
import Data.Bool (not)
2626
import qualified Data.Char as C
27+
import qualified Data.Foldable as F
2728
import qualified Data.JSString as JSS
2829
import qualified Data.JSString.Text as JSS
2930
import qualified Data.List as List
@@ -749,30 +750,41 @@ stripSearch sJ
749750
| Just sJ' <- JSS.stripSuffix "$" sJ = sJ'
750751
| otherwise = sJ
751752

752-
filterBySearchBox :: [JSS.JSString] -> JSS.JSString -> [JSS.JSString]
753-
filterBySearchBox pkgs sJ
754-
| JSS.isInfixOf "^" sJ = filter (JSS.isPrefixOf (stripSearch sJ)) pkgs
755-
| JSS.isInfixOf "$" sJ = filter (JSS.isSuffixOf (stripSearch sJ)) pkgs
756-
| otherwise = filter (JSS.isInfixOf (stripSearch sJ))pkgs
757-
758753
splitInfixPkg :: JSS.JSString -> JSS.JSString -> (T.Text, T.Text, T.Text)
759754
splitInfixPkg stripSJ pkg = (frontT, midT, backT)
760755
where
761756
textS = JSS.textFromJSString stripSJ
762757
(frontT, reminderT) = T.breakOn textS (JSS.textFromJSString pkg)
763758
(midT, backT) = T.breakOnEnd textS reminderT
764759

765-
calcMatchesNew :: [JSS.JSString] -> JSS.JSString -> Matches
766-
calcMatchesNew pkgs sJss =
760+
calcMatch :: JSS.JSString -> JSS.JSString -> (Map.Map T.Text (), Map.Map T.Text (T.Text,T.Text,T.Text))
761+
calcMatch sJss pkg =
762+
let stripSJ = stripSearch sJss
763+
in
764+
case stripSJ == pkg of
765+
True -> (Map.singleton (JSS.textFromJSString pkg) (), Map.empty)
766+
False -> filterPkgSearch stripSJ pkg
767+
768+
filterPkgSearch :: JSS.JSString -> JSS.JSString -> (Map.Map T.Text (), Map.Map T.Text (T.Text,T.Text,T.Text))
769+
filterPkgSearch sJss pkg
770+
| Just sJ' <- JSS.stripPrefix "^" sJss = if JSS.isPrefixOf sJ' pkg
771+
then (Map.empty, Map.singleton (JSS.textFromJSString pkg) (splitInfixPkg sJ' pkg))
772+
else (Map.empty, Map.empty)
773+
| Just sJ' <- JSS.stripSuffix "$" sJss = if JSS.isSuffixOf sJ' pkg
774+
then (Map.empty, Map.singleton (JSS.textFromJSString pkg) (splitInfixPkg sJ' pkg))
775+
else (Map.empty, Map.empty)
776+
| otherwise = if JSS.isInfixOf sJss pkg
777+
then (Map.empty, Map.singleton (JSS.textFromJSString pkg) (splitInfixPkg sJss pkg))
778+
else (Map.empty, Map.empty)
779+
780+
calcMatches :: [JSS.JSString] -> JSS.JSString -> Matches
781+
calcMatches pkgs sJss =
767782
if JSS.length sJss < 3
768783
then matchesEmpty
769784
else Matches { matchesInput = textS, matchesExact = exactMap, matchesInfix = othersMap}
770785
where
771786
textS = JSS.textFromJSString sJss
772-
stripSJ = stripSearch sJss
773-
(exactPkg, infixPkg) = List.partition (== stripSJ) pkgs
774-
exactMap = (Map.fromList . fmap (\p -> (JSS.textFromJSString p,()))) exactPkg
775-
othersMap = (Map.fromList . fmap (\p -> (JSS.textFromJSString p, splitInfixPkg stripSJ p))) (filterBySearchBox infixPkg sJss)
787+
(exactMap,othersMap) = F.foldMap (calcMatch sJss) pkgs
776788

777789
searchBoxWidget :: forall t m. (SupportsServantReflex t m, MonadFix m, MonadIO m, MonadHold t m, PostBuild t m, DomBuilder t m, Adjustable t m, DomBuilderSpace m ~ GhcjsDomSpace)
778790
=> Dynamic t (Vector PkgN)
@@ -785,7 +797,7 @@ searchBoxWidget dynPkgs0 = mdo
785797
debounce 0.1 $ leftmost [clickPkgE, (_inputElement_input sVal0)]
786798
let
787799
dynPackagesJss = V.toList . fmap (JSS.textToJSString . pkgNToText) <$> dynPkgs0
788-
matchesE = calcMatchesNew <$> current dynPackagesJss <@> (JSS.textToJSString <$> searchInputE)
800+
matchesE = calcMatches <$> current dynPackagesJss <@> (JSS.textToJSString <$> searchInputE)
789801
matchesDyn <- holdDyn matchesEmpty matchesE
790802
clickPkgE <- searchResultWidget matchesDyn
791803
pure ()

0 commit comments

Comments
 (0)