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: And I have the following page:
| layout | content | | layout | content |
| default | Jump | | 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 When I run jekyll build
Then the _site directory should exist Then the _site directory should exist
And I should see exactly "The rule of 3: Fly, Run, Jump," in "_site/bird.html" 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 # input - the object array
# key - key within each object to filter by # 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 # Returns the filtered array of objects
def sort(input, key = nil, nils_last = false) def sort(input, key = nil, nils = 'first')
if key.nil? if key.nil?
input.sort input.sort
else else
input.sort { |a, b| input.sort { |a, b|
if a[key].nil? and !b[key].nil? if a[key].nil? and !b[key].nil?
nils_last ? +1 : -1 nils == 'first' ? -1 : +1
elsif !a[key].nil? and b[key].nil? elsif !a[key].nil? and b[key].nil?
nils_last ? -1 : +1 nils == 'first' ? +1 : -1
else else
a[key] <=> b[key] a[key] <=> b[key]
end end

View File

@ -187,12 +187,13 @@ common tasks easier.
<tr> <tr>
<td> <td>
<p class="name"><strong>Sort</strong></p> <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>
<td class="align-center"> <td class="align-center">
<p> <p>
<code class="filter">{% raw %}{{ page.tags | sort }}{% endraw %}</code> <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> </p>
</td> </td>
</tr> </tr>

View File

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