From 5c965d6a203d66a4543c11032f3912209782a748 Mon Sep 17 00:00:00 2001 From: Tim Banks Date: Tue, 22 Nov 2016 17:38:33 -0600 Subject: [PATCH] Add connector param to array_to_sentence_string filter --- docs/_docs/templates.md | 8 +++++++- lib/jekyll/filters.rb | 4 ++-- test/test_filters.rb | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/_docs/templates.md b/docs/_docs/templates.md index daae34c2..a203172e 100644 --- a/docs/_docs/templates.md +++ b/docs/_docs/templates.md @@ -208,7 +208,7 @@ common tasks easier.

Array to Sentence

-

Convert an array into a sentence. Useful for listing tags.

+

Convert an array into a sentence. Useful for listing tags. Optional argument for connector.

@@ -217,6 +217,12 @@ common tasks easier.

foo, bar, and baz

+

+ {% raw %}{{ page.tags | array_to_sentence_string: 'or' }}{% endraw %} +

+

+ foo, bar, or baz +

diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 29f2a2b5..ddd4fea9 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -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 "" diff --git a/test/test_filters.rb b/test/test_filters.rb index 20587610..48f90bbb 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -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")