filters: allow sample(n) instead of just sample(1)

This commit is contained in:
Parker Moore 2015-12-04 09:40:57 -08:00
parent 96bc62c666
commit 86195655d7
3 changed files with 13 additions and 3 deletions

View File

@ -281,9 +281,9 @@ module Jekyll
new_ary
end
def sample(input)
def sample(input, num = 1)
return input unless input.respond_to?(:sample)
input.sample(1)
input.sample(num)
end
# Convert an object into its String representation for debugging

View File

@ -249,12 +249,15 @@ common tasks easier.
<tr>
<td>
<p class="name"><strong>Sample</strong></p>
<p>Pick a random value from an array.</p>
<p>Pick a random value from an array. Optional: pick multiple values.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.pages | sample }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sample:2 }}{% endraw %}</code>
</p>
</td>
</tr>
</tbody>

View File

@ -393,6 +393,13 @@ class TestFilters < JekyllUnitTest
input = %w(hey there bernie)
assert_includes input, @filter.sample(input)
end
should "allow sampling of multiple values (n > 1)" do
input = %w(hey there bernie)
@filter.sample(input, 2).each do |val|
assert_includes val, input
end
end
end
end