Change nils argument to string

This commit is contained in:
Anatol Broder 2014-05-06 21:36:13 +02:00
parent 14e0abc4e2
commit 7c1709fab4
4 changed files with 10 additions and 9 deletions

View File

@ -101,7 +101,7 @@ Feature: Embed filters
And I have the following page:
| layout | content |
| default | Jump |
And I have a default layout that contains "{% assign sorted_pages = site.pages | sort: 'title', true %}The rule of {{ sorted_pages.size }}: {% for p in sorted_pages %}{{ p.content | strip_html | strip_newlines }}, {% endfor %}"
And I have a default layout that contains "{% assign sorted_pages = site.pages | sort: 'title', 'last' %}The rule of {{ sorted_pages.size }}: {% for p in sorted_pages %}{{ p.content | strip_html | strip_newlines }}, {% endfor %}"
When I run jekyll build
Then the _site directory should exist
And I should see exactly "The rule of 3: Fly, Run, Jump," in "_site/bird.html"

View File

@ -194,18 +194,18 @@ module Jekyll
#
# input - the object array
# key - key within each object to filter by
# nils_last - nils appear after non-nil values in the sort ordering
# nils ('first' | 'last') - nils appear before or after non-nil values
#
# Returns the filtered array of objects
def sort(input, key = nil, nils_last = false)
def sort(input, key = nil, nils = 'first')
if key.nil?
input.sort
else
input.sort { |a, b|
if a[key].nil? and !b[key].nil?
nils_last ? +1 : -1
nils == 'first' ? -1 : +1
elsif !a[key].nil? and b[key].nil?
nils_last ? -1 : +1
nils == 'first' ? +1 : -1
else
a[key] <=> b[key]
end

View File

@ -187,12 +187,13 @@ common tasks easier.
<tr>
<td>
<p class="name"><strong>Sort</strong></p>
<p>Sort array. Optional arguments: property, nils last.</p>
<p>Sort array. Optional arguments: property name; nils order (*first* or *last*).</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | sort }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.pages | sort: 'title', true }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.posts | sort: 'author' }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}</code>
</p>
</td>
</tr>

View File

@ -169,11 +169,11 @@ class TestFilters < Test::Unit::TestCase
should "return sorted by property array with nils first" do
ary = [{"a" => 2}, {"b" => 1}, {"a" => 1}]
assert_equal [{"b" => 1}, {"a" => 1}, {"a" => 2}], @filter.sort(ary, "a")
assert_equal @filter.sort(ary, "a"), @filter.sort(ary, "a", false)
assert_equal @filter.sort(ary, "a"), @filter.sort(ary, "a", "first")
end
should "return sorted by property array with nils last" do
assert_equal [{"a" => 1}, {"a" => 2}, {"b" => 1}],
@filter.sort([{"a" => 2}, {"b" => 1}, {"a" => 1}], "a", true)
@filter.sort([{"a" => 2}, {"b" => 1}, {"a" => 1}], "a", "last")
end
end