From 36f3c1cff5ce45dfc256bf6c5c5414b1939c6030 Mon Sep 17 00:00:00 2001 From: Radek Stepan Date: Tue, 13 Aug 2013 16:49:52 +0100 Subject: [PATCH] into days orderly --- src/issues.coffee | 7 +++++-- test/issues.coffee | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/issues.coffee b/src/issues.coffee index 00c6e2b..f1e93af 100644 --- a/src/issues.coffee +++ b/src/issues.coffee @@ -54,7 +54,7 @@ module.exports = catch err return cb err, warnings - # Map a collection of closed issues into days. + # Map a collection of closed issues into days (does not assume coll to be sorted). 'into_days': (collection, cb) -> days = {} for issue in collection @@ -63,8 +63,11 @@ module.exports = return "Issue ##{number} does not have a `closed_at` parameter" unless closed_at unless matches = closed_at.match /^(\d{4}-\d{2}-\d{2})T(.*)/ return "Issue ##{number} does not match the `closed_at` pattern" + # Explode the matches. [ date, time ] = matches[1...] + # Init the date? days[date] ?= [] - days[date].push issue + # Insert into an already sorted array. + days[date].splice _.sortedIndex(days[date], issue, 'closed_at'), 0, issue cb null, days \ No newline at end of file diff --git a/test/issues.coffee b/test/issues.coffee index 82c3328..b478b1e 100644 --- a/test/issues.coffee +++ b/test/issues.coffee @@ -192,8 +192,8 @@ module.exports = assert.ifError err assert.deepEqual data, '2013-05-09': [ - { number: 1, closed_at: '2013-05-09T10:04:53Z' } { number: 2, closed_at: '2013-05-09T09:04:53Z' } + { number: 1, closed_at: '2013-05-09T10:04:53Z' } ] '2013-05-10': [ { number: 3, closed_at: '2013-05-10T09:04:53Z' }