Utils.purs 3.03 KB
Newer Older
1
module Gargantext.Utils where
2

3
import DOM.Simple.Window (window)
4
import Data.Either (Either(..))
5
import Data.Foldable (class Foldable, foldr)
6
import Data.Lens (Lens', lens)
7
import Data.Newtype (class Newtype, unwrap, wrap)
8
import Data.Set (Set)
9
import Data.Set as Set
10
import Data.Sequence.Ordered as OSeq
11
import Data.String as S
12
import Data.Unfoldable (class Unfoldable)
13 14 15 16
import Effect (Effect)
import FFI.Simple ((..))
import FFI.Simple.Functions (delay)
import Prelude
17

18 19 20
-- | TODO (hard coded)
csrfMiddlewareToken :: String
csrfMiddlewareToken = "Wy52D2nor8kC1r1Y4GrsrSIxQ2eqW8UwkdiQQshMoRwobzU4uldknRUhP0j4WcEM"
James Laver's avatar
James Laver committed
21

22 23 24 25 26 27
setterv :: forall nt record field.
           Newtype nt record
           => (record -> field -> record)
           -> field
           -> nt
           -> nt
28 29
setterv fn v t = (setter (flip fn v) t)

30 31 32 33 34
setter :: forall nt record.
          Newtype nt record
          => (record -> record)
          -> nt
          -> nt
35 36
setter fn = wrap <<< fn <<< unwrap

37 38 39 40 41
getter :: forall record field nt.
          Newtype nt record
          => (record -> field)
          -> nt
          -> field
42
getter fn = fn <<< unwrap
43 44 45 46 47 48

-- TODO: not optimal but Data.Set lacks some function (Set.alter)
toggleSet :: forall a. Ord a => a -> Set a -> Set a
toggleSet a s
  | Set.member a s = Set.delete a s
  | otherwise      = Set.insert a s
49 50 51 52 53 54 55 56 57 58

-- Default sort order is ascending, we may want descending
invertOrdering :: Ordering -> Ordering
invertOrdering LT = GT
invertOrdering GT = LT
invertOrdering EQ = EQ

-- A lens that always returns unit
_unit :: forall s. Lens' s Unit
_unit = lens (\_ -> unit) (\s _ -> s)
59

60
glyphicon :: String -> String
61
glyphicon t = "btn glyphitem fa fa-" <> t
62

63 64 65
glyphiconActive :: String -> Boolean -> String
glyphiconActive icon b = glyphicon icon <> if b then " active" else ""

66 67 68 69
-- | Format a number with specified amount of zero-padding
zeroPad :: Int -> Int -> String
zeroPad pad num = zeros <> (show num)
  where
70
    numDigits = S.length $ show num
71 72 73
    zeros = if numDigits < pad then zeros' (pad - numDigits) else ""
    zeros' 0 = ""
    zeros' n = "0" <> (zeros' (n - 1))
74 75 76 77 78

queryMatchesLabel :: String -> String -> Boolean
queryMatchesLabel q l = S.contains (S.Pattern $ normalize q) (normalize l)
  where
    normalize = S.toLower
79 80 81 82 83


mapLeft :: forall l m r. (l -> m) -> Either l r -> Either m r
mapLeft f (Left  l) = Left (f l)
mapLeft _ (Right r) = Right r
84 85 86 87 88

-- | Get current Window Location
location :: Effect String
location = delay unit $ \_ -> pure $ window .. "location"

89
data On a b = On a b
90

91 92
instance eqOn :: Eq a => Eq (On a b) where
  eq (On x _) (On y _) = eq x y
93

94 95 96 97 98 99 100 101 102 103 104
instance ordOn :: Ord a => Ord (On a b) where
  compare (On x _) (On y _) = compare x y

-- same as
-- https://github.com/purescript/purescript-arrays/blob/v5.3.1/src/Data/Array.purs#L715-L715
sortWith :: forall a b f. Functor f =>
                          Foldable f =>
                          Unfoldable f =>
                          Ord b =>
                          (a -> b) -> f a -> f a
sortWith f = map (\(On _ y) -> y) <<< OSeq.toUnfoldable <<< foldr (\x -> OSeq.insert (On (f x) x)) OSeq.empty