Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add a contrib_humanize module for the intcomma filter #52

Merged
merged 1 commit into from

2 participants

Loïc Hoguin Evan Miller
Loïc Hoguin

Here you go. Tell me anything that needs changing.

Loïc Hoguin essen referenced this pull request
Closed

Adding filters to erlydtl #48

Evan Miller
Owner

Looks good, thanks!

Evan Miller evanmiller merged commit 6d1fcaf into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 20, 2012
  1. Loïc Hoguin
This page is out of date. Refresh to see the latest.
18 src/erlydtl_contrib_humanize.erl
View
@@ -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 tests/src/erlydtl_unittests.erl
View
@@ -1059,12 +1059,18 @@ tests() ->
{"unicode", [
{"(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() ->
io:format("Running unit tests...~n"),
- DefaultOptions = [],
+ DefaultOptions = [{custom_filters_modules, [erlydtl_contrib_humanize]}],
Failures = lists:foldl(
fun({Group, Assertions}, GroupAcc) ->
io:format(" Test group ~p...~n", [Group]),
Something went wrong with that request. Please try again.