From d1806abe0cb8a46455ad4cd3f0d8bc4fb317ccb3 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 25 Mar 2014 14:15:52 -0700 Subject: [PATCH] add sort by function --- lib/index.js | 28 +++++++++++++--------- test/fixtures/sort-function/build/one.md | 1 + test/fixtures/sort-function/build/three.md | 1 + test/fixtures/sort-function/build/two.md | 1 + test/fixtures/sort-function/src/one.md | 6 +++++ test/fixtures/sort-function/src/three.md | 6 +++++ test/fixtures/sort-function/src/two.md | 6 +++++ test/index.js | 20 ++++++++++++++++ 8 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/sort-function/build/one.md create mode 100644 test/fixtures/sort-function/build/three.md create mode 100644 test/fixtures/sort-function/build/two.md create mode 100644 test/fixtures/sort-function/src/one.md create mode 100644 test/fixtures/sort-function/src/three.md create mode 100644 test/fixtures/sort-function/src/two.md diff --git a/lib/index.js b/lib/index.js index d6391c2..ce43eeb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -41,7 +41,7 @@ function plugin(opts){ }); /** - * Sort the collection. + * Sort the collections. */ keys.forEach(function(key){ @@ -49,16 +49,22 @@ function plugin(opts){ var settings = opts[key]; var sort = settings.sortBy || 'date'; var col = metadata[key]; - col.sort(function(a, b){ - a = a[sort]; - b = b[sort]; - if (!a && !b) return 0; - if (!a) return -1; - if (!b) return 1; - if (b > a) return -1; - if (a > b) return 1; - return 0; - }); + + if ('function' == typeof sort) { + col.sort(sort); + } else { + col.sort(function(a, b){ + a = a[sort]; + b = b[sort]; + if (!a && !b) return 0; + if (!a) return -1; + if (!b) return 1; + if (b > a) return -1; + if (a > b) return 1; + return 0; + }); + } + if (settings.reverse) col.reverse(); }); diff --git a/test/fixtures/sort-function/build/one.md b/test/fixtures/sort-function/build/one.md new file mode 100644 index 0000000..43dd47e --- /dev/null +++ b/test/fixtures/sort-function/build/one.md @@ -0,0 +1 @@ +one \ No newline at end of file diff --git a/test/fixtures/sort-function/build/three.md b/test/fixtures/sort-function/build/three.md new file mode 100644 index 0000000..1d19714 --- /dev/null +++ b/test/fixtures/sort-function/build/three.md @@ -0,0 +1 @@ +three \ No newline at end of file diff --git a/test/fixtures/sort-function/build/two.md b/test/fixtures/sort-function/build/two.md new file mode 100644 index 0000000..64c5e58 --- /dev/null +++ b/test/fixtures/sort-function/build/two.md @@ -0,0 +1 @@ +two \ No newline at end of file diff --git a/test/fixtures/sort-function/src/one.md b/test/fixtures/sort-function/src/one.md new file mode 100644 index 0000000..d37b9bf --- /dev/null +++ b/test/fixtures/sort-function/src/one.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Alpha +--- + +one \ No newline at end of file diff --git a/test/fixtures/sort-function/src/three.md b/test/fixtures/sort-function/src/three.md new file mode 100644 index 0000000..bf34f25 --- /dev/null +++ b/test/fixtures/sort-function/src/three.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Gamma +--- + +three \ No newline at end of file diff --git a/test/fixtures/sort-function/src/two.md b/test/fixtures/sort-function/src/two.md new file mode 100644 index 0000000..7a944e4 --- /dev/null +++ b/test/fixtures/sort-function/src/two.md @@ -0,0 +1,6 @@ +--- +collection: articles +title: Beta +--- + +two \ No newline at end of file diff --git a/test/index.js b/test/index.js index b8e2d6e..2d08eb0 100644 --- a/test/index.js +++ b/test/index.js @@ -72,6 +72,26 @@ describe('metalsmith-collections', function(){ }); }); + it('should accept a "sortBy" function', function(done){ + var metalsmith = Metalsmith('test/fixtures/sort'); + metalsmith + .use(collections({ articles: { sortBy: sort }})) + .build(function(err){ + if (err) return done(err); + var articles = metalsmith.metadata().articles; + assert.equal('Gamma', articles[0].title); + assert.equal('Beta', articles[1].title); + assert.equal('Alpha', articles[2].title); + done(); + }); + + function sort(a, b){ + a = a.title.slice(1); + b = b.title.slice(1); + return a > b ? 1 : -1; + } + }); + it('should accept a "reverse" option', function(done){ var metalsmith = Metalsmith('test/fixtures/sort'); metalsmith