Merge pull request #2299 from penibelst/sort-nils

This commit is contained in:
Matt Rogers 2014-05-06 16:49:40 -05:00
commit 5c109ee8dc
5 changed files with 113 additions and 1 deletions

View File

@ -73,3 +73,35 @@ Feature: Embed filters
Then the _site directory should exist
And I should see exactly "Page-2, Page-1" in "_site/page-1.html"
And I should see exactly "Page-2, Page-1" in "_site/page-2.html"
Scenario: Sort pages by the title
Given I have a _layouts directory
And I have the following page:
| title | layout | content |
| Dog | default | Run |
And I have the following page:
| title | layout | content |
| Bird | default | Fly |
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' %}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: Jump, Fly, Run," in "_site/bird.html"
Scenario: Sort pages by the title ordering pages without title last
Given I have a _layouts directory
And I have the following page:
| title | layout | content |
| Dog | default | Run |
And I have the following page:
| title | layout | content |
| Bird | default | Fly |
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', '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

@ -28,7 +28,11 @@ def run_jekyll(args)
end
def slug(title)
if title
title.downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-')
else
Time.now.strftime("%s%9N") # nanoseconds since the Epoch
end
end
def location(folder, direction)

View File

@ -190,6 +190,40 @@ module Jekyll
input.select { |object| object[key] == value }
end
# Sort an array of objects
#
# input - the object array
# key - key within each object to filter by
# nils ('first' | 'last') - nils appear before or after non-nil values
#
# Returns the filtered array of objects
def sort(input, key = nil, nils = "first")
if key.nil?
input.sort
else
case
when nils == "first"
order = - 1
when nils == "last"
order = + 1
else
Jekyll.logger.error "Invalid nils order:",
"'#{nils}' is not a valid nils order. It must be 'first' or 'last'."
exit(1)
end
input.sort { |a, b|
if !a[key].nil? && b[key].nil?
- order
elsif a[key].nil? && !b[key].nil?
+ order
else
a[key] <=> b[key]
end
}
end
end
private
def time(input)
case input

View File

@ -210,6 +210,23 @@ common tasks easier.
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Sort</strong></p>
<p>Sort an array. Optional arguments for hashes: 1.&nbsp;property name 2.&nbsp;nils order (<em>first</em> or <em>last</em>).</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | sort }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.posts | sort: 'author' }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}</code>
</p>
</td>
</tr>
</tbody>
</table>
</div>

View File

@ -152,5 +152,30 @@ class TestFilters < Test::Unit::TestCase
end
end
context "sort filter" do
should "return sorted numbers" do
assert_equal [1, 2, 2.2, 3], @filter.sort([3, 2.2, 2, 1])
end
should "return sorted strings" do
assert_equal ["10", "2"], @filter.sort(["10", "2"])
assert_equal [{"a" => "10"}, {"a" => "2"}], @filter.sort([{"a" => "10"}, {"a" => "2"}], "a")
assert_equal ["FOO", "Foo", "foo"], @filter.sort(["foo", "Foo", "FOO"])
assert_equal ["_foo", "foo", "foo_"], @filter.sort(["foo_", "_foo", "foo"])
end
should "return sorted by property array" do
assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}],
@filter.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a")
end
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", "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", "last")
end
end
end
end