QueryString.purs 1.04 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 31 32 33 34 35 36
joinQueryStrings :: Array String -> String
joinQueryStrings qs =
  case uncons qs of
    Nothing -> ""
    Just { head, tail } -> "?" <> head <> (joinQS tail)
      where
        joinQS ys =
          case uncons ys of
            Nothing -> ""
            Just { tail: ys } -> "&" <> (joinWith "&" ys)