QueryString.purs 1.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
module Gargantext.Utils.QueryString where

import Data.Array
import Data.Maybe
import Data.String.Common (joinWith)

import Gargantext.Prelude

queryParam :: forall a. Show a => String -> a -> String
queryParam key value = key <> "=" <> show value

queryParamS :: String -> String -> String
queryParamS key value = key <> "=" <> value

mQueryParam :: forall a. Show a => String -> Maybe a -> String
mQueryParam _ Nothing = ""
mQueryParam key (Just v) = queryParam key v

19
mQueryParamS :: forall a. String -> (a -> String) ->  Maybe a -> String
20 21 22
mQueryParamS _ _ Nothing = ""
mQueryParamS key mFunc (Just v) = queryParamS key $ mFunc v

23 24 25 26
mQueryParamS' :: String -> Maybe String -> String
mQueryParamS' _ Nothing = ""
mQueryParamS' key (Just v) = queryParamS key v

27 28 29 30
joinQueryStrings :: Array String -> String
joinQueryStrings qs =
  case uncons qs of
    Nothing -> ""
31
    Just { head, tail } -> "?" <> head <> (joinTail tail)
32
      where
33 34
        joinTail :: Array String -> String
        joinTail ys =
35 36
          case uncons ys of
            Nothing -> ""
37
            Just { head: h, tail: ys } -> "&" <> h <> (joinTail ys)