Merge pull request #5597 from thetimbanks/add_array_connector_param

Merge pull request 5597
This commit is contained in:
jekyllbot 2016-11-23 08:23:01 -08:00 committed by GitHub
commit 7044549ae1
3 changed files with 14 additions and 3 deletions

View File

@ -208,7 +208,7 @@ common tasks easier.
<tr>
<td>
<p class="name"><strong>Array to Sentence</strong></p>
<p>Convert an array into a sentence. Useful for listing tags.</p>
<p>Convert an array into a sentence. Useful for listing tags. Optional argument for connector.</p>
</td>
<td class="align-center">
<p>
@ -217,6 +217,12 @@ common tasks easier.
<p>
<code class="output">foo, bar, and baz</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | array_to_sentence_string: 'or' }}{% endraw %}</code>
</p>
<p>
<code class="output">foo, bar, or baz</code>
</p>
</td>
</tr>
<tr>

View File

@ -175,6 +175,7 @@ module Jekyll
# word "and" for the last one.
#
# array - The Array of Strings to join.
# connector - Word used to connect the last 2 items in the array
#
# Examples
#
@ -182,8 +183,7 @@ module Jekyll
# # => "apples, oranges, and grapes"
#
# Returns the formatted String.
def array_to_sentence_string(array)
connector = "and"
def array_to_sentence_string(array, connector = "and")
case array.length
when 0
""

View File

@ -143,6 +143,11 @@ class TestFilters < JekyllUnitTest
)
end
should "convert array to sentence string with different connector" do
assert_equal "1 or 2", @filter.array_to_sentence_string([1, 2], "or")
assert_equal "1, 2, 3, or 4", @filter.array_to_sentence_string([1, 2, 3, 4], "or")
end
context "normalize_whitespace filter" do
should "replace newlines with a space" do
assert_equal "a b", @filter.normalize_whitespace("a\nb")