filters: allow sample(n) instead of just sample(1)
This commit is contained in:
parent
96bc62c666
commit
86195655d7
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue