From 7c1709fab4b0ef1ce9957cbcc6635f538850ae3c Mon Sep 17 00:00:00 2001 From: Anatol Broder Date: Tue, 6 May 2014 21:36:13 +0200 Subject: [PATCH] Change nils argument to string --- features/embed_filters.feature | 2 +- lib/jekyll/filters.rb | 8 ++++---- site/docs/templates.md | 5 +++-- test/test_filters.rb | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/features/embed_filters.feature b/features/embed_filters.feature index b1a420f5..889a1fc7 100644 --- a/features/embed_filters.feature +++ b/features/embed_filters.feature @@ -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" diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index ced9623f..fe438834 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -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 diff --git a/site/docs/templates.md b/site/docs/templates.md index 460e4d32..dd8c8107 100644 --- a/site/docs/templates.md +++ b/site/docs/templates.md @@ -187,12 +187,13 @@ common tasks easier.

Sort

-

Sort array. Optional arguments: property, nils last.

+

Sort array. Optional arguments: property name; nils order (*first* or *last*).

{% raw %}{{ page.tags | sort }}{% endraw %} - {% raw %}{{ site.pages | sort: 'title', true }}{% endraw %} + {% raw %}{{ site.posts | sort: 'author' }}{% endraw %} + {% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}

diff --git a/test/test_filters.rb b/test/test_filters.rb index 66db980e..2f54538a 100644 --- a/test/test_filters.rb +++ b/test/test_filters.rb @@ -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