-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathurl.ex
More file actions
66 lines (53 loc) · 2.05 KB
/
Copy pathurl.ex
File metadata and controls
66 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
defmodule LiveDebugger.Utils.URL do
@moduledoc """
URL utilities for managing URLs and query params.
"""
@doc """
Converts an absolute URL to a relative URL.
## Examples
iex> URL.to_relative("http://example.com/foo?bar=baz")
"/foo?bar=baz"
"""
@spec to_relative(utl :: String.t()) :: String.t()
def to_relative(url) when is_binary(url) do
%{path: path, query: query} = URI.parse(url)
URI.to_string(%URI{path: path, query: query})
end
@spec update_path(url :: String.t(), path :: String.t()) :: String.t()
def update_path(url, path) when is_binary(url) and is_binary(path) do
uri = URI.parse(url)
URI.to_string(%URI{uri | path: path})
end
@spec upsert_query_param(url :: String.t(), key :: String.t(), value :: String.t()) ::
String.t()
def upsert_query_param(url, key, value)
when is_binary(url) and is_binary(key) and is_binary(value) do
upsert_query_params(url, %{key => value})
end
@spec upsert_query_params(url :: String.t(), params :: %{String.t() => String.t()}) ::
String.t()
def upsert_query_params(url, params) when is_binary(url) and is_map(params) do
modify_query_params(url, &Map.merge(&1, params))
end
@spec remove_query_param(url :: String.t(), key :: String.t()) :: String.t()
def remove_query_param(url, key) when is_binary(url) and is_binary(key) do
modify_query_params(url, &Map.delete(&1, key))
end
@spec remove_query_params(url :: String.t(), keys :: [String.t()]) :: String.t()
def remove_query_params(url, keys) when is_binary(url) and is_list(keys) do
modify_query_params(url, &Map.drop(&1, keys))
end
@spec modify_query_params(url :: String.t(), fun :: (map() -> map())) :: String.t()
def modify_query_params(url, fun) when is_binary(url) and is_function(fun) do
uri = URI.parse(url)
params =
(uri.query || "")
|> URI.decode_query()
|> fun.()
|> case do
params when map_size(params) == 0 -> nil
params -> URI.encode_query(params)
end
URI.to_string(%URI{uri | query: params})
end
end