Created
June 7, 2011 05:38
-
-
Save ToddG/1011743 to your computer and use it in GitHub Desktop.
Capitalize and CamelCase Words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(capitalize). | |
-behaviour(gen_server). | |
-define(SERVER, ?MODULE). | |
-include_lib("eunit/include/eunit.hrl"). | |
%% ------------------------------------------------------------------ | |
%% API Function Exports | |
%% ------------------------------------------------------------------ | |
-export([ | |
start_link/0, | |
capitalize_word/1, | |
capitalize_word_list/1, | |
camel_case_word/1, | |
camel_case_word_list/1 | |
]). | |
%% ------------------------------------------------------------------ | |
%% gen_server Function Exports | |
%% ------------------------------------------------------------------ | |
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). | |
%% ------------------------------------------------------------------ | |
%% API Function Definitions | |
%% ------------------------------------------------------------------ | |
start_link() -> | |
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). | |
capitalize_word(Text) -> | |
gen_server:call(?SERVER, {capitalize_word, Text}). | |
capitalize_word_list(Text) -> | |
gen_server:call(?SERVER, {capitalize_word_list, Text}). | |
camel_case_word(Text) -> | |
gen_server:call(?SERVER, {camel_case_word, Text}). | |
camel_case_word_list(Text) -> | |
gen_server:call(?SERVER, {camel_case_word_list, Text}). | |
%% ------------------------------------------------------------------ | |
%% gen_server Function Definitions | |
%% ------------------------------------------------------------------ | |
init(Args) -> | |
{ok, Args}. | |
handle_call({RequestType, T}, _From, State) -> | |
case RequestType of | |
capitalize_word -> | |
{reply, {text_capitalized, capitalize_text(T)}, State}; | |
capitalize_word_list -> | |
{reply, {text_capitalized, capitalize_list(T)}, State}; | |
camel_case_word -> | |
{reply, {text_camelized, camel_case_text(T)}, State}; | |
camel_case_word_list -> | |
{reply, {text_camelized, camel_case_list(T)}, State}; | |
_ -> | |
{noreply, ok, State} | |
end. | |
handle_cast(_Msg, State) -> | |
{noreply, State}. | |
handle_info(_Info, State) -> | |
{noreply, State}. | |
terminate(_Reason, _State) -> | |
ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. | |
%% ------------------------------------------------------------------ | |
%% Internal Function Definitions | |
%% ------------------------------------------------------------------ | |
capitalize_list(T) -> | |
lists:map(fun(X) -> capitalize_text(X) end, T). | |
capitalize_text(T) when is_binary(T) -> | |
capitalize_text(binary_to_list(T)); | |
capitalize_text(T) -> | |
string:to_upper(T). | |
camel_case_list(T) -> | |
lists:map(fun(X) -> camel_case_text(X) end, T). | |
camel_case_text(T) when is_list(T)-> | |
camel_case_text(list_to_binary(T)); | |
camel_case_text(T) when is_binary(T)-> | |
%% ?debugFmt("T:~p~n",[T]), | |
<<X:8, Rest/binary>> = T, | |
%% ?debugFmt("X:~p~n",[X]), | |
C = string:to_upper(X), | |
%% ?debugFmt("C:~p~n",[C]), | |
binary_to_list(list_to_binary([<<C>>, Rest])). | |
%% ------------------------------------------------------------------ | |
%% Internal Function Definitions | |
%% ------------------------------------------------------------------ | |
-ifdef(EUNIT). | |
capitalize_text_test() -> | |
?assert(string:equal("ABC", capitalize_text("abc"))), | |
?assert(string:equal("ABCD", capitalize_text("AbCd"))), | |
?assert(string:equal("ABCD", capitalize_text(<<"AbCd">>))), | |
[X,Y] = capitalize_list([<<"AbCd">>,<<"aaa">>]), | |
?assert(string:equal("ABCD", X)), | |
?assert(string:equal("AAA", Y)). | |
capitalize_text_list_test() -> | |
Expected=["A", "BC", "DEF"], | |
Input = ["a", "bc", "DeF"], | |
Returned = capitalize_list(Input), | |
Data = lists:zip(Expected, Returned), | |
lists:map(fun({E,R}) -> io:format("~nexpected: ~p, returned: ~p", [E, R]) end, Data), | |
lists:map(fun({LHS, RHS}) -> ?assert(string:equal(LHS, RHS)) end, Data). | |
camel_case_test() -> | |
Result = camel_case_text("abc"), | |
io:format("input:~p, camelcased: ~p~n",["abc", Result]), | |
?assert(string:equal("Abc", Result)). | |
camel_case_list_test() -> | |
Expected=["A", "Bc", "Def"], | |
Input = ["a", "bc", "def"], | |
Returned = camel_case_list(Input), | |
Data = lists:zip(Expected, Returned), | |
lists:map(fun({E,R}) -> io:format("~nexpected: ~p, returned: ~p", [E, R]) end, Data), | |
lists:map(fun({LHS, RHS}) -> ?assert(string:equal(LHS, RHS)) end, Data). | |
application_test() -> | |
ok = application:start(capitalize), | |
?assertNot(undefined == whereis(capitalize)), | |
?assertNot(undefined == whereis(capitalize_sup)). | |
%% DO NEXT : test the gen_server api | |
%% handle_call_capitalize_test() -> | |
%% %% cap a single word | |
%% {text_capitalized, "ABC"} = capitalize:capitalize("abc"), | |
%% %% cap a list of words | |
%% Expected=["A", "BC", "DEF"], | |
%% Input = ["a", "bc", "DeF"], | |
%% {text_capitalized, Returned} = capitalize:capitalize(Input), | |
%% Data = lists:zip(Expected, Returned), | |
%% lists:map(fun({E,R}) -> io:format("~nexpected: ~p, returned: ~p", [E, R]) end, Data), | |
%% lists:map(fun({LHS, RHS}) -> ?assert(string:equal(LHS, RHS)) end, Data). | |
%% ======================== EUnit ======================== | |
%% module 'capitalize' | |
%% capitalize: capitalize_text_test...ok | |
%% capitalize: capitalize_text_list_test...ok | |
%% capitalize: camel_case_test...ok | |
%% capitalize: camel_case_list_test...ok | |
%% capitalize: application_test...start_link()init()[0.010 s] ok | |
%% [done in 0.025 s] | |
%% module 'capitalize_app' | |
%% module 'capitalize_sup' | |
%% ======================================================= | |
%% All 5 tests passed. | |
%% DEBUG: Postdirs: [] | |
-endif. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment