Skip to content

Commit

Permalink
Merge pull request #52 from essen/intcomma
Browse files Browse the repository at this point in the history
Add a contrib_humanize module for the intcomma filter
  • Loading branch information
evanmiller committed Nov 20, 2012
2 parents 28c946e + 32d586d commit 6d1fcaf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/erlydtl_contrib_humanize.erl
@@ -0,0 +1,18 @@
-module(erlydtl_contrib_humanize).

-export([intcomma/1]).

intcomma(Value) when is_integer(Value) ->
intcomma(integer_to_list(Value));
intcomma(Value) ->
ValueBin = iolist_to_binary(Value),
intcomma(ValueBin, size(ValueBin) rem 3, <<>>).

intcomma(<<>>, _, Acc) ->
Acc;
intcomma(<< C, Rest/bits >>, 0, <<>>) ->
intcomma(Rest, 2, << C >>);
intcomma(<< C, Rest/bits >>, 0, Acc) ->
intcomma(Rest, 2, << Acc/binary, $,, C >>);
intcomma(<< C, Rest/bits >>, N, Acc) ->
intcomma(Rest, N - 1, << Acc/binary, C >>).
8 changes: 7 additions & 1 deletion tests/src/erlydtl_unittests.erl
Expand Up @@ -1059,12 +1059,18 @@ tests() ->
{"unicode", [ {"unicode", [
{"(tm) somewhere", {"(tm) somewhere",
<<"">>, [], <<"">>} <<"">>, [], <<"">>}
]},
{"contrib_humanize", [
{"intcomma",
<<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
[{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}],
<<"999 123,456,789 12,345 1,234,567,890">>}
]} ]}
]. ].


run_tests() -> run_tests() ->
io:format("Running unit tests...~n"), io:format("Running unit tests...~n"),
DefaultOptions = [], DefaultOptions = [{custom_filters_modules, [erlydtl_contrib_humanize]}],
Failures = lists:foldl( Failures = lists:foldl(
fun({Group, Assertions}, GroupAcc) -> fun({Group, Assertions}, GroupAcc) ->
io:format(" Test group ~p...~n", [Group]), io:format(" Test group ~p...~n", [Group]),
Expand Down

0 comments on commit 6d1fcaf

Please sign in to comment.