module Extra.URIQuery
( modify
, del
, put
, copy
) where
import Data.List (partition)
import Data.Maybe (listToMaybe)
import Extra.URI
modify :: String -> (Maybe String -> Maybe String) -> URI -> URI
modify a vf uri =
let (vs, other) = partition ((== a) . fst) (parseURIQuery uri) in
setURIQuery (case vf (listToMaybe (map snd vs)) of
Just v' -> (a, v') : other
Nothing -> other) uri
del :: String -> URI -> URI
del a uri = modify a (const Nothing) uri
put :: String -> String -> URI -> URI
put a v uri = modify a (const (Just v)) uri
copy :: String -> URI -> URI -> URI
copy a src dst = modify a (const (lookup a (parseURIQuery src))) dst