Merge pull request #4223 from jekyll/pull/sample-filter

Merge pull request 4223
This commit is contained in:
Parker Moore 2015-12-04 14:12:18 -08:00
commit c6255d5f28
4 changed files with 41 additions and 0 deletions

View File

@ -19,6 +19,9 @@ env:
matrix: matrix:
- TEST_SUITE=test - TEST_SUITE=test
- TEST_SUITE=cucumber - TEST_SUITE=cucumber
branches:
only:
- master
before_script: bundle update before_script: bundle update
script: script/cibuild script: script/cibuild
notifications: notifications:

View File

@ -281,6 +281,16 @@ module Jekyll
new_ary new_ary
end end
def sample(input, num = 1)
return input unless input.respond_to?(:sample)
n = num.to_i rescue 1
if n == 1
input.sample
else
input.sample(n)
end
end
# Convert an object into its String representation for debugging # Convert an object into its String representation for debugging
# #
# input - The Object to be converted # input - The Object to be converted

View File

@ -246,6 +246,20 @@ common tasks easier.
</p> </p>
</td> </td>
</tr> </tr>
<tr>
<td>
<p class="name"><strong>Sample</strong></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> </tbody>
</table> </table>
</div> </div>

View File

@ -388,5 +388,19 @@ class TestFilters < JekyllUnitTest
end end
end end
context "sample filter" do
should "return a random item from the array" do
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 input, val
end
end
end
end end
end end