1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgmp_util). |
17 |
|
|
18 |
|
|
19 |
|
-export([is_exported/3]). |
20 |
|
-export([semantic_version/1]). |
21 |
|
-export([snake_case/1]). |
22 |
|
-export([split_on_snake_case/1]). |
23 |
|
-export([tl_snake_case/1]). |
24 |
|
|
25 |
|
|
26 |
|
snake_case([_ | _] = Labels) -> |
27 |
21606 |
list_to_atom(lists:concat(lists:join("_", Labels))). |
28 |
|
|
29 |
|
|
30 |
|
split_on_snake_case(Name) -> |
31 |
314 |
string:split(atom_to_list(Name), "_", all). |
32 |
|
|
33 |
|
tl_snake_case(Name) -> |
34 |
277 |
case split_on_snake_case(Name) of |
35 |
|
[_] -> |
36 |
10 |
Name; |
37 |
|
|
38 |
|
Names -> |
39 |
267 |
snake_case(tl(Names)) |
40 |
|
end. |
41 |
|
|
42 |
|
|
43 |
|
is_exported(M, F, A) -> |
44 |
130 |
_ = case erlang:module_loaded(M) of |
45 |
|
false -> |
46 |
:-( |
code:ensure_loaded(M); |
47 |
|
|
48 |
|
true -> |
49 |
130 |
ok |
50 |
|
end, |
51 |
130 |
erlang:function_exported(M, F, A). |
52 |
|
|
53 |
|
|
54 |
|
semantic_version(Version) -> |
55 |
10 |
{ok, MP} = re:compile( |
56 |
|
"(?<major>\\d+)(\\.(?<minor>\\d+)(\\.(?<patch>\\d+))?)?"), |
57 |
10 |
{namelist, NL} = re:inspect(MP, namelist), |
58 |
10 |
{match, Matches} = re:run(Version, MP, [{capture, all_names, binary}]), |
59 |
10 |
lists:foldl( |
60 |
|
fun |
61 |
|
({_, <<>>}, A) -> |
62 |
9 |
A; |
63 |
|
({K, V}, A) -> |
64 |
21 |
A#{binary_to_atom(K) => binary_to_integer(V)} |
65 |
|
end, |
66 |
|
#{}, |
67 |
|
lists:zip(NL, Matches)). |