Skip to content

Commit

Permalink
optimize batch function and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz “Felds” Liscia committed Feb 21, 2013
1 parent 15336c1 commit ccf3cd1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 31 deletions.
7 changes: 4 additions & 3 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,9 +1328,10 @@ function twig_array_batch($items, $size, $fill = null)

if (null !== $fill) {
$last = count($result) - 1;
while (count($result[$last]) < $size) {
$result[$last][] = $fill;
}
$result[$last] = array_merge(
$result[$last],
array_fill(0, $size - count($result[$last]), $fill)
);
}

return $result;
Expand Down
50 changes: 22 additions & 28 deletions test/Twig/Tests/Fixtures/filters/batch.test
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
--TEST--
"batch" filter
--TEMPLATE--
<table>
{% for row in items|batch(3, '') %}
<tr>
{% for row in items|batch(3) %}
<div class=row>
{% for column in row %}
<td>{{ column }}</td>
<div class=item>{{ column }}</div>
{% endfor %}
</tr>
</div>
{% endfor %}
</table>
--DATA--
return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
--EXPECT--
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td>i</td>
</tr>
<tr>
<td>j</td>
<td></td>
<td></td>
</tr>
</table>
<div class=row>
<div class=item>a</div>
<div class=item>b</div>
<div class=item>c</div>
</div>
<div class=row>
<div class=item>d</div>
<div class=item>e</div>
<div class=item>f</div>
</div>
<div class=row>
<div class=item>g</div>
<div class=item>h</div>
<div class=item>i</div>
</div>
<div class=row>
<div class=item>j</div>
</div>
37 changes: 37 additions & 0 deletions test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
"batch" filter
--TEMPLATE--
<table>
{% for row in items|batch(3, '') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
--DATA--
return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
--EXPECT--
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td>i</td>
</tr>
<tr>
<td>j</td>
<td></td>
<td></td>
</tr>
</table>
37 changes: 37 additions & 0 deletions test/Twig/Tests/Fixtures/filters/batch_with_fill.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
"batch" filter
--TEMPLATE--
<table>
{% for row in items|batch(3, 'fill') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
--DATA--
return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
--EXPECT--
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td>i</td>
</tr>
<tr>
<td>j</td>
<td>fill</td>
<td>fill</td>
</tr>
</table>

0 comments on commit ccf3cd1

Please sign in to comment.