Skip to content
This repository
Browse code

Merge pull request #38 from srstrong/master

Changes to the add filter
  • Loading branch information...
commit 8797a292b9bd26c85dbe6db9be2584eb3328d36e 2 parents f5b3d31 + 7c29274
Evan Miller authored August 14, 2012
53  src/erlydtl_filters.erl
@@ -152,13 +152,52 @@
152 152
 -define(SECONDS_PER_MONTH, (30 * ?SECONDS_PER_DAY)).
153 153
 -define(SECONDS_PER_YEAR, (365 * ?SECONDS_PER_DAY)).
154 154
  
155  
-%% @doc Adds a number to the value.
156  
-add(Input, Number) when is_binary(Input) ->
157  
-    list_to_binary(add(binary_to_list(Input), Number));
158  
-add(Input, Number) when is_list(Input) ->
159  
-    integer_to_list(add(list_to_integer(Input), Number));
160  
-add(Input, Number) when is_integer(Input) ->
161  
-    Input + Number.
  155
+%% @doc Adds to values
  156
+add(LHS, RHS) when is_number(LHS), is_number(RHS) ->
  157
+    LHS + RHS;
  158
+add(LHS, RHS) when is_binary(LHS) ->
  159
+    add(binary_to_list(LHS), RHS);
  160
+add(LHS, RHS) when is_binary(RHS) ->
  161
+    add(LHS, binary_to_list(RHS));
  162
+add(LHS, RHS) when is_list(LHS), is_list(RHS) ->
  163
+    case {to_numeric(LHS), to_numeric(RHS)} of
  164
+	{{number, LHSNum}, {number, RHSNum}} ->
  165
+	    LHSNum + RHSNum;
  166
+	_ ->
  167
+	    LHS ++ RHS
  168
+    end;
  169
+add(LHS, RHS) when is_list(LHS), is_number(RHS) ->
  170
+    case to_numeric(LHS) of
  171
+	{number, LHSNum} ->
  172
+	    LHSNum + RHS;
  173
+	_ ->
  174
+	    LHS ++ to_string(RHS)
  175
+    end;
  176
+add(LHS, RHS) when is_number(LHS), is_list(RHS) ->
  177
+    case to_numeric(RHS) of
  178
+	{number, RHSNum} ->
  179
+	    LHS + RHSNum;
  180
+	_ ->
  181
+	    to_string(LHS) ++ RHS
  182
+    end.
  183
+
  184
+to_string(Num) when is_integer(Num) ->
  185
+    integer_to_list(Num);
  186
+to_string(Num) when is_float(Num) ->
  187
+    float_to_list(Num).
  188
+
  189
+to_numeric(List) ->
  190
+    try
  191
+	{number, list_to_integer(List)}
  192
+    catch
  193
+	error:badarg ->
  194
+	    try
  195
+		{number, list_to_float(List)}
  196
+	    catch
  197
+		error:badarg ->
  198
+		    undefined
  199
+	    end
  200
+    end.
162 201
  
163 202
 %% @doc Adds slashes before quotes.
164 203
 addslashes(Input) when is_binary(Input) ->
24  tests/src/erlydtl_unittests.erl
@@ -366,9 +366,27 @@ tests() ->
366 366
                 {"Escape is applied last",
367 367
                     <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
368 368
                     <<"&lt;br /&gt;">>},
369  
-                {"|add:4",
370  
-                    <<"{{ one|add:4 }}">>, [{one, "1"}],
371  
-                    <<"5">>},
  369
+		{"add; lhs number, rhs number",
  370
+		      <<"{{ one|add:4}}">>, [{one, 1}],
  371
+		      <<"5">>},
  372
+		{"add; lhs numeric string, rhs number",
  373
+		      <<"{{ one|add:4}}">>, [{one, "1"}],
  374
+		      <<"5">>},
  375
+		{"add; lhs number, rhs numeric string",
  376
+		      <<"{{ one|add:'4'}}">>, [{one, 1}],
  377
+		      <<"5">>},
  378
+		{"add; lhs non-numeric string, rhs number",
  379
+		      <<"{{ one|add:4}}">>, [{one, "foo"}],
  380
+		      <<"foo4">>},
  381
+		{"add; lhs number, rhs non-numeric string",
  382
+		      <<"{{ one|add:'foo'}}">>, [{one, 1}],
  383
+		      <<"1foo">>},
  384
+		{"add; lhs non-numeric string, rhs non-numeric string",
  385
+		      <<"{{ one|add:'bar'}}">>, [{one, "foo"}],
  386
+		      <<"foobar">>},
  387
+		{"add; lhs numeric string, rhs numeric string",
  388
+		      <<"{{ one|add:'4'}}">>, [{one, "1"}],
  389
+		      <<"5">>},
372 390
                 {"|addslashes",
373 391
                     <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
374 392
                     <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},

0 notes on commit 8797a29

Please sign in to comment.
Something went wrong with that request. Please try again.