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 new_ary
end end
def sample(input) def sample(input, num = 1)
return input unless input.respond_to?(:sample) return input unless input.respond_to?(:sample)
input.sample(1) input.sample(num)
end end
# Convert an object into its String representation for debugging # Convert an object into its String representation for debugging

View File

@ -249,12 +249,15 @@ common tasks easier.
<tr> <tr>
<td> <td>
<p class="name"><strong>Sample</strong></p> <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>
<td class="align-center"> <td class="align-center">
<p> <p>
<code class="filter">{% raw %}{{ site.pages | sample }}{% endraw %}</code> <code class="filter">{% raw %}{{ site.pages | sample }}{% endraw %}</code>
</p> </p>
<p>
<code class="filter">{% raw %}{{ site.pages | sample:2 }}{% endraw %}</code>
</p>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -393,6 +393,13 @@ class TestFilters < JekyllUnitTest
input = %w(hey there bernie) input = %w(hey there bernie)
assert_includes input, @filter.sample(input) assert_includes input, @filter.sample(input)
end 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
end end